From 181dc66b8c969a642ec151bd5a864667ee02e11d Mon Sep 17 00:00:00 2001 From: braks <78412429+bcakmakoglu@users.noreply.github.com> Date: Tue, 20 Aug 2024 13:04:41 +0200 Subject: [PATCH 1/3] feat(react,svelte): add `getHandleConnections` helper to `useReactFlow` & `useSvelteFlow` --- packages/react/src/hooks/useReactFlow.ts | 7 +++++ packages/react/src/types/instance.ts | 19 ++++++++++++- .../svelte/src/lib/hooks/useSvelteFlow.ts | 28 ++++++++++++++++++- 3 files changed, 52 insertions(+), 2 deletions(-) diff --git a/packages/react/src/hooks/useReactFlow.ts b/packages/react/src/hooks/useReactFlow.ts index faf9d31f..8f303787 100644 --- a/packages/react/src/hooks/useReactFlow.ts +++ b/packages/react/src/hooks/useReactFlow.ts @@ -230,6 +230,13 @@ export function useReactFlow + Array.from( + store + .getState() + .connectionLookup.get(`${nodeId}-${type}-${id ?? null}`) + ?.values() ?? [] + ), }; }, []); diff --git a/packages/react/src/types/instance.ts b/packages/react/src/types/instance.ts index 5f4aa3b3..01bb0c13 100644 --- a/packages/react/src/types/instance.ts +++ b/packages/react/src/types/instance.ts @@ -1,5 +1,5 @@ /* eslint-disable @typescript-eslint/no-namespace */ -import type { Rect, Viewport } from '@xyflow/system'; +import type { HandleConnection, HandleType, Rect, Viewport } from '@xyflow/system'; import type { Node, Edge, ViewportHelperFunctions, InternalNode } from '.'; export type ReactFlowJsonObject = { @@ -173,6 +173,23 @@ export type GeneralHelpers | ((edge: EdgeType) => Partial), options?: { replace: boolean } ) => void; + /** + * 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[]; }; export type ReactFlowInstance = GeneralHelpers< diff --git a/packages/svelte/src/lib/hooks/useSvelteFlow.ts b/packages/svelte/src/lib/hooks/useSvelteFlow.ts index 4ed08350..7fb7f5cf 100644 --- a/packages/svelte/src/lib/hooks/useSvelteFlow.ts +++ b/packages/svelte/src/lib/hooks/useSvelteFlow.ts @@ -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 }; } From b65aed19840c515949bef236a23d5f0a754cdeb4 Mon Sep 17 00:00:00 2001 From: braks <78412429+bcakmakoglu@users.noreply.github.com> Date: Tue, 20 Aug 2024 13:08:02 +0200 Subject: [PATCH 2/3] chore(changeset): add --- .changeset/new-schools-watch.md | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 .changeset/new-schools-watch.md diff --git a/.changeset/new-schools-watch.md b/.changeset/new-schools-watch.md new file mode 100644 index 00000000..22292626 --- /dev/null +++ b/.changeset/new-schools-watch.md @@ -0,0 +1,6 @@ +--- +'@xyflow/react': minor +'@xyflow/svelte': minor +--- + +Add `getHandleConnections` helper to `useReactFlow` & `useSvelteFlow` From 9ae914561adc082262a471c1c38a9fcf85d09b6a Mon Sep 17 00:00:00 2001 From: braks <78412429+bcakmakoglu@users.noreply.github.com> Date: Tue, 20 Aug 2024 13:12:33 +0200 Subject: [PATCH 3/3] chore: update comment --- packages/react/src/types/instance.ts | 2 +- packages/svelte/src/lib/hooks/useSvelteFlow.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/react/src/types/instance.ts b/packages/react/src/types/instance.ts index 01bb0c13..5806353f 100644 --- a/packages/react/src/types/instance.ts +++ b/packages/react/src/types/instance.ts @@ -177,7 +177,7 @@ export type GeneralHelpers