mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-30 18:39:51 +02:00
added export constant enum for schema violations (#1532)
* added export constant enum for schema violations * updated examples to use the schema violations enum * use SchemaViolations enum in tests and docs * fixed path for schema violations import
This commit is contained in:
committed by
Zach Schneider
parent
e6c7934cb9
commit
7d5a33025b
@@ -12,7 +12,7 @@ import Node from './models/node'
|
||||
import Operation from './models/operation'
|
||||
import Operations from './operations'
|
||||
import Range from './models/range'
|
||||
import Schema from './models/schema'
|
||||
import Schema, { SchemaViolations } from './models/schema'
|
||||
import Stack from './models/stack'
|
||||
import Text from './models/text'
|
||||
import Value from './models/value'
|
||||
@@ -39,6 +39,7 @@ export {
|
||||
Operations,
|
||||
Range,
|
||||
Schema,
|
||||
SchemaViolations,
|
||||
Stack,
|
||||
Text,
|
||||
Value,
|
||||
@@ -61,6 +62,7 @@ export default {
|
||||
Operations,
|
||||
Range,
|
||||
Schema,
|
||||
SchemaViolations,
|
||||
Stack,
|
||||
Text,
|
||||
Value,
|
||||
|
@@ -31,6 +31,23 @@ 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.
|
||||
*
|
||||
|
@@ -1,5 +1,6 @@
|
||||
/** @jsx h */
|
||||
|
||||
import { SchemaViolations } from '../../..'
|
||||
import h from '../../helpers/h'
|
||||
|
||||
export const schema = {
|
||||
@@ -11,7 +12,7 @@ export const schema = {
|
||||
{ objects: ['block'], types: ['paragraph'], min: 1 }
|
||||
],
|
||||
normalize: (change, reason, { node, child }) => {
|
||||
if (reason == 'child_object_invalid') {
|
||||
if (reason == SchemaViolations.ChildObjectInvalid) {
|
||||
change.wrapBlockByKey(child.key, 'paragraph')
|
||||
}
|
||||
}
|
||||
|
@@ -1,5 +1,6 @@
|
||||
/** @jsx h */
|
||||
|
||||
import { SchemaViolations } from '../../..'
|
||||
import h from '../../helpers/h'
|
||||
|
||||
export const schema = {
|
||||
@@ -10,7 +11,7 @@ export const schema = {
|
||||
{ objects: ['block'] },
|
||||
],
|
||||
normalize: (change, reason, { child }) => {
|
||||
if (reason == 'child_object_invalid') {
|
||||
if (reason == SchemaViolations.ChildObjectInvalid) {
|
||||
change.wrapBlockByKey(child.key, 'paragraph')
|
||||
}
|
||||
}
|
||||
|
@@ -1,5 +1,6 @@
|
||||
/** @jsx h */
|
||||
|
||||
import { SchemaViolations } from '../../..'
|
||||
import h from '../../helpers/h'
|
||||
|
||||
export const schema = {
|
||||
@@ -10,7 +11,7 @@ export const schema = {
|
||||
{ types: ['paragraph'], min: 2 },
|
||||
],
|
||||
normalize: (change, reason, { node, index }) => {
|
||||
if (reason == 'child_required') {
|
||||
if (reason == SchemaViolations.ChildRequired) {
|
||||
change.insertNodeByKey(node.key, index, { object: 'block', type: 'paragraph' })
|
||||
}
|
||||
}
|
||||
|
@@ -1,5 +1,6 @@
|
||||
/** @jsx h */
|
||||
|
||||
import { SchemaViolations } from '../../..'
|
||||
import h from '../../helpers/h'
|
||||
|
||||
export const schema = {
|
||||
@@ -10,7 +11,7 @@ export const schema = {
|
||||
{ types: ['paragraph'] },
|
||||
],
|
||||
normalize: (change, reason, { child }) => {
|
||||
if (reason == 'child_type_invalid') {
|
||||
if (reason == SchemaViolations.ChildTypeInvalid) {
|
||||
change.wrapBlockByKey(child.key, 'paragraph')
|
||||
}
|
||||
}
|
||||
|
@@ -1,5 +1,6 @@
|
||||
/** @jsx h */
|
||||
|
||||
import { SchemaViolations } from '../../..'
|
||||
import h from '../../helpers/h'
|
||||
|
||||
export const schema = {
|
||||
@@ -10,7 +11,7 @@ export const schema = {
|
||||
{ types: ['paragraph'], max: 1 },
|
||||
],
|
||||
normalize: (change, reason, { node, child, index }) => {
|
||||
if (reason == 'child_unknown') {
|
||||
if (reason == SchemaViolations.ChildUnknown) {
|
||||
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,5 +1,6 @@
|
||||
/** @jsx h */
|
||||
|
||||
import { SchemaViolations } from '../../..'
|
||||
import h from '../../helpers/h'
|
||||
|
||||
export const schema = {
|
||||
@@ -8,7 +9,7 @@ export const schema = {
|
||||
quote: {
|
||||
first: { objects: ['block'] },
|
||||
normalize: (change, reason, { child }) => {
|
||||
if (reason == 'first_child_object_invalid') {
|
||||
if (reason == SchemaViolations.FirstChildObjectInvalid) {
|
||||
change.wrapBlockByKey(child.key, 'paragraph')
|
||||
}
|
||||
}
|
||||
|
@@ -1,5 +1,6 @@
|
||||
/** @jsx h */
|
||||
|
||||
import { SchemaViolations } from '../../..'
|
||||
import h from '../../helpers/h'
|
||||
|
||||
export const schema = {
|
||||
@@ -8,7 +9,7 @@ export const schema = {
|
||||
quote: {
|
||||
first: { types: ['paragraph'] },
|
||||
normalize: (change, reason, { child }) => {
|
||||
if (reason == 'first_child_type_invalid') {
|
||||
if (reason == SchemaViolations.FirstChildTypeInvalid) {
|
||||
change.wrapBlockByKey(child.key, 'paragraph')
|
||||
}
|
||||
}
|
||||
|
@@ -1,5 +1,6 @@
|
||||
/** @jsx h */
|
||||
|
||||
import { SchemaViolations } from '../../..'
|
||||
import h from '../../helpers/h'
|
||||
|
||||
export const schema = {
|
||||
@@ -8,7 +9,7 @@ export const schema = {
|
||||
quote: {
|
||||
last: { objects: ['block'] },
|
||||
normalize: (change, reason, { child }) => {
|
||||
if (reason == 'last_child_object_invalid') {
|
||||
if (reason == SchemaViolations.LastChildObjectInvalid) {
|
||||
change.wrapBlockByKey(child.key, 'paragraph')
|
||||
}
|
||||
}
|
||||
|
@@ -1,5 +1,6 @@
|
||||
/** @jsx h */
|
||||
|
||||
import { SchemaViolations } from '../../..'
|
||||
import h from '../../helpers/h'
|
||||
|
||||
export const schema = {
|
||||
@@ -8,7 +9,7 @@ export const schema = {
|
||||
quote: {
|
||||
last: { types: ['paragraph'] },
|
||||
normalize: (change, reason, { child }) => {
|
||||
if (reason == 'last_child_type_invalid') {
|
||||
if (reason == SchemaViolations.LastChildTypeInvalid) {
|
||||
change.wrapBlockByKey(child.key, 'paragraph')
|
||||
}
|
||||
}
|
||||
|
@@ -1,5 +1,6 @@
|
||||
/** @jsx h */
|
||||
|
||||
import { SchemaViolations } from '../../..'
|
||||
import h from '../../helpers/h'
|
||||
|
||||
export const schema = {
|
||||
@@ -9,7 +10,7 @@ export const schema = {
|
||||
thing: v => v == 'value'
|
||||
},
|
||||
normalize: (change, reason, { node, key }) => {
|
||||
if (reason == 'node_data_invalid') {
|
||||
if (reason == SchemaViolations.NodeDataInvalid) {
|
||||
change.setNodeByKey(node.key, { data: { thing: 'value' }})
|
||||
}
|
||||
}
|
||||
|
@@ -1,5 +1,6 @@
|
||||
/** @jsx h */
|
||||
|
||||
import { SchemaViolations } from '../../..'
|
||||
import h from '../../helpers/h'
|
||||
|
||||
export const schema = {
|
||||
@@ -7,7 +8,7 @@ export const schema = {
|
||||
paragraph: {
|
||||
isVoid: false,
|
||||
normalize: (change, reason, { node }) => {
|
||||
if (reason == 'node_is_void_invalid') {
|
||||
if (reason == SchemaViolations.NodeIsVoidInvalid) {
|
||||
change.removeNodeByKey(node.key, 'paragraph')
|
||||
}
|
||||
}
|
||||
|
@@ -1,5 +1,6 @@
|
||||
/** @jsx h */
|
||||
|
||||
import { SchemaViolations } from '../../..'
|
||||
import h from '../../helpers/h'
|
||||
|
||||
export const schema = {
|
||||
@@ -7,7 +8,7 @@ export const schema = {
|
||||
paragraph: {
|
||||
marks: [{ type: 'bold' }],
|
||||
normalize: (change, reason, { node }) => {
|
||||
if (reason == 'node_mark_invalid') {
|
||||
if (reason == SchemaViolations.NodeMarkInvalid) {
|
||||
node.nodes.forEach(n => change.removeNodeByKey(n.key))
|
||||
}
|
||||
}
|
||||
|
@@ -1,5 +1,6 @@
|
||||
/** @jsx h */
|
||||
|
||||
import { SchemaViolations } from '../../..'
|
||||
import h from '../../helpers/h'
|
||||
|
||||
export const schema = {
|
||||
@@ -7,7 +8,7 @@ export const schema = {
|
||||
paragraph: {
|
||||
text: /^\d*$/,
|
||||
normalize: (change, reason, { node }) => {
|
||||
if (reason == 'node_text_invalid') {
|
||||
if (reason == SchemaViolations.NodeTextInvalid) {
|
||||
node.nodes.forEach(n => change.removeNodeByKey(n.key))
|
||||
}
|
||||
}
|
||||
|
@@ -1,5 +1,6 @@
|
||||
/** @jsx h */
|
||||
|
||||
import { SchemaViolations } from '../../..'
|
||||
import h from '../../helpers/h'
|
||||
|
||||
export const schema = {
|
||||
@@ -7,7 +8,7 @@ export const schema = {
|
||||
link: {
|
||||
parent: { objects: ['block'] },
|
||||
normalize: (change, reason, { node }) => {
|
||||
if (reason == 'parent_object_invalid') {
|
||||
if (reason == SchemaViolations.ParentObjectInvalid) {
|
||||
change.unwrapNodeByKey(node.key)
|
||||
}
|
||||
}
|
||||
|
@@ -1,5 +1,6 @@
|
||||
/** @jsx h */
|
||||
|
||||
import { SchemaViolations } from '../../..'
|
||||
import h from '../../helpers/h'
|
||||
|
||||
export const schema = {
|
||||
@@ -8,7 +9,7 @@ export const schema = {
|
||||
item: {
|
||||
parent: { types: ['list'] },
|
||||
normalize: (change, reason, { node }) => {
|
||||
if (reason == 'parent_type_invalid') {
|
||||
if (reason == SchemaViolations.ParentTypeInvalid) {
|
||||
change.wrapBlockByKey(node.key, 'list')
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user