refactor(getNodesBounds): use system/getNodesBounds for hook
This commit is contained in:
@@ -1,15 +1,12 @@
|
|||||||
import { useMemo } from 'react';
|
import { useMemo } from 'react';
|
||||||
import {
|
import {
|
||||||
boxToRect,
|
|
||||||
EdgeRemoveChange,
|
EdgeRemoveChange,
|
||||||
evaluateAbsolutePosition,
|
evaluateAbsolutePosition,
|
||||||
getBoundsOfBoxes,
|
|
||||||
getElementsToRemove,
|
getElementsToRemove,
|
||||||
|
getNodesBounds,
|
||||||
getOverlappingArea,
|
getOverlappingArea,
|
||||||
isInternalNodeBase,
|
|
||||||
isRectObject,
|
isRectObject,
|
||||||
NodeRemoveChange,
|
NodeRemoveChange,
|
||||||
nodeToBox,
|
|
||||||
nodeToRect,
|
nodeToRect,
|
||||||
type Rect,
|
type Rect,
|
||||||
} from '@xyflow/system';
|
} from '@xyflow/system';
|
||||||
@@ -235,28 +232,8 @@ export function useReactFlow<NodeType extends Node = Node, EdgeType extends Edge
|
|||||||
);
|
);
|
||||||
},
|
},
|
||||||
getNodesBounds: (nodes: (NodeType | InternalNode | string)[]): Rect => {
|
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 { nodeLookup, nodeOrigin } = store.getState();
|
||||||
|
return getNodesBounds(nodes, { nodeLookup, nodeOrigin });
|
||||||
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);
|
|
||||||
},
|
},
|
||||||
getHandleConnections: ({ type, id, nodeId }) =>
|
getHandleConnections: ({ type, id, nodeId }) =>
|
||||||
Array.from(
|
Array.from(
|
||||||
|
|||||||
@@ -14,13 +14,10 @@ import {
|
|||||||
getViewportForBounds,
|
getViewportForBounds,
|
||||||
getElementsToRemove,
|
getElementsToRemove,
|
||||||
rendererPointToPoint,
|
rendererPointToPoint,
|
||||||
nodeToBox,
|
|
||||||
getBoundsOfBoxes,
|
|
||||||
boxToRect,
|
|
||||||
isInternalNodeBase,
|
|
||||||
evaluateAbsolutePosition,
|
evaluateAbsolutePosition,
|
||||||
type HandleType,
|
type HandleType,
|
||||||
type HandleConnection
|
type HandleConnection,
|
||||||
|
getNodesBounds
|
||||||
} from '@xyflow/system';
|
} from '@xyflow/system';
|
||||||
|
|
||||||
import { useStore } from '$lib/store';
|
import { useStore } from '$lib/store';
|
||||||
@@ -555,31 +552,10 @@ export function useSvelteFlow(): {
|
|||||||
nodes.update((nds) => nds);
|
nodes.update((nds) => nds);
|
||||||
},
|
},
|
||||||
getNodesBounds: (nodes) => {
|
getNodesBounds: (nodes) => {
|
||||||
if (nodes.length === 0) {
|
|
||||||
return { x: 0, y: 0, width: 0, height: 0 };
|
|
||||||
}
|
|
||||||
|
|
||||||
const _nodeLookup = get(nodeLookup);
|
const _nodeLookup = get(nodeLookup);
|
||||||
const _nodeOrigin = get(nodeOrigin);
|
const _nodeOrigin = get(nodeOrigin);
|
||||||
|
|
||||||
const box = nodes.reduce(
|
return getNodesBounds(nodes, { nodeLookup: _nodeLookup, nodeOrigin: _nodeOrigin });
|
||||||
(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);
|
|
||||||
},
|
},
|
||||||
getHandleConnections: ({ type, id, nodeId }) =>
|
getHandleConnections: ({ type, id, nodeId }) =>
|
||||||
Array.from(
|
Array.from(
|
||||||
|
|||||||
@@ -118,23 +118,26 @@ export const getNodePositionWithOrigin = (node: NodeBase, nodeOrigin: NodeOrigin
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
export type GetNodesBoundsParams = {
|
export type GetNodesBoundsParams<NodeType extends NodeBase = NodeBase> = {
|
||||||
nodeOrigin?: NodeOrigin;
|
nodeOrigin?: NodeOrigin;
|
||||||
|
nodeLookup?: NodeLookup<InternalNodeBase<NodeType>>;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Determines a bounding box that contains all given nodes in an array
|
* Determines a bounding box that contains all given nodes in an array
|
||||||
* @deprecated This function yields erronous results for subflows. Use getNodesBounds from useReactFlow/useSvelteFlow hook instead.
|
|
||||||
* @public
|
* @public
|
||||||
* @remarks Useful when combined with {@link getViewportForBounds} to calculate the correct transform to fit the given nodes in a viewport.
|
* @remarks Useful when combined with {@link getViewportForBounds} to calculate the correct transform to fit the given nodes in a viewport.
|
||||||
* @param nodes - Nodes to calculate the bounds for
|
* @param nodes - Nodes to calculate the bounds for
|
||||||
* @param params.nodeOrigin - Origin of the nodes: [0, 0] - top left, [0.5, 0.5] - center
|
* @param params.nodeOrigin - Origin of the nodes: [0, 0] - top left, [0.5, 0.5] - center
|
||||||
* @returns Bounding box enclosing all nodes
|
* @returns Bounding box enclosing all nodes
|
||||||
*/
|
*/
|
||||||
export const getNodesBounds = (nodes: NodeBase[], params: GetNodesBoundsParams = { nodeOrigin: [0, 0] }): Rect => {
|
export const getNodesBounds = <NodeType extends NodeBase = NodeBase>(
|
||||||
if (process.env.NODE_ENV === 'development') {
|
nodes: (NodeType | InternalNodeBase<NodeType> | string)[],
|
||||||
|
params: GetNodesBoundsParams<NodeType> = { nodeOrigin: [0, 0], nodeLookup: undefined }
|
||||||
|
): Rect => {
|
||||||
|
if (process.env.NODE_ENV === 'development' && !params.nodeLookup) {
|
||||||
console.warn(
|
console.warn(
|
||||||
'[DEPRECATED] `getNodesBounds` is deprecated because it yields erronous results for subflows. Use `getNodesBounds` from useReactFlow/useSvelteFlow hook instead.'
|
'Please use `getNodesBounds` from useReactFlow/useSvelteFlow hook to ensure correct values for sub flows.'
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -143,8 +146,19 @@ export const getNodesBounds = (nodes: NodeBase[], params: GetNodesBoundsParams =
|
|||||||
}
|
}
|
||||||
|
|
||||||
const box = nodes.reduce(
|
const box = nodes.reduce(
|
||||||
(currBox, node) => {
|
(currBox, nodeOrId) => {
|
||||||
const nodeBox = nodeToBox(node, params.nodeOrigin);
|
const isId = typeof nodeOrId === 'string';
|
||||||
|
let currentNode = !params.nodeLookup && !isId ? nodeOrId : undefined;
|
||||||
|
|
||||||
|
if (params.nodeLookup) {
|
||||||
|
currentNode = isId
|
||||||
|
? params.nodeLookup.get(nodeOrId)
|
||||||
|
: !isInternalNodeBase(nodeOrId)
|
||||||
|
? params.nodeLookup.get(nodeOrId.id)
|
||||||
|
: nodeOrId;
|
||||||
|
}
|
||||||
|
|
||||||
|
const nodeBox = currentNode ? nodeToBox(currentNode, params.nodeOrigin) : { x: 0, y: 0, x2: 0, y2: 0 };
|
||||||
return getBoundsOfBoxes(currBox, nodeBox);
|
return getBoundsOfBoxes(currBox, nodeBox);
|
||||||
},
|
},
|
||||||
{ x: Infinity, y: Infinity, x2: -Infinity, y2: -Infinity }
|
{ x: Infinity, y: Infinity, x2: -Infinity, y2: -Infinity }
|
||||||
|
|||||||
Reference in New Issue
Block a user