mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-02-25 01:33:37 +01:00
Refactor to move all normalization rules in "plugins/schema.js"
This commit is contained in:
parent
7091c9489e
commit
6f0a31c90f
@ -7,6 +7,7 @@ import React from 'react'
|
||||
import String from '../utils/string'
|
||||
import getWindow from 'get-window'
|
||||
import { IS_MAC } from '../constants/environment'
|
||||
import { rules } from './schema'
|
||||
|
||||
/**
|
||||
* Debug.
|
||||
@ -680,6 +681,10 @@ function Plugin(options = {}) {
|
||||
.apply()
|
||||
}
|
||||
|
||||
/**
|
||||
* Extend core schema with rendering
|
||||
*/
|
||||
|
||||
/**
|
||||
* A default schema rule to render block nodes.
|
||||
*
|
||||
@ -729,79 +734,11 @@ function Plugin(options = {}) {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A default schema rule to only allow block nodes in documents.
|
||||
*
|
||||
* @type {Object}
|
||||
*/
|
||||
|
||||
const DOCUMENT_CHILDREN_RULE = {
|
||||
match: (node) => {
|
||||
return node.kind == 'document'
|
||||
},
|
||||
validate: (document) => {
|
||||
const { nodes } = document
|
||||
const invalids = nodes.filter(n => n.kind != 'block')
|
||||
return invalids.size ? invalids : null
|
||||
},
|
||||
normalize: (transform, document, invalids) => {
|
||||
return invalids.reduce((t, n) => t.removeNodeByKey(n.key), transform)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A default schema rule to only allow block, inline and text nodes in blocks.
|
||||
*
|
||||
* @type {Object}
|
||||
*/
|
||||
|
||||
const BLOCK_CHILDREN_RULE = {
|
||||
match: (node) => {
|
||||
return node.kind == 'block'
|
||||
},
|
||||
validate: (block) => {
|
||||
const { nodes } = block
|
||||
const invalids = nodes.filter(n => n.kind != 'block' && n.kind != 'inline' && n.kind != 'text')
|
||||
return invalids.size ? invalids : null
|
||||
},
|
||||
normalize: (transform, block, invalids) => {
|
||||
return invalids.reduce((t, n) => t.removeNodeByKey(n.key), transform)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A default schema rule to only allow inline and text nodes in inlines.
|
||||
*
|
||||
* @type {Object}
|
||||
*/
|
||||
|
||||
const INLINE_CHILDREN_RULE = {
|
||||
match: (object) => {
|
||||
return object.kind == 'inline'
|
||||
},
|
||||
validate: (inline) => {
|
||||
const { nodes } = inline
|
||||
const invalids = nodes.filter(n => n.kind != 'inline' && n.kind != 'text')
|
||||
return invalids.size ? invalids : null
|
||||
},
|
||||
normalize: (transform, inline, invalids) => {
|
||||
return invalids.reduce((t, n) => t.removeNodeByKey(n.key), transform)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The default schema.
|
||||
*
|
||||
* @type {Object}
|
||||
*/
|
||||
|
||||
const schema = {
|
||||
rules: [
|
||||
BLOCK_RENDER_RULE,
|
||||
INLINE_RENDER_RULE,
|
||||
DOCUMENT_CHILDREN_RULE,
|
||||
BLOCK_CHILDREN_RULE,
|
||||
INLINE_CHILDREN_RULE,
|
||||
...rules
|
||||
]
|
||||
}
|
||||
|
||||
|
76
src/plugins/schema.js
Normal file
76
src/plugins/schema.js
Normal file
@ -0,0 +1,76 @@
|
||||
|
||||
/**
|
||||
* A default schema rule to only allow block nodes in documents.
|
||||
*
|
||||
* @type {Object}
|
||||
*/
|
||||
|
||||
const DOCUMENT_CHILDREN_RULE = {
|
||||
match: (node) => {
|
||||
return node.kind == 'document'
|
||||
},
|
||||
validate: (document) => {
|
||||
const { nodes } = document
|
||||
const invalids = nodes.filter(n => n.kind != 'block')
|
||||
return invalids.size ? invalids : null
|
||||
},
|
||||
normalize: (transform, document, invalids) => {
|
||||
return invalids.reduce((t, n) => t.removeNodeByKey(n.key), transform)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A default schema rule to only allow block, inline and text nodes in blocks.
|
||||
*
|
||||
* @type {Object}
|
||||
*/
|
||||
|
||||
const BLOCK_CHILDREN_RULE = {
|
||||
match: (node) => {
|
||||
return node.kind == 'block'
|
||||
},
|
||||
validate: (block) => {
|
||||
const { nodes } = block
|
||||
const invalids = nodes.filter(n => n.kind != 'block' && n.kind != 'inline' && n.kind != 'text')
|
||||
return invalids.size ? invalids : null
|
||||
},
|
||||
normalize: (transform, block, invalids) => {
|
||||
return invalids.reduce((t, n) => t.removeNodeByKey(n.key), transform)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A default schema rule to only allow inline and text nodes in inlines.
|
||||
*
|
||||
* @type {Object}
|
||||
*/
|
||||
|
||||
const INLINE_CHILDREN_RULE = {
|
||||
match: (object) => {
|
||||
return object.kind == 'inline'
|
||||
},
|
||||
validate: (inline) => {
|
||||
const { nodes } = inline
|
||||
const invalids = nodes.filter(n => n.kind != 'inline' && n.kind != 'text')
|
||||
return invalids.size ? invalids : null
|
||||
},
|
||||
normalize: (transform, inline, invalids) => {
|
||||
return invalids.reduce((t, n) => t.removeNodeByKey(n.key), transform)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The default schema.
|
||||
*
|
||||
* @type {Object}
|
||||
*/
|
||||
|
||||
const schema = {
|
||||
rules: [
|
||||
DOCUMENT_CHILDREN_RULE,
|
||||
BLOCK_CHILDREN_RULE,
|
||||
INLINE_CHILDREN_RULE,
|
||||
]
|
||||
}
|
||||
|
||||
export default schema
|
Loading…
x
Reference in New Issue
Block a user