From 2e61a26fd094f50fe06f77130bdb1ccbb4c2e5dd Mon Sep 17 00:00:00 2001 From: Mo Date: Thu, 5 May 2022 11:05:26 -0500 Subject: [PATCH] fix: improve saveNote function --- src/Screens/Compose/Compose.tsx | 52 ++++++++++++++------------------- 1 file changed, 22 insertions(+), 30 deletions(-) diff --git a/src/Screens/Compose/Compose.tsx b/src/Screens/Compose/Compose.tsx index b4d899a4..d708a144 100644 --- a/src/Screens/Compose/Compose.tsx +++ b/src/Screens/Compose/Compose.tsx @@ -338,23 +338,9 @@ export class Compose extends React.Component { - if (!this.note) { - return - } - - const editor = this.editor - const { title } = this.state - const text = newNoteText || this.note.text - - if (editor.isTemplateNote) { - await editor.insertTemplatedNote() + saveNote = async (params: { newTitle?: string; newText?: string }) => { + if (this.editor.isTemplateNote) { + await this.editor.insertTemplatedNote() } if (!this.context?.items.findItem(this.note.uuid)) { @@ -362,34 +348,38 @@ export class Compose extends React.Component { const noteMutator = mutator as NoteMutator - noteMutator.title = title - noteMutator.text = text - if (!dontUpdatePreviews) { - const truncate = text.length > NOTE_PREVIEW_CHAR_LIMIT - const substring = text.substring(0, NOTE_PREVIEW_CHAR_LIMIT) - const previewPlain = substring + (truncate ? '...' : '') + if (newTitle != null) { + noteMutator.title = newTitle + } + + if (newText != null) { + noteMutator.text = newText + + const substring = newText.substring(0, NOTE_PREVIEW_CHAR_LIMIT) + const shouldTruncate = newText.length > NOTE_PREVIEW_CHAR_LIMIT + const previewPlain = substring + (shouldTruncate ? '...' : '') noteMutator.preview_plain = previewPlain noteMutator.preview_html = undefined } }, - isUserModified + true ) if (this.saveTimeout) { clearTimeout(this.saveTimeout) } - const noDebounce = bypassDebouncer || this.context?.noAccount() + const noDebounce = this.context?.noAccount() const syncDebouceMs = noDebounce ? SAVE_TIMEOUT_NO_DEBOUNCE : SAVE_TIMEOUT_DEBOUNCE + this.saveTimeout = setTimeout(() => { void this.context?.sync.sync() - if (closeAfterSync) { - this.context?.getAppState().closeEditor(editor) - } }, syncDebouceMs) } @@ -398,11 +388,12 @@ export class Compose extends React.Component this.saveNote(false, true, true, false) + () => this.saveNote({ newTitle: newTitle }) ) } @@ -411,7 +402,8 @@ export class Compose extends React.Component {