fix(npc-dialogue): persist node drags into the flow field value
Dragging a card updated the canvas but never wrote to the Payload field, so document Save persisted stale positions. Wrap onNodesChange and serialize once a drag settles (the final position change with dragging === false), keeping the write off the mousemove hot path. Selection and in-progress-drag flags are stripped before serializing so clicking a card never marks the document dirty.
This commit is contained in:
parent
46f1abd4da
commit
3c70fa70d8
1 changed files with 28 additions and 3 deletions
|
|
@ -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<Node>[]) => {
|
||||
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() {
|
|||
<ReactFlow
|
||||
nodes={nodes}
|
||||
edges={syncedEdges}
|
||||
onNodesChange={onNodesChange}
|
||||
onNodesChange={handleNodesChange}
|
||||
onConnect={(c: Connection) => {
|
||||
if (c.source && c.target) applyConnection(c.source, c.target, c.sourceHandle);
|
||||
}}
|
||||
|
|
|
|||
Loading…
Reference in a new issue