1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-18 13:11:17 +02:00

Don't normalize when schema has no normalization rules

This commit is contained in:
Samy Pessé
2016-10-25 10:15:37 +02:00
parent b09e326426
commit 218bb0c472
3 changed files with 15 additions and 94 deletions

View File

@@ -1,102 +1,10 @@
import React from 'react'
import includes from 'lodash/includes'
import isReactComponent from '../utils/is-react-component'
import typeOf from 'type-of'
import memoize from '../utils/memoize'
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.
*
@@ -137,6 +45,15 @@ class Schema extends new Record(DEFAULTS) {
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`.
*

View File

@@ -742,8 +742,7 @@ function Plugin(options = {}) {
const schema = {
rules: [
BLOCK_RENDER_RULE,
INLINE_RENDER_RULE,
...defaultSchema.rules
INLINE_RENDER_RULE
]
}

View File

@@ -137,6 +137,11 @@ export function normalizeWith(transform, schema) {
const { state } = transform
const { document } = state
// Schema was not rule to edit the document
if (!schema.isNormalization) {
return transform
}
return transform.normalizeNodeWith(schema, document)
}