Skip to content

Commit 257c7aa

Browse files
committed
examples: add confirm delete example (#1466)
* examples: add confirm delete example * docs: add confirm delete example
1 parent 67da354 commit 257c7aa

File tree

12 files changed

+339
-1
lines changed

12 files changed

+339
-1
lines changed

docs/examples/confirm-delete/App.vue

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<script setup>
2+
import { ref } from 'vue'
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()
11+
12+
const nodes = ref([
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([
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 = []
28+
29+
for (const change of changes) {
30+
if (change.type === 'remove') {
31+
const isConfirmed = await dialog.confirm(`Do you really want to delete this node: ${change.id}?`)
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 = []
46+
47+
for (const change of changes) {
48+
if (change.type === 'remove') {
49+
const isConfirmed = await dialog.confirm(`Do you really want to delete this edge: ${change.id}?`)
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>
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<script setup>
2+
import { useDialogState } from './useDialog.js'
3+
4+
const { isVisible, message, resolve } = useDialogState()
5+
6+
function confirm() {
7+
resolve(true)
8+
isVisible.value = false
9+
}
10+
11+
function cancel() {
12+
resolve(false)
13+
isVisible.value = false
14+
}
15+
</script>
16+
17+
<template>
18+
<div v-if="isVisible" class="dialog-overlay">
19+
<div class="dialog">
20+
<p>{{ message }}</p>
21+
22+
<div class="dialog-actions">
23+
<button @click="confirm">Confirm</button>
24+
<button @click="cancel">Cancel</button>
25+
</div>
26+
</div>
27+
</div>
28+
</template>
29+
30+
<style>
31+
.dialog-overlay {
32+
position: fixed;
33+
top: 0;
34+
left: 0;
35+
width: 100%;
36+
height: 100%;
37+
display: flex;
38+
justify-content: center;
39+
align-items: center;
40+
background-color: rgba(0, 0, 0, 0.5);
41+
z-index: 1000;
42+
}
43+
44+
.dialog {
45+
background: white;
46+
padding: 20px;
47+
border-radius: 5px;
48+
text-align: center;
49+
}
50+
51+
.dialog-actions {
52+
margin-top: 20px;
53+
display: flex;
54+
justify-content: center;
55+
gap: 8px;
56+
}
57+
</style>

docs/examples/confirm-delete/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export { default as ConfirmApp } from './App.vue?raw'
2+
export { default as ConfirmDialog } from './Dialog.vue?raw'
3+
export { default as useDialog } from './useDialog.js?raw'
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { ref } from 'vue'
2+
3+
const isVisible = ref(false)
4+
const message = ref('')
5+
let resolveCallback
6+
7+
export function useDialogState() {
8+
return {
9+
isVisible,
10+
message,
11+
resolve: (value) => {
12+
if (resolveCallback) {
13+
resolveCallback(value)
14+
}
15+
},
16+
}
17+
}
18+
19+
export function useDialog() {
20+
return {
21+
confirm(msg) {
22+
isVisible.value = true
23+
message.value = msg
24+
return new Promise((resolve) => {
25+
resolveCallback = resolve
26+
})
27+
},
28+
}
29+
}

docs/examples/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import { NodeResizerApp, ResizableNode } from './node-resizer'
2020
import { ToolbarApp, ToolbarNode } from './node-toolbar'
2121
import { LayoutApp, LayoutEdge, LayoutElements, LayoutIcon, LayoutNode, useLayout, useRunProcess, useShuffle } from './layout'
2222
import { MathApp, MathCSS, MathElements, MathIcon, MathOperatorNode, MathResultNode, MathValueNode } from './math'
23+
import { ConfirmApp, ConfirmDialog, useDialog } from './confirm-delete'
2324

2425
export const exampleImports = {
2526
basic: {
@@ -150,4 +151,9 @@ export const exampleImports = {
150151
'style.css': MathCSS,
151152
'initial-elements.js': MathElements,
152153
},
154+
confirmDelete: {
155+
'App.vue': ConfirmApp,
156+
'Dialog.vue': ConfirmDialog,
157+
'useDialog.js': useDialog,
158+
},
153159
}

docs/src/.vitepress/config.mts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ export default defineConfigWithTheme<DefaultTheme.Config>({
208208
{ text: 'Save & Restore', link: '/examples/save' },
209209
{ text: 'Math Operation Flow', link: '/examples/math' },
210210
{ text: 'Screenshot', link: '/examples/screenshot' },
211+
{ text: 'Confirm Delete', link: '/examples/confirm' },
211212
{ text: 'Node Visibility', link: '/examples/hidden' },
212213
{ text: 'Node Intersections', link: '/examples/intersection' },
213214
{ text: 'Multiple Flows', link: '/examples/multi' },

docs/src/examples/confirm.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Confirm Delete
2+
3+
Sometimes you want to confirm a delete action before it happens. Here's how you can do that with a simple dialog.
4+
5+
This example also demonstrates how to take-over the application of changes to the graph. This is useful when you want to confirm changes before they are applied.
6+
7+
<div class="mt-6">
8+
<Repl example="confirmDelete"></Repl>
9+
</div>

examples/vite/router.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,10 @@ export const routes: RouterOptions['routes'] = [
134134
path: '/screenshot',
135135
component: () => import('./src/Screenshot/ScreenshotExample.vue'),
136136
},
137+
{
138+
path: '/confirm-delete',
139+
component: () => import('./src/ConfirmDelete/ConfirmDeleteExample.vue'),
140+
},
137141
]
138142

139143
export const router = createRouter({
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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>
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<script setup lang="ts">
2+
import { useDialogState } from './useDialog'
3+
4+
const { isVisible, message, resolve } = useDialogState()
5+
6+
function confirm() {
7+
resolve(true)
8+
isVisible.value = false
9+
}
10+
11+
function cancel() {
12+
resolve(false)
13+
isVisible.value = false
14+
}
15+
</script>
16+
17+
<template>
18+
<div v-if="isVisible" class="dialog-overlay">
19+
<div class="dialog">
20+
<p>{{ message }}</p>
21+
22+
<div class="dialog-actions">
23+
<button @click="confirm">Confirm</button>
24+
<button @click="cancel">Cancel</button>
25+
</div>
26+
</div>
27+
</div>
28+
</template>
29+
30+
<style>
31+
.dialog-overlay {
32+
position: fixed;
33+
top: 0;
34+
left: 0;
35+
width: 100%;
36+
height: 100%;
37+
display: flex;
38+
justify-content: center;
39+
align-items: center;
40+
background-color: rgba(0, 0, 0, 0.5);
41+
z-index: 1000;
42+
}
43+
44+
.dialog {
45+
background: white;
46+
padding: 20px;
47+
border-radius: 5px;
48+
text-align: center;
49+
}
50+
51+
.dialog-actions {
52+
margin-top: 20px;
53+
display: flex;
54+
justify-content: center;
55+
gap: 8px;
56+
}
57+
</style>

0 commit comments

Comments
 (0)