1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-28 09:29:49 +02:00

Fix inverse of sibling move operation (#3691)

* Fix path transform against sibling move operations

* Fix my jibberish

* Add some comments
This commit is contained in:
Jason Tamulonis
2020-05-28 14:54:05 -07:00
committed by GitHub
parent 9054d3a381
commit 4e7327d238
9 changed files with 88 additions and 3 deletions

View File

@@ -228,9 +228,18 @@ export const Operation = {
return op
}
// We need to get the original path here, but sometimes the `newPath`
// is a younger sibling of (or ends before) the original, and this
// accounts for it.
// If the move happens completely within a single parent the path and
// newPath are stable with respect to each other.
if (Path.isSibling(path, newPath)) {
return { ...op, path: newPath, newPath: path }
}
// If the move does not happen within a single parent it is possible
// for the move to impact the true path to the location where the node
// was removed from and where it was inserted. We have to adjust for this
// and find the original path. We can accomplish this (only in non-sibling)
// moves by looking at the impact of the move operation on the node
// after the original move path.
const inversePath = Path.transform(path, op)!
const inverseNewPath = Path.transform(Path.next(path), op)!
return { ...op, path: inversePath, newPath: inverseNewPath }

View File

@@ -0,0 +1,9 @@
import { Operation } from 'slate'
export const input = { type: 'move_node', path: [0, 2], newPath: [0, 1] }
export const test = value => {
return Operation.inverse(value)
}
export const output = { type: 'move_node', path: [0, 1], newPath: [0, 2] }

View File

@@ -0,0 +1,9 @@
import { Operation } from 'slate'
export const input = { type: 'move_node', path: [0, 2, 1], newPath: [0, 3] }
export const test = value => {
return Operation.inverse(value)
}
export const output = { type: 'move_node', path: [0, 3], newPath: [0, 2, 1] }

View File

@@ -0,0 +1,9 @@
import { Operation } from 'slate'
export const input = { type: 'move_node', path: [0, 2, 1], newPath: [0, 1] }
export const test = value => {
return Operation.inverse(value)
}
export const output = { type: 'move_node', path: [0, 1], newPath: [0, 3, 1] }

View File

@@ -0,0 +1,12 @@
import { Operation } from 'slate'
// This test covers moving a child to the location of where the current parent is (not becoming its parent).
// When the move happens the child is inserted infront of its old parent causing its former parent's index to shiftp
// back within its former grandparent (now parent).
export const input = { type: 'move_node', path: [0, 2, 1], newPath: [0, 2] }
export const test = value => {
return Operation.inverse(value)
}
export const output = { type: 'move_node', path: [0, 2], newPath: [0, 3, 1] }

View File

@@ -0,0 +1,9 @@
import { Operation } from 'slate'
export const input = { type: 'move_node', path: [0, 3], newPath: [0, 2, 1] }
export const test = value => {
return Operation.inverse(value)
}
export const output = { type: 'move_node', path: [0, 2, 1], newPath: [0, 3] }

View File

@@ -0,0 +1,10 @@
import { Operation } from 'slate'
export const input = { type: 'move_node', path: [0, 1], newPath: [0, 2, 1] }
export const test = value => {
return Operation.inverse(value)
}
// The path has changed here because the removal of [0, 1] caused [0, 2] to shift forward into its location.
export const output = { type: 'move_node', path: [0, 1, 1], newPath: [0, 1] }

View File

@@ -0,0 +1,9 @@
import { Operation } from 'slate'
export const input = { type: 'move_node', path: [0, 1], newPath: [0, 2] }
export const test = value => {
return Operation.inverse(value)
}
export const output = { type: 'move_node', path: [0, 2], newPath: [0, 1] }

View File

@@ -0,0 +1,9 @@
import { Operation } from 'slate'
export const input = { type: 'move_node', path: [0, 2], newPath: [1, 0, 0] }
export const test = value => {
return Operation.inverse(value)
}
export const output = { type: 'move_node', path: [1, 0, 0], newPath: [0, 2] }