Merge pull request #4477 from xyflow/nodesbounds

Added getNodesBounds function to useReactFlow/useSvelteFlow
This commit is contained in:
Moritz Klack
2024-09-05 16:32:06 +02:00
committed by GitHub
6 changed files with 69 additions and 12 deletions
+18 -4
View File
@@ -16,7 +16,8 @@ import {
rendererPointToPoint,
evaluateAbsolutePosition,
type HandleType,
type HandleConnection
type HandleConnection,
getNodesBounds
} from '@xyflow/system';
import { useStore } from '$lib/store';
@@ -233,7 +234,14 @@ export function useSvelteFlow(): {
*/
toObject: () => { nodes: Node[]; edges: Edge[]; viewport: Viewport };
/**
* Gets all connections for a given handle belonging to a specific node.
* 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 | InternalNode | string)[]) => Rect;
/** Gets all connections for a given handle belonging to a specific node.
*
* @param type - handle type 'source' or 'target'
* @param id - the handle id (this is only needed if you have multiple handles of the same type, meaning you have to provide a unique id for each handle)
@@ -266,9 +274,9 @@ export function useSvelteFlow(): {
edges,
domNode,
nodeLookup,
nodeOrigin,
edgeLookup,
connectionLookup,
nodeOrigin
connectionLookup
} = useStore();
const getNodeRect = (node: Node | { id: Node['id'] }): Rect | null => {
@@ -543,6 +551,12 @@ export function useSvelteFlow(): {
nodes.update((nds) => nds);
},
getNodesBounds: (nodes) => {
const _nodeLookup = get(nodeLookup);
const _nodeOrigin = get(nodeOrigin);
return getNodesBounds(nodes, { nodeLookup: _nodeLookup, nodeOrigin: _nodeOrigin });
},
getHandleConnections: ({ type, id, nodeId }) =>
Array.from(
get(connectionLookup)
@@ -1,11 +1,12 @@
<script lang="ts">
import { getContext } from 'svelte';
import { getNodesBounds, Position, getNodeToolbarTransform } from '@xyflow/system';
import { Position, getNodeToolbarTransform } from '@xyflow/system';
import portal from '$lib/actions/portal';
import type { InternalNode } from '$lib/types';
import { useStore } from '$lib/store';
import type { NodeToolbarProps } from './types';
import { useSvelteFlow } from '$lib/hooks/useSvelteFlow';
type $$Props = NodeToolbarProps;
@@ -15,7 +16,8 @@
export let offset: $$Props['offset'] = undefined;
export let isVisible: $$Props['isVisible'] = undefined;
const { domNode, viewport, nodeLookup, nodes, nodeOrigin } = useStore();
const { domNode, viewport, nodeLookup, nodes } = useStore();
const { getNodesBounds } = useSvelteFlow();
const contextNodeId = getContext<string>('svelteflow__node_id');
let transform: string;
@@ -42,7 +44,7 @@
}
$: {
const nodeRect = getNodesBounds(toolbarNodes, { nodeOrigin: $nodeOrigin });
const nodeRect = getNodesBounds(toolbarNodes);
if (nodeRect) {
transform = getNodeToolbarTransform(nodeRect, $viewport, _position, _offset, _align);