From 5e53d6cfee5ce1d471ce2a9811e45ca5be039faf Mon Sep 17 00:00:00 2001 From: Ashish Jain Date: Fri, 3 May 2024 12:50:02 +0200 Subject: [PATCH] #5237 fix for node duplication --- cypress/platform/knsv2.html | 9 +++++++-- .../mermaid/src/diagrams/state/stateDb.js | 19 +++++++++++++++++-- .../state/stateRenderer-v3-unified.ts | 2 +- 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/cypress/platform/knsv2.html b/cypress/platform/knsv2.html index 9b7f25f6fd..d62e888671 100644 --- a/cypress/platform/knsv2.html +++ b/cypress/platform/knsv2.html @@ -74,8 +74,13 @@
 stateDiagram-v2
-    Chimp --> Gorilla
-    Chimp --> Bonobo
+    [*] --> First
+    state First {
+        [*] --> second
+        second --> [*]
+    }
+
+
 
   
diff --git a/packages/mermaid/src/diagrams/state/stateDb.js b/packages/mermaid/src/diagrams/state/stateDb.js index ff366b15b5..c96c204c66 100644 --- a/packages/mermaid/src/diagrams/state/stateDb.js +++ b/packages/mermaid/src/diagrams/state/stateDb.js @@ -716,7 +716,12 @@ const dataFetcher = (parent, parsedItem, diagramStates, nodes, edges, altFlag, u //add parent id to noteData noteData.parentId = parentId; - nodes.push(groupData, noteData, nodeData); + //insert groupData + insertOrUpdateNode(nodes, groupData); + //insert noteData + insertOrUpdateNode(nodes, noteData); + //insert nodeData + insertOrUpdateNode(nodes, nodeData); let from = itemId; let to = noteData.id; @@ -742,7 +747,7 @@ const dataFetcher = (parent, parsedItem, diagramStates, nodes, edges, altFlag, u useRough, }); } else { - nodes.push(nodeData); + insertOrUpdateNode(nodes, nodeData); } console.log('Nodes:', nodes); @@ -753,6 +758,16 @@ const dataFetcher = (parent, parsedItem, diagramStates, nodes, edges, altFlag, u } }; +function insertOrUpdateNode(nodes, nodeData) { + const existingNodeData = nodes.find((node) => node.id === nodeData.id); + if (existingNodeData) { + //update the existing nodeData + Object.assign(existingNodeData, nodeData); + } else { + nodes.push(nodeData); + } +} + /** * Create a standard string for the dom ID of an item. * If a type is given, insert that before the counter, preceded by the type spacer diff --git a/packages/mermaid/src/diagrams/state/stateRenderer-v3-unified.ts b/packages/mermaid/src/diagrams/state/stateRenderer-v3-unified.ts index 2632ffd27b..19bebb2cb7 100644 --- a/packages/mermaid/src/diagrams/state/stateRenderer-v3-unified.ts +++ b/packages/mermaid/src/diagrams/state/stateRenderer-v3-unified.ts @@ -88,7 +88,7 @@ export const draw = async function (text: string, id: string, _version: string, data4Layout.type = diag.type; data4Layout.layoutAlgorithm = 'dagre-wrapper'; - // data4Layout.layoutAlgorithm = 'elk'; + //data4Layout.layoutAlgorithm = 'elk'; data4Layout.direction = DIR; data4Layout.nodeSpacing = conf.nodeSpacing || 50; data4Layout.rankSpacing = conf.rankSpacing || 50;