mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-18 13:11:17 +02:00
refactor schema violations to be a separate package
This commit is contained in:
@@ -73,4 +73,5 @@
|
||||
- [`slate-plain-serializer`](./reference/slate-plain-serializer/index.md)
|
||||
- [`slate-prop-types`](./reference/slate-prop-types/index.md)
|
||||
- [`slate-simulator`](./reference/slate-simulator/index.md)
|
||||
- [`slate-schema-violations`](./reference/slate-schema-violations/index.md)
|
||||
|
||||
|
217
docs/reference/slate-schema-violations/index.md
Normal file
217
docs/reference/slate-schema-violations/index.md
Normal file
@@ -0,0 +1,217 @@
|
||||
|
||||
# `slate-schema-violations`
|
||||
|
||||
```js
|
||||
import {
|
||||
CHILD_OBJECT_INVALID,
|
||||
CHILD_REQUIRED,
|
||||
CHILD_TYPE_INVALID,
|
||||
CHILD_UNKNOWN,
|
||||
FIRST_CHILD_OBJECT_INVALID,
|
||||
FIRST_CHILD_TYPE_INVALID,
|
||||
LAST_CHILD_OBJECT_INVALID,
|
||||
LAST_CHILD_TYPE_INVALID,
|
||||
NODE_DATA_INVALID,
|
||||
NODE_IS_VOID_INVALID,
|
||||
NODE_MARK_INVALID,
|
||||
NODE_TEXT_INVALID,
|
||||
PARENT_OBJECT_INVALID,
|
||||
PARENT_TYPE_INVALID,
|
||||
} from 'slate-schema-violations'
|
||||
```
|
||||
|
||||
A set of constants for the built-in violations in a Slate schema.
|
||||
|
||||
|
||||
## Example
|
||||
|
||||
```js
|
||||
import React from 'react'
|
||||
import Types from 'slate-prop-types'
|
||||
|
||||
class Toolbar extends React.Component {
|
||||
|
||||
propTypes = {
|
||||
block: Types.block,
|
||||
schema: Types.schema.isRequired,
|
||||
value: Types.value.isRequired,
|
||||
}
|
||||
|
||||
...
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
## Exports
|
||||
|
||||
### `CHILD_OBJECT_INVALID`
|
||||
|
||||
```js
|
||||
{
|
||||
child: Node,
|
||||
index: Number,
|
||||
node: Node,
|
||||
rule: Object,
|
||||
}
|
||||
```
|
||||
|
||||
Raised when the `object` property of a child node is invalid.
|
||||
|
||||
### `CHILD_REQUIRED`
|
||||
|
||||
```js
|
||||
{
|
||||
index: Number,
|
||||
node: Node,
|
||||
rule: Object,
|
||||
}
|
||||
```
|
||||
|
||||
Raised when a child node was required but none was found.
|
||||
|
||||
### `CHILD_TYPE_INVALID`
|
||||
|
||||
```js
|
||||
{
|
||||
child: Node,
|
||||
index: Number,
|
||||
node: Node,
|
||||
rule: Object,
|
||||
}
|
||||
```
|
||||
|
||||
Raised when the `type` property of a child node is invalid.
|
||||
|
||||
### `CHILD_UNKNOWN`
|
||||
|
||||
```js
|
||||
{
|
||||
child: Node,
|
||||
index: Number,
|
||||
node: Node,
|
||||
rule: Object,
|
||||
}
|
||||
```
|
||||
|
||||
Raised when a child was not expected but one was found.
|
||||
|
||||
### `FIRST_CHILD_OBJECT_INVALID`
|
||||
|
||||
```js
|
||||
{
|
||||
child: Node,
|
||||
node: Node,
|
||||
rule: Object,
|
||||
}
|
||||
```
|
||||
|
||||
Raised when the `object` property of the first child node is invalid, when a specific `first` rule was defined in a schema.
|
||||
|
||||
### `FIRST_CHILD_TYPE_INVALID`
|
||||
|
||||
```js
|
||||
{
|
||||
child: Node,
|
||||
node: Node,
|
||||
rule: Object,
|
||||
}
|
||||
```
|
||||
|
||||
Raised when the `type` property of the first child node is invalid, when a specific `first` rule was defined in a schema.
|
||||
|
||||
### `LAST_CHILD_OBJECT_INVALID`
|
||||
|
||||
```js
|
||||
{
|
||||
child: Node,
|
||||
node: Node,
|
||||
rule: Object,
|
||||
}
|
||||
```
|
||||
|
||||
Raised when the `object` property of the last child node is invalid, when a specific `last` rule was defined in a schema.
|
||||
|
||||
### `LAST_CHILD_TYPE_INVALID`
|
||||
|
||||
```js
|
||||
{
|
||||
child: Node,
|
||||
node: Node,
|
||||
rule: Object,
|
||||
}
|
||||
```
|
||||
|
||||
Raised when the `type` property of the last child node is invalid, when a specific `last` rule was defined in a schema.
|
||||
|
||||
### `NODE_DATA_INVALID`
|
||||
|
||||
```js
|
||||
{
|
||||
key: String,
|
||||
node: Node,
|
||||
rule: Object,
|
||||
value: Mixed,
|
||||
}
|
||||
```
|
||||
|
||||
Raised when the `data` property of a node contains an invalid entry.
|
||||
|
||||
### `NODE_IS_VOID_INVALID`
|
||||
|
||||
```js
|
||||
{
|
||||
node: Node,
|
||||
rule: Object,
|
||||
}
|
||||
```
|
||||
|
||||
Raised when the `isVoid` property of a node is invalid.
|
||||
|
||||
### `NODE_MARK_INVALID`
|
||||
|
||||
```js
|
||||
{
|
||||
mark: Mark,
|
||||
node: Node,
|
||||
rule: Object,
|
||||
}
|
||||
```
|
||||
|
||||
Raised when one of the marks in a node is invalid.
|
||||
|
||||
### `NODE_TEXT_INVALID`
|
||||
|
||||
```js
|
||||
{
|
||||
text: String,
|
||||
node: Node,
|
||||
rule: Object,
|
||||
}
|
||||
```
|
||||
|
||||
Raised when the text content of a node is invalid.
|
||||
|
||||
### `PARENT_OBJECT_INVALID`
|
||||
|
||||
```js
|
||||
{
|
||||
node: Node,
|
||||
parent: Node,
|
||||
rule: Object,
|
||||
}
|
||||
```
|
||||
|
||||
Raised when the `object` property of the parent of a node is invalid, when a specific `parent` rule was defined in a schema.
|
||||
|
||||
### `PARENT_TYPE_INVALID`
|
||||
|
||||
```js
|
||||
{
|
||||
node: Node,
|
||||
parent: Node,
|
||||
rule: Object,
|
||||
}
|
||||
```
|
||||
|
||||
Raised when the `type` property of the parent of a node is invalid, when a specific `parent` rule was defined in a schema.
|
@@ -144,11 +144,11 @@ Will validate a node's children. The `nodes` definitions can declare the `object
|
||||
> 🤖 The `nodes` array is order-sensitive! The example above will require that the first node be either an `image` or `video`, and that it be followed by one or more `paragraph` nodes.
|
||||
|
||||
### `normalize`
|
||||
`normalize(change: Change, reason: String, context: Object) => Void`
|
||||
`normalize(change: Change, violation: String, context: Object) => Void`
|
||||
|
||||
```js
|
||||
{
|
||||
normalize: (change, reason, context) => {
|
||||
normalize: (change, violation, context) => {
|
||||
case 'child_object_invalid':
|
||||
change.wrapBlockByKey(context.child.key, 'paragraph')
|
||||
return
|
||||
@@ -161,7 +161,7 @@ Will validate a node's children. The `nodes` definitions can declare the `object
|
||||
|
||||
A function that can be provided to override the default behavior in the case of a rule being invalid. By default Slate will do what it can, but since it doesn't know much about your schema, it will often remove invalid nodes. If you want to override this behavior, and "fix" the node instead of removing it, pass a custom `normalize` function.
|
||||
|
||||
For more information on the arguments passed to `normalize`, see the [Invalid Reasons](#invalid-reasons) reference.
|
||||
For more information on the arguments passed to `normalize`, see the [Violations](#violations) reference.
|
||||
|
||||
### `parent`
|
||||
`Array`
|
||||
@@ -212,162 +212,9 @@ Returns a boolean if the passed in argument is a `Schema`.
|
||||
Returns a JSON representation of the schema.
|
||||
|
||||
|
||||
## Invalid Reasons
|
||||
## Violations
|
||||
|
||||
When supplying your own `normalize` property for a schema rule, it will be called with `(change, reason, context)`. The `reason` will be one of a set of reasons, and `context` will vary depending on the reason. Invalid reason constants are available through the`SchemaViolations` object.
|
||||
When supplying your own `normalize` property for a schema rule, it will be called with `(change, violation, context)`. The `violation` will be one of a set of potential violation strings, and `context` will vary depending on the violation.
|
||||
|
||||
`import { SchemaViolations } from 'slate'`
|
||||
A set of the invalid violation strings are available as constants via the [`slate-schema-violations`](../slate-schema-violations) package.
|
||||
|
||||
Here's the full set:
|
||||
|
||||
### `SchemaViolations.ChildObjectInvalid`
|
||||
|
||||
```js
|
||||
{
|
||||
child: Node,
|
||||
index: Number,
|
||||
node: Node,
|
||||
rule: Object,
|
||||
}
|
||||
```
|
||||
|
||||
### `SchemaViolations.ChildRequired`
|
||||
|
||||
```js
|
||||
{
|
||||
index: Number,
|
||||
node: Node,
|
||||
rule: Object,
|
||||
}
|
||||
```
|
||||
|
||||
### `SchemaViolations.ChildTypeInvalid`
|
||||
|
||||
```js
|
||||
{
|
||||
child: Node,
|
||||
index: Number,
|
||||
node: Node,
|
||||
rule: Object,
|
||||
}
|
||||
```
|
||||
|
||||
### `SchemaViolations.ChildUnknown`
|
||||
|
||||
```js
|
||||
{
|
||||
child: Node,
|
||||
index: Number,
|
||||
node: Node,
|
||||
rule: Object,
|
||||
}
|
||||
```
|
||||
|
||||
### `SchemaViolations.FirstChildObjectInvalid`
|
||||
|
||||
```js
|
||||
{
|
||||
child: Node,
|
||||
node: Node,
|
||||
rule: Object,
|
||||
}
|
||||
```
|
||||
|
||||
### `SchemaViolations.FirstChildTypeInvalid`
|
||||
|
||||
```js
|
||||
{
|
||||
child: Node,
|
||||
node: Node,
|
||||
rule: Object,
|
||||
}
|
||||
```
|
||||
|
||||
### `SchemaViolations.LastChildObjectInvalid`
|
||||
|
||||
```js
|
||||
{
|
||||
child: Node,
|
||||
node: Node,
|
||||
rule: Object,
|
||||
}
|
||||
```
|
||||
|
||||
### `SchemaViolations.LastChildTypeInvalid`
|
||||
|
||||
```js
|
||||
{
|
||||
child: Node,
|
||||
node: Node,
|
||||
rule: Object,
|
||||
}
|
||||
```
|
||||
|
||||
### `SchemaViolations.NodeDataInvalid`
|
||||
|
||||
```js
|
||||
{
|
||||
key: String,
|
||||
node: Node,
|
||||
rule: Object,
|
||||
value: Mixed,
|
||||
}
|
||||
```
|
||||
|
||||
### `SchemaViolations.NodeIsVoidInvalid`
|
||||
|
||||
```js
|
||||
{
|
||||
node: Node,
|
||||
rule: Object,
|
||||
}
|
||||
```
|
||||
|
||||
### `SchemaViolations.NodeObjectInvalid`
|
||||
|
||||
```js
|
||||
{
|
||||
node: Node,
|
||||
rule: Object,
|
||||
}
|
||||
```
|
||||
|
||||
### `SchemaViolations.NodeMarkInvalid`
|
||||
|
||||
```js
|
||||
{
|
||||
mark: Mark,
|
||||
node: Node,
|
||||
rule: Object,
|
||||
}
|
||||
```
|
||||
|
||||
### `SchemaViolations.NodeTextInvalid`
|
||||
|
||||
```js
|
||||
{
|
||||
text: String,
|
||||
node: Node,
|
||||
rule: Object,
|
||||
}
|
||||
```
|
||||
|
||||
### `SchemaViolations.ParentObjectInvalid`
|
||||
|
||||
```js
|
||||
{
|
||||
node: Node,
|
||||
parent: Node,
|
||||
rule: Object,
|
||||
}
|
||||
```
|
||||
|
||||
### `SchemaViolations.ParentTypeInvalid`
|
||||
|
||||
```js
|
||||
{
|
||||
node: Node,
|
||||
parent: Node,
|
||||
rule: Object,
|
||||
}
|
||||
```
|
||||
|
@@ -1,6 +1,7 @@
|
||||
|
||||
import { Editor } from 'slate-react'
|
||||
import { Block, Value, SchemaViolations } from 'slate'
|
||||
import { Block, Value } from 'slate'
|
||||
import { CHILD_REQUIRED, CHILD_TYPE_INVALID } from 'slate-schema-violations'
|
||||
|
||||
import React from 'react'
|
||||
import initialValue from './value.json'
|
||||
@@ -17,12 +18,12 @@ const schema = {
|
||||
{ types: ['title'], min: 1, max: 1 },
|
||||
{ types: ['paragraph'], min: 1 },
|
||||
],
|
||||
normalize: (change, reason, { node, child, index }) => {
|
||||
switch (reason) {
|
||||
case SchemaViolations.ChildTypeInvalid: {
|
||||
normalize: (change, violation, { node, child, index }) => {
|
||||
switch (violation) {
|
||||
case CHILD_TYPE_INVALID: {
|
||||
return change.setNodeByKey(child.key, index == 0 ? 'title' : 'paragraph')
|
||||
}
|
||||
case SchemaViolations.ChildRequired: {
|
||||
case CHILD_REQUIRED: {
|
||||
const block = Block.create(index == 0 ? 'title' : 'paragraph')
|
||||
return change.insertNodeByKey(node.key, index, block)
|
||||
}
|
||||
|
@@ -61,7 +61,8 @@
|
||||
"slate": "*",
|
||||
"slate-html-serializer": "*",
|
||||
"slate-plain-serializer": "*",
|
||||
"slate-react": "*"
|
||||
"slate-react": "*",
|
||||
"slate-schema-violations": "*"
|
||||
},
|
||||
"scripts": {
|
||||
"benchmark": "mkdir -p ./tmp && babel-node ./node_modules/.bin/_matcha --reporter ./support/benchmark-reporter ./packages/*/benchmark/index.js > ./tmp/benchmark-comparison.json && babel-node ./support/benchmark-compare",
|
||||
|
13
packages/slate-schema-violations/Changelog.md
Normal file
13
packages/slate-schema-violations/Changelog.md
Normal file
@@ -0,0 +1,13 @@
|
||||
|
||||
# Changelog
|
||||
|
||||
This document maintains a list of changes to the `slate-schema-violations` package with each new version. Until `1.0.0` is released, breaking changes will be added as minor version bumps, and smaller changes won't be accounted for since the library is moving quickly.
|
||||
|
||||
|
||||
---
|
||||
|
||||
|
||||
### `0.1.0` — January 26, 2018
|
||||
|
||||
:tada:
|
||||
|
2
packages/slate-schema-violations/Readme.md
Normal file
2
packages/slate-schema-violations/Readme.md
Normal file
@@ -0,0 +1,2 @@
|
||||
|
||||
This package contains a set of constants for the built-in violations in a Slate schema.
|
35
packages/slate-schema-violations/package.json
Normal file
35
packages/slate-schema-violations/package.json
Normal file
@@ -0,0 +1,35 @@
|
||||
{
|
||||
"name": "slate-schema-violations",
|
||||
"description": "A set of constants for the built-in violations in a Slate schema.",
|
||||
"version": "0.0.0",
|
||||
"license": "MIT",
|
||||
"repository": "git://github.com/ianstormtaylor/slate.git",
|
||||
"main": "./lib/index.js",
|
||||
"files": [
|
||||
"dist/",
|
||||
"lib/"
|
||||
],
|
||||
"dependencies": {},
|
||||
"peerDependencies": {},
|
||||
"devDependencies": {
|
||||
"babel-cli": "^6.10.1",
|
||||
"browserify": "^13.0.1",
|
||||
"mocha": "^2.5.3",
|
||||
"slate": "^0.32.1",
|
||||
"uglify-js": "^2.7.0"
|
||||
},
|
||||
"scripts": {
|
||||
"build": "babel --out-dir ./lib ./src",
|
||||
"build:max": "mkdir -p ./dist && NODE_ENV=production browserify ./src/index.js --transform babelify --transform envify --transform [ browserify-global-shim --global ] --standalone SlateSchemaViolations > ./dist/slate-schema-violations.js",
|
||||
"build:min": "mkdir -p ./dist && NODE_ENV=production browserify ./src/index.js --transform babelify --transform envify --transform [ browserify-global-shim --global ] --transform uglifyify --standalone SlateSchemaViolations | uglifyjs > ./dist/slate-schema-violations.min.js",
|
||||
"clean": "rm -rf ./dist ./lib ./node_modules",
|
||||
"prepublish": "yarn run build && yarn run build:max && yarn run build:min",
|
||||
"watch": "babel --watch --out-dir ./lib ./src --source-maps inline"
|
||||
},
|
||||
"keywords": [
|
||||
"constants",
|
||||
"schema",
|
||||
"slate",
|
||||
"violation"
|
||||
]
|
||||
}
|
21
packages/slate-schema-violations/src/index.js
Normal file
21
packages/slate-schema-violations/src/index.js
Normal file
@@ -0,0 +1,21 @@
|
||||
|
||||
/**
|
||||
* Schema violations.
|
||||
*
|
||||
* @type {String}
|
||||
*/
|
||||
|
||||
export const CHILD_OBJECT_INVALID = 'child_object_invalid'
|
||||
export const CHILD_REQUIRED = 'child_required'
|
||||
export const CHILD_TYPE_INVALID = 'child_type_invalid'
|
||||
export const CHILD_UNKNOWN = 'child_unknown'
|
||||
export const FIRST_CHILD_OBJECT_INVALID = 'first_child_object_invalid'
|
||||
export const FIRST_CHILD_TYPE_INVALID = 'first_child_type_invalid'
|
||||
export const LAST_CHILD_OBJECT_INVALID = 'last_child_object_invalid'
|
||||
export const LAST_CHILD_TYPE_INVALID = 'last_child_type_invalid'
|
||||
export const NODE_DATA_INVALID = 'node_data_invalid'
|
||||
export const NODE_IS_VOID_INVALID = 'node_is_void_invalid'
|
||||
export const NODE_MARK_INVALID = 'node_mark_invalid'
|
||||
export const NODE_TEXT_INVALID = 'node_text_invalid'
|
||||
export const PARENT_OBJECT_INVALID = 'parent_object_invalid'
|
||||
export const PARENT_TYPE_INVALID = 'parent_type_invalid'
|
0
packages/slate-schema-violations/test/index.js
Normal file
0
packages/slate-schema-violations/test/index.js
Normal file
@@ -17,6 +17,7 @@
|
||||
"is-plain-object": "^2.0.4",
|
||||
"lodash": "^4.17.4",
|
||||
"slate-dev-logger": "^0.1.36",
|
||||
"slate-schema-violations": "^0.0.0",
|
||||
"type-of": "^2.0.1"
|
||||
},
|
||||
"peerDependencies": {
|
||||
|
@@ -5,49 +5,28 @@ import logger from 'slate-dev-logger'
|
||||
import mergeWith from 'lodash/mergeWith'
|
||||
import { Record } from 'immutable'
|
||||
|
||||
import {
|
||||
CHILD_OBJECT_INVALID,
|
||||
CHILD_REQUIRED,
|
||||
CHILD_TYPE_INVALID,
|
||||
CHILD_UNKNOWN,
|
||||
FIRST_CHILD_OBJECT_INVALID,
|
||||
FIRST_CHILD_TYPE_INVALID,
|
||||
LAST_CHILD_OBJECT_INVALID,
|
||||
LAST_CHILD_TYPE_INVALID,
|
||||
NODE_DATA_INVALID,
|
||||
NODE_IS_VOID_INVALID,
|
||||
NODE_MARK_INVALID,
|
||||
NODE_TEXT_INVALID,
|
||||
PARENT_OBJECT_INVALID,
|
||||
PARENT_TYPE_INVALID,
|
||||
} from 'slate-schema-violations'
|
||||
|
||||
import CORE_SCHEMA_RULES from '../constants/core-schema-rules'
|
||||
import MODEL_TYPES from '../constants/model-types'
|
||||
import Stack from './stack'
|
||||
import memoize from '../utils/memoize'
|
||||
|
||||
/**
|
||||
* Validation failure reasons.
|
||||
*
|
||||
* @type {Object}
|
||||
*/
|
||||
|
||||
const CHILD_OBJECT_INVALID = 'child_object_invalid'
|
||||
const CHILD_REQUIRED = 'child_required'
|
||||
const CHILD_TYPE_INVALID = 'child_type_invalid'
|
||||
const CHILD_UNKNOWN = 'child_unknown'
|
||||
const FIRST_CHILD_OBJECT_INVALID = 'first_child_object_invalid'
|
||||
const FIRST_CHILD_TYPE_INVALID = 'first_child_type_invalid'
|
||||
const LAST_CHILD_OBJECT_INVALID = 'last_child_object_invalid'
|
||||
const LAST_CHILD_TYPE_INVALID = 'last_child_type_invalid'
|
||||
const NODE_DATA_INVALID = 'node_data_invalid'
|
||||
const NODE_IS_VOID_INVALID = 'node_is_void_invalid'
|
||||
const NODE_MARK_INVALID = 'node_mark_invalid'
|
||||
const NODE_TEXT_INVALID = 'node_text_invalid'
|
||||
const PARENT_OBJECT_INVALID = 'parent_object_invalid'
|
||||
const PARENT_TYPE_INVALID = 'parent_type_invalid'
|
||||
|
||||
export const SchemaViolations = Object.freeze({
|
||||
ChildObjectInvalid: CHILD_OBJECT_INVALID,
|
||||
ChildRequired: CHILD_REQUIRED,
|
||||
ChildTypeInvalid: CHILD_TYPE_INVALID,
|
||||
ChildUnknown: CHILD_UNKNOWN,
|
||||
FirstChildObjectInvalid: FIRST_CHILD_OBJECT_INVALID,
|
||||
FirstChildTypeInvalid: FIRST_CHILD_TYPE_INVALID,
|
||||
LastChildObjectInvalid: LAST_CHILD_OBJECT_INVALID,
|
||||
LastChildTypeInvalid: LAST_CHILD_TYPE_INVALID,
|
||||
NodeDataInvalid: NODE_DATA_INVALID,
|
||||
NodeIsVoidInvalid: NODE_IS_VOID_INVALID,
|
||||
NodeMarkInvalid: NODE_MARK_INVALID,
|
||||
NodeTextInvalid: NODE_TEXT_INVALID,
|
||||
ParentObjectInvalid: PARENT_OBJECT_INVALID,
|
||||
ParentTypeInvalid: PARENT_TYPE_INVALID,
|
||||
})
|
||||
|
||||
/**
|
||||
* Debug.
|
||||
*
|
||||
@@ -203,32 +182,32 @@ class Schema extends Record(DEFAULTS) {
|
||||
/**
|
||||
* Fail validation by returning a normalizing change function.
|
||||
*
|
||||
* @param {String} reason
|
||||
* @param {String} violation
|
||||
* @param {Object} context
|
||||
* @return {Function}
|
||||
*/
|
||||
|
||||
fail(reason, context) {
|
||||
fail(violation, context) {
|
||||
return (change) => {
|
||||
debug(`normalizing`, { reason, context })
|
||||
debug(`normalizing`, { violation, context })
|
||||
const { rule } = context
|
||||
const { size } = change.operations
|
||||
if (rule.normalize) rule.normalize(change, reason, context)
|
||||
if (rule.normalize) rule.normalize(change, violation, context)
|
||||
if (change.operations.size > size) return
|
||||
this.normalize(change, reason, context)
|
||||
this.normalize(change, violation, context)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalize an invalid value with `reason` and `context`.
|
||||
* Normalize an invalid value with `violation` and `context`.
|
||||
*
|
||||
* @param {Change} change
|
||||
* @param {String} reason
|
||||
* @param {String} violation
|
||||
* @param {Mixed} context
|
||||
*/
|
||||
|
||||
normalize(change, reason, context) {
|
||||
switch (reason) {
|
||||
normalize(change, violation, context) {
|
||||
switch (violation) {
|
||||
case CHILD_OBJECT_INVALID:
|
||||
case CHILD_TYPE_INVALID:
|
||||
case CHILD_UNKNOWN:
|
||||
|
@@ -1,6 +1,6 @@
|
||||
/** @jsx h */
|
||||
|
||||
import { SchemaViolations } from '../../..'
|
||||
import { CHILD_OBJECT_INVALID } from 'slate-schema-violations'
|
||||
import h from '../../helpers/h'
|
||||
|
||||
export const schema = {
|
||||
@@ -12,7 +12,7 @@ export const schema = {
|
||||
{ objects: ['block'], types: ['paragraph'], min: 1 }
|
||||
],
|
||||
normalize: (change, reason, { node, child }) => {
|
||||
if (reason == SchemaViolations.ChildObjectInvalid) {
|
||||
if (reason == CHILD_OBJECT_INVALID) {
|
||||
change.wrapBlockByKey(child.key, 'paragraph')
|
||||
}
|
||||
}
|
||||
|
@@ -1,6 +1,6 @@
|
||||
/** @jsx h */
|
||||
|
||||
import { SchemaViolations } from '../../..'
|
||||
import { CHILD_OBJECT_INVALID } from 'slate-schema-violations'
|
||||
import h from '../../helpers/h'
|
||||
|
||||
export const schema = {
|
||||
@@ -11,7 +11,7 @@ export const schema = {
|
||||
{ objects: ['block'] },
|
||||
],
|
||||
normalize: (change, reason, { child }) => {
|
||||
if (reason == SchemaViolations.ChildObjectInvalid) {
|
||||
if (reason == CHILD_OBJECT_INVALID) {
|
||||
change.wrapBlockByKey(child.key, 'paragraph')
|
||||
}
|
||||
}
|
||||
|
@@ -1,6 +1,6 @@
|
||||
/** @jsx h */
|
||||
|
||||
import { SchemaViolations } from '../../..'
|
||||
import { CHILD_REQUIRED } from 'slate-schema-violations'
|
||||
import h from '../../helpers/h'
|
||||
|
||||
export const schema = {
|
||||
@@ -11,7 +11,7 @@ export const schema = {
|
||||
{ types: ['paragraph'], min: 2 },
|
||||
],
|
||||
normalize: (change, reason, { node, index }) => {
|
||||
if (reason == SchemaViolations.ChildRequired) {
|
||||
if (reason == CHILD_REQUIRED) {
|
||||
change.insertNodeByKey(node.key, index, { object: 'block', type: 'paragraph' })
|
||||
}
|
||||
}
|
||||
|
@@ -1,6 +1,6 @@
|
||||
/** @jsx h */
|
||||
|
||||
import { SchemaViolations } from '../../..'
|
||||
import { CHILD_TYPE_INVALID } from 'slate-schema-violations'
|
||||
import h from '../../helpers/h'
|
||||
|
||||
export const schema = {
|
||||
@@ -11,7 +11,7 @@ export const schema = {
|
||||
{ types: ['paragraph'] },
|
||||
],
|
||||
normalize: (change, reason, { child }) => {
|
||||
if (reason == SchemaViolations.ChildTypeInvalid) {
|
||||
if (reason == CHILD_TYPE_INVALID) {
|
||||
change.wrapBlockByKey(child.key, 'paragraph')
|
||||
}
|
||||
}
|
||||
|
@@ -1,6 +1,6 @@
|
||||
/** @jsx h */
|
||||
|
||||
import { SchemaViolations } from '../../..'
|
||||
import { CHILD_UNKNOWN } from 'slate-schema-violations'
|
||||
import h from '../../helpers/h'
|
||||
|
||||
export const schema = {
|
||||
@@ -11,7 +11,7 @@ export const schema = {
|
||||
{ types: ['paragraph'], max: 1 },
|
||||
],
|
||||
normalize: (change, reason, { node, child, index }) => {
|
||||
if (reason == SchemaViolations.ChildUnknown) {
|
||||
if (reason == CHILD_UNKNOWN) {
|
||||
const previous = node.getPreviousSibling(child.key)
|
||||
const offset = previous.nodes.size
|
||||
child.nodes.forEach((n, i) => change.moveNodeByKey(n.key, previous.key, offset + i, { normalize: false }))
|
||||
|
@@ -1,6 +1,6 @@
|
||||
/** @jsx h */
|
||||
|
||||
import { SchemaViolations } from '../../..'
|
||||
import { FIRST_CHILD_OBJECT_INVALID } from 'slate-schema-violations'
|
||||
import h from '../../helpers/h'
|
||||
|
||||
export const schema = {
|
||||
@@ -9,7 +9,7 @@ export const schema = {
|
||||
quote: {
|
||||
first: { objects: ['block'] },
|
||||
normalize: (change, reason, { child }) => {
|
||||
if (reason == SchemaViolations.FirstChildObjectInvalid) {
|
||||
if (reason == FIRST_CHILD_OBJECT_INVALID) {
|
||||
change.wrapBlockByKey(child.key, 'paragraph')
|
||||
}
|
||||
}
|
||||
|
@@ -1,6 +1,6 @@
|
||||
/** @jsx h */
|
||||
|
||||
import { SchemaViolations } from '../../..'
|
||||
import { FIRST_CHILD_TYPE_INVALID } from 'slate-schema-violations'
|
||||
import h from '../../helpers/h'
|
||||
|
||||
export const schema = {
|
||||
@@ -9,7 +9,7 @@ export const schema = {
|
||||
quote: {
|
||||
first: { types: ['paragraph'] },
|
||||
normalize: (change, reason, { child }) => {
|
||||
if (reason == SchemaViolations.FirstChildTypeInvalid) {
|
||||
if (reason == FIRST_CHILD_TYPE_INVALID) {
|
||||
change.wrapBlockByKey(child.key, 'paragraph')
|
||||
}
|
||||
}
|
||||
|
@@ -1,6 +1,6 @@
|
||||
/** @jsx h */
|
||||
|
||||
import { SchemaViolations } from '../../..'
|
||||
import { LAST_CHILD_OBJECT_INVALID } from 'slate-schema-violations'
|
||||
import h from '../../helpers/h'
|
||||
|
||||
export const schema = {
|
||||
@@ -9,7 +9,7 @@ export const schema = {
|
||||
quote: {
|
||||
last: { objects: ['block'] },
|
||||
normalize: (change, reason, { child }) => {
|
||||
if (reason == SchemaViolations.LastChildObjectInvalid) {
|
||||
if (reason == LAST_CHILD_OBJECT_INVALID) {
|
||||
change.wrapBlockByKey(child.key, 'paragraph')
|
||||
}
|
||||
}
|
||||
|
@@ -1,6 +1,6 @@
|
||||
/** @jsx h */
|
||||
|
||||
import { SchemaViolations } from '../../..'
|
||||
import { LAST_CHILD_TYPE_INVALID } from 'slate-schema-violations'
|
||||
import h from '../../helpers/h'
|
||||
|
||||
export const schema = {
|
||||
@@ -9,7 +9,7 @@ export const schema = {
|
||||
quote: {
|
||||
last: { types: ['paragraph'] },
|
||||
normalize: (change, reason, { child }) => {
|
||||
if (reason == SchemaViolations.LastChildTypeInvalid) {
|
||||
if (reason == LAST_CHILD_TYPE_INVALID) {
|
||||
change.wrapBlockByKey(child.key, 'paragraph')
|
||||
}
|
||||
}
|
||||
|
@@ -1,6 +1,6 @@
|
||||
/** @jsx h */
|
||||
|
||||
import { SchemaViolations } from '../../..'
|
||||
import { NODE_DATA_INVALID } from 'slate-schema-violations'
|
||||
import h from '../../helpers/h'
|
||||
|
||||
export const schema = {
|
||||
@@ -10,7 +10,7 @@ export const schema = {
|
||||
thing: v => v == 'value'
|
||||
},
|
||||
normalize: (change, reason, { node, key }) => {
|
||||
if (reason == SchemaViolations.NodeDataInvalid) {
|
||||
if (reason == NODE_DATA_INVALID) {
|
||||
change.setNodeByKey(node.key, { data: { thing: 'value' }})
|
||||
}
|
||||
}
|
||||
|
@@ -1,6 +1,6 @@
|
||||
/** @jsx h */
|
||||
|
||||
import { SchemaViolations } from '../../..'
|
||||
import { NODE_IS_VOID_INVALID } from 'slate-schema-violations'
|
||||
import h from '../../helpers/h'
|
||||
|
||||
export const schema = {
|
||||
@@ -8,7 +8,7 @@ export const schema = {
|
||||
paragraph: {
|
||||
isVoid: false,
|
||||
normalize: (change, reason, { node }) => {
|
||||
if (reason == SchemaViolations.NodeIsVoidInvalid) {
|
||||
if (reason == NODE_IS_VOID_INVALID) {
|
||||
change.removeNodeByKey(node.key, 'paragraph')
|
||||
}
|
||||
}
|
||||
|
@@ -1,6 +1,6 @@
|
||||
/** @jsx h */
|
||||
|
||||
import { SchemaViolations } from '../../..'
|
||||
import { NODE_MARK_INVALID } from 'slate-schema-violations'
|
||||
import h from '../../helpers/h'
|
||||
|
||||
export const schema = {
|
||||
@@ -8,7 +8,7 @@ export const schema = {
|
||||
paragraph: {
|
||||
marks: [{ type: 'bold' }],
|
||||
normalize: (change, reason, { node }) => {
|
||||
if (reason == SchemaViolations.NodeMarkInvalid) {
|
||||
if (reason == NODE_MARK_INVALID) {
|
||||
node.nodes.forEach(n => change.removeNodeByKey(n.key))
|
||||
}
|
||||
}
|
||||
|
@@ -1,6 +1,6 @@
|
||||
/** @jsx h */
|
||||
|
||||
import { SchemaViolations } from '../../..'
|
||||
import { NODE_TEXT_INVALID } from 'slate-schema-violations'
|
||||
import h from '../../helpers/h'
|
||||
|
||||
export const schema = {
|
||||
@@ -8,7 +8,7 @@ export const schema = {
|
||||
paragraph: {
|
||||
text: /^\d*$/,
|
||||
normalize: (change, reason, { node }) => {
|
||||
if (reason == SchemaViolations.NodeTextInvalid) {
|
||||
if (reason == NODE_TEXT_INVALID) {
|
||||
node.nodes.forEach(n => change.removeNodeByKey(n.key))
|
||||
}
|
||||
}
|
||||
|
@@ -1,6 +1,6 @@
|
||||
/** @jsx h */
|
||||
|
||||
import { SchemaViolations } from '../../..'
|
||||
import { PARENT_OBJECT_INVALID } from 'slate-schema-violations'
|
||||
import h from '../../helpers/h'
|
||||
|
||||
export const schema = {
|
||||
@@ -8,7 +8,7 @@ export const schema = {
|
||||
link: {
|
||||
parent: { objects: ['block'] },
|
||||
normalize: (change, reason, { node }) => {
|
||||
if (reason == SchemaViolations.ParentObjectInvalid) {
|
||||
if (reason == PARENT_OBJECT_INVALID) {
|
||||
change.unwrapNodeByKey(node.key)
|
||||
}
|
||||
}
|
||||
|
@@ -1,6 +1,6 @@
|
||||
/** @jsx h */
|
||||
|
||||
import { SchemaViolations } from '../../..'
|
||||
import { PARENT_TYPE_INVALID } from 'slate-schema-violations'
|
||||
import h from '../../helpers/h'
|
||||
|
||||
export const schema = {
|
||||
@@ -9,7 +9,7 @@ export const schema = {
|
||||
item: {
|
||||
parent: { types: ['list'] },
|
||||
normalize: (change, reason, { node }) => {
|
||||
if (reason == SchemaViolations.ParentTypeInvalid) {
|
||||
if (reason == PARENT_TYPE_INVALID) {
|
||||
change.wrapBlockByKey(node.key, 'list')
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user