diff --git a/packages/system/src/utils/edges/positions.ts b/packages/system/src/utils/edges/positions.ts index 6e61ce8c..ff79d45c 100644 --- a/packages/system/src/utils/edges/positions.ts +++ b/packages/system/src/utils/edges/positions.ts @@ -42,8 +42,6 @@ export function getEdgePosition(params: GetEdgePositionParams): EdgePosition | n : (targetHandleBounds?.target ?? []).concat(targetHandleBounds?.source ?? []), params.targetHandle ); - const sourcePosition = sourceHandle?.position || Position.Bottom; - const targetPosition = targetHandle?.position || Position.Top; if (!sourceHandle || !targetHandle) { params.onError?.( @@ -58,8 +56,10 @@ export function getEdgePosition(params: GetEdgePositionParams): EdgePosition | n return null; } - const [sourceX, sourceY] = getHandlePosition(sourcePosition, sourceNode, sourceHandle); - const [targetX, targetY] = getHandlePosition(targetPosition, targetNode, targetHandle); + const sourcePosition = sourceHandle?.position || Position.Bottom; + const targetPosition = targetHandle?.position || Position.Top; + const [sourceX, sourceY] = getHandlePosition(sourceNode, sourceHandle, sourcePosition); + const [targetX, targetY] = getHandlePosition(targetNode, targetHandle, targetPosition); return { sourceX, @@ -96,10 +96,15 @@ function toHandleBounds(handles?: NodeHandle[]) { }; } -function getHandlePosition(position: Position, node: InternalNodeBase, handle: HandleElement | null = null): number[] { +export function getHandlePosition( + node: InternalNodeBase, + handle: HandleElement | null, + fallbackPosition: Position = Position.Left +): number[] { const x = (handle?.x ?? 0) + node.internals.positionAbsolute.x; const y = (handle?.y ?? 0) + node.internals.positionAbsolute.y; const { width, height } = handle ?? getNodeDimensions(node); + const position = handle?.position ?? fallbackPosition; switch (position) { case Position.Top: diff --git a/packages/system/src/xyhandle/utils.ts b/packages/system/src/xyhandle/utils.ts index ba18e79a..31dbfe61 100644 --- a/packages/system/src/xyhandle/utils.ts +++ b/packages/system/src/xyhandle/utils.ts @@ -1,3 +1,4 @@ +import { getHandlePosition } from '../utils'; import { ConnectionStatus, type HandleType, @@ -16,14 +17,15 @@ export function getHandles( type: HandleType, currentHandle: string ): ConnectionHandle[] { - return (handleBounds[type] || []).reduce((res, h) => { - if (`${node.id}-${h.id}-${type}` !== currentHandle) { + return (handleBounds[type] || []).reduce((res, handle) => { + if (`${node.id}-${handle.id}-${type}` !== currentHandle) { + const [x, y] = getHandlePosition(node, handle); res.push({ - id: h.id || null, + id: handle.id || null, type, nodeId: node.id, - x: node.internals.positionAbsolute.x + h.x + h.width / 2, - y: node.internals.positionAbsolute.y + h.y + h.height / 2, + x, + y, }); } return res;