examples: add confirm delete example (#1466)
* examples: add confirm delete example * docs: add confirm delete example
This commit is contained in:
@@ -0,0 +1,69 @@
|
|||||||
|
<script setup>
|
||||||
|
import { ref } from 'vue'
|
||||||
|
import { VueFlow, useVueFlow } from '@vue-flow/core'
|
||||||
|
import { Background } from '@vue-flow/background'
|
||||||
|
import { useDialog } from './useDialog'
|
||||||
|
import Dialog from './Dialog.vue'
|
||||||
|
|
||||||
|
const { onConnect, addEdges, onNodesChange, onEdgesChange, applyNodeChanges, applyEdgeChanges } = useVueFlow()
|
||||||
|
|
||||||
|
const dialog = useDialog()
|
||||||
|
|
||||||
|
const nodes = ref([
|
||||||
|
{ id: '1', type: 'input', label: 'Node 1', position: { x: 250, y: 5 }, class: 'light' },
|
||||||
|
{ id: '2', label: 'Node 2', position: { x: 100, y: 100 }, class: 'light' },
|
||||||
|
{ id: '3', label: 'Node 3', position: { x: 400, y: 100 }, class: 'light' },
|
||||||
|
{ id: '4', label: 'Node 4', position: { x: 400, y: 200 }, class: 'light' },
|
||||||
|
])
|
||||||
|
|
||||||
|
const edges = ref([
|
||||||
|
{ id: 'e1-2', source: '1', target: '2', animated: true },
|
||||||
|
{ id: 'e1-3', source: '1', target: '3' },
|
||||||
|
])
|
||||||
|
|
||||||
|
onConnect(addEdges)
|
||||||
|
|
||||||
|
onNodesChange(async (changes) => {
|
||||||
|
const nextChanges = []
|
||||||
|
|
||||||
|
for (const change of changes) {
|
||||||
|
if (change.type === 'remove') {
|
||||||
|
const isConfirmed = await dialog.confirm(`Do you really want to delete this node: ${change.id}?`)
|
||||||
|
|
||||||
|
if (isConfirmed) {
|
||||||
|
nextChanges.push(change)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
nextChanges.push(change)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
applyNodeChanges(nextChanges)
|
||||||
|
})
|
||||||
|
|
||||||
|
onEdgesChange(async (changes) => {
|
||||||
|
const nextChanges = []
|
||||||
|
|
||||||
|
for (const change of changes) {
|
||||||
|
if (change.type === 'remove') {
|
||||||
|
const isConfirmed = await dialog.confirm(`Do you really want to delete this edge: ${change.id}?`)
|
||||||
|
|
||||||
|
if (isConfirmed) {
|
||||||
|
nextChanges.push(change)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
nextChanges.push(change)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
applyEdgeChanges(nextChanges)
|
||||||
|
})
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<template>
|
||||||
|
<VueFlow :nodes="nodes" :edges="edges" :apply-default="false" fit-view-on-init class="vue-flow-basic-example">
|
||||||
|
<Background />
|
||||||
|
|
||||||
|
<Dialog />
|
||||||
|
</VueFlow>
|
||||||
|
</template>
|
||||||
@@ -0,0 +1,57 @@
|
|||||||
|
<script setup>
|
||||||
|
import { useDialogState } from './useDialog.js'
|
||||||
|
|
||||||
|
const { isVisible, message, resolve } = useDialogState()
|
||||||
|
|
||||||
|
function confirm() {
|
||||||
|
resolve(true)
|
||||||
|
isVisible.value = false
|
||||||
|
}
|
||||||
|
|
||||||
|
function cancel() {
|
||||||
|
resolve(false)
|
||||||
|
isVisible.value = false
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<template>
|
||||||
|
<div v-if="isVisible" class="dialog-overlay">
|
||||||
|
<div class="dialog">
|
||||||
|
<p>{{ message }}</p>
|
||||||
|
|
||||||
|
<div class="dialog-actions">
|
||||||
|
<button @click="confirm">Confirm</button>
|
||||||
|
<button @click="cancel">Cancel</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<style>
|
||||||
|
.dialog-overlay {
|
||||||
|
position: fixed;
|
||||||
|
top: 0;
|
||||||
|
left: 0;
|
||||||
|
width: 100%;
|
||||||
|
height: 100%;
|
||||||
|
display: flex;
|
||||||
|
justify-content: center;
|
||||||
|
align-items: center;
|
||||||
|
background-color: rgba(0, 0, 0, 0.5);
|
||||||
|
z-index: 1000;
|
||||||
|
}
|
||||||
|
|
||||||
|
.dialog {
|
||||||
|
background: white;
|
||||||
|
padding: 20px;
|
||||||
|
border-radius: 5px;
|
||||||
|
text-align: center;
|
||||||
|
}
|
||||||
|
|
||||||
|
.dialog-actions {
|
||||||
|
margin-top: 20px;
|
||||||
|
display: flex;
|
||||||
|
justify-content: center;
|
||||||
|
gap: 8px;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
export { default as ConfirmApp } from './App.vue?raw'
|
||||||
|
export { default as ConfirmDialog } from './Dialog.vue?raw'
|
||||||
|
export { default as useDialog } from './useDialog.js?raw'
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
import { ref } from 'vue'
|
||||||
|
|
||||||
|
const isVisible = ref(false)
|
||||||
|
const message = ref('')
|
||||||
|
let resolveCallback
|
||||||
|
|
||||||
|
export function useDialogState() {
|
||||||
|
return {
|
||||||
|
isVisible,
|
||||||
|
message,
|
||||||
|
resolve: (value) => {
|
||||||
|
if (resolveCallback) {
|
||||||
|
resolveCallback(value)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function useDialog() {
|
||||||
|
return {
|
||||||
|
confirm(msg) {
|
||||||
|
isVisible.value = true
|
||||||
|
message.value = msg
|
||||||
|
return new Promise((resolve) => {
|
||||||
|
resolveCallback = resolve
|
||||||
|
})
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -20,6 +20,7 @@ import { NodeResizerApp, ResizableNode } from './node-resizer'
|
|||||||
import { ToolbarApp, ToolbarNode } from './node-toolbar'
|
import { ToolbarApp, ToolbarNode } from './node-toolbar'
|
||||||
import { LayoutApp, LayoutEdge, LayoutElements, LayoutIcon, LayoutNode, useLayout, useRunProcess, useShuffle } from './layout'
|
import { LayoutApp, LayoutEdge, LayoutElements, LayoutIcon, LayoutNode, useLayout, useRunProcess, useShuffle } from './layout'
|
||||||
import { MathApp, MathCSS, MathElements, MathIcon, MathOperatorNode, MathResultNode, MathValueNode } from './math'
|
import { MathApp, MathCSS, MathElements, MathIcon, MathOperatorNode, MathResultNode, MathValueNode } from './math'
|
||||||
|
import { ConfirmApp, ConfirmDialog, useDialog } from './confirm-delete'
|
||||||
|
|
||||||
export const exampleImports = {
|
export const exampleImports = {
|
||||||
basic: {
|
basic: {
|
||||||
@@ -150,4 +151,9 @@ export const exampleImports = {
|
|||||||
'style.css': MathCSS,
|
'style.css': MathCSS,
|
||||||
'initial-elements.js': MathElements,
|
'initial-elements.js': MathElements,
|
||||||
},
|
},
|
||||||
|
confirmDelete: {
|
||||||
|
'App.vue': ConfirmApp,
|
||||||
|
'Dialog.vue': ConfirmDialog,
|
||||||
|
'useDialog.js': useDialog,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -208,6 +208,7 @@ export default defineConfigWithTheme<DefaultTheme.Config>({
|
|||||||
{ text: 'Save & Restore', link: '/examples/save' },
|
{ text: 'Save & Restore', link: '/examples/save' },
|
||||||
{ text: 'Math Operation Flow', link: '/examples/math' },
|
{ text: 'Math Operation Flow', link: '/examples/math' },
|
||||||
{ text: 'Screenshot', link: '/examples/screenshot' },
|
{ text: 'Screenshot', link: '/examples/screenshot' },
|
||||||
|
{ text: 'Confirm Delete', link: '/examples/confirm' },
|
||||||
{ text: 'Node Visibility', link: '/examples/hidden' },
|
{ text: 'Node Visibility', link: '/examples/hidden' },
|
||||||
{ text: 'Node Intersections', link: '/examples/intersection' },
|
{ text: 'Node Intersections', link: '/examples/intersection' },
|
||||||
{ text: 'Multiple Flows', link: '/examples/multi' },
|
{ text: 'Multiple Flows', link: '/examples/multi' },
|
||||||
|
|||||||
@@ -0,0 +1,9 @@
|
|||||||
|
# Confirm Delete
|
||||||
|
|
||||||
|
Sometimes you want to confirm a delete action before it happens. Here's how you can do that with a simple dialog.
|
||||||
|
|
||||||
|
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.
|
||||||
|
|
||||||
|
<div class="mt-6">
|
||||||
|
<Repl example="confirmDelete"></Repl>
|
||||||
|
</div>
|
||||||
@@ -134,6 +134,10 @@ export const routes: RouterOptions['routes'] = [
|
|||||||
path: '/screenshot',
|
path: '/screenshot',
|
||||||
component: () => import('./src/Screenshot/ScreenshotExample.vue'),
|
component: () => import('./src/Screenshot/ScreenshotExample.vue'),
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
path: '/confirm-delete',
|
||||||
|
component: () => import('./src/ConfirmDelete/ConfirmDeleteExample.vue'),
|
||||||
|
},
|
||||||
]
|
]
|
||||||
|
|
||||||
export const router = createRouter({
|
export const router = createRouter({
|
||||||
|
|||||||
@@ -0,0 +1,69 @@
|
|||||||
|
<script lang="ts" setup>
|
||||||
|
import type { Edge, EdgeChange, Node, NodeChange } from '@vue-flow/core'
|
||||||
|
import { VueFlow, useVueFlow } from '@vue-flow/core'
|
||||||
|
import { Background } from '@vue-flow/background'
|
||||||
|
import { useDialog } from './useDialog'
|
||||||
|
import Dialog from './Dialog.vue'
|
||||||
|
|
||||||
|
const { onConnect, addEdges, onNodesChange, onEdgesChange, applyNodeChanges, applyEdgeChanges } = useVueFlow()
|
||||||
|
|
||||||
|
const dialog = useDialog({ message: 'Do you really want to delete this item?' })
|
||||||
|
|
||||||
|
const nodes = ref<Node[]>([
|
||||||
|
{ id: '1', type: 'input', label: 'Node 1', position: { x: 250, y: 5 }, class: 'light' },
|
||||||
|
{ id: '2', label: 'Node 2', position: { x: 100, y: 100 }, class: 'light' },
|
||||||
|
{ id: '3', label: 'Node 3', position: { x: 400, y: 100 }, class: 'light' },
|
||||||
|
{ id: '4', label: 'Node 4', position: { x: 400, y: 200 }, class: 'light' },
|
||||||
|
])
|
||||||
|
|
||||||
|
const edges = ref<Edge[]>([
|
||||||
|
{ id: 'e1-2', source: '1', target: '2', animated: true },
|
||||||
|
{ id: 'e1-3', source: '1', target: '3' },
|
||||||
|
])
|
||||||
|
|
||||||
|
onConnect(addEdges)
|
||||||
|
|
||||||
|
onNodesChange(async (changes) => {
|
||||||
|
const nextChanges: NodeChange[] = []
|
||||||
|
|
||||||
|
for (const change of changes) {
|
||||||
|
if (change.type === 'remove') {
|
||||||
|
const isConfirmed = await dialog.confirm()
|
||||||
|
|
||||||
|
if (isConfirmed) {
|
||||||
|
nextChanges.push(change)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
nextChanges.push(change)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
applyNodeChanges(nextChanges)
|
||||||
|
})
|
||||||
|
|
||||||
|
onEdgesChange(async (changes) => {
|
||||||
|
const nextChanges: EdgeChange[] = []
|
||||||
|
|
||||||
|
for (const change of changes) {
|
||||||
|
if (change.type === 'remove') {
|
||||||
|
const isConfirmed = await dialog.confirm()
|
||||||
|
|
||||||
|
if (isConfirmed) {
|
||||||
|
nextChanges.push(change)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
nextChanges.push(change)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
applyEdgeChanges(nextChanges)
|
||||||
|
})
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<template>
|
||||||
|
<VueFlow :nodes="nodes" :edges="edges" :apply-default="false" fit-view-on-init class="vue-flow-basic-example">
|
||||||
|
<Background />
|
||||||
|
|
||||||
|
<Dialog />
|
||||||
|
</VueFlow>
|
||||||
|
</template>
|
||||||
@@ -0,0 +1,57 @@
|
|||||||
|
<script setup lang="ts">
|
||||||
|
import { useDialogState } from './useDialog'
|
||||||
|
|
||||||
|
const { isVisible, message, resolve } = useDialogState()
|
||||||
|
|
||||||
|
function confirm() {
|
||||||
|
resolve(true)
|
||||||
|
isVisible.value = false
|
||||||
|
}
|
||||||
|
|
||||||
|
function cancel() {
|
||||||
|
resolve(false)
|
||||||
|
isVisible.value = false
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<template>
|
||||||
|
<div v-if="isVisible" class="dialog-overlay">
|
||||||
|
<div class="dialog">
|
||||||
|
<p>{{ message }}</p>
|
||||||
|
|
||||||
|
<div class="dialog-actions">
|
||||||
|
<button @click="confirm">Confirm</button>
|
||||||
|
<button @click="cancel">Cancel</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<style>
|
||||||
|
.dialog-overlay {
|
||||||
|
position: fixed;
|
||||||
|
top: 0;
|
||||||
|
left: 0;
|
||||||
|
width: 100%;
|
||||||
|
height: 100%;
|
||||||
|
display: flex;
|
||||||
|
justify-content: center;
|
||||||
|
align-items: center;
|
||||||
|
background-color: rgba(0, 0, 0, 0.5);
|
||||||
|
z-index: 1000;
|
||||||
|
}
|
||||||
|
|
||||||
|
.dialog {
|
||||||
|
background: white;
|
||||||
|
padding: 20px;
|
||||||
|
border-radius: 5px;
|
||||||
|
text-align: center;
|
||||||
|
}
|
||||||
|
|
||||||
|
.dialog-actions {
|
||||||
|
margin-top: 20px;
|
||||||
|
display: flex;
|
||||||
|
justify-content: center;
|
||||||
|
gap: 8px;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
import { ref } from 'vue'
|
||||||
|
|
||||||
|
const isVisible = ref(false)
|
||||||
|
const message = ref('')
|
||||||
|
let resolveCallback: (value: boolean) => void
|
||||||
|
|
||||||
|
export function useDialogState() {
|
||||||
|
return {
|
||||||
|
isVisible,
|
||||||
|
message,
|
||||||
|
resolve: (value: boolean) => {
|
||||||
|
if (resolveCallback) {
|
||||||
|
resolveCallback(value)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function useDialog(opts: { message?: string }) {
|
||||||
|
message.value = opts.message || 'Are you sure?'
|
||||||
|
|
||||||
|
return {
|
||||||
|
confirm(): Promise<boolean> {
|
||||||
|
isVisible.value = true
|
||||||
|
return new Promise<boolean>((resolve) => {
|
||||||
|
resolveCallback = resolve
|
||||||
|
})
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -26,7 +26,11 @@ export default {
|
|||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
<Handle type="target" :position="Position.Left" :style="targetHandleStyle" />
|
<Handle type="target" :position="Position.Left" :style="targetHandleStyle">
|
||||||
|
<svg viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg" :width="10" :height="10">
|
||||||
|
<circle cx="8" cy="8" r="7" fill="hotpink" />
|
||||||
|
</svg>
|
||||||
|
</Handle>
|
||||||
<div>
|
<div>
|
||||||
Custom Color Picker Node: <strong>{{ data.color }}</strong>
|
Custom Color Picker Node: <strong>{{ data.color }}</strong>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
Reference in New Issue
Block a user