mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-21 14:41:23 +02:00
refactor core schema
This commit is contained in:
@@ -43,8 +43,10 @@ const BLOCK_CHILDREN_RULE = {
|
|||||||
return node.kind == 'block'
|
return node.kind == 'block'
|
||||||
},
|
},
|
||||||
validate: (block) => {
|
validate: (block) => {
|
||||||
const { nodes } = block
|
const invalids = block.nodes.filter((n) => {
|
||||||
const invalids = nodes.filter(n => n.kind != 'block' && n.kind != 'inline' && n.kind != 'text')
|
return n.kind != 'block' && n.kind != 'inline' && n.kind != 'text'
|
||||||
|
})
|
||||||
|
|
||||||
return invalids.size ? invalids : null
|
return invalids.size ? invalids : null
|
||||||
},
|
},
|
||||||
normalize: (transform, block, invalids) => {
|
normalize: (transform, block, invalids) => {
|
||||||
@@ -65,8 +67,7 @@ const MIN_TEXT_RULE = {
|
|||||||
return object.kind == 'block' || object.kind == 'inline'
|
return object.kind == 'block' || object.kind == 'inline'
|
||||||
},
|
},
|
||||||
validate: (node) => {
|
validate: (node) => {
|
||||||
const { nodes } = node
|
return node.nodes.size == 0 ? true : null
|
||||||
return nodes.size == 0 ? true : null
|
|
||||||
},
|
},
|
||||||
normalize: (transform, node) => {
|
normalize: (transform, node) => {
|
||||||
const text = Text.create()
|
const text = Text.create()
|
||||||
@@ -85,8 +86,7 @@ const INLINE_CHILDREN_RULE = {
|
|||||||
return object.kind == 'inline'
|
return object.kind == 'inline'
|
||||||
},
|
},
|
||||||
validate: (inline) => {
|
validate: (inline) => {
|
||||||
const { nodes } = inline
|
const invalids = inline.nodes.filter(n => n.kind != 'inline' && n.kind != 'text')
|
||||||
const invalids = nodes.filter(n => n.kind != 'inline' && n.kind != 'text')
|
|
||||||
return invalids.size ? invalids : null
|
return invalids.size ? invalids : null
|
||||||
},
|
},
|
||||||
normalize: (transform, inline, invalids) => {
|
normalize: (transform, inline, invalids) => {
|
||||||
@@ -158,7 +158,8 @@ const VOID_TEXT_RULE = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Ensure that inline void nodes are surrounded with text nodes.
|
* Ensure that inline void nodes are surrounded by text nodes, by adding extra
|
||||||
|
* blank text nodes if necessary.
|
||||||
*
|
*
|
||||||
* @type {Object}
|
* @type {Object}
|
||||||
*/
|
*/
|
||||||
@@ -168,42 +169,39 @@ const INLINE_VOID_TEXTS_AROUND_RULE = {
|
|||||||
return object.kind == 'block' || object.kind == 'inline'
|
return object.kind == 'block' || object.kind == 'inline'
|
||||||
},
|
},
|
||||||
validate: (block) => {
|
validate: (block) => {
|
||||||
const invalids = block.nodes.reduce((accu, child, index) => {
|
const invalids = block.nodes.reduce((list, child, index) => {
|
||||||
if (child.kind === 'block' || !child.isVoid) {
|
if (child.kind == 'block') return list
|
||||||
return accu
|
if (!child.isVoid) return list
|
||||||
|
|
||||||
|
const prev = index > 0 ? block.nodes.get(index - 1) : null
|
||||||
|
const next = block.nodes.get(index + 1)
|
||||||
|
const insertBefore = !prev
|
||||||
|
const insertAfter = !next || isInlineVoid(next)
|
||||||
|
|
||||||
|
if (insertAfter || insertBefore) {
|
||||||
|
list = list.push({ insertAfter, insertBefore, index })
|
||||||
}
|
}
|
||||||
|
|
||||||
const prevNode = index > 0 ? block.nodes.get(index - 1) : null
|
return list
|
||||||
const nextNode = block.nodes.get(index + 1)
|
|
||||||
|
|
||||||
const prev = !prevNode
|
|
||||||
const next = (!nextNode || isInlineVoid(nextNode))
|
|
||||||
|
|
||||||
if (next || prev) {
|
|
||||||
return accu.push({ next, prev, index })
|
|
||||||
} else {
|
|
||||||
return accu
|
|
||||||
}
|
|
||||||
}, new List())
|
}, new List())
|
||||||
|
|
||||||
return !invalids.isEmpty() ? invalids : null
|
return invalids.size ? invalids : null
|
||||||
},
|
},
|
||||||
normalize: (transform, block, invalids) => {
|
normalize: (transform, block, invalids) => {
|
||||||
// Shift for every text node inserted previously
|
// Shift for every text node inserted previously.
|
||||||
let shift = 0
|
let shift = 0
|
||||||
|
|
||||||
return invalids.reduce((t, { index, next, prev }) => {
|
invalids.forEach(({ index, insertAfter, insertBefore }) => {
|
||||||
if (prev) {
|
if (insertBefore) {
|
||||||
t = t.insertNodeByKey(block.key, shift + index, Text.create(), OPTS)
|
transform.insertNodeByKey(block.key, shift + index, Text.create(), OPTS)
|
||||||
shift = shift + 1
|
shift++
|
||||||
}
|
|
||||||
if (next) {
|
|
||||||
t = t.insertNodeByKey(block.key, shift + index + 1, Text.create(), OPTS)
|
|
||||||
shift = shift + 1
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return t
|
if (insertAfter) {
|
||||||
}, transform)
|
transform.insertNodeByKey(block.key, shift + index + 1, Text.create(), OPTS)
|
||||||
|
shift++
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user