-
-
Notifications
You must be signed in to change notification settings - Fork 319
/
Copy pathindex.ts
163 lines (140 loc) · 5.66 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import { computed, defineComponent, h, inject } from 'vue'
import { getBezierPath, getMarkerId, getSmoothStepPath } from '@xyflow/system'
import type { HandleElement } from '../../types'
import { ConnectionLineType, ConnectionMode, Position } from '../../types'
import { getHandlePosition, oppositePosition } from '../../utils'
import { useVueFlow } from '../../composables'
import { Slots } from '../../context'
import { getSimpleBezierPath } from '../Edges/SimpleBezierEdge'
const ConnectionLine = defineComponent({
name: 'ConnectionLine',
compatConfig: { MODE: 3 },
setup() {
const {
id,
connectionMode,
connectionStartHandle,
connectionEndHandle,
connectionPosition,
connectionLineType,
connectionLineStyle,
connectionLineOptions,
connectionStatus,
viewport,
findNode,
} = useVueFlow()
const connectionLineComponent = inject(Slots)?.['connection-line']
const fromNode = computed(() => findNode(connectionStartHandle.value?.nodeId))
const toNode = computed(() => findNode(connectionEndHandle.value?.nodeId) ?? null)
const toXY = computed(() => {
return {
x: (connectionPosition.value.x - viewport.value.x) / viewport.value.zoom,
y: (connectionPosition.value.y - viewport.value.y) / viewport.value.zoom,
}
})
const markerStart = computed(() =>
connectionLineOptions.value.markerStart ? `url(#${getMarkerId(connectionLineOptions.value.markerStart, id)})` : '',
)
const markerEnd = computed(() =>
connectionLineOptions.value.markerEnd ? `url(#${getMarkerId(connectionLineOptions.value.markerEnd, id)})` : '',
)
return () => {
if (!fromNode.value || !connectionStartHandle.value) {
return null
}
const startHandleId = connectionStartHandle.value.id
const handleType = connectionStartHandle.value.type
const fromHandleBounds = fromNode.value.handleBounds
let handleBounds = fromHandleBounds?.[handleType] || []
if (connectionMode.value === ConnectionMode.Loose) {
const oppositeBounds = fromHandleBounds?.[handleType === 'source' ? 'target' : 'source'] || []
handleBounds = [...handleBounds, ...oppositeBounds] || oppositeBounds
}
if (!handleBounds) {
return null
}
const fromHandle = (startHandleId ? handleBounds.find((d) => d.id === startHandleId) : handleBounds[0]) ?? null
const fromPosition = fromHandle?.position || Position.Top
const { x: fromX, y: fromY } = getHandlePosition(fromNode.value, fromHandle, fromPosition)
let toHandle: HandleElement | null = null
if (toNode.value) {
// if connection mode is strict, we only look for handles of the opposite type
if (connectionMode.value === ConnectionMode.Strict) {
toHandle =
toNode.value.handleBounds[handleType === 'source' ? 'target' : 'source']?.find(
(d) => d.id === connectionEndHandle.value?.id,
) || null
} else {
// if connection mode is loose, look for the handle in both source and target bounds
toHandle =
[...(toNode.value.handleBounds.source || []), ...(toNode.value.handleBounds.target || [])]?.find(
(d) => d.id === connectionEndHandle.value?.id,
) || null
}
}
const toPosition = connectionEndHandle.value?.position ?? (fromPosition ? oppositePosition[fromPosition] : null)
if (!fromPosition || !toPosition) {
return null
}
const type = connectionLineType.value ?? connectionLineOptions.value.type ?? ConnectionLineType.Bezier
let dAttr = ''
const pathParams = {
sourceX: fromX,
sourceY: fromY,
sourcePosition: fromPosition,
targetX: toXY.value.x,
targetY: toXY.value.y,
targetPosition: toPosition,
}
if (type === ConnectionLineType.Bezier) {
;[dAttr] = getBezierPath(pathParams)
} else if (type === ConnectionLineType.Step) {
;[dAttr] = getSmoothStepPath({
...pathParams,
borderRadius: 0,
})
} else if (type === ConnectionLineType.SmoothStep) {
;[dAttr] = getSmoothStepPath(pathParams)
} else if (type === ConnectionLineType.SimpleBezier) {
;[dAttr] = getSimpleBezierPath(pathParams)
} else {
dAttr = `M${fromX},${fromY} ${toXY.value.x},${toXY.value.y}`
}
return h(
'svg',
{ class: 'vue-flow__edges vue-flow__connectionline vue-flow__container' },
h(
'g',
{ class: 'vue-flow__connection' },
connectionLineComponent
? h(connectionLineComponent, {
sourceX: fromX,
sourceY: fromY,
sourcePosition: fromPosition,
targetX: toXY.value.x,
targetY: toXY.value.y,
targetPosition: toPosition,
sourceNode: fromNode.value,
sourceHandle: fromHandle,
targetNode: toNode.value,
targetHandle: toHandle,
markerEnd: markerEnd.value,
markerStart: markerStart.value,
connectionStatus: connectionStatus.value,
})
: h('path', {
'd': dAttr,
'class': [connectionLineOptions.value.class, connectionStatus, 'vue-flow__connection-path'],
'style': {
...connectionLineStyle.value,
...connectionLineOptions.value.style,
},
'marker-end': markerEnd.value,
'marker-start': markerStart.value,
}),
),
)
}
},
})
export default ConnectionLine