Skip to content

Commit 02f4fe4

Browse files
committed
feat: hide empty parent nodes (array/object)
1 parent e6a185b commit 02f4fe4

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

src/features/editor/views/GraphView/lib/jsonParser.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,31 @@ export function parser(jsonStr: string): Graph {
7373
}
7474
}
7575

76-
states.graph.nodes = states.graph.nodes.map(node => ({
76+
// filter parent nodes that have no children
77+
// not the best way to do this, but it works
78+
const filteredNodes = states.graph.nodes.filter(node => {
79+
if (node.data.isParent && node.data.childrenCount === 0) {
80+
return false;
81+
}
82+
83+
return true;
84+
});
85+
86+
// add path to nodes
87+
states.graph.nodes = filteredNodes.map(node => ({
7788
...node,
7889
path: getNodePath(states.graph.nodes, states.graph.edges, node.id),
7990
}));
8091

92+
// filter edges that have no from or to node (since we filtered empty parent nodes)
93+
states.graph.edges = states.graph.edges.filter(edge => {
94+
const fromNode = states.graph.nodes.find(node => node.id === edge.from);
95+
const toNode = states.graph.nodes.find(node => node.id === edge.to);
96+
97+
if (!fromNode || !toNode) return false;
98+
return true;
99+
});
100+
81101
return states.graph;
82102
} catch (error) {
83103
console.error(error);

0 commit comments

Comments
 (0)