fix: prevent Vue Flow edge render crash by clearing edges before structural node changes
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
<script setup lang="ts">
|
||||
import { watch, ref, onMounted } from 'vue'
|
||||
import { watch, onMounted, nextTick } from 'vue'
|
||||
import { VueFlow, useVueFlow } from '@vue-flow/core'
|
||||
import { Background } from '@vue-flow/background'
|
||||
import { Controls } from '@vue-flow/controls'
|
||||
@@ -23,8 +23,8 @@ const emit = defineEmits<{
|
||||
|
||||
const { nodes, edges, onNodeClick, onConnect, fitView } = useVueFlow()
|
||||
|
||||
function rebuild() {
|
||||
nodes.value = props.sceneNodes.map((n, i) => ({
|
||||
function buildNodes() {
|
||||
return props.sceneNodes.map((n, i) => ({
|
||||
id: n.id,
|
||||
type: 'default',
|
||||
position: { x: (i % 4) * 220 + 50, y: Math.floor(i / 4) * 120 + 50 },
|
||||
@@ -38,8 +38,10 @@ function rebuild() {
|
||||
targetPosition: 'left' as const,
|
||||
connectable: true,
|
||||
}))
|
||||
}
|
||||
|
||||
edges.value = props.sceneEdges.map((e) => ({
|
||||
function buildEdges() {
|
||||
return props.sceneEdges.map((e) => ({
|
||||
id: e.id,
|
||||
source: e.source,
|
||||
target: e.target,
|
||||
@@ -51,9 +53,33 @@ function rebuild() {
|
||||
}))
|
||||
}
|
||||
|
||||
let lastNodesLen = 0
|
||||
let lastEdgesLen = 0
|
||||
|
||||
async function structuralRebuild() {
|
||||
edges.value = []
|
||||
await nextTick()
|
||||
nodes.value = buildNodes()
|
||||
await nextTick()
|
||||
edges.value = buildEdges()
|
||||
lastNodesLen = props.sceneNodes.length
|
||||
lastEdgesLen = props.sceneEdges.length
|
||||
}
|
||||
|
||||
function styleOnlyRebuild() {
|
||||
nodes.value = buildNodes()
|
||||
edges.value = buildEdges()
|
||||
}
|
||||
|
||||
watch(() => [props.sceneNodes, props.sceneEdges, props.startScene, props.selectedNodeId], () => {
|
||||
rebuild()
|
||||
}, { deep: true, immediate: true })
|
||||
const nodesLen = props.sceneNodes.length
|
||||
const edgesLen = props.sceneEdges.length
|
||||
if (nodesLen !== lastNodesLen || edgesLen !== lastEdgesLen) {
|
||||
structuralRebuild()
|
||||
} else {
|
||||
styleOnlyRebuild()
|
||||
}
|
||||
})
|
||||
|
||||
onNodeClick((event) => {
|
||||
emit('selectNode', event.node.id)
|
||||
|
||||
Reference in New Issue
Block a user