From 2561ce4a8965c2070954ca3257d92558d94f1cf6 Mon Sep 17 00:00:00 2001 From: Eric Edem Date: Wed, 24 Oct 2018 16:06:53 -0700 Subject: [PATCH] fix: use ancestors for dirt paths (#2316) * fix: use ancestors for dirt paths Not just parents. * fix: also revalidate ancestors when mutating existing nodes. --- packages/slate/src/controllers/change.js | 24 ++++++++++++++---------- packages/slate/src/utils/path-utils.js | 18 ++++++++++++++++++ 2 files changed, 32 insertions(+), 10 deletions(-) diff --git a/packages/slate/src/controllers/change.js b/packages/slate/src/controllers/change.js index e16559142..5594722e6 100644 --- a/packages/slate/src/controllers/change.js +++ b/packages/slate/src/controllers/change.js @@ -326,26 +326,27 @@ function getDirtyPaths(operation) { case 'remove_text': case 'set_mark': case 'set_node': { - return [path] + const ancestors = PathUtils.getAncestors(path).toArray() + return [...ancestors, path] } case 'insert_node': { const table = node.getKeysToPathsTable() const paths = Object.values(table).map(p => path.concat(p)) - const parentPath = PathUtils.lift(path) - return [parentPath, path, ...paths] + const ancestors = PathUtils.getAncestors(path).toArray() + return [...ancestors, path, ...paths] } case 'split_node': { - const parentPath = PathUtils.lift(path) + const ancestors = PathUtils.getAncestors(path).toArray() const nextPath = PathUtils.increment(path) - return [parentPath, path, nextPath] + return [...ancestors, path, nextPath] } case 'merge_node': { - const parentPath = PathUtils.lift(path) + const ancestors = PathUtils.getAncestors(path).toArray() const previousPath = PathUtils.decrement(path) - return [parentPath, previousPath] + return [...ancestors, previousPath] } case 'move_node': { @@ -364,12 +365,15 @@ function getDirtyPaths(operation) { } } - return [parentPath, newParentPath] + const oldAncestors = PathUtils.getAncestors(parentPath).toArray() + const newAncestors = PathUtils.getAncestors(newParentPath).toArray() + + return [...oldAncestors, parentPath, ...newAncestors, newParentPath] } case 'remove_node': { - const parentPath = PathUtils.lift(path) - return [parentPath] + const ancestors = PathUtils.getAncestors(path).toArray() + return [...ancestors] } default: { diff --git a/packages/slate/src/utils/path-utils.js b/packages/slate/src/utils/path-utils.js index d63526caf..2afe61073 100644 --- a/packages/slate/src/utils/path-utils.js +++ b/packages/slate/src/utils/path-utils.js @@ -76,6 +76,23 @@ function decrement(path, n = 1, index = path.size - 1) { return increment(path, 0 - n, index) } +/** + * Get all ancestor paths of th given path. + * + * @param {List} path + * @returns {List} + */ + +function getAncestors(path) { + let ancestors = new List() + + for (let i = 0; i < path.size; i++) { + ancestors = ancestors.push(path.slice(0, i)) + } + + return ancestors +} + /** * Increment a `path` by `n` at `index`, defaulting to the last index. * @@ -358,6 +375,7 @@ export default { create, crop, decrement, + getAncestors, increment, isAbove, isAfter,