Delete Edge on Drop
In this example we are showing how to delete an edge by using the @reconnect, @reconnect-start and @reconnect-end events.
<script setup lang="ts">
import type {
Connection,
Edge,
EdgeReconnectEndEvent,
EdgeReconnectEvent,
Node,
} from '@xyflow/vue';
import { shallowRef } from 'vue';
import {
addEdge,
Background,
Controls,
reconnectEdge,
VueFlow,
} from '@xyflow/vue';
const nodes = shallowRef<Node[]>([
{
id: '1',
type: 'input',
data: { label: 'Node A' },
position: { x: 250, y: 0 },
},
{ id: '2', data: { label: 'Node B' }, position: { x: 100, y: 200 } },
{ id: '3', data: { label: 'Node C' }, position: { x: 350, y: 200 } },
]);
const edges = shallowRef<Edge[]>([
{ id: 'e1-2', source: '1', target: '2', label: 'reconnectable edge' },
]);
let edgeReconnectSuccessful = true;
function onConnect(connection: Connection) {
edges.value = addEdge(connection, edges.value);
}
function onReconnectStart() {
edgeReconnectSuccessful = false;
}
function onReconnect(event: EdgeReconnectEvent) {
edgeReconnectSuccessful = true;
edges.value = reconnectEdge(event.edge, event.connection, edges.value);
}
function onReconnectEnd(event: EdgeReconnectEndEvent) {
if (!edgeReconnectSuccessful) {
edges.value = edges.value.filter((edge) => edge.id !== event.edge.id);
}
edgeReconnectSuccessful = true;
}
</script>
<template>
<VueFlow
v-model:nodes="nodes"
v-model:edges="edges"
snap-to-grid
attribution-position="top-right"
fit-view
@connect="onConnect"
@reconnect="onReconnect"
@reconnect-start="onReconnectStart"
@reconnect-end="onReconnectEnd"
>
<Controls />
<Background />
</VueFlow>
</template>