-
-
Notifications
You must be signed in to change notification settings - Fork 209
/
ConnectionLine.vue
130 lines (112 loc) 路 3.83 KB
/
ConnectionLine.vue
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
<script lang="ts" setup>
import type { GraphNode } from '../../types'
import { ConnectionLineType, ConnectionMode, Position } from '../../types'
import { getMarkerId } from '../../utils/graph'
const { sourceNode } = defineProps<{ sourceNode: GraphNode }>()
const {
connectionMode,
connectionStartHandle,
connectionPosition,
connectionLineType,
connectionLineStyle,
connectionLineOptions,
connectionStatus,
viewport,
} = $(useVueFlow())
const oppositePosition = {
[Position.Left]: Position.Right,
[Position.Right]: Position.Left,
[Position.Top]: Position.Bottom,
[Position.Bottom]: Position.Top,
}
const connectionLineComponent = inject(Slots)?.['connection-line']
const handleId = $computed(() => connectionStartHandle!.handleId)
const type = computed(() => connectionStartHandle!.type)
const sourceHandle = $computed(
() =>
(connectionMode === ConnectionMode.Strict
? sourceNode.handleBounds[type.value]?.find((d) => d.id === handleId)
: [...(sourceNode.handleBounds.source || []), ...(sourceNode.handleBounds.target || [])]?.find((d) => d.id === handleId)) ||
sourceNode.handleBounds[type.value ?? 'source']?.[0],
)
const sourceHandleX = $computed(() => (sourceHandle ? sourceHandle.x + sourceHandle.width / 2 : sourceNode.dimensions.width / 2))
const sourceHandleY = $computed(() => (sourceHandle ? sourceHandle.y + sourceHandle.height / 2 : sourceNode.dimensions.height))
const sourceX = $computed(() => sourceNode.computedPosition.x + sourceHandleX)
const sourceY = $computed(() => sourceNode.computedPosition.y + sourceHandleY)
const fromPosition = computed(() => sourceHandle?.position)
const targetPosition = computed(() => (fromPosition.value ? oppositePosition[fromPosition.value] : undefined))
const targetX = $computed(() => (connectionPosition.x - viewport.x) / viewport.zoom)
const targetY = $computed(() => (connectionPosition.y - viewport.y) / viewport.zoom)
const dAttr = computed(() => {
let path = `M${sourceX},${sourceY} ${targetX},${targetY}`
const pathParams = {
sourceX,
sourceY,
sourcePosition: fromPosition.value,
targetX,
targetY,
targetPosition: targetPosition.value,
}
const type = connectionLineType ?? connectionLineOptions.type
switch (type) {
case ConnectionLineType.Bezier:
;[path] = getBezierPath(pathParams)
break
case ConnectionLineType.Step:
;[path] = getSmoothStepPath({
...pathParams,
borderRadius: 0,
})
break
case ConnectionLineType.SmoothStep:
;[path] = getSmoothStepPath(pathParams)
break
case ConnectionLineType.SimpleBezier:
;[path] = getSimpleBezierPath(pathParams)
break
case ConnectionLineType.Straight:
;[path] = getStraightPath(pathParams)
break
default:
;[path] = getBezierPath(pathParams)
break
}
return path
})
</script>
<script lang="ts">
export default {
name: 'ConnectionLine',
compatConfig: { MODE: 3 },
}
</script>
<template>
<g class="vue-flow__connection">
<component
:is="connectionLineComponent"
v-if="connectionLineComponent"
v-bind="{
sourceX,
sourceY,
sourcePosition: sourceHandle?.position,
targetX,
targetY,
targetPosition,
sourceNode,
sourceHandle,
markerEnd: `url(#${getMarkerId(connectionLineOptions.markerEnd)})`,
markerStart: `url(#${getMarkerId(connectionLineOptions.markerStart)})`,
connectionStatus,
}"
/>
<path
v-else
:d="dAttr"
class="vue-flow__connection-path"
:class="[connectionLineOptions.class, connectionStatus]"
:style="connectionLineStyle || connectionLineOptions.style || {}"
:marker-end="`url(#${getMarkerId(connectionLineOptions.markerEnd)})`"
:marker-start="`url(#${getMarkerId(connectionLineOptions.markerStart)})`"
/>
</g>
</template>