From 3cc33aa7cb90eb0b60797ec4ba9fe8a7d2045b09 Mon Sep 17 00:00:00 2001 From: moklick Date: Tue, 2 Nov 2021 14:49:16 +0100 Subject: [PATCH] refactor(conenctionline): simplify getSourceHandle helper --- package.json | 2 +- src/components/ConnectionLine/index.tsx | 26 +++++++++---------------- 2 files changed, 10 insertions(+), 18 deletions(-) diff --git a/package.json b/package.json index abcfbf85..5dada299 100644 --- a/package.json +++ b/package.json @@ -82,4 +82,4 @@ "dist", "nocss" ] -} \ No newline at end of file +} diff --git a/src/components/ConnectionLine/index.tsx b/src/components/ConnectionLine/index.tsx index 1246dc52..98b162e5 100644 --- a/src/components/ConnectionLine/index.tsx +++ b/src/components/ConnectionLine/index.tsx @@ -27,6 +27,14 @@ interface ConnectionLineProps { CustomConnectionLineComponent?: ConnectionLineComponent; } +const getSourceHandle = (handleId: ElementId | null, sourceNode: Node, connectionHandleType: HandleType) => { + const handleTypeInverted = connectionHandleType === 'source' ? 'target' : 'source'; + const handleBound = + sourceNode.__rf.handleBounds[connectionHandleType] || sourceNode.__rf.handleBounds[handleTypeInverted]; + + return handleId ? handleBound.find((d: HandleElement) => d.id === handleId) : handleBound[0]; +}; + export default ({ connectionNodeId, connectionHandleId, @@ -53,22 +61,7 @@ export default ({ return null; } - const getSourceHandle = (handleId: ElementId | null, sourceNode: Node) => { - const handleBound = sourceNode.__rf.handleBounds[connectionHandleType]; - if (handleBound) { - if (handleId) { - return sourceNode.__rf.handleBounds[connectionHandleType].find((d: HandleElement) => d.id === handleId); - } - return sourceNode.__rf.handleBounds[connectionHandleType][0] - } - const handleType = connectionHandleType === 'source' ? 'target' : 'source'; - if (handleId) { - return sourceNode.__rf.handleBounds[handleType].find((d: HandleElement) => d.id === handleId); - } - return sourceNode.__rf.handleBounds[handleType][0] - } - - const sourceHandle = getSourceHandle(handleId, sourceNode); + const sourceHandle = getSourceHandle(handleId, sourceNode, connectionHandleType); const sourceHandleX = sourceHandle ? sourceHandle.x + sourceHandle.width / 2 : sourceNode.__rf.width / 2; const sourceHandleY = sourceHandle ? sourceHandle.y + sourceHandle.height / 2 : sourceNode.__rf.height; const sourceX = sourceNode.__rf.position.x + sourceHandleX; @@ -81,7 +74,6 @@ export default ({ const targetPosition = isRightOrLeft ? Position.Left : Position.Top; if (CustomConnectionLineComponent) { - return (