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 { LayoutApp, LayoutEdge, LayoutElements, LayoutIcon, LayoutNode, useLayout, useRunProcess, useShuffle } from './layout'
|
||||
import { MathApp, MathCSS, MathElements, MathIcon, MathOperatorNode, MathResultNode, MathValueNode } from './math'
|
||||
import { ConfirmApp, ConfirmDialog, useDialog } from './confirm-delete'
|
||||
|
||||
export const exampleImports = {
|
||||
basic: {
|
||||
@@ -150,4 +151,9 @@ export const exampleImports = {
|
||||
'style.css': MathCSS,
|
||||
'initial-elements.js': MathElements,
|
||||
},
|
||||
confirmDelete: {
|
||||
'App.vue': ConfirmApp,
|
||||
'Dialog.vue': ConfirmDialog,
|
||||
'useDialog.js': useDialog,
|
||||
},
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user