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'
4
3
import { ConnectionLineType , ConnectionMode , Position } from '../../types'
5
4
import { getHandlePosition , getMarkerId } from '../../utils'
6
5
import { useVueFlow } from '../../composables'
@@ -32,29 +31,37 @@ const ConnectionLine = defineComponent({
32
31
findNode,
33
32
} = useVueFlow ( )
34
33
35
- const connectionLineComponent = inject ( Slots ) ?. [ 'connection-line' ] as DefineComponent < ConnectionLineProps > | undefined
34
+ const connectionLineComponent = inject ( Slots ) ?. [ 'connection-line' ]
36
35
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 ,
40
44
}
45
+ } )
46
+
47
+ const markerStart = computed ( ( ) =>
48
+ connectionLineOptions . value . markerStart ? `url(#${ getMarkerId ( connectionLineOptions . value . markerStart , id ) } )` : '' ,
49
+ )
41
50
42
- const fromNode = findNode ( connectionStartHandle . value . nodeId )
51
+ const markerEnd = computed ( ( ) =>
52
+ connectionLineOptions . value . markerEnd ? `url(#${ getMarkerId ( connectionLineOptions . value . markerEnd , id ) } )` : '' ,
53
+ )
43
54
44
- if ( ! fromNode ) {
55
+ return ( ) => {
56
+ if ( ! fromNode . value || ! connectionStartHandle . value ) {
45
57
return null
46
58
}
47
59
48
60
const startHandleId = connectionStartHandle . value . handleId
49
61
50
62
const handleType = connectionStartHandle . value . type
51
63
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
58
65
let handleBounds = fromHandleBounds ?. [ handleType ]
59
66
60
67
if ( connectionMode . value === ConnectionMode . Loose ) {
@@ -69,24 +76,28 @@ const ConnectionLine = defineComponent({
69
76
const fromPosition = fromHandle ?. position || Position . Top
70
77
const { x : fromX , y : fromY } = getHandlePosition (
71
78
fromPosition ,
72
- { ...fromNode . dimensions , ...fromNode . computedPosition } ,
79
+ { ...fromNode . value . dimensions , ...fromNode . value . computedPosition } ,
73
80
fromHandle ,
74
81
)
75
82
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
+ }
89
99
100
+ // we assume the target position is opposite to the source position
90
101
const toPosition = fromPosition ? oppositePosition [ fromPosition ] : null
91
102
92
103
if ( ! fromPosition || ! toPosition ) {
@@ -101,13 +112,12 @@ const ConnectionLine = defineComponent({
101
112
sourceX : fromX ,
102
113
sourceY : fromY ,
103
114
sourcePosition : fromPosition ,
104
- targetX : toX ,
105
- targetY : toY ,
115
+ targetX : toXY . value . x ,
116
+ targetY : toXY . value . y ,
106
117
targetPosition : toPosition ,
107
118
}
108
119
109
120
if ( type === ConnectionLineType . Bezier ) {
110
- // we assume the destination position is opposite to the source position
111
121
; [ dAttr ] = getBezierPath ( pathParams )
112
122
} else if ( type === ConnectionLineType . Step ) {
113
123
; [ dAttr ] = getSmoothStepPath ( {
@@ -119,7 +129,7 @@ const ConnectionLine = defineComponent({
119
129
} else if ( type === ConnectionLineType . SimpleBezier ) {
120
130
; [ dAttr ] = getSimpleBezierPath ( pathParams )
121
131
} else {
122
- dAttr = `M${ fromX } ,${ fromY } ${ toX } ,${ toY } `
132
+ dAttr = `M${ fromX } ,${ fromY } ${ toXY . value . x } ,${ toXY . value . y } `
123
133
}
124
134
125
135
return h (
@@ -133,15 +143,15 @@ const ConnectionLine = defineComponent({
133
143
sourceX : fromX ,
134
144
sourceY : fromY ,
135
145
sourcePosition : fromPosition ,
136
- targetX : toX ,
137
- targetY : toY ,
146
+ targetX : toXY . value . x ,
147
+ targetY : toXY . value . y ,
138
148
targetPosition : toPosition ,
139
- sourceNode : fromNode ,
149
+ sourceNode : fromNode . value ,
140
150
sourceHandle : fromHandle ,
141
- targetNode,
151
+ targetNode : toNode . value ,
142
152
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 ,
145
155
connectionStatus : connectionStatus . value ,
146
156
} )
147
157
: h ( 'path' , {
@@ -151,8 +161,8 @@ const ConnectionLine = defineComponent({
151
161
...connectionLineStyle . value ,
152
162
...connectionLineOptions . value . style ,
153
163
} ,
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 ,
156
166
} ) ,
157
167
) ,
158
168
)
0 commit comments