diff --git a/src/components/admin/npc-dialogue/NpcDialogueEditor.tsx b/src/components/admin/npc-dialogue/NpcDialogueEditor.tsx index 7aa99795..1f7552c1 100644 --- a/src/components/admin/npc-dialogue/NpcDialogueEditor.tsx +++ b/src/components/admin/npc-dialogue/NpcDialogueEditor.tsx @@ -14,6 +14,7 @@ import { type Edge, MarkerType, type Node, + type NodeChange, ReactFlow, ReactFlowProvider, useNodesState, @@ -142,9 +143,12 @@ function FlowEditor() { ); const serialize = useCallback((nds: Node[]) => { + // Volatile interaction flags (selection, in-progress drags) are not part + // of the graph and must not leak into the persisted value. + const clean = nds.map(({ selected, dragging, ...rest }) => rest) as DialogueNode[]; const payload: FlowData = { - nodes: nds as DialogueNode[], - edges: deriveEdges(nds as DialogueNode[]), + nodes: clean, + edges: deriveEdges(clean), }; const serialized = JSON.stringify(payload); if (serialized !== lastSerialized.current) { @@ -153,6 +157,27 @@ function FlowEditor() { } }, [setValue]); + // Dragging fires a position change per mousemove; only the settled one + // (dragging === false) is worth writing to the field value. The flag is + // consumed by the effect below, which sees the updated nodes state. + const pendingPositionSave = useRef(false); + + const handleNodesChange = useCallback( + (changes: NodeChange[]) => { + if (changes.some((c) => c.type === "position" && !c.dragging)) { + pendingPositionSave.current = true; + } + onNodesChange(changes); + }, + [onNodesChange], + ); + + useEffect(() => { + if (!pendingPositionSave.current) return; + pendingPositionSave.current = false; + serialize(nodes); + }, [nodes, serialize]); + /** * Adds a node. Without an explicit position it lands in the middle of the * current viewport (with a small cascade offset), so toolbar-added nodes @@ -495,7 +520,7 @@ function FlowEditor() { { if (c.source && c.target) applyConnection(c.source, c.target, c.sourceHandle); }}