From b6fc16593e780931f08d54a0ea1bb760a753cc97 Mon Sep 17 00:00:00 2001 From: AlbertHilb Date: Wed, 21 Jun 2017 23:29:51 +0200 Subject: [PATCH] Don't mutate `path` array, use instead a copy. (#894) --- src/transforms/apply-operation.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/transforms/apply-operation.js b/src/transforms/apply-operation.js index 7971bae3a..b55d839b4 100644 --- a/src/transforms/apply-operation.js +++ b/src/transforms/apply-operation.js @@ -187,8 +187,8 @@ function moveNode(state, operation) { const { path, newPath, newIndex } = operation let { document } = state const node = document.assertPath(path) - const index = path.pop() - const parentDepth = path.length + const index = path[path.length - 1] + const parentPath = path.slice(0, -1) // Remove the node from its current parent let parent = document.getParent(node.key) @@ -196,13 +196,13 @@ function moveNode(state, operation) { document = parent.kind === 'document' ? parent : document.updateDescendant(parent) // Check if `parent` is an anchestor of `target` - const isAncestor = path.every((x, i) => x === newPath[i]) + const isAncestor = parentPath.every((x, i) => x === newPath[i]) let target - // If `parent` ia an ancestor of `target` and they have the same depth, - // then `parent` and `target` are the same node. - if (isAncestor && parentDepth === newPath.length) { + // If `parent` is an ancestor of `target` and their paths have same length, + // then `parent` and `target` are equal. + if (isAncestor && parentPath.length === newPath.length) { target = parent } @@ -210,8 +210,8 @@ function moveNode(state, operation) { // the index of the `target` ancestor with the same depth of `node`, // then removing `node` changes the path to `target`. // So we have to adjust `newPath` before picking `target`. - else if (isAncestor && index < newPath[parentDepth]) { - newPath[parentDepth]-- + else if (isAncestor && index < newPath[parentPath.length]) { + newPath[parentPath.length]-- target = document.assertPath(newPath) }