added getNodesBounds inside main hook
This commit is contained in:
@@ -1,11 +1,14 @@
|
|||||||
import { useMemo } from 'react';
|
import { useMemo } from 'react';
|
||||||
import {
|
import {
|
||||||
|
boxToRect,
|
||||||
EdgeRemoveChange,
|
EdgeRemoveChange,
|
||||||
evaluateAbsolutePosition,
|
evaluateAbsolutePosition,
|
||||||
|
getBoundsOfBoxes,
|
||||||
getElementsToRemove,
|
getElementsToRemove,
|
||||||
getOverlappingArea,
|
getOverlappingArea,
|
||||||
isRectObject,
|
isRectObject,
|
||||||
NodeRemoveChange,
|
NodeRemoveChange,
|
||||||
|
nodeToBox,
|
||||||
nodeToRect,
|
nodeToRect,
|
||||||
type Rect,
|
type Rect,
|
||||||
} from '@xyflow/system';
|
} from '@xyflow/system';
|
||||||
@@ -230,6 +233,26 @@ export function useReactFlow<NodeType extends Node = Node, EdgeType extends Edge
|
|||||||
options
|
options
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
|
getNodesBounds: (nodes: (NodeType | 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) : node.parentId ? 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);
|
||||||
|
},
|
||||||
};
|
};
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
|
|||||||
@@ -173,6 +173,14 @@ export type GeneralHelpers<NodeType extends Node = Node, EdgeType extends Edge =
|
|||||||
dataUpdate: Partial<EdgeType['data']> | ((edge: EdgeType) => Partial<EdgeType['data']>),
|
dataUpdate: Partial<EdgeType['data']> | ((edge: EdgeType) => Partial<EdgeType['data']>),
|
||||||
options?: { replace: boolean }
|
options?: { replace: boolean }
|
||||||
) => void;
|
) => void;
|
||||||
|
/**
|
||||||
|
* Returns the bounds of the given nodes or node ids.
|
||||||
|
*
|
||||||
|
* @param nodes - the nodes or node ids to calculate the bounds for
|
||||||
|
*
|
||||||
|
* @returns the bounds of the given nodes
|
||||||
|
*/
|
||||||
|
getNodesBounds: (nodes: (NodeType | string)[]) => Rect;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type ReactFlowInstance<NodeType extends Node = Node, EdgeType extends Edge = Edge> = GeneralHelpers<
|
export type ReactFlowInstance<NodeType extends Node = Node, EdgeType extends Edge = Edge> = GeneralHelpers<
|
||||||
|
|||||||
@@ -14,7 +14,10 @@ import {
|
|||||||
getViewportForBounds,
|
getViewportForBounds,
|
||||||
getElementsToRemove,
|
getElementsToRemove,
|
||||||
rendererPointToPoint,
|
rendererPointToPoint,
|
||||||
nodeHasDimensions
|
nodeHasDimensions,
|
||||||
|
nodeToBox,
|
||||||
|
getBoundsOfBoxes,
|
||||||
|
boxToRect
|
||||||
} from '@xyflow/system';
|
} from '@xyflow/system';
|
||||||
|
|
||||||
import { useStore } from '$lib/store';
|
import { useStore } from '$lib/store';
|
||||||
@@ -230,6 +233,14 @@ export function useSvelteFlow(): {
|
|||||||
* @returns the nodes, edges and the viewport as a JSON object
|
* @returns the nodes, edges and the viewport as a JSON object
|
||||||
*/
|
*/
|
||||||
toObject: () => { nodes: Node[]; edges: Edge[]; viewport: Viewport };
|
toObject: () => { nodes: Node[]; edges: Edge[]; viewport: Viewport };
|
||||||
|
/**
|
||||||
|
* Returns the bounds of the given nodes or node ids.
|
||||||
|
*
|
||||||
|
* @param nodes - the nodes or node ids to calculate the bounds for
|
||||||
|
*
|
||||||
|
* @returns the bounds of the given nodes
|
||||||
|
*/
|
||||||
|
getNodesBounds: (nodes: (Node | string)[]) => Rect;
|
||||||
} {
|
} {
|
||||||
const {
|
const {
|
||||||
zoomIn,
|
zoomIn,
|
||||||
@@ -247,6 +258,7 @@ export function useSvelteFlow(): {
|
|||||||
edges,
|
edges,
|
||||||
domNode,
|
domNode,
|
||||||
nodeLookup,
|
nodeLookup,
|
||||||
|
nodeOrigin,
|
||||||
edgeLookup
|
edgeLookup
|
||||||
} = useStore();
|
} = useStore();
|
||||||
|
|
||||||
@@ -506,7 +518,34 @@ export function useSvelteFlow(): {
|
|||||||
|
|
||||||
nodes.update((nds) => nds);
|
nodes.update((nds) => nds);
|
||||||
},
|
},
|
||||||
viewport
|
viewport,
|
||||||
|
getNodesBounds: (nodes) => {
|
||||||
|
if (nodes.length === 0) {
|
||||||
|
return { x: 0, y: 0, width: 0, height: 0 };
|
||||||
|
}
|
||||||
|
|
||||||
|
const _nodeLookup = get(nodeLookup);
|
||||||
|
const _nodeOrigin = get(nodeOrigin);
|
||||||
|
|
||||||
|
const box = nodes.reduce(
|
||||||
|
(currBox, node) => {
|
||||||
|
const internalNode =
|
||||||
|
typeof node === 'string'
|
||||||
|
? _nodeLookup.get(node)
|
||||||
|
: node.parentId
|
||||||
|
? _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);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
function getElements(lookup: Map<string, InternalNode>, ids: string[]): Node[];
|
function getElements(lookup: Map<string, InternalNode>, ids: string[]): Node[];
|
||||||
|
|||||||
Reference in New Issue
Block a user