Skip to content

Commit d429394

Browse files
committed
refactor(core): cleanup connection line component (#1470)
1 parent d658c1d commit d429394

File tree

1 file changed

+51
-41
lines changed
  • packages/core/src/components/ConnectionLine

1 file changed

+51
-41
lines changed

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

Lines changed: 51 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import type { DefineComponent } from 'vue'
2-
import { defineComponent, h, inject } from 'vue'
3-
import type { ConnectionLineProps } from '../../types'
1+
import { computed, defineComponent, h, inject } from 'vue'
2+
import type { HandleElement } from '../../types'
43
import { ConnectionLineType, ConnectionMode, Position } from '../../types'
54
import { getHandlePosition, getMarkerId } from '../../utils'
65
import { useVueFlow } from '../../composables'
@@ -32,29 +31,37 @@ const ConnectionLine = defineComponent({
3231
findNode,
3332
} = useVueFlow()
3433

35-
const connectionLineComponent = inject(Slots)?.['connection-line'] as DefineComponent<ConnectionLineProps> | undefined
34+
const connectionLineComponent = inject(Slots)?.['connection-line']
3635

37-
return () => {
38-
if (!connectionStartHandle.value) {
39-
return null
36+
const fromNode = computed(() => findNode(connectionStartHandle.value?.nodeId))
37+
38+
const toNode = computed(() => findNode(connectionEndHandle.value?.nodeId) ?? null)
39+
40+
const toXY = computed(() => {
41+
return {
42+
x: (connectionPosition.value.x - viewport.value.x) / viewport.value.zoom,
43+
y: (connectionPosition.value.y - viewport.value.y) / viewport.value.zoom,
4044
}
45+
})
46+
47+
const markerStart = computed(() =>
48+
connectionLineOptions.value.markerStart ? `url(#${getMarkerId(connectionLineOptions.value.markerStart, id)})` : '',
49+
)
4150

42-
const fromNode = findNode(connectionStartHandle.value.nodeId)
51+
const markerEnd = computed(() =>
52+
connectionLineOptions.value.markerEnd ? `url(#${getMarkerId(connectionLineOptions.value.markerEnd, id)})` : '',
53+
)
4354

44-
if (!fromNode) {
55+
return () => {
56+
if (!fromNode.value || !connectionStartHandle.value) {
4557
return null
4658
}
4759

4860
const startHandleId = connectionStartHandle.value.handleId
4961

5062
const handleType = connectionStartHandle.value.type
5163

52-
const targetNode = (connectionEndHandle.value && findNode(connectionEndHandle.value.nodeId)) || null
53-
54-
const toX = (connectionPosition.value.x - viewport.value.x) / viewport.value.zoom
55-
const toY = (connectionPosition.value.y - viewport.value.y) / viewport.value.zoom
56-
57-
const fromHandleBounds = fromNode.handleBounds
64+
const fromHandleBounds = fromNode.value.handleBounds
5865
let handleBounds = fromHandleBounds?.[handleType]
5966

6067
if (connectionMode.value === ConnectionMode.Loose) {
@@ -69,24 +76,28 @@ const ConnectionLine = defineComponent({
6976
const fromPosition = fromHandle?.position || Position.Top
7077
const { x: fromX, y: fromY } = getHandlePosition(
7178
fromPosition,
72-
{ ...fromNode.dimensions, ...fromNode.computedPosition },
79+
{ ...fromNode.value.dimensions, ...fromNode.value.computedPosition },
7380
fromHandle,
7481
)
7582

76-
// todo: this is a bit of a mess, we should refactor this
77-
const toHandle =
78-
(targetNode &&
79-
connectionEndHandle.value?.handleId &&
80-
((connectionMode.value === ConnectionMode.Strict
81-
? targetNode.handleBounds[handleType === 'source' ? 'target' : 'source']?.find(
82-
(d) => d.id === connectionEndHandle.value?.handleId,
83-
)
84-
: [...(targetNode.handleBounds.source || []), ...(targetNode.handleBounds.target || [])]?.find(
85-
(d) => d.id === connectionEndHandle.value?.handleId,
86-
)) ||
87-
targetNode.handleBounds[handleType ?? 'target']?.[0])) ||
88-
null
83+
let toHandle: HandleElement | null = null
84+
if (toNode.value && connectionEndHandle.value?.handleId) {
85+
// if connection mode is strict, we only look for handles of the opposite type
86+
if (connectionMode.value === ConnectionMode.Strict) {
87+
toHandle =
88+
toNode.value.handleBounds[handleType === 'source' ? 'target' : 'source']?.find(
89+
(d) => d.id === connectionEndHandle.value?.handleId,
90+
) || null
91+
} else {
92+
// if connection mode is loose, look for the handle in both source and target bounds
93+
toHandle =
94+
[...(toNode.value.handleBounds.source || []), ...(toNode.value.handleBounds.target || [])]?.find(
95+
(d) => d.id === connectionEndHandle.value?.handleId,
96+
) || null
97+
}
98+
}
8999

100+
// we assume the target position is opposite to the source position
90101
const toPosition = fromPosition ? oppositePosition[fromPosition] : null
91102

92103
if (!fromPosition || !toPosition) {
@@ -101,13 +112,12 @@ const ConnectionLine = defineComponent({
101112
sourceX: fromX,
102113
sourceY: fromY,
103114
sourcePosition: fromPosition,
104-
targetX: toX,
105-
targetY: toY,
115+
targetX: toXY.value.x,
116+
targetY: toXY.value.y,
106117
targetPosition: toPosition,
107118
}
108119

109120
if (type === ConnectionLineType.Bezier) {
110-
// we assume the destination position is opposite to the source position
111121
;[dAttr] = getBezierPath(pathParams)
112122
} else if (type === ConnectionLineType.Step) {
113123
;[dAttr] = getSmoothStepPath({
@@ -119,7 +129,7 @@ const ConnectionLine = defineComponent({
119129
} else if (type === ConnectionLineType.SimpleBezier) {
120130
;[dAttr] = getSimpleBezierPath(pathParams)
121131
} else {
122-
dAttr = `M${fromX},${fromY} ${toX},${toY}`
132+
dAttr = `M${fromX},${fromY} ${toXY.value.x},${toXY.value.y}`
123133
}
124134

125135
return h(
@@ -133,15 +143,15 @@ const ConnectionLine = defineComponent({
133143
sourceX: fromX,
134144
sourceY: fromY,
135145
sourcePosition: fromPosition,
136-
targetX: toX,
137-
targetY: toY,
146+
targetX: toXY.value.x,
147+
targetY: toXY.value.y,
138148
targetPosition: toPosition,
139-
sourceNode: fromNode,
149+
sourceNode: fromNode.value,
140150
sourceHandle: fromHandle,
141-
targetNode,
151+
targetNode: toNode.value,
142152
targetHandle: toHandle,
143-
markerEnd: `url(#${getMarkerId(connectionLineOptions.value.markerEnd, id)})`,
144-
markerStart: `url(#${getMarkerId(connectionLineOptions.value.markerStart, id)})`,
153+
markerEnd: markerEnd.value,
154+
markerStart: markerStart.value,
145155
connectionStatus: connectionStatus.value,
146156
})
147157
: h('path', {
@@ -151,8 +161,8 @@ const ConnectionLine = defineComponent({
151161
...connectionLineStyle.value,
152162
...connectionLineOptions.value.style,
153163
},
154-
'marker-end': `url(#${getMarkerId(connectionLineOptions.value.markerEnd, id)})`,
155-
'marker-start': `url(#${getMarkerId(connectionLineOptions.value.markerStart, id)})`,
164+
'marker-end': markerEnd.value,
165+
'marker-start': markerStart.value,
156166
}),
157167
),
158168
)

0 commit comments

Comments
 (0)