From 03bd77c107a4c460f579f9a9c8cd18615060e57f Mon Sep 17 00:00:00 2001 From: Henry Heino <46334387+personalizedrefrigerator@users.noreply.github.com> Date: Sun, 22 Oct 2023 04:00:19 -0700 Subject: [PATCH] Desktop: Resolves #8749: Use plain text editor in safe mode (#8750) --- .eslintignore | 1 + .gitignore | 1 + .../app-desktop/gui/MainScreen/MainScreen.tsx | 4 +- .../NoteBody/PlainEditor/PlainEditor.tsx | 56 +++++++++++++++++++ .../NoteBody/PlainEditor/PlainEditor_tsx.txt | 50 ----------------- .../app-desktop/gui/NoteEditor/NoteEditor.tsx | 7 ++- .../utils/useWindowCommandHandler.ts | 18 +++--- .../commands/stateToWhenClauseContext.ts | 5 +- 8 files changed, 80 insertions(+), 62 deletions(-) create mode 100644 packages/app-desktop/gui/NoteEditor/NoteBody/PlainEditor/PlainEditor.tsx delete mode 100644 packages/app-desktop/gui/NoteEditor/NoteBody/PlainEditor/PlainEditor_tsx.txt diff --git a/.eslintignore b/.eslintignore index af0c2646a..55c6201db 100644 --- a/.eslintignore +++ b/.eslintignore @@ -242,6 +242,7 @@ packages/app-desktop/gui/NoteEditor/NoteBody/CodeMirror/v5/Editor.js packages/app-desktop/gui/NoteEditor/NoteBody/CodeMirror/v6/CodeMirror.js packages/app-desktop/gui/NoteEditor/NoteBody/CodeMirror/v6/Editor.js packages/app-desktop/gui/NoteEditor/NoteBody/CodeMirror/v6/useEditorCommands.js +packages/app-desktop/gui/NoteEditor/NoteBody/PlainEditor/PlainEditor.js packages/app-desktop/gui/NoteEditor/NoteBody/TinyMCE/TinyMCE.js packages/app-desktop/gui/NoteEditor/NoteBody/TinyMCE/styles/index.js packages/app-desktop/gui/NoteEditor/NoteBody/TinyMCE/utils/joplinCommandToTinyMceCommands.js diff --git a/.gitignore b/.gitignore index 0369bbbcf..c7934e5d6 100644 --- a/.gitignore +++ b/.gitignore @@ -224,6 +224,7 @@ packages/app-desktop/gui/NoteEditor/NoteBody/CodeMirror/v5/Editor.js packages/app-desktop/gui/NoteEditor/NoteBody/CodeMirror/v6/CodeMirror.js packages/app-desktop/gui/NoteEditor/NoteBody/CodeMirror/v6/Editor.js packages/app-desktop/gui/NoteEditor/NoteBody/CodeMirror/v6/useEditorCommands.js +packages/app-desktop/gui/NoteEditor/NoteBody/PlainEditor/PlainEditor.js packages/app-desktop/gui/NoteEditor/NoteBody/TinyMCE/TinyMCE.js packages/app-desktop/gui/NoteEditor/NoteBody/TinyMCE/styles/index.js packages/app-desktop/gui/NoteEditor/NoteBody/TinyMCE/utils/joplinCommandToTinyMceCommands.js diff --git a/packages/app-desktop/gui/MainScreen/MainScreen.tsx b/packages/app-desktop/gui/MainScreen/MainScreen.tsx index ad4b98ede..ed3c79665 100644 --- a/packages/app-desktop/gui/MainScreen/MainScreen.tsx +++ b/packages/app-desktop/gui/MainScreen/MainScreen.tsx @@ -740,7 +740,9 @@ class MainScreenComponent extends React.Component { editor: () => { let bodyEditor = this.props.settingEditorCodeView ? 'CodeMirror' : 'TinyMCE'; - if (this.props.settingEditorCodeView && this.props.enableBetaMarkdownEditor) { + if (this.props.isSafeMode) { + bodyEditor = 'PlainText'; + } else if (this.props.settingEditorCodeView && this.props.enableBetaMarkdownEditor) { bodyEditor = 'CodeMirror6'; } return ; diff --git a/packages/app-desktop/gui/NoteEditor/NoteBody/PlainEditor/PlainEditor.tsx b/packages/app-desktop/gui/NoteEditor/NoteBody/PlainEditor/PlainEditor.tsx new file mode 100644 index 000000000..518e1a1e3 --- /dev/null +++ b/packages/app-desktop/gui/NoteEditor/NoteBody/PlainEditor/PlainEditor.tsx @@ -0,0 +1,56 @@ + +// Used in safe mode + +import * as React from 'react'; +import { ForwardedRef } from 'react'; +import { useEffect, useCallback, useRef, forwardRef, useImperativeHandle } from 'react'; +import { NoteBodyEditorProps, NoteBodyEditorRef } from '../../utils/types'; + +const PlainEditor = (props: NoteBodyEditorProps, ref: ForwardedRef) => { + const editorRef = useRef(); + + useImperativeHandle(ref, () => { + return { + content: () => editorRef.current?.value ?? '', + resetScroll: () => { + editorRef.current.scrollTop = 0; + }, + scrollTo: () => { + // Not supported + }, + + supportsCommand: _name => { + return false; + }, + execCommand: async _command => { + // Not supported + }, + }; + }, []); + + useEffect(() => { + if (!editorRef.current) return; + + if (editorRef.current.value !== props.content) { + editorRef.current.value = props.content; + } + }, [props.content]); + + const onChange = useCallback((event: any) => { + props.onChange({ changeId: null, content: event.target.value }); + }, [props.onChange]); + + return ( +
+