mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-18 05:01:17 +02:00
Merge branch 'schema-normalize-transforms' into schema-normalize
This commit is contained in:
@@ -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`.
|
||||
*
|
||||
|
@@ -742,8 +742,7 @@ function Plugin(options = {}) {
|
||||
const schema = {
|
||||
rules: [
|
||||
BLOCK_RENDER_RULE,
|
||||
INLINE_RENDER_RULE,
|
||||
...defaultSchema.rules
|
||||
INLINE_RENDER_RULE
|
||||
]
|
||||
}
|
||||
|
||||
|
@@ -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)
|
||||
}
|
||||
|
||||
@@ -161,10 +166,7 @@ export function normalize(transform) {
|
||||
*/
|
||||
|
||||
export function normalizeDocument(transform) {
|
||||
console.time('normalizeDocument')
|
||||
transform = transform.normalizeWith(defaultSchema)
|
||||
console.timeEnd('normalizeDocument')
|
||||
return transform
|
||||
return transform.normalizeWith(defaultSchema)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -176,14 +178,11 @@ export function normalizeDocument(transform) {
|
||||
*/
|
||||
|
||||
export function normalizeNodeByKey(transform, key) {
|
||||
console.time('normalizeNodeByKey')
|
||||
const { state } = transform
|
||||
const { document } = state
|
||||
const node = document.key == key ? document : document.assertDescendant(key)
|
||||
|
||||
transform = transform.normalizeNodeWith(defaultSchema, node)
|
||||
console.timeEnd('normalizeNodeByKey')
|
||||
return transform
|
||||
return transform.normalizeNodeWith(defaultSchema, node)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -195,14 +194,11 @@ export function normalizeNodeByKey(transform, key) {
|
||||
*/
|
||||
|
||||
export function normalizeParentsByKey(transform, key) {
|
||||
console.time('normalizeParentsByKey')
|
||||
const { state } = transform
|
||||
const { document } = state
|
||||
const node = document.key == key ? document : document.assertDescendant(key)
|
||||
|
||||
transform = transform.normalizeParentsWith(defaultSchema, node)
|
||||
console.timeEnd('normalizeParentsByKey')
|
||||
return transform
|
||||
return transform.normalizeParentsWith(defaultSchema, node)
|
||||
}
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user