mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-29 18:09:49 +02:00
Fix normalization to prevent extra empty text nodes
This commit is contained in:
@@ -5,6 +5,9 @@ import Text from '../models/text'
|
|||||||
This module contains the default schema to normalize documents
|
This module contains the default schema to normalize documents
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
function isInlineVoid(node) {
|
||||||
|
return (node.kind == 'inline' && node.isVoid)
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A default schema rule to only allow block nodes in documents.
|
* A default schema rule to only allow block nodes in documents.
|
||||||
@@ -144,8 +147,8 @@ const INLINE_VOID_TEXTS_AROUND_RULE = {
|
|||||||
const prevNode = index > 0 ? block.nodes.get(index - 1) : null
|
const prevNode = index > 0 ? block.nodes.get(index - 1) : null
|
||||||
const nextNode = block.nodes.get(index + 1)
|
const nextNode = block.nodes.get(index + 1)
|
||||||
|
|
||||||
const prev = (!prevNode || prevNode.kind !== 'text')
|
const prev = (!prevNode || isInlineVoid(prevNode))
|
||||||
const next = (!nextNode || nextNode.kind !== 'text')
|
const next = (!nextNode || isInlineVoid(nextNode.kind))
|
||||||
|
|
||||||
if (next || prev) {
|
if (next || prev) {
|
||||||
accu.push({ next, prev, index })
|
accu.push({ next, prev, index })
|
||||||
@@ -229,10 +232,23 @@ const NO_EMPTY_TEXT_RULE = {
|
|||||||
const next = nodes.get(i + 1)
|
const next = nodes.get(i + 1)
|
||||||
const prev = i > 0 ? nodes.get(i - 1) : null
|
const prev = i > 0 ? nodes.get(i - 1) : null
|
||||||
|
|
||||||
return (
|
// If last one and previous is an inline void, we need to preserve it
|
||||||
(!next || !(next.kind === 'inline' && next.isVoid))
|
if (!next && isInlineVoid(prev)) {
|
||||||
&& (!prev || !(prev.kind === 'inline' && prev.isVoid))
|
return
|
||||||
)
|
}
|
||||||
|
|
||||||
|
// If first one and next one is an inline, we preserve it
|
||||||
|
if (!prev && isInlineVoid(next)) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// If surrounded by inline void, we preserve it
|
||||||
|
if (next && prev && isInlineVoid(next) && isInlineVoid(prev)) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Otherwise we remove it
|
||||||
|
return true
|
||||||
})
|
})
|
||||||
|
|
||||||
return invalids.size ? invalids : null
|
return invalids.size ? invalids : null
|
||||||
|
Reference in New Issue
Block a user