determine correct handle positions in for connections (#4384)

* determine correct handle positions in for connections

* refactor(handle-utils): cleanup

* refactor(handle-utils): cleanup

---------

Co-authored-by: moklick <info@moritzklack.com>
This commit is contained in:
Peter Kogo
2024-06-20 10:28:50 +02:00
committed by GitHub
co-authored by moklick
parent 3b48228437
commit 57b62f8d46
2 changed files with 17 additions and 10 deletions
+10 -5
View File
@@ -42,8 +42,6 @@ export function getEdgePosition(params: GetEdgePositionParams): EdgePosition | n
: (targetHandleBounds?.target ?? []).concat(targetHandleBounds?.source ?? []), : (targetHandleBounds?.target ?? []).concat(targetHandleBounds?.source ?? []),
params.targetHandle params.targetHandle
); );
const sourcePosition = sourceHandle?.position || Position.Bottom;
const targetPosition = targetHandle?.position || Position.Top;
if (!sourceHandle || !targetHandle) { if (!sourceHandle || !targetHandle) {
params.onError?.( params.onError?.(
@@ -58,8 +56,10 @@ export function getEdgePosition(params: GetEdgePositionParams): EdgePosition | n
return null; return null;
} }
const [sourceX, sourceY] = getHandlePosition(sourcePosition, sourceNode, sourceHandle); const sourcePosition = sourceHandle?.position || Position.Bottom;
const [targetX, targetY] = getHandlePosition(targetPosition, targetNode, targetHandle); const targetPosition = targetHandle?.position || Position.Top;
const [sourceX, sourceY] = getHandlePosition(sourceNode, sourceHandle, sourcePosition);
const [targetX, targetY] = getHandlePosition(targetNode, targetHandle, targetPosition);
return { return {
sourceX, 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 x = (handle?.x ?? 0) + node.internals.positionAbsolute.x;
const y = (handle?.y ?? 0) + node.internals.positionAbsolute.y; const y = (handle?.y ?? 0) + node.internals.positionAbsolute.y;
const { width, height } = handle ?? getNodeDimensions(node); const { width, height } = handle ?? getNodeDimensions(node);
const position = handle?.position ?? fallbackPosition;
switch (position) { switch (position) {
case Position.Top: case Position.Top:
+7 -5
View File
@@ -1,3 +1,4 @@
import { getHandlePosition } from '../utils';
import { import {
ConnectionStatus, ConnectionStatus,
type HandleType, type HandleType,
@@ -16,14 +17,15 @@ export function getHandles(
type: HandleType, type: HandleType,
currentHandle: string currentHandle: string
): ConnectionHandle[] { ): ConnectionHandle[] {
return (handleBounds[type] || []).reduce<ConnectionHandle[]>((res, h) => { return (handleBounds[type] || []).reduce<ConnectionHandle[]>((res, handle) => {
if (`${node.id}-${h.id}-${type}` !== currentHandle) { if (`${node.id}-${handle.id}-${type}` !== currentHandle) {
const [x, y] = getHandlePosition(node, handle);
res.push({ res.push({
id: h.id || null, id: handle.id || null,
type, type,
nodeId: node.id, nodeId: node.id,
x: node.internals.positionAbsolute.x + h.x + h.width / 2, x,
y: node.internals.positionAbsolute.y + h.y + h.height / 2, y,
}); });
} }
return res; return res;