mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-09-01 03:11:44 +02:00
Use node level normalization in transform "removeTextByKey"
This commit is contained in:
@@ -154,31 +154,9 @@ export function removeTextByKey(transform, key, offset, length) {
|
|||||||
const { state } = transform
|
const { state } = transform
|
||||||
let { document } = state
|
let { document } = state
|
||||||
const path = document.getPath(key)
|
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 parent = document.getParent(key)
|
||||||
const previous = document.getPreviousSibling(key)
|
return transform.removeTextOperation(path, offset, length)
|
||||||
const next = document.getNextSibling(key)
|
.normalizeNodeByKey(parent)
|
||||||
|
|
||||||
// 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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -214,12 +192,7 @@ export function setNodeByKey(transform, key, properties) {
|
|||||||
properties = Normalize.nodeProperties(properties)
|
properties = Normalize.nodeProperties(properties)
|
||||||
const { state } = transform
|
const { state } = transform
|
||||||
const { document } = state
|
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 path = document.getPath(key)
|
||||||
const previous = document.getPreviousSibling(key)
|
|
||||||
const next = document.getNextSibling(key)
|
|
||||||
transform.setNodeOperation(path, properties)
|
transform.setNodeOperation(path, properties)
|
||||||
|
|
||||||
return transform
|
return transform
|
||||||
|
@@ -147,7 +147,8 @@ import {
|
|||||||
import {
|
import {
|
||||||
normalize,
|
normalize,
|
||||||
normalizeWith,
|
normalizeWith,
|
||||||
normalizeSelection
|
normalizeSelection,
|
||||||
|
normalizeNodeByKey
|
||||||
} from './normalize'
|
} from './normalize'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -291,6 +292,7 @@ export default {
|
|||||||
|
|
||||||
normalize,
|
normalize,
|
||||||
normalizeWith,
|
normalizeWith,
|
||||||
normalizeSelection
|
normalizeSelection,
|
||||||
|
normalizeNodeByKey
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -6,20 +6,15 @@ import { default as defaultSchema } from '../plugins/schema'
|
|||||||
*
|
*
|
||||||
* @param {Transform} transform
|
* @param {Transform} transform
|
||||||
* @param {Node} node
|
* @param {Node} node
|
||||||
* @param {Node} prevNode
|
|
||||||
* @return {Transform}
|
* @return {Transform}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export function normalizeWith(transform, schema, node, prevNode) {
|
export function normalizeWith(transform, schema, node) {
|
||||||
let { state } = transform
|
let { state } = transform
|
||||||
|
|
||||||
// If no node specific, normalize the whole document
|
// If no node specific, normalize the whole document
|
||||||
node = node || state.document
|
node = node || state.document
|
||||||
|
|
||||||
if (node === prevNode) {
|
|
||||||
return transform
|
|
||||||
}
|
|
||||||
|
|
||||||
const failure = schema.__validate(node)
|
const failure = schema.__validate(node)
|
||||||
|
|
||||||
if (failure) {
|
if (failure) {
|
||||||
@@ -37,7 +32,7 @@ export function normalizeWith(transform, schema, node, prevNode) {
|
|||||||
return transform
|
return transform
|
||||||
}
|
}
|
||||||
|
|
||||||
return transform.normalizeWith(schema, node, prevNode)
|
return transform.normalizeWith(schema, node)
|
||||||
}
|
}
|
||||||
|
|
||||||
// No child, stop here
|
// No child, stop here
|
||||||
@@ -46,14 +41,12 @@ export function normalizeWith(transform, schema, node, prevNode) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return node.nodes.reduce((t, child) => {
|
return node.nodes.reduce((t, child) => {
|
||||||
const prevChild = prevNode ? prevNode.getChild(child.key) : null
|
return t.normalizeWith(schema, child)
|
||||||
return t.normalizeWith(schema, child, prevChild)
|
|
||||||
}, transform)
|
}, transform)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Normalize the state using the core schema.
|
* Normalize the state using the core schema.
|
||||||
* TODO: calling this transform should be useless
|
|
||||||
*
|
*
|
||||||
* @param {Transform} transform
|
* @param {Transform} transform
|
||||||
* @return {Transform}
|
* @return {Transform}
|
||||||
@@ -65,6 +58,21 @@ export function normalize(transform) {
|
|||||||
.normalizeSelection()
|
.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.
|
* Normalize the selection.
|
||||||
*
|
*
|
||||||
|
Reference in New Issue
Block a user