Explorar o código

feat: don't create an empty node in a root array

victorbrambati %!s(int64=2) %!d(string=hai) anos
pai
achega
36fcb6f55b
Modificáronse 1 ficheiros con 23 adicións e 19 borrados
  1. 23 19
      src/utils/jsonParser.ts

+ 23 - 19
src/utils/jsonParser.ts

@@ -302,30 +302,34 @@ export const parser = (jsonStr: string, isFolded = false) => {
         }
       }
     };
+    
     if (json) {
       traverse(json);
-    }
-    if (notHaveParent.length > 1) {
-      const text = "";
-      const { width, height } = calculateSize(text, false, isFolded);
-      const emptyId = addNodes(text, width, height, false, true);
-      notHaveParent.forEach(children => {
-        addEdges(emptyId, children);
-      });
-    }
-    if (nodes.length === 0 && json) {
-      if (json.type === "array") {
-        const text = "[]";
-        const { width, height } = calculateSize(text, false, isFolded);
-        addNodes(text, width, height, false);
-      } else {
-        const text = "{}";
-        const { width, height } = calculateSize(text, false, isFolded);
-        addNodes(text, width, height, false);
+
+      if (notHaveParent.length > 1) {
+        if (json.type !== "array") {
+          const text = "";
+          const { width, height } = calculateSize(text, false, isFolded);
+          const emptyId = addNodes(text, width, height, false, true);
+          notHaveParent.forEach(children => {
+            addEdges(emptyId, children);
+          });
+        }
+      }
+      
+      if (nodes.length === 0) {
+        if (json.type === "array") {
+          const text = "[]";
+          const { width, height } = calculateSize(text, false, isFolded);
+          addNodes(text, width, height, false);
+        } else {
+          const text = "{}";
+          const { width, height } = calculateSize(text, false, isFolded);
+          addNodes(text, width, height, false);
+        }
       }
     }
 
-    console.log(nodes);
     return { nodes, edges };
   } catch (error) {
     console.error(error);