Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix(react/ribbon): attribute editor sometimes not clearing between notes
Elian Doran461eb273d951
diff --git a/apps/client/src/widgets/react/CKEditor.tsx b/apps/client/src/widgets/react/CKEditor.tsxindex 27985fda58..8b8839a382 100644--- a/apps/client/src/widgets/react/CKEditor.tsx+++ b/apps/client/src/widgets/react/CKEditor.tsx@@ -3,7 +3,15 @@ import { useEffect, useImperativeHandle, useRef } from "preact/compat";import { MutableRef } from "preact/hooks";export interface CKEditorApi {- focus: () => void;+ focus(): void;+ /**+ * Imperatively sets the text in the editor.+ *+ * Prefer setting `currentValue` prop where possible.+ *+ * @param text text to set in the editor+ */+ setText(text: string): void;}interface CKEditorOpts {@@ -34,6 +42,9 @@ export default function CKEditor({ apiRef, currentValue, editor, config, disablewriter.setSelection(writer.createPositionAt(documentRoot, "end"));}});+ },+ setText(text: string) {+ textEditorRef.current?.setData(text);}};}, [ editorContainerRef ]);diff --git a/apps/client/src/widgets/ribbon/components/AttributeEditor.tsx b/apps/client/src/widgets/ribbon/components/AttributeEditor.tsxindex 73a37fb4af..9c60845417 100644--- a/apps/client/src/widgets/ribbon/components/AttributeEditor.tsx+++ b/apps/client/src/widgets/ribbon/components/AttributeEditor.tsx@@ -86,10 +86,9 @@ export default function AttributeEditor({ note, componentId, notePath, ntxId, hiconst [ state, setState ] = useState<"normal" | "showHelpTooltip" | "showAttributeDetail">();const [ error, setError ] = useState<unknown>();const [ needsSaving, setNeedsSaving ] = useState(false);- const [ initialValue, setInitialValue ] = useState<string>("");const lastSavedContent = useRef<string>();- const currentValueRef = useRef(initialValue);+ const currentValueRef = useRef("");const wrapperRef = useRef<HTMLDivElement>(null);const editorRef = useRef<CKEditorApi>();@@ -126,7 +125,7 @@ export default function AttributeEditor({ note, componentId, notePath, ntxId, hihtmlAttrs += " ";}- setInitialValue(htmlAttrs);+ editorRef.current?.setText(htmlAttrs);}function parseAttributes() {@@ -290,7 +289,7 @@ export default function AttributeEditor({ note, componentId, notePath, ntxId, hiclassName="attribute-list-editor"tabIndex={200}editor={CKEditorAttributeEditor}- currentValue={initialValue}+ currentValue="" // handled imperativelyconfig={{toolbar: { items: [] },placeholder: t("attribute_editor.placeholder"),