1
1
<script setup>
2
- import { VueFlow , isNode , useVueFlow } from ' @vue-flow/core'
2
+ import { ref } from ' vue'
3
+ import { VueFlow , useVueFlow } from ' @vue-flow/core'
3
4
import { Background } from ' @vue-flow/background'
4
5
import { ControlButton , Controls } from ' @vue-flow/controls'
5
6
import { MiniMap } from ' @vue-flow/minimap'
6
- import { ref } from ' vue '
7
+ import { initialEdges , initialNodes } from ' ./initial-elements.js '
7
8
import Icon from ' ./Icon.vue'
8
- import { initialElements } from ' ./initial-elements.js'
9
9
10
- // useVueFlow returns all the event-hooks and methods you need to interact with the graph
10
+ /**
11
+ * useVueFlow provides all event handlers and store properties
12
+ * You can pass the composable an object that has the same properties as the VueFlow component props
13
+ */
11
14
const { onPaneReady , onNodeDragStop , onConnect , addEdges , setViewport , toObject } = useVueFlow ()
12
15
13
- // Elements v-model
14
- const elements = ref (initialElements)
16
+ const nodes = ref (initialNodes)
15
17
16
- // Dark mode toggle
18
+ const edges = ref (initialEdges)
19
+
20
+ // our dark mode toggle flag
17
21
const dark = ref (false )
18
22
19
23
/**
20
- * Event Hooks
24
+ * This is a Vue Flow event-hook which can be listened to from anywhere you call the composable, instead of only on the main component
25
+ * Any event that is available as `@event -name` on the VueFlow component is also available as `onEventName` on the composable and vice versa
21
26
*
22
- * These are Vue Flow event-hooks which can be used to listen to events happening on the graph.
27
+ * onPaneReady is called when viewpane & nodes have visible dimensions
23
28
*/
24
-
25
- // onPaneReady is called when viewpane is ready (dimensions exist and are not 0)
26
29
onPaneReady (({ fitView }) => {
27
30
fitView ()
28
31
})
29
32
30
- // onNodeDragStop is called when a node is dragged and then released
31
- onNodeDragStop ((e ) => console .log (' drag stop' , e))
33
+ /**
34
+ * onNodeDragStop is called when a node is done being dragged
35
+ *
36
+ * Node drag events provide you with:
37
+ * 1. the event object
38
+ * 2. the nodes array (if multiple nodes are dragged)
39
+ * 3. the node that initiated the drag
40
+ * 4. any intersections with other nodes
41
+ */
42
+ onNodeDragStop (({ event , nodes, node, intersections }) => {
43
+ console .log (' Node Drag Stop' , { event , nodes, node, intersections })
44
+ })
32
45
33
46
/**
34
47
* onConnect is called when a new connection is created.
35
- * You can add additional properties to your new edge (like a type or label) or block the creation altogether
48
+ *
49
+ * You can add additional properties to your new edge (like a type or label) or block the creation altogether by not calling `addEdges`
36
50
*/
37
- onConnect ((params ) => addEdges (params))
51
+ onConnect ((connection ) => {
52
+ addEdges (connection)
53
+ })
38
54
39
55
/**
40
- * To update node properties you can simply use your elements v-model and mutate the elements directly
41
- * Changes should always be reflected on the graph reactively, without the need to overwrite the original elements
42
- * Be aware that VueFlow will change your initial `Node[]` and `Edge[]` elements to `GraphNode[]` and `GraphEdge[]`
56
+ * To update a node or multiple nodes, you can
57
+ * 1. Mutate the node objects *if* you're using `v-model`
58
+ * 2. Use the `updateNode` method (from `useVueFlow`) to update the node(s)
59
+ * 3. Create a new array of nodes and pass it to the `nodes` ref
43
60
*/
44
61
function updatePos () {
45
- return elements .value .forEach ((el ) => {
46
- if (isNode (el)) {
47
- el .position = {
62
+ nodes .value = nodes .value .map ((node ) => {
63
+ return {
64
+ ... node,
65
+ position: {
48
66
x: Math .random () * 400 ,
49
67
y: Math .random () * 400 ,
50
- }
68
+ },
51
69
}
52
70
})
53
71
}
54
72
73
+ /**
74
+ * toObject transforms your current graph data to an easily persist-able object
75
+ */
55
76
function logToObject () {
56
- // `toObject` transforms your current graph data to an easily persist-able object
57
- return console .log (toObject ())
77
+ console .log (toObject ())
58
78
}
59
79
80
+ /**
81
+ * Resets the current viewport transformation (zoom & pan)
82
+ */
60
83
function resetTransform () {
61
- return setViewport ({ x: 0 , y: 0 , zoom: 1 })
84
+ setViewport ({ x: 0 , y: 0 , zoom: 1 })
62
85
}
63
86
64
- function toggleClass () {
65
- return ( dark .value = ! dark .value )
87
+ function toggleDarkMode () {
88
+ dark .value = ! dark .value
66
89
}
67
90
</script >
68
91
69
92
<template >
70
- <VueFlow v-model =" elements" :class =" { dark }" class =" basicflow" :default-viewport =" { zoom: 1.5 }" :min-zoom =" 0.2" :max-zoom =" 4" >
93
+ <VueFlow
94
+ :nodes =" nodes"
95
+ :edges =" edges"
96
+ :class =" { dark }"
97
+ class =" basicflow"
98
+ :default-viewport =" { zoom: 1.5 }"
99
+ :min-zoom =" 0.2"
100
+ :max-zoom =" 4"
101
+ >
71
102
<Background pattern-color =" #aaa" :gap =" 16" />
72
103
73
104
<MiniMap />
@@ -81,7 +112,7 @@ function toggleClass() {
81
112
<Icon name =" update" />
82
113
</ControlButton >
83
114
84
- <ControlButton title =" Toggle Mode" @click =" toggleClass " >
115
+ <ControlButton title =" Toggle Dark Mode" @click =" toggleDarkMode " >
85
116
<Icon v-if =" dark" name =" sun" />
86
117
<Icon v-else name =" moon" />
87
118
</ControlButton >
0 commit comments