Skip to content

Commit

Permalink
refactor invert.move_node
Browse files Browse the repository at this point in the history
  • Loading branch information
Joel Tadmor committed Jan 22, 2019
1 parent 27c93d9 commit 8e37ec3
Showing 1 changed file with 5 additions and 24 deletions.
29 changes: 5 additions & 24 deletions packages/slate/src/operations/invert.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,36 +45,17 @@ function invertOperation(op) {
let inversePath = newPath
let inverseNewPath = path

const pathLast = path.size - 1
const newPathLast = newPath.size - 1
const position = PathUtils.compare(path, newPath)

// 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.size < inversePath.size &&
path.slice(0, pathLast).every((e, i) => e == inversePath.get(i)) &&
path.last() < inversePath.get(pathLast)
) {
inversePath = inversePath
.slice(0, pathLast)
.concat(inversePath.get(pathLast) - 1)
.concat(inversePath.slice(pathLast + 1, inversePath.size))
}

// 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.size < inverseNewPath.size &&
newPath
.slice(0, newPathLast)
.every((e, i) => e == inverseNewPath.get(i)) &&
newPath.last() <= inverseNewPath.get(newPathLast)
) {
inverseNewPath = inverseNewPath
.slice(0, newPathLast)
.concat(inverseNewPath.get(newPathLast) + 1)
.concat(inverseNewPath.slice(newPathLast + 1, inverseNewPath.size))
if (path.size < newPath.size && position === -1) {
inversePath = PathUtils.decrement(newPath, 1, path.size - 1)
} else if (path.size > newPath.size && position !== -1) {
inverseNewPath = PathUtils.increment(path, 1, newPath.size - 1)
}

const inverse = op.set('path', inversePath).set('newPath', inverseNewPath)
Expand Down

0 comments on commit 8e37ec3

Please sign in to comment.