refactor(getNodesBounds): use system/getNodesBounds for hook

This commit is contained in:
moklick
2024-09-04 12:51:43 +02:00
parent c906192ab1
commit 091d6bac68
3 changed files with 26 additions and 59 deletions
+2 -25
View File
@@ -1,15 +1,12 @@
import { useMemo } from 'react';
import {
boxToRect,
EdgeRemoveChange,
evaluateAbsolutePosition,
getBoundsOfBoxes,
getElementsToRemove,
getNodesBounds,
getOverlappingArea,
isInternalNodeBase,
isRectObject,
NodeRemoveChange,
nodeToBox,
nodeToRect,
type Rect,
} from '@xyflow/system';
@@ -235,28 +232,8 @@ export function useReactFlow<NodeType extends Node = Node, EdgeType extends Edge
);
},
getNodesBounds: (nodes: (NodeType | InternalNode | string)[]): Rect => {
if (nodes.length === 0) {
return { x: 0, y: 0, width: 0, height: 0 };
}
const { nodeLookup, nodeOrigin } = store.getState();
const box = nodes.reduce(
(currBox, node) => {
const internalNode =
typeof node === 'string'
? nodeLookup.get(node)
: !isInternalNodeBase(node)
? nodeLookup.get(node.id)
: node;
const nodeBox = internalNode ? nodeToBox(internalNode, nodeOrigin) : { x: 0, y: 0, x2: 0, y2: 0 };
return getBoundsOfBoxes(currBox, nodeBox);
},
{ x: Infinity, y: Infinity, x2: -Infinity, y2: -Infinity }
);
return boxToRect(box);
return getNodesBounds(nodes, { nodeLookup, nodeOrigin });
},
getHandleConnections: ({ type, id, nodeId }) =>
Array.from(