feat(react,svelte): add getHandleConnections helper to useReactFlow & useSvelteFlow

This commit is contained in:
braks
2024-08-20 13:04:41 +02:00
parent 9b768b2dae
commit 181dc66b8c
3 changed files with 52 additions and 2 deletions
+27 -1
View File
@@ -14,7 +14,9 @@ import {
getViewportForBounds,
getElementsToRemove,
rendererPointToPoint,
evaluateAbsolutePosition
evaluateAbsolutePosition,
type HandleType,
type HandleConnection
} from '@xyflow/system';
import { useStore } from '$lib/store';
@@ -230,6 +232,23 @@ export function useSvelteFlow(): {
* @returns the nodes, edges and the viewport as a JSON object
*/
toObject: () => { nodes: Node[]; edges: Edge[]; viewport: Viewport };
/**
* Gets all connections for a given handle belonging to a specific node.
*
* @param type - handle type 'source' or 'target'
* @param id - the handle id
* @param nodeId - the node id the handle belongs to
* @returns an array with handle connections
*/
getHandleConnections: ({
type,
id,
nodeId
}: {
type: HandleType;
nodeId: string;
id?: string | null;
}) => HandleConnection[];
} {
const {
zoomIn,
@@ -248,6 +267,7 @@ export function useSvelteFlow(): {
domNode,
nodeLookup,
edgeLookup,
connectionLookup,
nodeOrigin
} = useStore();
@@ -523,6 +543,12 @@ export function useSvelteFlow(): {
nodes.update((nds) => nds);
},
getHandleConnections: ({ type, id, nodeId }) =>
Array.from(
get(connectionLookup)
.get(`${nodeId}-${type}-${id ?? null}`)
?.values() ?? []
),
viewport
};
}