1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-02-15 12:44:38 +01:00

Properly invert merge_node operations (#1477)

This commit is contained in:
Justin Weiss 2017-12-29 10:57:21 -08:00 committed by Ian Storm Taylor
parent 29bcb1c8d6
commit 23ab02626f
3 changed files with 111 additions and 1 deletions

View File

@ -48,7 +48,34 @@ function invertOperation(op) {
if (type == 'move_node') {
const { newPath, path } = op
const inverse = op.set('path', newPath).set('newPath', path)
let inversePath = newPath
let inverseNewPath = path
const pathLast = path.length - 1
const newPathLast = newPath.length - 1
// If the node's old position was a left sibling of an ancestor of
// its new position, we need to adjust part of the path by -1.
if (path.length < inversePath.length &&
path.slice(0, pathLast).every((e, i) => e == inversePath[i]) &&
path[pathLast] < inversePath[pathLast]) {
inversePath = inversePath.slice(0, pathLast)
.concat([inversePath[pathLast] - 1])
.concat(inversePath.slice(pathLast + 1, inversePath.length))
}
// If the node's new position is an ancestor of the old position,
// or a left sibling of an ancestor of its old position, we need
// to adjust part of the path by 1.
if (newPath.length < inverseNewPath.length &&
newPath.slice(0, newPathLast).every((e, i) => e == inverseNewPath[i]) &&
newPath[newPathLast] <= inverseNewPath[newPathLast]) {
inverseNewPath = inverseNewPath.slice(0, newPathLast)
.concat([inverseNewPath[newPathLast] + 1])
.concat(inverseNewPath.slice(newPathLast + 1, inverseNewPath.length))
}
const inverse = op.set('path', inversePath).set('newPath', inverseNewPath)
return inverse
}

View File

@ -0,0 +1,36 @@
/** @jsx h */
import h from '../../helpers/h'
export default function (value) {
return value
.change()
.moveNodeByKey('c', 'd', 1)
.value
.change()
.undo()
.value
}
export const input = (
<value>
<document key="a">
<paragraph key="b">
one
</paragraph>
<paragraph key="c">
two
</paragraph>
<paragraph key="d">
<paragraph key="e">
three
</paragraph>
</paragraph>
<paragraph key="f">
four
</paragraph>
</document>
</value>
)
export const output = input

View File

@ -0,0 +1,47 @@
/** @jsx h */
import h from '../../helpers/h'
export default function (value) {
return value
.change()
.moveNodeByKey('h', 'a', 0)
.value
.change()
.undo()
.value
}
export const input = (
<value>
<document key="a">
<paragraph key="b">
one
</paragraph>
<paragraph key="c">
<paragraph key="d">
two
</paragraph>
<paragraph key="e">
<paragraph key="f">
three
</paragraph>
<paragraph key="g">
four
</paragraph>
<paragraph key="h">
five
</paragraph>
</paragraph>
</paragraph>
<paragraph key="i">
six
</paragraph>
<paragraph key="j">
seven
</paragraph>
</document>
</value>
)
export const output = input