feat(edge-utils): add getHandlePosition and getHandle
This commit is contained in:
@@ -1,13 +1,14 @@
|
||||
import { getHandlePosition } from '@reactflow/edge-utils';
|
||||
import {
|
||||
type NodeHandleBounds,
|
||||
type Rect,
|
||||
type Node,
|
||||
type HandleElement,
|
||||
type XYPosition,
|
||||
Position,
|
||||
internalsSymbol
|
||||
} from '@reactflow/system';
|
||||
|
||||
import type { Node } from '$lib/types';
|
||||
|
||||
export function getNodeData(node?: Node): [Rect, NodeHandleBounds | null, boolean] {
|
||||
const handleBounds = node?.[internalsSymbol]?.handleBounds || null;
|
||||
|
||||
@@ -30,54 +31,6 @@ export function getNodeData(node?: Node): [Rect, NodeHandleBounds | null, boolea
|
||||
];
|
||||
}
|
||||
|
||||
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 (handleId) {
|
||||
return bounds.find((d) => d.id === handleId)!;
|
||||
} else if (bounds.length === 1) {
|
||||
return bounds[0];
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
export type EdgePosition = {
|
||||
sourceX: number;
|
||||
sourceY: number;
|
||||
|
||||
Reference in New Issue
Block a user