From 1be6dfa8e3bfa19b05cfec318371c1ff417d5c11 Mon Sep 17 00:00:00 2001 From: braks <78412429+bcakmakoglu@users.noreply.github.com> Date: Thu, 16 Feb 2023 17:22:05 +0100 Subject: [PATCH] feat(core): add option to enable/disable replacing id when updating an edge --- packages/core/src/utils/graph.ts | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/packages/core/src/utils/graph.ts b/packages/core/src/utils/graph.ts index c7772a1c..4b2983b1 100644 --- a/packages/core/src/utils/graph.ts +++ b/packages/core/src/utils/graph.ts @@ -93,32 +93,34 @@ export const addEdge = (edgeParams: Edge | Connection, edges: Edge[]): Edge[] => return edges.concat(edge); }; -export const updateEdge = (oldEdge: Edge, newConnection: Connection, edges: Edge[]): Edge[] => { +export const updateEdge = (oldEdge: Edge, newConnection: Connection, edges: Edge[], shouldReplaceId = true): Edge[] => { + const { id: oldEdgeId, ...rest } = oldEdge; + if (!newConnection.source || !newConnection.target) { devWarn('006', errorMessages['006']()); return edges; } - const foundEdge = edges.find((e) => e.id === oldEdge.id) as Edge; + const foundEdge = edges.find((e) => e.id === oldEdgeId) as Edge; if (!foundEdge) { - devWarn('007', errorMessages['007'](oldEdge.id)); + devWarn('007', errorMessages['007'](oldEdgeId)); return edges; } // Remove old edge and create the new edge with parameters of old edge. const edge = { - ...oldEdge, - id: getEdgeId(newConnection), + ...rest, + id: shouldReplaceId ? getEdgeId(newConnection) : oldEdgeId, source: newConnection.source, target: newConnection.target, sourceHandle: newConnection.sourceHandle, targetHandle: newConnection.targetHandle, } as Edge; - return edges.filter((e) => e.id !== oldEdge.id).concat(edge); + return edges.filter((e) => e.id !== oldEdgeId).concat(edge); }; export const pointToRendererPoint = (