make fitView fire even when onNodesChange is not implmented

This commit is contained in:
peterkogo
2025-04-01 12:39:42 +02:00
parent 245b6265a6
commit aae3f49eec
@@ -42,13 +42,16 @@ export function BatchProvider<NodeType extends Node = Node, EdgeType extends Edg
if (hasDefaultNodes) { if (hasDefaultNodes) {
setNodes(next); setNodes(next);
} else if (onNodesChange) { } else {
// When a controlled flow is used we need to collect the changes
const changes = getElementsDiffChanges({ const changes = getElementsDiffChanges({
items: next, items: next,
lookup: nodeLookup, lookup: nodeLookup,
}) as NodeChange<NodeType>[]; }) as NodeChange<NodeType>[];
// We only want to fire onNodesChange if there are changes to the nodes
if (changes.length > 0) { if (changes.length > 0) {
onNodesChange(changes); onNodesChange?.(changes);
} else if (fitViewQueued) { } else if (fitViewQueued) {
// If there are no changes to the nodes, we still need to call setNodes // If there are no changes to the nodes, we still need to call setNodes
// to trigger a re-render and fitView. // to trigger a re-render and fitView.
@@ -61,6 +64,7 @@ export function BatchProvider<NodeType extends Node = Node, EdgeType extends Edg
} }
} }
}, []); }, []);
const nodeQueue = useQueue<NodeType>(nodeQueueHandler); const nodeQueue = useQueue<NodeType>(nodeQueueHandler);
const edgeQueueHandler = useCallback((queueItems: QueueItem<EdgeType>[]) => { const edgeQueueHandler = useCallback((queueItems: QueueItem<EdgeType>[]) => {