-
-
Notifications
You must be signed in to change notification settings - Fork 209
/
NodeWrapper.vue
305 lines (261 loc) 路 8.66 KB
/
NodeWrapper.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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
<script lang="ts" setup>
import { isNumber } from '@vueuse/core'
import type { GraphNode, HandleConnectable, NodeComponent } from '../../types'
import { ARIA_NODE_DESC_KEY } from '../../utils/a11y'
const { id, type, name, draggable, selectable, connectable, ...props } = defineProps<{
id: string
draggable: boolean
selectable: boolean
connectable: HandleConnectable
focusable: boolean
type: NodeComponent | Function | Object | false
name: string
node: GraphNode
resizeObserver: ResizeObserver
}>()
provide(NodeId, id)
const {
id: vueFlowId,
edges,
noPanClassName,
selectNodesOnDrag,
nodesSelectionActive,
multiSelectionActive,
emits,
findNode,
removeSelectedNodes,
addSelectedNodes,
updateNodeDimensions,
onUpdateNodeInternals,
getIntersectingNodes,
getNodeTypes,
nodeExtent,
elevateNodesOnSelect,
disableKeyboardA11y,
ariaLiveMessage,
} = $(useVueFlow())
const updateNodePositions = useUpdateNodePositions()
const node = $(useVModel(props, 'node'))
const parentNode = $computed(() => (node.parentNode ? findNode(node.parentNode) : undefined))
const connectedEdges = $computed(() => getConnectedEdges([node], edges))
const nodeElement = ref<HTMLDivElement>()
provide(NodeRef, nodeElement)
const { emit, on } = useNodeHooks(node, emits)
const dragging = useDrag({
id,
el: nodeElement,
disabled: computed(() => !draggable),
onStart(event, node, nodes) {
emit.dragStart({ event, node, nodes, intersections: getIntersectingNodes(node) })
},
onDrag(event, node, nodes) {
emit.drag({ event, node, nodes, intersections: getIntersectingNodes(node) })
},
onStop(event, node, nodes) {
emit.dragStop({ event, node, nodes, intersections: getIntersectingNodes(node) })
},
})
const getClass = computed(() => (node.class instanceof Function ? node.class(node) : node.class))
const getStyle = computed(() => {
const styles = (node.style instanceof Function ? node.style(node) : node.style) || {}
const width = node.width instanceof Function ? node.width(node) : node.width
const height = node.height instanceof Function ? node.height(node) : node.height
if (width) styles.width = typeof width === 'string' ? width : `${width}px`
if (height) styles.height = typeof height === 'string' ? height : `${height}px`
return styles
})
onUpdateNodeInternals((updateIds) => {
if (updateIds.includes(id)) {
updateInternals()
}
})
watchEffect(
(onCleanup) => {
if (!node.hidden) {
props.resizeObserver.observe(nodeElement.value as HTMLDivElement)
onCleanup(() => props.resizeObserver.unobserve(nodeElement.value as HTMLDivElement))
}
},
{ flush: 'post' },
)
watch(
[() => node.type, () => node.sourcePosition, () => node.targetPosition],
() => {
updateNodeDimensions([{ id, nodeElement: nodeElement.value as HTMLDivElement, forceUpdate: true }])
},
{ flush: 'pre' },
)
/** this watcher only updates XYZPosition (when dragging a parent etc) */
watch(
[
() => node.position.x,
() => node.position.y,
() => parentNode?.computedPosition.x,
() => parentNode?.computedPosition.y,
() => parentNode?.computedPosition.z,
() => node.selected,
() => node.dimensions.height,
() => node.dimensions.width,
() => parentNode?.dimensions.height,
() => parentNode?.dimensions.width,
() => node.zIndex,
],
([newX, newY, parentX, parentY, parentZ]) => {
let zIndex = isNumber(node.zIndex) ? node.zIndex : 0
zIndex = isNumber(getStyle.value.zIndex) ? getStyle.value.zIndex : zIndex
const xyzPos = {
x: newX,
y: newY,
z: (zIndex || 0) + (elevateNodesOnSelect ? (node.selected ? 1000 : 0) : 0),
}
if (isNumber(parentX) && isNumber(parentY)) {
node.computedPosition = getXYZPos({ x: parentX, y: parentY, z: parentZ! }, xyzPos)
} else {
node.computedPosition = xyzPos
}
},
{ flush: 'pre', immediate: true },
)
watch([() => node.extent, () => nodeExtent], ([nodeExtent, globalExtent], [oldNodeExtent, oldGlobalExtent]) => {
// update position if extent has actually changed
if (nodeExtent !== oldNodeExtent || globalExtent !== oldGlobalExtent) {
clampPosition()
}
})
// clamp initial position to nodes' extent
// if extent is parent, we need dimensions to properly clamp the position
if (node.extent === 'parent' || (typeof node.extent === 'object' && 'range' in node.extent && node.extent.range === 'parent')) {
until(() => node.initialized)
.toBe(true)
.then(clampPosition)
}
// if extent is not parent, we can clamp it immediately
else {
clampPosition()
}
/** this re-calculates the current position, necessary for clamping by a node's extent */
function clampPosition() {
const { computedPosition, position } = calcNextPosition(node, node.computedPosition, nodeExtent, parentNode)
// only overwrite positions if there are changes when clamping
if (node.computedPosition.x !== computedPosition.x || node.computedPosition.y !== computedPosition.y) {
node.computedPosition = { ...node.computedPosition, ...computedPosition }
}
if (node.position.x !== position.x || node.position.y !== position.y) {
node.position = position
}
}
function updateInternals() {
if (nodeElement.value) updateNodeDimensions([{ id, nodeElement: nodeElement.value, forceUpdate: true }])
}
function onMouseEnter(event: MouseEvent) {
if (!dragging?.value) {
emit.mouseEnter({ event, node, connectedEdges })
}
}
function onMouseMove(event: MouseEvent) {
if (!dragging?.value) {
emit.mouseMove({ event, node, connectedEdges })
}
}
function onMouseLeave(event: MouseEvent) {
if (!dragging?.value) {
emit.mouseLeave({ event, node, connectedEdges })
}
}
function onContextMenu(event: MouseEvent) {
return emit.contextMenu({ event, node, connectedEdges })
}
function onDoubleClick(event: MouseEvent) {
return emit.doubleClick({ event, node, connectedEdges })
}
function onSelectNode(event: MouseEvent) {
if (selectable && (!selectNodesOnDrag || !draggable)) {
handleNodeClick(node, multiSelectionActive, addSelectedNodes, removeSelectedNodes, $$(nodesSelectionActive))
}
emit.click({ event, node, connectedEdges })
}
function onKeyDown(event: KeyboardEvent) {
if (isInputDOMNode(event)) return
if (elementSelectionKeys.includes(event.key) && selectable) {
const unselect = event.key === 'Escape'
if (unselect) {
nodeElement.value?.blur()
}
handleNodeClick(node, multiSelectionActive, addSelectedNodes, removeSelectedNodes, $$(nodesSelectionActive), unselect)
} else if (!disableKeyboardA11y && draggable && node.selected && arrowKeyDiffs[event.key]) {
$$(ariaLiveMessage).value = `Moved selected node ${event.key.replace('Arrow', '').toLowerCase()}. New position, x: ${~~node
.position.x}, y: ${~~node.position.y}`
updateNodePositions(
{
x: arrowKeyDiffs[event.key].x,
y: arrowKeyDiffs[event.key].y,
},
event.shiftKey,
)
}
}
</script>
<script lang="ts">
export default {
name: 'Node',
inheritAttrs: false,
compatConfig: { MODE: 3 },
}
</script>
<template>
<div
ref="nodeElement"
class="vue-flow__node"
:class="[
`vue-flow__node-${type === false ? 'default' : name}`,
noPanClassName,
{
dragging,
selected: node.selected,
selectable,
},
getClass,
]"
:style="{
zIndex: node.computedPosition.z ?? 0,
transform: `translate(${node.computedPosition.x}px,${node.computedPosition.y}px)`,
pointerEvents: selectable || draggable ? 'all' : 'none',
...getStyle,
}"
:tabIndex="focusable ? 0 : undefined"
:role="focusable ? 'button' : undefined"
:aria-describedby="disableKeyboardA11y ? undefined : `${ARIA_NODE_DESC_KEY}-${vueFlowId}`"
:aria-label="node.ariaLabel"
:data-id="node.id"
@mouseenter="onMouseEnter"
@mousemove="onMouseMove"
@mouseleave="onMouseLeave"
@contextmenu="onContextMenu"
@click="onSelectNode"
@dblclick="onDoubleClick"
@keydown="onKeyDown"
>
<component
:is="type === false ? getNodeTypes.default : type"
:id="node.id"
:type="node.type"
:data="node.data"
:events="{ ...node.events, ...on }"
:selected="!!node.selected"
:resizing="!!node.resizing"
:dragging="dragging"
:connectable="connectable"
:position="node.position"
:dimensions="node.dimensions"
:is-valid-target-pos="node.isValidTargetPos"
:is-valid-source-pos="node.isValidSourcePos"
:parent-node="node.parentNode"
:z-index="node.computedPosition.z"
:target-position="node.targetPosition"
:source-position="node.sourcePosition"
:label="node.label"
:drag-handle="node.dragHandle"
@update-node-internals="updateInternals"
/>
</div>
</template>