|
1 | 1 | <script setup>
|
2 |
| -import { VueFlow, useVueFlow } from '@vue-flow/core' |
3 | 2 | import { ref } from 'vue'
|
4 |
| -import { initialElements } from './initial-elements.js' |
| 3 | +import { VueFlow, useVueFlow } from '@vue-flow/core' |
5 | 4 |
|
6 | 5 | /**
|
7 | 6 | * You can either use `getIntersectingNodes` to check if a given node intersects with others
|
8 | 7 | * or `isNodeIntersecting` to check if a node is intersecting with a given area
|
9 | 8 | */
|
10 | 9 | // eslint-disable-next-line @typescript-eslint/no-unused-vars
|
11 |
| -const { onNodeDrag, getIntersectingNodes, isNodeIntersecting, getNodes } = useVueFlow() |
| 10 | +const { onNodeDrag, getIntersectingNodes, isNodeIntersecting, getNodes, updateNode } = useVueFlow() |
12 | 11 |
|
13 |
| -const elements = ref(initialElements) |
| 12 | +const nodes = ref([ |
| 13 | + { id: '1', label: 'Node 1', position: { x: 0, y: 0 } }, |
| 14 | + { id: '2', label: 'Node 2', position: { x: 400, y: 400 } }, |
| 15 | + { id: '3', label: 'Node 3', position: { x: 400, y: 0 } }, |
| 16 | + { id: '4', label: 'Node 4', position: { x: 0, y: 400 } }, |
| 17 | + { id: '5', style: { borderColor: 'red' }, label: 'Drag me over another node', position: { x: 200, y: 200 } }, |
| 18 | +]) |
14 | 19 |
|
15 | 20 | onNodeDrag(({ intersections }) => {
|
16 | 21 | const intersectionIds = intersections.map((intersection) => intersection.id)
|
17 | 22 |
|
18 |
| - getNodes.value.forEach((n) => { |
19 |
| - const isIntersecting = intersectionIds.includes(n.id) |
20 |
| - n.class = isIntersecting ? 'intersecting' : '' |
21 |
| - }) |
| 23 | + for (const node of getNodes.value) { |
| 24 | + const isIntersecting = intersectionIds.includes(node.id) |
| 25 | +
|
| 26 | + updateNode(node.id, { class: isIntersecting ? 'intersecting' : '' }) |
| 27 | + } |
22 | 28 | })
|
23 | 29 | </script>
|
24 | 30 |
|
25 | 31 | <template>
|
26 |
| - <VueFlow v-model="elements" fit-view-on-init :default-viewport="{ zoom: 1.5 }" :min-zoom="0.2" :max-zoom="4" /> |
| 32 | + <VueFlow :nodes="nodes" fit-view-on-init /> |
27 | 33 | </template>
|
0 commit comments