From 9809414eb06078f7e014a6ee82ac5ab0b293653e Mon Sep 17 00:00:00 2001 From: Evan Simkowitz Date: Tue, 11 Jun 2024 15:10:20 -0700 Subject: [PATCH] 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. --- frontend/faraday/lib/layoutState.ts | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/frontend/faraday/lib/layoutState.ts b/frontend/faraday/lib/layoutState.ts index 91ef5f19..c76a7a1b 100644 --- a/frontend/faraday/lib/layoutState.ts +++ b/frontend/faraday/lib/layoutState.ts @@ -277,11 +277,6 @@ function moveNode(layoutTreeState: LayoutTreeState, 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(layoutTreeState: LayoutTreeState, 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;