From 5ded81e33598dbf58b839b01b4856ac6c0e3dae6 Mon Sep 17 00:00:00 2001 From: Samy Pesse Date: Mon, 24 Oct 2016 23:16:26 +0200 Subject: [PATCH] Add transform normalizeParentsByKey --- src/transforms/by-key.js | 2 +- src/transforms/index.js | 9 ++++-- src/transforms/normalize.js | 56 +++++++++++++++++++++++++++++++++---- 3 files changed, 58 insertions(+), 9 deletions(-) diff --git a/src/transforms/by-key.js b/src/transforms/by-key.js index 6ab368822..56581533b 100644 --- a/src/transforms/by-key.js +++ b/src/transforms/by-key.js @@ -225,7 +225,7 @@ export function removeTextByKey(transform, key, offset, length, options = {}) { transform = transform.removeTextOperation(path, offset, length) if (normalize) { - transform = transform.normalizeNodeByKey(parent.key) + transform = transform.normalizeParentsByKey(parent.key) } return transform diff --git a/src/transforms/index.js b/src/transforms/index.js index 238de0aa8..bc9ac26bc 100644 --- a/src/transforms/index.js +++ b/src/transforms/index.js @@ -148,9 +148,11 @@ import { normalize, normalizeWith, normalizeNodeWith, + normalizeParentsWith, normalizeDocument, normalizeSelection, - normalizeNodeByKey + normalizeNodeByKey, + normalizeParentsByKey, } from './normalize' /** @@ -295,8 +297,9 @@ export default { normalize, normalizeWith, normalizeNodeWith, + normalizeParentsWith, normalizeDocument, normalizeSelection, - normalizeNodeByKey - + normalizeNodeByKey, + normalizeParentsByKey, } diff --git a/src/transforms/normalize.js b/src/transforms/normalize.js index 24a87e625..b1a2ad573 100644 --- a/src/transforms/normalize.js +++ b/src/transforms/normalize.js @@ -95,6 +95,36 @@ export function normalizeNodeWith(transform, schema, node) { return transform } +/** + * Normalize a node its parents using a schema. + * + * @param {Transform} transform + * @param {Schema} schema + * @param {Node} node + * @return {Transform} + */ + +export function normalizeParentsWith(transform, schema, node) { + transform = _normalizeNodeWith(transform, schema, node) + + // Normalize went back up to the document + if (node.kind == 'document') { + return transform + } + + // We search for the new parent + node = _refreshNode(transform, node) + if (!node) { + return transform + } + + const { state } = transform + const { document } = state + const parent = document.getParent(node.key) + + return normalizeParentsWith(transform, schema, parent) +} + /** * Normalize state using a schema. * @@ -135,7 +165,7 @@ export function normalizeDocument(transform) { } /** - * Normalize a specific node using core schema + * Normalize a node and its children using core schema * * @param {Transform} transform * @param {Node or String} key @@ -143,11 +173,27 @@ export function normalizeDocument(transform) { */ export function normalizeNodeByKey(transform, key) { - const { state } = transform - const { document } = state - const node = document.key == key ? document : document.assertDescendant(key) + const { state } = transform + const { document } = state + const node = document.key == key ? document : document.assertDescendant(key) - return transform.normalizeNodeWith(defaultSchema, node) + return transform.normalizeNodeWith(defaultSchema, node) +} + +/** + * Normalize a node and its parent using core schema + * + * @param {Transform} transform + * @param {Node or String} key + * @return {Transform} transform + */ + +export function normalizeParentsByKey(transform, key) { + const { state } = transform + const { document } = state + const node = document.key == key ? document : document.assertDescendant(key) + + return transform.normalizeParentsWith(defaultSchema, node) } /**