feat(edge-utils): add getHandlePosition and getHandle

This commit is contained in:
moklick
2023-03-14 13:18:59 +01:00
parent 6ee3c66371
commit 97e2863c49
4 changed files with 51 additions and 96 deletions
@@ -6,9 +6,10 @@ import { errorMessages, ConnectionMode, Position } from '@reactflow/system';
import { useStore } from '../../hooks/useStore';
import useVisibleEdges from '../../hooks/useVisibleEdges';
import MarkerDefinitions from './MarkerDefinitions';
import { getEdgePositions, getHandle, getNodeData } from './utils';
import { getEdgePositions, getNodeData } from './utils';
import { GraphViewProps } from '../GraphView';
import type { Edge, ReactFlowState } from '../../types';
import { getHandle } from '@reactflow/edge-utils';
type EdgeRendererProps = Pick<
GraphViewProps,
@@ -13,6 +13,7 @@ import { rectToBox } from '@reactflow/utils';
import { BezierEdge, SmoothStepEdge, StepEdge, StraightEdge, SimpleBezierEdge } from '../../components/Edges';
import wrapEdge from '../../components/Edges/wrapEdge';
import type { EdgeProps, EdgeTypes, EdgeTypesWrapped, Node } from '../../types';
import { getHandlePosition } from '@reactflow/edge-utils';
export type CreateEdgeTypes = (edgeTypes: EdgeTypes) => EdgeTypesWrapped;
@@ -40,50 +41,6 @@ export function createEdgeTypes(edgeTypes: EdgeTypes): EdgeTypesWrapped {
};
}
export function getHandlePosition(position: Position, nodeRect: Rect, handle: HandleElement | null = null): XYPosition {
const x = (handle?.x || 0) + nodeRect.x;
const y = (handle?.y || 0) + nodeRect.y;
const width = handle?.width || nodeRect.width;
const height = handle?.height || nodeRect.height;
switch (position) {
case Position.Top:
return {
x: x + width / 2,
y,
};
case Position.Right:
return {
x: x + width,
y: y + height / 2,
};
case Position.Bottom:
return {
x: x + width / 2,
y: y + height,
};
case Position.Left:
return {
x,
y: y + height / 2,
};
}
}
export function getHandle(bounds: HandleElement[], handleId?: string | null): HandleElement | null {
if (!bounds) {
return null;
}
if (bounds.length === 1 || !handleId) {
return bounds[0];
} else if (handleId) {
return bounds.find((d) => d.id === handleId) || null;
}
return null;
}
interface EdgePositions {
sourceX: number;
sourceY: number;