|
| 1 | +<script lang="ts" setup> |
| 2 | +import type { Edge, EdgeChange, Node, NodeChange } from '@vue-flow/core' |
| 3 | +import { VueFlow, useVueFlow } from '@vue-flow/core' |
| 4 | +import { Background } from '@vue-flow/background' |
| 5 | +import { useDialog } from './useDialog' |
| 6 | +import Dialog from './Dialog.vue' |
| 7 | +
|
| 8 | +const { onConnect, addEdges, onNodesChange, onEdgesChange, applyNodeChanges, applyEdgeChanges } = useVueFlow() |
| 9 | +
|
| 10 | +const dialog = useDialog({ message: 'Do you really want to delete this item?' }) |
| 11 | +
|
| 12 | +const nodes = ref<Node[]>([ |
| 13 | + { id: '1', type: 'input', label: 'Node 1', position: { x: 250, y: 5 }, class: 'light' }, |
| 14 | + { id: '2', label: 'Node 2', position: { x: 100, y: 100 }, class: 'light' }, |
| 15 | + { id: '3', label: 'Node 3', position: { x: 400, y: 100 }, class: 'light' }, |
| 16 | + { id: '4', label: 'Node 4', position: { x: 400, y: 200 }, class: 'light' }, |
| 17 | +]) |
| 18 | +
|
| 19 | +const edges = ref<Edge[]>([ |
| 20 | + { id: 'e1-2', source: '1', target: '2', animated: true }, |
| 21 | + { id: 'e1-3', source: '1', target: '3' }, |
| 22 | +]) |
| 23 | +
|
| 24 | +onConnect(addEdges) |
| 25 | +
|
| 26 | +onNodesChange(async (changes) => { |
| 27 | + const nextChanges: NodeChange[] = [] |
| 28 | +
|
| 29 | + for (const change of changes) { |
| 30 | + if (change.type === 'remove') { |
| 31 | + const isConfirmed = await dialog.confirm() |
| 32 | +
|
| 33 | + if (isConfirmed) { |
| 34 | + nextChanges.push(change) |
| 35 | + } |
| 36 | + } else { |
| 37 | + nextChanges.push(change) |
| 38 | + } |
| 39 | + } |
| 40 | +
|
| 41 | + applyNodeChanges(nextChanges) |
| 42 | +}) |
| 43 | +
|
| 44 | +onEdgesChange(async (changes) => { |
| 45 | + const nextChanges: EdgeChange[] = [] |
| 46 | +
|
| 47 | + for (const change of changes) { |
| 48 | + if (change.type === 'remove') { |
| 49 | + const isConfirmed = await dialog.confirm() |
| 50 | +
|
| 51 | + if (isConfirmed) { |
| 52 | + nextChanges.push(change) |
| 53 | + } |
| 54 | + } else { |
| 55 | + nextChanges.push(change) |
| 56 | + } |
| 57 | + } |
| 58 | +
|
| 59 | + applyEdgeChanges(nextChanges) |
| 60 | +}) |
| 61 | +</script> |
| 62 | + |
| 63 | +<template> |
| 64 | + <VueFlow :nodes="nodes" :edges="edges" :apply-default="false" fit-view-on-init class="vue-flow-basic-example"> |
| 65 | + <Background /> |
| 66 | + |
| 67 | + <Dialog /> |
| 68 | + </VueFlow> |
| 69 | +</template> |
0 commit comments