1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-07 15:56:35 +02:00

refactor core schema

This commit is contained in:
Ian Storm Taylor
2016-11-22 14:01:35 -08:00
parent c6318415cd
commit 5530b54c3b

View File

@@ -67,7 +67,7 @@ const MIN_TEXT_RULE = {
return object.kind == 'block' || object.kind == 'inline' return object.kind == 'block' || object.kind == 'inline'
}, },
validate: (node) => { validate: (node) => {
return node.nodes.size == 0 ? true : null return node.nodes.size == 0
}, },
normalize: (transform, node) => { normalize: (transform, node) => {
const text = Text.create() const text = Text.create()
@@ -216,14 +216,11 @@ const NO_ADJACENT_TEXT_RULE = {
return object.kind == 'block' || object.kind == 'inline' return object.kind == 'block' || object.kind == 'inline'
}, },
validate: (node) => { validate: (node) => {
const { nodes } = node const invalids = node.nodes
const invalids = nodes
.map((child, i) => { .map((child, i) => {
const next = nodes.get(i + 1) const next = node.nodes.get(i + 1)
if (child.kind !== 'text' || !next || next.kind !== 'text') { if (child.kind != 'text') return
return if (!next || next.kind != 'text') return
}
return [child, next] return [child, next]
}) })
.filter(Boolean) .filter(Boolean)
@@ -241,7 +238,7 @@ const NO_ADJACENT_TEXT_RULE = {
} }
/** /**
* Prevent extra empty text nodes. * Prevent extra empty text nodes, except when adjacent to inline void nodes.
* *
* @type {Object} * @type {Object}
*/ */
@@ -252,36 +249,25 @@ const NO_EMPTY_TEXT_RULE = {
}, },
validate: (node) => { validate: (node) => {
const { nodes } = node const { nodes } = node
if (nodes.size <= 1) return
if (nodes.size <= 1) {
return
}
const invalids = nodes.filter((desc, i) => { const invalids = nodes.filter((desc, i) => {
if (desc.kind != 'text' || desc.length > 0) { if (desc.kind != 'text') return
return if (desc.length > 0) return
}
// Empty text nodes are only allowed near inline void node.
const next = nodes.get(i + 1)
const prev = i > 0 ? nodes.get(i - 1) : null const prev = i > 0 ? nodes.get(i - 1) : null
const next = nodes.get(i + 1)
// If last one and previous is an inline void, we need to preserve it. // If it's the first node, and the next is a void, preserve it.
if (!next && isInlineVoid(prev)) { if (!prev && isInlineVoid(next)) return
return
}
// If first one and next one is an inline, we preserve it. // It it's the last node, and the previous is a void, preserve it.
if (!prev && isInlineVoid(next)) { if (!next && isInlineVoid(prev)) return
return
}
// If surrounded by inline void, we preserve it. // If it's surrounded by voids, preserve it.
if (next && prev && isInlineVoid(next) && isInlineVoid(prev)) { if (next && prev && isInlineVoid(next) && isInlineVoid(prev)) return
return
}
// Otherwise we remove it. // Otherwise, remove it.
return true return true
}) })