diff --git a/examples/Stress/StressExample.vue b/examples/Stress/StressExample.vue index 386c0c50..1d4b4304 100644 --- a/examples/Stress/StressExample.vue +++ b/examples/Stress/StressExample.vue @@ -49,7 +49,7 @@ const updateElements = () => { } diff --git a/src/container/VueFlow/VueFlow.vue b/src/container/VueFlow/VueFlow.vue index b3c54d4c..026332d6 100644 --- a/src/container/VueFlow/VueFlow.vue +++ b/src/container/VueFlow/VueFlow.vue @@ -130,9 +130,10 @@ invoke(async () => { }) watch( elements, - (val) => { + (val, oldVal) => { nextTick(() => { - store.setElements(val) + const hasDiff = diff(val, oldVal) + if (hasDiff.length > 0) store.setElements(val) }) }, { flush: 'pre', deep: true }, @@ -140,9 +141,10 @@ watch( watch( () => store.elements, - (val) => { + (val, oldVal) => { nextTick(() => { - elements.value = val + const hasDiff = diff(val, oldVal) + if (hasDiff.length > 0) elements.value = val }) }, { flush: 'pre', deep: true }, diff --git a/src/store/utils.ts b/src/store/utils.ts index dc050e2e..e7ffc95d 100644 --- a/src/store/utils.ts +++ b/src/store/utils.ts @@ -13,7 +13,7 @@ export const parseElements = (elements: Elements, nodes: Node[], edges: Edge[], } for (const element of elements) { if (isNode(element)) { - const storeNode = nodes.find((node) => node.id === element.id) + const storeNode = nodes[nodes.map((x) => x.id).indexOf(element.id)] if (storeNode) { const updatedNode: Node = { @@ -37,7 +37,7 @@ export const parseElements = (elements: Elements, nodes: Node[], edges: Edge[], nextElements.nextNodes.push(parseNode(element, nodeExtent)) } } else if (isEdge(element)) { - const storeEdge = edges.find((se) => se.id === element.id) + const storeEdge = edges[edges.map((x) => x.id).indexOf(element.id)] if (storeEdge) { nextElements.nextEdges.push({ diff --git a/src/utils/graph.ts b/src/utils/graph.ts index 3ea16347..df37f528 100644 --- a/src/utils/graph.ts +++ b/src/utils/graph.ts @@ -272,25 +272,13 @@ export const getConnectedEdges = (nodes: Node[], edges: Edge[]): Edge[] => { return edges.filter((edge) => nodeIds.includes(edge.source) || nodeIds.includes(edge.target)) } -const parseElements = (nodes: Node[], edges: Edge[]): Elements => [ - ...nodes.map((node) => { - const n = { ...node } - - if (n.__rf?.position) n.position = n.__rf?.position - - return n - }), - ...edges.map((e) => ({ ...e })), -] - -export const onLoadGetElements = (currentStore: FlowStore) => (): Elements => - parseElements(currentStore.nodes || [], currentStore.edges || []) +export const onLoadGetElements = (currentStore: FlowStore) => (): Elements => currentStore.elements export const onLoadToObject = (currentStore: FlowStore) => (): FlowExportObject => { // we have to stringify/parse so objects containing refs (like nodes and edges) can potentially be saved in a storage return JSON.parse( JSON.stringify({ - elements: parseElements(currentStore.nodes || [], currentStore.edges || []), + elements: currentStore.elements, position: [currentStore.transform[0], currentStore.transform[1]], zoom: currentStore.transform[2], }),