From f30d0898ec629c392472311a6eeb418764ed9013 Mon Sep 17 00:00:00 2001 From: Aman Harwara Date: Wed, 30 Nov 2022 18:22:41 +0530 Subject: [PATCH] fix: "Prevent editing" option not working correctly for Super notes --- .../Plugins/ReadonlyPlugin/ReadonlyPlugin.tsx | 30 +++++++++++++++++++ .../NoteView/SuperEditor/SuperEditor.tsx | 2 ++ 2 files changed, 32 insertions(+) create mode 100644 packages/web/src/javascripts/Components/NoteView/SuperEditor/Plugins/ReadonlyPlugin/ReadonlyPlugin.tsx diff --git a/packages/web/src/javascripts/Components/NoteView/SuperEditor/Plugins/ReadonlyPlugin/ReadonlyPlugin.tsx b/packages/web/src/javascripts/Components/NoteView/SuperEditor/Plugins/ReadonlyPlugin/ReadonlyPlugin.tsx new file mode 100644 index 000000000..1b91c2ed7 --- /dev/null +++ b/packages/web/src/javascripts/Components/NoteView/SuperEditor/Plugins/ReadonlyPlugin/ReadonlyPlugin.tsx @@ -0,0 +1,30 @@ +import { useApplication } from '@/Components/ApplicationView/ApplicationProvider' +import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext' +import { SNNote, ContentType } from '@standardnotes/snjs' +import { useState, useEffect } from 'react' + +const ReadonlyPlugin = ({ note }: { note: SNNote }) => { + const application = useApplication() + const [editor] = useLexicalComposerContext() + const [readOnly, setReadOnly] = useState(note.locked) + + useEffect(() => { + return application.streamItems(ContentType.Note, ({ changed }) => { + const changedNoteItem = changed.find((changedItem) => changedItem.uuid === note.uuid) + + if (changedNoteItem) { + setReadOnly(changedNoteItem.locked) + } + }) + }, [application, note.uuid]) + + useEffect(() => { + editor.update(() => { + editor.setEditable(!readOnly) + }) + }, [editor, readOnly]) + + return null +} + +export default ReadonlyPlugin diff --git a/packages/web/src/javascripts/Components/NoteView/SuperEditor/SuperEditor.tsx b/packages/web/src/javascripts/Components/NoteView/SuperEditor/SuperEditor.tsx index 717c29cbc..f2f178f1a 100644 --- a/packages/web/src/javascripts/Components/NoteView/SuperEditor/SuperEditor.tsx +++ b/packages/web/src/javascripts/Components/NoteView/SuperEditor/SuperEditor.tsx @@ -37,6 +37,7 @@ import { ExportPlugin } from './Plugins/ExportPlugin/ExportPlugin' import GetMarkdownPlugin, { GetMarkdownPluginInterface } from './Plugins/GetMarkdownPlugin/GetMarkdownPlugin' import { AutoFocusPlugin } from '@lexical/react/LexicalAutoFocusPlugin' import { getPlaintextFontSize } from '@/Utils/getPlaintextFontSize' +import ReadonlyPlugin from './Plugins/ReadonlyPlugin/ReadonlyPlugin' const NotePreviewCharLimit = 160 @@ -190,6 +191,7 @@ export const SuperEditor: FunctionComponent = ({ + {controller.isTemplateNote ? : null}