feat(react, svelte) added getIncomers & getOutgoers to use...Flow()

This commit is contained in:
Peter
2023-10-05 13:28:04 +02:00
parent 1b0790e79f
commit 6fd9d19797
4 changed files with 64 additions and 6 deletions
+34 -2
View File
@@ -1,5 +1,13 @@
import { useCallback, useMemo } from 'react';
import { getElementsToRemove, getOverlappingArea, isRectObject, nodeToRect, type Rect } from '@xyflow/system';
import {
getElementsToRemove,
getIncomersBase,
getOutgoersBase,
getOverlappingArea,
isRectObject,
nodeToRect,
type Rect,
} from '@xyflow/system';
import useViewportHelper from './useViewportHelper';
import { useStoreApi } from '../hooks/useStore';
@@ -224,7 +232,7 @@ export default function useReactFlow<NodeData = any, EdgeData = any>(): ReactFlo
);
const getConnectedEdges = useCallback<Instance.getConnectedEdges>((node) => {
const edges = store.getState().edges;
const { edges } = store.getState();
const nodeIds = new Set();
if (typeof node === 'string') {
@@ -238,6 +246,26 @@ export default function useReactFlow<NodeData = any, EdgeData = any>(): ReactFlo
return edges.filter((edge) => nodeIds.has(edge.source) || nodeIds.has(edge.target));
}, []);
const getIncomers = useCallback<Instance.getIncomers>((node) => {
const { nodes, edges } = store.getState();
if (typeof node === 'string') {
return getIncomersBase({ id: node }, nodes, edges);
}
return getIncomersBase(node, nodes, edges);
}, []);
const getOutgoers = useCallback<Instance.getOutgoers>((node) => {
const { nodes, edges } = store.getState();
if (typeof node == 'string') {
return getOutgoersBase({ id: node }, nodes, edges);
}
return getOutgoersBase(node, nodes, edges);
}, []);
return useMemo(() => {
return {
...viewportHelper,
@@ -254,6 +282,8 @@ export default function useReactFlow<NodeData = any, EdgeData = any>(): ReactFlo
getIntersectingNodes,
isNodeIntersecting,
getConnectedEdges,
getIncomers,
getOutgoers,
};
}, [
viewportHelper,
@@ -270,5 +300,7 @@ export default function useReactFlow<NodeData = any, EdgeData = any>(): ReactFlo
getIntersectingNodes,
isNodeIntersecting,
getConnectedEdges,
getIncomers,
getOutgoers,
]);
}
+2
View File
@@ -40,6 +40,8 @@ export namespace Instance {
partially?: boolean
) => boolean;
export type getConnectedEdges = (id: string | (Partial<Node> & { id: Node['id'] })[]) => Edge[];
export type getIncomers = (node: string | (Partial<Node> & { id: Node['id'] })) => Node[];
export type getOutgoers = (node: string | (Partial<Node> & { id: Node['id'] })) => Node[];
}
export type ReactFlowInstance<NodeData = any, EdgeData = any> = {