From 5e5b6cdc4215bfa54f137370c1717b2f1de761d4 Mon Sep 17 00:00:00 2001 From: Laurent Cozic Date: Thu, 26 Mar 2020 17:19:13 +0000 Subject: [PATCH] Desktop: WYSIWYG: Restored focus logic and fixed undo issue --- ElectronClient/gui/NoteText2.tsx | 22 +++++++++++++++++++--- ElectronClient/gui/editors/TinyMCE.tsx | 7 +++++++ 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/ElectronClient/gui/NoteText2.tsx b/ElectronClient/gui/NoteText2.tsx index 8d7c005d7..f71b27771 100644 --- a/ElectronClient/gui/NoteText2.tsx +++ b/ElectronClient/gui/NoteText2.tsx @@ -442,20 +442,36 @@ function NoteText2(props:NoteTextProps) { saveNoteIfWillChange(formNote, editorRef, props.dispatch); - const loadNote = async () => { + function handleAutoFocus(noteIsTodo:boolean) { + if (!props.isProvisional) return; + + const focusSettingName = noteIsTodo ? 'newTodoFocus' : 'newNoteFocus'; + + requestAnimationFrame(() => { + if (Setting.value(focusSettingName) === 'title') { + if (titleInputRef.current) titleInputRef.current.focus(); + } else { + if (editorRef.current) editorRef.current.execCommand({ name: 'focus' }); + } + }); + } + + async function loadNote() { const n = await Note.load(props.noteId); if (cancelled) return; if (!n) throw new Error(`Cannot find note with ID: ${props.noteId}`); reg.logger().debug('Loaded note:', n); initNoteState(n, setFormNote, setDefaultEditorState); - }; + + handleAutoFocus(!!n.is_todo); + } loadNote(); return () => { cancelled = true; }; - }, [props.noteId, formNote, waitingToSaveNote]); + }, [props.noteId, props.isProvisional, formNote, waitingToSaveNote]); const onFieldChange = useCallback((field:string, value:any, changeId: number = 0) => { handleProvisionalFlag(); diff --git a/ElectronClient/gui/editors/TinyMCE.tsx b/ElectronClient/gui/editors/TinyMCE.tsx index 090b5755e..7299769db 100644 --- a/ElectronClient/gui/editors/TinyMCE.tsx +++ b/ElectronClient/gui/editors/TinyMCE.tsx @@ -415,6 +415,13 @@ const TinyMCE = (props:TinyMCEProps, ref:any) => { editor.getDoc().addEventListener('click', onEditorContentClick); + // Need to clear UndoManager to avoid this problem: + // - Load note 1 + // - Make a change + // - Load note 2 + // - Undo => content is that of note 1 + editor.undoManager.clear(); + dispatchDidUpdate(editor); };