From 63e3a27b345e77601f641aeb54a78662c62e0a24 Mon Sep 17 00:00:00 2001 From: Elian Doran Date: Sat, 20 Sep 2025 09:47:28 +0300 Subject: [PATCH] refactor(react/type_widget): simplify handling of new notes --- apps/client/src/widgets/react/hooks.tsx | 11 ++++++++++- apps/client/src/widgets/type_widgets/code/Code.tsx | 14 +++++--------- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/apps/client/src/widgets/react/hooks.tsx b/apps/client/src/widgets/react/hooks.tsx index 85e43ab09..5d5091a8a 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, interval = return spacedUpdateRef.current; } -export function useEditorSpacedUpdate({ note, getData, dataSaved }: { +export function useEditorSpacedUpdate({ note, getData, onContentChange, dataSaved }: { note: FNote, getData: () => Promise | 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 24899b67e..a910bab1e 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(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 (