@@ -17,13 +17,14 @@ This guide will show you how to take control of Vue Flow and apply these changes
17
17
18
18
## The ` applyChanges ` option
19
19
20
- The ` applyChanges ` option is a flag that can be passed to the ` VueFlow ` component to enable or disable automatic change handling.
20
+ The ` applyChanges ` option is a prop that can be passed to the ` < VueFlow> ` component to enable or disable automatic change handling.
21
21
22
- By setting this option to ` false ` , we tell Vue Flow to not apply changes automatically anymore.
22
+ By setting this option to ` false ` , we tell Vue Flow to not apply changes automatically anymore,
23
+ that way we can take control of changes and apply them manually.
23
24
24
25
``` vue
25
26
<template>
26
- <VueFlow v-model="elements " :apply-changes="false" />
27
+ <VueFlow :nodes="nodes" :edges="edges " :apply-changes="false" />
27
28
</template>
28
29
```
29
30
@@ -34,14 +35,28 @@ These events are emitted regardless of the `applyChanges` option, so you can use
34
35
35
36
``` vue
36
37
<script setup>
38
+ import { VueFlow, useVueFlow } from '@vue-flow/core'
39
+
40
+ const { onNodesChange, onEdgesChange } = useVueFlow()
41
+
42
+ onNodesChange((changes) => {
43
+ // changes are arrays of type `NodeChange`
44
+ console.log(changes)
45
+ })
46
+
47
+ onEdgesChange((changes) => {
48
+ // changes are arrays of type `EdgeChange`
49
+ console.log(changes)
50
+ })
51
+
37
52
const onChange = (changes) => {
38
53
// changes are arrays of type `NodeChange` or `EdgeChange`
39
- console.log(changes) // will log the changes
54
+ console.log(changes)
40
55
}
41
56
</script>
42
57
43
58
<template>
44
- <VueFlow v-model="elements " @nodes-change="onChange" @edges-change="onChange" />
59
+ <VueFlow :nodes="nodes" :edges="edges " @nodes-change="onChange" @edges-change="onChange" />
45
60
</template>
46
61
```
47
62
@@ -72,52 +87,60 @@ In this example, we will first disable automatic change handlers with `applyChan
72
87
then use the ` onNodesChange ` event to listen to changes and validate delete changes and,
73
88
if they are valid, use ` applyNodeChanges ` to apply them.
74
89
90
+ ::: info
91
+ Checkout the [ confirm delete example] ( /examples/confirm ) .
92
+ :::
93
+
75
94
``` vue
76
95
<script setup>
77
- import { useVueFlow, VueFlow } from '@vue-flow/core'
78
96
import { ref } from 'vue'
97
+ import { useVueFlow, VueFlow } from '@vue-flow/core'
79
98
80
99
const { applyNodeChanges } = useVueFlow();
81
100
82
101
const { confirm } = useConfirm();
83
102
84
- const elements = ref([
103
+ const nodes = ref([
85
104
{
86
105
id: '1',
87
- label: 'Node 1',
88
106
position: { x: 0, y: 0 },
107
+ data: { label: 'Node 1' },
89
108
},
90
109
{
91
110
id: '2',
92
- label: 'Node 2',
93
111
position: { x: 100, y: 100 },
112
+ data: { label: 'Node 2' },
94
113
},
114
+ ])
115
+
116
+ const edges = ref([
95
117
{
96
- id: 'e1-2',
118
+ id: 'e1-> 2',
97
119
source: '1',
98
120
target: '2',
99
121
},
100
122
])
101
123
102
- const onNodesChange = (changes) => {
103
- changes.forEach(async (change) => {
104
- // if the change is a remove change, we want to validate it first
105
- if (change.type === 'remove') {
106
- const isConfirmed = await confirm();
107
-
108
- if (isConfirmed) {
109
- // if confirmed, apply the change
110
- applyNodeChanges([change])
111
- }
112
- } else {
113
- // apply all other changes
114
- applyNodeChanges([change])
115
- }
116
- })
124
+ const onNodesChange = async (changes) => {
125
+ const nextChanges = []
126
+
127
+ for (const change of changes) {
128
+ if (change.type === 'remove') {
129
+ const isConfirmed = await confirm('Are you sure you want to delete this node?')
130
+
131
+ if (isConfirmed) {
132
+ nextChanges.push(change)
133
+ }
134
+ } else {
135
+ nextChanges.push(change)
136
+ }
137
+ }
138
+
139
+ applyNodeChanges(nextChanges)
117
140
}
118
141
</script>
119
142
120
143
<template>
121
- <VueFlow v-model="elements " :apply-changes="false" @nodes-change="onNodesChange" />
144
+ <VueFlow :nodes="nodes" :edges="edges " :apply-changes="false" @nodes-change="onNodesChange" />
122
145
</template>
123
146
```
0 commit comments