1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-02-25 09:43:21 +01:00

Refactor to move all normalization rules in "plugins/schema.js"

This commit is contained in:
Samy Pesse 2016-10-18 18:47:45 +02:00
parent 7091c9489e
commit 6f0a31c90f
2 changed files with 128 additions and 115 deletions

View File

@ -7,6 +7,7 @@ import React from 'react'
import String from '../utils/string' import String from '../utils/string'
import getWindow from 'get-window' import getWindow from 'get-window'
import { IS_MAC } from '../constants/environment' import { IS_MAC } from '../constants/environment'
import { rules } from './schema'
/** /**
* Debug. * Debug.
@ -681,128 +682,64 @@ function Plugin(options = {}) {
} }
/** /**
* A default schema rule to render block nodes. * Extend core schema with rendering
*
* @type {Object}
*/ */
const BLOCK_RENDER_RULE = { /**
match: (node) => { * A default schema rule to render block nodes.
return node.kind == 'block' *
}, * @type {Object}
render: (props) => { */
return (
<div {...props.attributes} style={{ position: 'relative' }}>
{props.children}
{placeholder
? <Placeholder
className={placeholderClassName}
node={props.node}
parent={props.state.document}
state={props.state}
style={placeholderStyle}
>
{placeholder}
</Placeholder>
: null}
</div>
)
}
}
/** const BLOCK_RENDER_RULE = {
* A default schema rule to render inline nodes. match: (node) => {
* return node.kind == 'block'
* @type {Object} },
*/ render: (props) => {
return (
<div {...props.attributes} style={{ position: 'relative' }}>
{props.children}
{placeholder
? <Placeholder
className={placeholderClassName}
node={props.node}
parent={props.state.document}
state={props.state}
style={placeholderStyle}
>
{placeholder}
</Placeholder>
: null}
</div>
)
}
}
const INLINE_RENDER_RULE = { /**
match: (node) => { * A default schema rule to render inline nodes.
return node.kind == 'inline' *
}, * @type {Object}
render: (props) => { */
return (
<span {...props.attributes} style={{ position: 'relative' }}>
{props.children}
</span>
)
}
}
/** const INLINE_RENDER_RULE = {
* A default schema rule to only allow block nodes in documents. match: (node) => {
* return node.kind == 'inline'
* @type {Object} },
*/ render: (props) => {
return (
const DOCUMENT_CHILDREN_RULE = { <span {...props.attributes} style={{ position: 'relative' }}>
match: (node) => { {props.children}
return node.kind == 'document' </span>
}, )
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 = { const schema = {
rules: [ rules: [
BLOCK_RENDER_RULE, BLOCK_RENDER_RULE,
INLINE_RENDER_RULE, INLINE_RENDER_RULE,
DOCUMENT_CHILDREN_RULE, ...rules
BLOCK_CHILDREN_RULE, ]
INLINE_CHILDREN_RULE,
]
} }
/** /**

76
src/plugins/schema.js Normal file
View 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