mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-27 00:54: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
|
const { path, newPath, newIndex } = operation
|
||||||
let { document } = state
|
let { document } = state
|
||||||
const node = document.assertPath(path)
|
const node = document.assertPath(path)
|
||||||
const index = path.pop()
|
const index = path[path.length - 1]
|
||||||
const parentDepth = path.length
|
const parentPath = path.slice(0, -1)
|
||||||
|
|
||||||
// Remove the node from its current parent
|
// Remove the node from its current parent
|
||||||
let parent = document.getParent(node.key)
|
let parent = document.getParent(node.key)
|
||||||
@@ -196,13 +196,13 @@ function moveNode(state, operation) {
|
|||||||
document = parent.kind === 'document' ? parent : document.updateDescendant(parent)
|
document = parent.kind === 'document' ? parent : document.updateDescendant(parent)
|
||||||
|
|
||||||
// Check if `parent` is an anchestor of `target`
|
// 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
|
let target
|
||||||
|
|
||||||
// If `parent` ia an ancestor of `target` and they have the same depth,
|
// If `parent` is an ancestor of `target` and their paths have same length,
|
||||||
// then `parent` and `target` are the same node.
|
// then `parent` and `target` are equal.
|
||||||
if (isAncestor && parentDepth === newPath.length) {
|
if (isAncestor && parentPath.length === newPath.length) {
|
||||||
target = parent
|
target = parent
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -210,8 +210,8 @@ function moveNode(state, operation) {
|
|||||||
// the index of the `target` ancestor with the same depth of `node`,
|
// the index of the `target` ancestor with the same depth of `node`,
|
||||||
// then removing `node` changes the path to `target`.
|
// then removing `node` changes the path to `target`.
|
||||||
// So we have to adjust `newPath` before picking `target`.
|
// So we have to adjust `newPath` before picking `target`.
|
||||||
else if (isAncestor && index < newPath[parentDepth]) {
|
else if (isAncestor && index < newPath[parentPath.length]) {
|
||||||
newPath[parentDepth]--
|
newPath[parentPath.length]--
|
||||||
target = document.assertPath(newPath)
|
target = document.assertPath(newPath)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user