1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-06 23:36:31 +02:00

Add transform normalizeParentsByKey

This commit is contained in:
Samy Pesse
2016-10-24 23:16:26 +02:00
parent 07de7bf596
commit 5ded81e335
3 changed files with 58 additions and 9 deletions

View File

@@ -225,7 +225,7 @@ export function removeTextByKey(transform, key, offset, length, options = {}) {
transform = transform.removeTextOperation(path, offset, length) transform = transform.removeTextOperation(path, offset, length)
if (normalize) { if (normalize) {
transform = transform.normalizeNodeByKey(parent.key) transform = transform.normalizeParentsByKey(parent.key)
} }
return transform return transform

View File

@@ -148,9 +148,11 @@ import {
normalize, normalize,
normalizeWith, normalizeWith,
normalizeNodeWith, normalizeNodeWith,
normalizeParentsWith,
normalizeDocument, normalizeDocument,
normalizeSelection, normalizeSelection,
normalizeNodeByKey normalizeNodeByKey,
normalizeParentsByKey,
} from './normalize' } from './normalize'
/** /**
@@ -295,8 +297,9 @@ export default {
normalize, normalize,
normalizeWith, normalizeWith,
normalizeNodeWith, normalizeNodeWith,
normalizeParentsWith,
normalizeDocument, normalizeDocument,
normalizeSelection, normalizeSelection,
normalizeNodeByKey normalizeNodeByKey,
normalizeParentsByKey,
} }

View File

@@ -95,6 +95,36 @@ export function normalizeNodeWith(transform, schema, node) {
return transform 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. * 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 {Transform} transform
* @param {Node or String} key * @param {Node or String} key
@@ -143,11 +173,27 @@ export function normalizeDocument(transform) {
*/ */
export function normalizeNodeByKey(transform, key) { export function normalizeNodeByKey(transform, key) {
const { state } = transform const { state } = transform
const { document } = state const { document } = state
const node = document.key == key ? document : document.assertDescendant(key) 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)
} }
/** /**