1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-30 10:29:48 +02:00

Use node level normalization in transform "removeTextByKey"

This commit is contained in:
Samy Pesse
2016-10-20 00:17:39 +02:00
parent 8346bfc72f
commit a0a57f36f9
3 changed files with 24 additions and 41 deletions

View File

@@ -154,31 +154,9 @@ export function removeTextByKey(transform, key, offset, length) {
const { state } = transform
let { document } = state
const path = document.getPath(key)
transform.removeTextOperation(path, offset, length)
// If the text node is now empty, we might need to remove more nodes.
document = transform.state.document
const node = document.getDescendant(key)
const parent = document.getParent(key)
const previous = document.getPreviousSibling(key)
const next = document.getNextSibling(key)
// If the text node isn't empty, don't do anything more.
if (node.text != '') {
return transform
}
// If the empty text node is the only node remaining in a non-void inline,
// remove the inline completely.
if (
parent.kind == 'inline' &&
parent.isVoid == false &&
parent.nodes.size == 1
) {
transform.removeNodeByKey(parent.key)
}
return transform
return transform.removeTextOperation(path, offset, length)
.normalizeNodeByKey(parent)
}
/**
@@ -214,12 +192,7 @@ export function setNodeByKey(transform, key, properties) {
properties = Normalize.nodeProperties(properties)
const { state } = transform
const { document } = state
const node = document.assertDescendant(key)
const parent = document.getParent(key)
const index = parent.nodes.indexOf(node)
const path = document.getPath(key)
const previous = document.getPreviousSibling(key)
const next = document.getNextSibling(key)
transform.setNodeOperation(path, properties)
return transform

View File

@@ -147,7 +147,8 @@ import {
import {
normalize,
normalizeWith,
normalizeSelection
normalizeSelection,
normalizeNodeByKey
} from './normalize'
/**
@@ -291,6 +292,7 @@ export default {
normalize,
normalizeWith,
normalizeSelection
normalizeSelection,
normalizeNodeByKey
}

View File

@@ -6,20 +6,15 @@ import { default as defaultSchema } from '../plugins/schema'
*
* @param {Transform} transform
* @param {Node} node
* @param {Node} prevNode
* @return {Transform}
*/
export function normalizeWith(transform, schema, node, prevNode) {
export function normalizeWith(transform, schema, node) {
let { state } = transform
// If no node specific, normalize the whole document
node = node || state.document
if (node === prevNode) {
return transform
}
const failure = schema.__validate(node)
if (failure) {
@@ -37,7 +32,7 @@ export function normalizeWith(transform, schema, node, prevNode) {
return transform
}
return transform.normalizeWith(schema, node, prevNode)
return transform.normalizeWith(schema, node)
}
// No child, stop here
@@ -46,14 +41,12 @@ export function normalizeWith(transform, schema, node, prevNode) {
}
return node.nodes.reduce((t, child) => {
const prevChild = prevNode ? prevNode.getChild(child.key) : null
return t.normalizeWith(schema, child, prevChild)
return t.normalizeWith(schema, child)
}, transform)
}
/**
* Normalize the state using the core schema.
* TODO: calling this transform should be useless
*
* @param {Transform} transform
* @return {Transform}
@@ -65,6 +58,21 @@ export function normalize(transform) {
.normalizeSelection()
}
/**
* Normalize a specific node of the document using core schema
*
* @param {Transform} transform
* @return {Transform}
*/
export function normalizeNodeByKey(transform, key) {
const { state } = transform
const { document } = state
const node = document.assertDescendant(key)
return transform.normalizeWith(defaultSchema, node)
}
/**
* Normalize the selection.
*