|
1 | 1 | <script setup>
|
2 |
| -import { Background } from '@vue-flow/background' |
3 |
| -import { Panel, VueFlow, isNode } from '@vue-flow/core' |
4 | 2 | import { ref } from 'vue'
|
| 3 | +import { Background } from '@vue-flow/background' |
| 4 | +import { Panel, VueFlow } from '@vue-flow/core' |
5 | 5 |
|
6 |
| -const elements = ref([ |
| 6 | +const nodes = ref([ |
7 | 7 | { id: '1', type: 'input', label: 'Node 1', position: { x: 250, y: 5 }, class: 'light' },
|
8 | 8 | { id: '2', label: 'Node 2', position: { x: 100, y: 100 }, class: 'light' },
|
9 | 9 | { id: '3', label: 'Node 3', position: { x: 400, y: 100 }, class: 'light' },
|
10 | 10 | { id: '4', label: 'Node 4', position: { x: 400, y: 200 }, class: 'light' },
|
11 |
| - { id: 'e1-2', source: '1', target: '2', animated: true }, |
| 11 | +]) |
| 12 | +
|
| 13 | +const edges = ref([ |
| 14 | + { id: 'e1-2', source: '1', target: '2' }, |
12 | 15 | { id: 'e1-3', source: '1', target: '3' },
|
| 16 | + { id: 'e3-4', source: '3', target: '4' }, |
13 | 17 | ])
|
14 | 18 |
|
15 | 19 | function toggleClass() {
|
16 |
| - return elements.value.forEach((el) => (el.class = el.class === 'light' ? 'dark' : 'light')) |
| 20 | + nodes.value = nodes.value.map((node) => { |
| 21 | + return { |
| 22 | + ...node, |
| 23 | + class: node.class === 'light' ? 'dark' : 'light', |
| 24 | + } |
| 25 | + }) |
17 | 26 | }
|
18 | 27 |
|
19 | 28 | function updatePos() {
|
20 |
| - return elements.value.forEach((el) => { |
21 |
| - if (isNode(el)) { |
22 |
| - el.position = { |
| 29 | + nodes.value = nodes.value.map((node) => { |
| 30 | + return { |
| 31 | + ...node, |
| 32 | + position: { |
23 | 33 | x: Math.random() * 400,
|
24 | 34 | y: Math.random() * 400,
|
25 |
| - } |
| 35 | + }, |
26 | 36 | }
|
27 | 37 | })
|
28 | 38 | }
|
29 | 39 | </script>
|
30 | 40 |
|
31 | 41 | <template>
|
32 |
| - <VueFlow v-model="elements" fit-view-on-init> |
| 42 | + <VueFlow :nodes="nodes" :edges="edges" fit-view-on-init> |
33 | 43 | <Background />
|
34 | 44 |
|
35 | 45 | <Panel position="top-right">
|
|
0 commit comments