1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-27 17:09:53 +02:00

update schema

This commit is contained in:
Ian Storm Taylor
2016-09-23 11:22:06 -07:00
parent 2ed1efbdd4
commit bd26153dfb
3 changed files with 48 additions and 11 deletions

View File

@@ -94,7 +94,7 @@ export function deleteAtRange(transform, range) {
const lonely = document.getFurthest(endBlock, p => p.nodes.size == 1) || endBlock const lonely = document.getFurthest(endBlock, p => p.nodes.size == 1) || endBlock
transform.removeNodeByKey(lonely.key) transform.removeNodeByKey(lonely.key)
transform.normalizeDocument() transform.normalize()
return transform return transform
} }
@@ -269,7 +269,7 @@ export function insertBlockAtRange(transform, range, block) {
transform.insertNodeByKey(parent.key, index + 1, block) transform.insertNodeByKey(parent.key, index + 1, block)
} }
transform.normalizeDocument() transform.normalize()
return transform return transform
} }
@@ -352,7 +352,7 @@ export function insertFragmentAtRange(transform, range, fragment) {
}) })
} }
transform.normalizeDocument() transform.normalize()
return transform return transform
} }
@@ -386,7 +386,7 @@ export function insertInlineAtRange(transform, range, inline) {
transform.splitNodeByKey(startKey, startOffset) transform.splitNodeByKey(startKey, startOffset)
transform.insertNodeByKey(parent.key, index + 1, inline) transform.insertNodeByKey(parent.key, index + 1, inline)
transform.normalizeDocument() transform.normalize()
return transform return transform
} }
@@ -523,7 +523,7 @@ export function splitBlockAtRange(transform, range, height = 1) {
} }
transform.splitNodeByKey(node.key, offset) transform.splitNodeByKey(node.key, offset)
transform.normalizeDocument() transform.normalize()
return transform return transform
} }
@@ -672,7 +672,7 @@ export function unwrapBlockAtRange(transform, range, properties) {
} }
}) })
transform.normalizeDocument() transform.normalize()
return transform return transform
} }
@@ -714,7 +714,7 @@ export function unwrapInlineAtRange(transform, range, properties) {
}) })
}) })
transform.normalizeDocument() transform.normalize()
return transform return transform
} }
@@ -879,7 +879,7 @@ export function wrapInlineAtRange(transform, range, inline) {
}) })
} }
transform.normalizeDocument() transform.normalize()
return transform return transform
} }

View File

@@ -142,6 +142,7 @@ import {
*/ */
import { import {
normalize,
normalizeDocument, normalizeDocument,
normalizeSelection, normalizeSelection,
} from './normalize' } from './normalize'
@@ -282,6 +283,7 @@ export default {
* Normalize. * Normalize.
*/ */
normalize,
normalizeDocument, normalizeDocument,
normalizeSelection, normalizeSelection,

View File

@@ -1,4 +1,6 @@
import Schema from '../models/schema'
/** /**
* Only allow block nodes in documents. * Only allow block nodes in documents.
* *
@@ -60,18 +62,51 @@ const INLINE_CHILDREN_RULE = {
} }
/** /**
* The default schema. * Join adjacent text nodes.
* *
* @type {Object} * @type {Object}
*/ */
const SCHEMA = { const NO_ADJACENT_TEXT_RULE = {
match: (object) => {
return object.kind == 'block' || object.kind == 'inline'
},
validate: (node) => {
const { nodes } = node
const invalids = nodes
.filter((n, i) => {
const next = nodes.get(i + 1)
return n.kind == 'text' && next && next.kind == 'text'
})
.map((n, i) => {
const next = nodes.get(i + 1)
return [n, next]
})
return invalids.size ? invalids : null
},
normalize: (transform, node, pairs) => {
return pairs.reduce((t, pair) => {
const [ first, second ] = pair
return t.joinNodeByKey(first.key, second.key)
})
}
}
/**
* The internal normalizing schema.
*
* @type {Schema}
*/
const SCHEMA = Schema.create({
rules: [ rules: [
DOCUMENT_CHILDREN_RULE, DOCUMENT_CHILDREN_RULE,
BLOCK_CHILDREN_RULE, BLOCK_CHILDREN_RULE,
INLINE_CHILDREN_RULE, INLINE_CHILDREN_RULE,
NO_ADJACENT_TEXT_RULE,
] ]
} })
/** /**
* Normalize the state. * Normalize the state.