Skip to content

Commit 440faf7

Browse files
authored
fix: build node2children map on node add (#299)
1 parent 1d0033e commit 440faf7

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

ui/src/lib/store/canvasSlice.tsx

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,9 @@ export const createCanvasSlice: StateCreator<MyState, [], [], CanvasSlice> = (
444444
if (parent !== "ROOT") {
445445
// we don't assign its parent when created, because we have to adjust its position to make it inside its parent.
446446
get().moveIntoScope(node.id, parent);
447+
} else {
448+
// moveIntoScope will build the node2children map, but we need to build it here for the ROOT nodes.
449+
get().buildNode2Children();
447450
}
448451
// Set initial width as about 30 characters.
449452
get().setNodeCharWidth(node.id, 30);
@@ -645,7 +648,6 @@ export const createCanvasSlice: StateCreator<MyState, [], [], CanvasSlice> = (
645648
});
646649
},
647650
moveIntoScope: (nodeId: string, scopeId: string) => {
648-
console.log(`Moving ${nodeId} into scope ${scopeId}`);
649651
// move a node into a scope.
650652
// 1. update the node's parentNode & position
651653
let nodesMap = get().ydoc.getMap<Node>("pods");
@@ -654,6 +656,14 @@ export const createCanvasSlice: StateCreator<MyState, [], [], CanvasSlice> = (
654656
console.warn("Node not found", node);
655657
return;
656658
}
659+
if (
660+
node.parentNode === scopeId ||
661+
(scopeId === "ROOT" && node.parentNode === undefined)
662+
) {
663+
console.warn("Node already in scope", node);
664+
return;
665+
}
666+
console.log(`Moving ${nodeId} into scope ${scopeId}`);
657667
let fromLevel = node?.data.level;
658668
let toLevel: number;
659669
let position: XYPosition;
@@ -928,7 +938,7 @@ export const createCanvasSlice: StateCreator<MyState, [], [], CanvasSlice> = (
928938
*/
929939
node2children: new Map<string, string[]>(),
930940
buildNode2Children: () => {
931-
// console.log("Building node2children..");
941+
console.debug("Building node2children..");
932942
// build a map from node to its children
933943
let nodesMap = get().ydoc.getMap<Node>("pods");
934944
let nodes: Node[] = Array.from(nodesMap.values());

0 commit comments

Comments
 (0)