mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-19 13:41:19 +02:00
Don't normalize when schema has no normalization rules
This commit is contained in:
@@ -1,102 +1,10 @@
|
|||||||
|
|
||||||
import React from 'react'
|
import React from 'react'
|
||||||
import includes from 'lodash/includes'
|
|
||||||
import isReactComponent from '../utils/is-react-component'
|
import isReactComponent from '../utils/is-react-component'
|
||||||
import typeOf from 'type-of'
|
import typeOf from 'type-of'
|
||||||
import memoize from '../utils/memoize'
|
import memoize from '../utils/memoize'
|
||||||
import { Record } from 'immutable'
|
import { Record } from 'immutable'
|
||||||
|
|
||||||
/**
|
|
||||||
* Checks that the schema can perform, ordered by performance.
|
|
||||||
*
|
|
||||||
* @type {Object}
|
|
||||||
*/
|
|
||||||
|
|
||||||
const CHECKS = {
|
|
||||||
|
|
||||||
kind(object, value) {
|
|
||||||
if (object.kind != value) return object.kind
|
|
||||||
},
|
|
||||||
|
|
||||||
type(object, value) {
|
|
||||||
if (object.type != value) return object.type
|
|
||||||
},
|
|
||||||
|
|
||||||
isVoid(object, value) {
|
|
||||||
if (object.isVoid != value) return object.isVoid
|
|
||||||
},
|
|
||||||
|
|
||||||
minChildren(object, value) {
|
|
||||||
if (object.nodes.size < value) return object.nodes.size
|
|
||||||
},
|
|
||||||
|
|
||||||
maxChildren(object, value) {
|
|
||||||
if (object.nodes.size > value) return object.nodes.size
|
|
||||||
},
|
|
||||||
|
|
||||||
kinds(object, value) {
|
|
||||||
if (!includes(value, object.kind)) return object.kind
|
|
||||||
},
|
|
||||||
|
|
||||||
types(object, value) {
|
|
||||||
if (!includes(value, object.type)) return object.type
|
|
||||||
},
|
|
||||||
|
|
||||||
minLength(object, value) {
|
|
||||||
const { length } = object
|
|
||||||
if (length < value) return length
|
|
||||||
},
|
|
||||||
|
|
||||||
maxLength(object, value) {
|
|
||||||
const { length } = object
|
|
||||||
if (length > value) return length
|
|
||||||
},
|
|
||||||
|
|
||||||
text(object, value) {
|
|
||||||
const { text } = object
|
|
||||||
switch (typeOf(value)) {
|
|
||||||
case 'function': if (value(text)) return text
|
|
||||||
case 'regexp': if (!text.match(value)) return text
|
|
||||||
default: if (text != value) return text
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
anyOf(object, value) {
|
|
||||||
const { nodes } = object
|
|
||||||
if (!nodes) return
|
|
||||||
const invalids = nodes.filterNot((child) => {
|
|
||||||
return value.some(match => match(child))
|
|
||||||
})
|
|
||||||
|
|
||||||
if (invalids.size) return invalids
|
|
||||||
},
|
|
||||||
|
|
||||||
noneOf(object, value) {
|
|
||||||
const { nodes } = object
|
|
||||||
if (!nodes) return
|
|
||||||
const invalids = nodes.filterNot((child) => {
|
|
||||||
return value.every(match => !match(child))
|
|
||||||
})
|
|
||||||
|
|
||||||
if (invalids.size) return invalids
|
|
||||||
},
|
|
||||||
|
|
||||||
exactlyOf(object, value) {
|
|
||||||
const { nodes } = object
|
|
||||||
if (!nodes) return
|
|
||||||
if (nodes.size != value.length) return nodes
|
|
||||||
|
|
||||||
const invalids = nodes.filterNot((child, i) => {
|
|
||||||
const match = value[i]
|
|
||||||
if (!match) return false
|
|
||||||
return match(child)
|
|
||||||
})
|
|
||||||
|
|
||||||
if (invalids.size) return invalids
|
|
||||||
},
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Default properties.
|
* Default properties.
|
||||||
*
|
*
|
||||||
@@ -137,6 +45,15 @@ class Schema extends new Record(DEFAULTS) {
|
|||||||
return 'schema'
|
return 'schema'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return true if one rule can normalize the document
|
||||||
|
*/
|
||||||
|
|
||||||
|
get isNormalization() {
|
||||||
|
const { rules } = this
|
||||||
|
return rules.some(rule => rule.validate)
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the renderer for an `object`.
|
* Return the renderer for an `object`.
|
||||||
*
|
*
|
||||||
|
@@ -742,8 +742,7 @@ function Plugin(options = {}) {
|
|||||||
const schema = {
|
const schema = {
|
||||||
rules: [
|
rules: [
|
||||||
BLOCK_RENDER_RULE,
|
BLOCK_RENDER_RULE,
|
||||||
INLINE_RENDER_RULE,
|
INLINE_RENDER_RULE
|
||||||
...defaultSchema.rules
|
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -137,6 +137,11 @@ export function normalizeWith(transform, schema) {
|
|||||||
const { state } = transform
|
const { state } = transform
|
||||||
const { document } = state
|
const { document } = state
|
||||||
|
|
||||||
|
// Schema was not rule to edit the document
|
||||||
|
if (!schema.isNormalization) {
|
||||||
|
return transform
|
||||||
|
}
|
||||||
|
|
||||||
return transform.normalizeNodeWith(schema, document)
|
return transform.normalizeNodeWith(schema, document)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user