Skip to content

Commit

Permalink
chore: allow directly setting tiptap options instead of immediate wat…
Browse files Browse the repository at this point in the history
…cher
  • Loading branch information
logotip4ik committed Oct 27, 2024
1 parent 323083a commit 829caee
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 21 deletions.
33 changes: 16 additions & 17 deletions components/Workspace/Note/Editor/NoteEditor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ const props = defineProps<{
onRefresh: () => Promise<void>
}>();
const content = computed(() => props.note.content || '');
const { shortcuts } = useAppConfig();
const mitt = useMitt();
const zeenk = useZeenk();
Expand All @@ -26,20 +28,9 @@ const {
editor,
isTyping,
onUpdate: onContentUpdate,
} = useTiptap();
let hasUnsavedChanges = false;
function updateContent(force?: boolean) {
const content = editor.getHTML();
return props
.onUpdate(content || '', force)
.then(() => {
hasUnsavedChanges = false;
});
}
} = useTiptap({ content: content.value, editable: props.editable });
watch(() => props.note.content, (content) => {
watch(content, (content) => {
if (isTyping.value || !editor) {
return;
}
Expand All @@ -49,14 +40,11 @@ watch(() => props.note.content, (content) => {
if (editorContent !== content) {
editor.commands.setContent(content || '');
}
}, {
immediate: import.meta.client,
deep: true, // this is really weird, but it only triggers watcher with deep: true ?
});
watch(() => props.editable, (editable) => {
editor.setOptions({ editable });
}, { immediate: import.meta.client });
});
watch(spellcheck, (spellcheck) => {
editor.setOptions({
Expand Down Expand Up @@ -93,6 +81,17 @@ zeenk.on('update-note', ({ path, steps }) => {
editor.view.dispatch(tr);
});
let hasUnsavedChanges = false;
function updateContent(force?: boolean) {
const content = editor.getHTML();
return props
.onUpdate(content || '', force)
.then(() => {
hasUnsavedChanges = false;
});
}
function saveUnsavedChanges() {
if (hasUnsavedChanges) {
updateContent(true);
Expand Down
9 changes: 5 additions & 4 deletions composables/tiptap/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,15 @@ const isTyping = /* #__PURE__ */ ref(false); // this will be removed in server b

const debouncedClearTyping = debounce(() => isTyping.value = false, 500);

function initTiptap() {
function initTiptap(opts?: { content?: string, editable?: boolean }) {
if (import.meta.server) {
return;
}

return new Editor({
autofocus: false,
editable: true,
editable: opts?.editable,
content: opts?.content,
editorProps: {
attributes: {
spellcheck: getSetting(settings.spellcheck).value === 'yes' ? 'true' : 'false',
Expand Down Expand Up @@ -97,8 +98,8 @@ function initTiptap() {
});
}

export function useTiptap() {
const editor = initTiptap()!;
export function useTiptap(opts?: { content?: string, editable?: boolean }) {
const editor = initTiptap(opts)!;

currentTiptap.value = editor;

Expand Down

0 comments on commit 829caee

Please sign in to comment.