diff --git a/src/lib/components/notes/NoteEditor.svelte b/src/lib/components/notes/NoteEditor.svelte index d3478eff18..34093004e5 100644 --- a/src/lib/components/notes/NoteEditor.svelte +++ b/src/lib/components/notes/NoteEditor.svelte @@ -10,7 +10,7 @@ import { toast } from 'svelte-sonner'; import equal from 'fast-deep-equal'; - import { goto } from '$app/navigation'; + import { goto, onNavigate } from '$app/navigation'; import dayjs from '$lib/dayjs'; import calendar from 'dayjs/plugin/calendar'; @@ -205,28 +205,47 @@ let debounceTimeout: NodeJS.Timeout | null = null; + const saveHandler = async () => { + const res = await updateNoteById(localStorage.token, id, { + title: note?.title === '' ? $i18n.t('Untitled') : note.title, + data: { + files: files + }, + access_grants: note?.access_grants ?? [] + }).catch((e) => { + toast.error(`${e}`); + }); + + if (res) { + pinnedNotes.set(await getPinnedNoteList(localStorage.token).catch(() => [])); + } + }; + const changeDebounceHandler = () => { if (debounceTimeout) { clearTimeout(debounceTimeout); } - debounceTimeout = setTimeout(async () => { - const res = await updateNoteById(localStorage.token, id, { - title: note?.title === '' ? $i18n.t('Untitled') : note.title, - data: { - files: files - }, - access_grants: note?.access_grants ?? [] - }).catch((e) => { - toast.error(`${e}`); - }); - - if (res) { - pinnedNotes.set(await getPinnedNoteList(localStorage.token).catch(() => [])); - } + debounceTimeout = setTimeout(() => { + debounceTimeout = null; + saveHandler(); }, 200); }; + onNavigate(() => { + if (titleInputFocused) { + changeDebounceHandler(); + } + + if (!debounceTimeout) { + return; + } + + clearTimeout(debounceTimeout); + debounceTimeout = null; + return saveHandler(); + }); + const applyExternalNoteContent = async (_note) => { const incomingContent = _note.data?.content; const contentLength = incomingContent?.md?.length ?? incomingContent?.html?.length ?? 0;