mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-26 16:44:22 +02:00
Don't mutate path
array, use instead a copy. (#894)
This commit is contained in:
committed by
Ian Storm Taylor
parent
13160a97b8
commit
b6fc16593e
@@ -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)
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user