Skip to content

Build your personal knowledge base with Trilium Notes

refactor(react/type_widget): simplify handling of new notes

Elian Doran63e3a27b345e
diff --git a/apps/client/src/widgets/react/hooks.tsx b/apps/client/src/widgets/react/hooks.tsx
index 85e43ab093..5d5091a8ab 100644
--- a/apps/client/src/widgets/react/hooks.tsx
+++ b/apps/client/src/widgets/react/hooks.tsx
@@ -75,12 +75,15 @@ export function useSpacedUpdate(callback: () => void | Promise<void>, interval =
return spacedUpdateRef.current;
}
-export function useEditorSpacedUpdate({ note, getData, dataSaved }: {
+export function useEditorSpacedUpdate({ note, getData, onContentChange, dataSaved }: {
note: FNote,
getData: () => Promise<object | undefined> | object | undefined,
+ onContentChange: (newContent: string) => void,
dataSaved?: () => void
}) {
const parentComponent = useContext(ParentComponent);
+ const blob = useNoteBlob(note);
+
const callback = useMemo(() => {
return async () => {
const data = await getData();
@@ -95,6 +98,12 @@ export function useEditorSpacedUpdate({ note, getData, dataSaved }: {
}
}, [ note, getData, dataSaved ])
const spacedUpdate = useSpacedUpdate(callback);
+
+ useEffect(() => {
+ if (!blob) return;
+ spacedUpdate.allowUpdateWithoutChange(() => onContentChange(blob.content));
+ }, [ blob ]);
+
return spacedUpdate;
}
diff --git a/apps/client/src/widgets/type_widgets/code/Code.tsx b/apps/client/src/widgets/type_widgets/code/Code.tsx
index 24899b67e5..a910bab1e4 100644
--- a/apps/client/src/widgets/type_widgets/code/Code.tsx
+++ b/apps/client/src/widgets/type_widgets/code/Code.tsx
@@ -31,21 +31,17 @@ export function ReadOnlyCode({ note, viewScope, ntxId }: TypeWidgetProps) {
export function EditableCode({ note, ntxId }: TypeWidgetProps) {
const editorRef = useRef<VanillaCodeMirror>(null);
- const blob = useNoteBlob(note);
const spacedUpdate = useEditorSpacedUpdate({
note,
- getData: () => ({ content: editorRef.current?.getText() })
- });
-
- useEffect(() => {
- spacedUpdate.allowUpdateWithoutChange(() => {
+ getData: () => ({ content: editorRef.current?.getText() }),
+ onContentChange: (content) => {
const codeEditor = editorRef.current;
if (!codeEditor) return;
- codeEditor.setText(blob?.content ?? "");
+ codeEditor.setText(content ?? "");
codeEditor.setMimeType(note.mime);
codeEditor.clearHistory();
- });
- }, [ blob ]);
+ }
+ });
return (
<div className="note-detail-code note-detail-printable">