Skip to content

Commit 6304cf0

Browse files
authored
fix: add a dfs function to update node levels (#145)
1 parent 715398a commit 6304cf0

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

ui/src/components/Canvas.tsx

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,6 +1005,17 @@ export function Canvas() {
10051005
return;
10061006
}
10071007

1008+
// update the level of a node as well as its all descendants
1009+
function updateLevel(id: string, level: number) {
1010+
const node = nodesMap.get(id);
1011+
if (node) {
1012+
(node as any).level = level;
1013+
node.style!.backgroundColor = level2color[level];
1014+
nodesMap.set(id, node);
1015+
getPod(id)?.children.forEach(({ id }) => updateLevel(id, level + 1));
1016+
}
1017+
}
1018+
10081019
// check if this position is inside parent scope
10091020
nodes.forEach((node) => {
10101021
let absX = node.position.x;
@@ -1031,14 +1042,14 @@ export function Canvas() {
10311042

10321043
const currentNode = nodesMap.get(node.id);
10331044
if (currentNode) {
1034-
currentNode.style!.backgroundColor = level2color[scope.level + 1];
1035-
(currentNode as any).level = scope.level + 1;
10361045
currentNode.parentNode = scope.id;
10371046
currentNode.data!.parent = scope.id;
10381047
currentNode.position = { x: absX, y: absY };
10391048
nodesMap.set(node.id, currentNode);
10401049
}
10411050

1051+
updateLevel(node.id, scope.level + 1);
1052+
10421053
// update
10431054
setPodPosition({
10441055
id: node.id,
@@ -1049,7 +1060,14 @@ export function Canvas() {
10491060
});
10501061
},
10511062
// We need to monitor nodes, so that getScopeAt can have all the nodes.
1052-
[reactFlowInstance, getScopeAt, setPodPosition, nodesMap, setPodParent]
1063+
[
1064+
reactFlowInstance,
1065+
getScopeAt,
1066+
setPodPosition,
1067+
nodesMap,
1068+
setPodParent,
1069+
getPod,
1070+
]
10531071
);
10541072

10551073
const onNodesDelete = useCallback(

0 commit comments

Comments
 (0)