Skip to content

Commit 7e61ac1

Browse files
authored
chore(core): cleanup handle utils (#1497)
1 parent 09c32c5 commit 7e61ac1

File tree

5 files changed

+30
-57
lines changed

5 files changed

+30
-57
lines changed

packages/core/src/components/ConnectionLine/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ const ConnectionLine = defineComponent({
7474

7575
const fromHandle = (startHandleId ? handleBounds.find((d) => d.id === startHandleId) : handleBounds[0]) ?? null
7676
const fromPosition = fromHandle?.position || Position.Top
77-
const { x: fromX, y: fromY } = getHandlePosition(fromPosition, fromNode.value, fromHandle)
77+
const { x: fromX, y: fromY } = getHandlePosition(fromNode.value, fromHandle, fromPosition)
7878

7979
let toHandle: HandleElement | null = null
8080
if (toNode.value && connectionEndHandle.value?.handleId) {

packages/core/src/components/Edges/EdgeWrapper.ts

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import {
88
ErrorCode,
99
VueFlowError,
1010
elementSelectionKeys,
11-
getEdgePositions,
1211
getHandle,
12+
getHandlePosition,
1313
getMarkerId,
1414
} from '../../utils'
1515
import EdgeAnchor from './EdgeAnchor'
@@ -160,19 +160,14 @@ const EdgeWrapper = defineComponent({
160160

161161
const targetHandle = getHandle(targetNodeHandles, edge.value.targetHandle)
162162

163-
const sourcePosition = sourceHandle ? sourceHandle.position : Position.Bottom
163+
const sourcePosition = sourceHandle?.position || Position.Bottom
164164

165-
const targetPosition = targetHandle ? targetHandle.position : Position.Top
165+
const targetPosition = targetHandle?.position || Position.Top
166166

167-
const { sourceX, sourceY, targetY, targetX } = getEdgePositions(
168-
sourceNode,
169-
sourceHandle,
170-
sourcePosition,
171-
targetNode,
172-
targetHandle,
173-
targetPosition,
174-
)
167+
const { x: sourceX, y: sourceY } = getHandlePosition(sourceNode, sourceHandle, sourcePosition)
168+
const { x: targetX, y: targetY } = getHandlePosition(targetNode, targetHandle, targetPosition)
175169

170+
// todo: let's avoid writing these here (in v2 we want to remove all of these self-managed refs)
176171
edge.value.sourceX = sourceX
177172
edge.value.sourceY = sourceY
178173
edge.value.targetX = targetX

packages/core/src/types/handle.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,10 @@ export interface HandleElement extends XYPosition, Dimensions {
1010
position: Position
1111
}
1212

13-
export interface ConnectionHandle {
13+
export interface ConnectionHandle extends XYPosition {
1414
id: string | null
1515
type: HandleType | null
1616
nodeId: string
17-
x: number
18-
y: number
1917
}
2018

2119
export interface ValidHandleResult {

packages/core/src/utils/edge.ts

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
1-
import type { Actions, EdgePositions, GraphEdge, GraphNode, HandleElement, ViewportTransform, XYPosition } from '../types'
1+
import type { Actions, GraphEdge, GraphNode, HandleElement, ViewportTransform, XYPosition } from '../types'
22
import { Position } from '../types'
3-
import { rectToBox } from '.'
3+
import { getNodeDimensions, rectToBox } from '.'
44

5-
export function getHandlePosition(position: Position, node: GraphNode, handle: HandleElement | null): XYPosition {
5+
export function getHandlePosition(
6+
node: GraphNode,
7+
handle: HandleElement | null,
8+
fallbackPosition: Position = Position.Left,
9+
): XYPosition {
610
const x = (handle?.x ?? 0) + node.computedPosition.x
711
const y = (handle?.y ?? 0) + node.computedPosition.y
8-
const width = handle?.width ?? node.dimensions.width
9-
const height = handle?.height ?? node.dimensions.height
12+
const { width, height } = handle ?? getNodeDimensions(node)
13+
const position = handle?.position ?? fallbackPosition
1014

1115
switch (position) {
1216
case Position.Top:
@@ -40,25 +44,6 @@ export function getHandle(bounds: HandleElement[] = [], handleId?: string | null
4044
return (!handleId ? bounds[0] : bounds.find((d) => d.id === handleId)) || null
4145
}
4246

43-
export function getEdgePositions(
44-
sourceNode: GraphNode,
45-
sourceHandle: HandleElement | null,
46-
sourcePosition: Position,
47-
targetNode: GraphNode,
48-
targetHandle: HandleElement | null,
49-
targetPosition: Position,
50-
): EdgePositions {
51-
const { x: sourceX, y: sourceY } = getHandlePosition(sourcePosition, sourceNode, sourceHandle)
52-
const { x: targetX, y: targetY } = getHandlePosition(targetPosition, targetNode, targetHandle)
53-
54-
return {
55-
sourceX,
56-
sourceY,
57-
targetX,
58-
targetY,
59-
}
60-
}
61-
6247
interface IsEdgeVisibleParams {
6348
sourcePos: XYPosition
6449
targetPos: XYPosition

packages/core/src/utils/handle.ts

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import { ConnectionMode } from '../types'
22
import type {
33
Actions,
44
Connection,
5+
ConnectionHandle,
56
ConnectionStatus,
6-
Dimensions,
77
GraphEdge,
88
GraphNode,
99
HandleType,
@@ -14,12 +14,6 @@ import type {
1414
} from '../types'
1515
import { getEventPosition, getHandlePosition } from '.'
1616

17-
export interface ConnectionHandle extends XYPosition, Dimensions {
18-
id: string | null
19-
type: HandleType | null
20-
nodeId: string
21-
}
22-
2317
function defaultValidHandleResult(): ValidHandleResult {
2418
return {
2519
handleDomNode: null,
@@ -41,22 +35,23 @@ export function getHandles(
4135
type: HandleType,
4236
currentHandle: string,
4337
): ConnectionHandle[] {
44-
return (handleBounds[type] || []).reduce<ConnectionHandle[]>((res, h) => {
45-
if (`${node.id}-${h.id}-${type}` !== currentHandle) {
46-
const handlePosition = getHandlePosition(h.position, node, h)
38+
const connectionHandles: ConnectionHandle[] = []
4739

48-
res.push({
49-
id: h.id || null,
40+
for (const handle of handleBounds[type] || []) {
41+
if (`${node.id}-${handle.id}-${type}` !== currentHandle) {
42+
const { x, y } = getHandlePosition(node, handle)
43+
44+
connectionHandles.push({
45+
id: handle.id || null,
5046
type,
5147
nodeId: node.id,
52-
x: handlePosition.x,
53-
y: handlePosition.y,
54-
width: h.width,
55-
height: h.height,
48+
x,
49+
y,
5650
})
5751
}
58-
return res
59-
}, [])
52+
}
53+
54+
return connectionHandles
6055
}
6156

6257
export function getClosestHandle(

0 commit comments

Comments
 (0)