From 39bd236799ee5d555bd82a00d0b38c656003cf80 Mon Sep 17 00:00:00 2001 From: Elian Doran Date: Mon, 29 Sep 2025 19:32:50 +0300 Subject: [PATCH] refactor(type_widgets): use editorspaced update for data --- .../src/widgets/type_widgets/RelationMap.tsx | 67 ++++++++++--------- 1 file changed, 34 insertions(+), 33 deletions(-) diff --git a/apps/client/src/widgets/type_widgets/RelationMap.tsx b/apps/client/src/widgets/type_widgets/RelationMap.tsx index 7067f9a96..41c7afcdb 100644 --- a/apps/client/src/widgets/type_widgets/RelationMap.tsx +++ b/apps/client/src/widgets/type_widgets/RelationMap.tsx @@ -37,9 +37,40 @@ const uniDirectionalOverlays: OverlaySpec[] = [ ]; export default function RelationMap({ note }: TypeWidgetProps) { - const data = useData(note); + const [ data, setData ] = useState(); const containerRef = useRef(null); const apiRef = useRef(null); + const spacedUpdate = useEditorSpacedUpdate({ + note, + getData() { + }, + onContentChange(content) { + if (content) { + try { + setData(JSON.parse(content)); + return; + } catch (e) { + console.log("Could not parse content: ", e); + } + } + + setData({ + notes: [], + // it is important to have this exact value here so that initial transform is the same as this + // which will guarantee note won't be saved on first conversion to the relation map note type + // this keeps the principle that note type change doesn't destroy note content unless user + // does some actual change + transform: { + x: 0, + y: 0, + scale: 1 + } + }); + }, + dataSaved() { + + } + }) const onTransform = useCallback(() => { if (!containerRef.current || !apiRef.current) return; @@ -60,7 +91,7 @@ export default function RelationMap({ note }: TypeWidgetProps) { return e.altKey; } }, - transformData: data.transform, + transformData: data?.transform, onTransform }); @@ -78,7 +109,7 @@ export default function RelationMap({ note }: TypeWidgetProps) { HoverPaintStyle: { stroke: "#777", strokeWidth: 1 }, }} > - {data.notes.map(note => ( + {data?.notes.map(note => ( ))} @@ -87,36 +118,6 @@ export default function RelationMap({ note }: TypeWidgetProps) { ) } -function useData(note: FNote) { - const blob = useNoteBlob(note); - let content: MapData | null = null; - - if (blob?.content) { - try { - content = JSON.parse(blob.content); - } catch (e) { - console.log("Could not parse content: ", e); - } - } - - if (!content) { - content = { - notes: [], - // it is important to have this exact value here so that initial transform is the same as this - // which will guarantee note won't be saved on first conversion to the relation map note type - // this keeps the principle that note type change doesn't destroy note content unless user - // does some actual change - transform: { - x: 0, - y: 0, - scale: 1 - } - }; - } - - return content; -} - function usePanZoom({ containerRef, options, transformData, onTransform }: { containerRef: RefObject; options: PanZoomOptions;