Fix order of operations when moving nodes (#40)

Insert the node at the new location before removing it from its old location. With the old order, removing a node from its parent could change the indexing of the parent node, if moving a node to a new location under the same parent.
This commit is contained in:
Evan Simkowitz 2024-06-11 15:10:20 -07:00 committed by GitHub
parent cff7a54507
commit 9809414eb0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -277,11 +277,6 @@ function moveNode<T>(layoutTreeState: LayoutTreeState<T>, action: LayoutTreeMove
console.log(node, parent, oldParent);
// Remove nodeToInsert from its old parent
if (oldParent) {
removeChild(oldParent, node);
}
if (!parent && action.insertAtRoot) {
if (!rootNode.children) {
addIntermediateNode(rootNode);
@ -292,6 +287,12 @@ function moveNode<T>(layoutTreeState: LayoutTreeState<T>, action: LayoutTreeMove
} else {
throw new Error("Invalid InsertOperation");
}
// Remove nodeToInsert from its old parent
if (oldParent) {
removeChild(oldParent, node);
}
const { node: newRootNode, leafs } = balanceNode(layoutTreeState.rootNode);
layoutTreeState.rootNode = newRootNode;
layoutTreeState.leafs = leafs;