From cba6be7c24443624af1b85ffaa0835a6806b1050 Mon Sep 17 00:00:00 2001 From: Ian Storm Taylor Date: Mon, 2 Dec 2019 11:51:53 -0500 Subject: [PATCH] fix child_* schema errors to reference child, closes #3197 --- packages/slate-schema/src/checkers.ts | 17 +++++++---------- packages/slate-schema/src/errors.ts | 13 +------------ packages/slate-schema/src/with-schema.ts | 17 +++++++++-------- 3 files changed, 17 insertions(+), 30 deletions(-) diff --git a/packages/slate-schema/src/checkers.ts b/packages/slate-schema/src/checkers.ts index 5ac56ba20..9a9d2572f 100644 --- a/packages/slate-schema/src/checkers.ts +++ b/packages/slate-schema/src/checkers.ts @@ -181,9 +181,8 @@ export const checkAncestor = ( } else { return { code: 'child_max_invalid', - node: parent, - path: parentPath, - index, + node: child, + path: childPath, count, max: group.max, } @@ -196,9 +195,8 @@ export const checkAncestor = ( if (group.min != null && count <= group.min) { return { code: 'child_min_invalid', - node: parent, - path: parentPath, - index, + node: child, + path: childPath, count, min: group.min, } @@ -233,16 +231,15 @@ export const checkAncestor = ( if (nc && Editor.isMatch(editor, [child, childPath], nc.match || {})) { return { code: 'child_min_invalid', - node: parent, - path: parentPath, - index, + node: child, + path: childPath, count, min: group.min, } } } - return { code: 'child_invalid', node: child, path: childPath, index } + return { code: 'child_invalid', node: child, path: childPath } } } diff --git a/packages/slate-schema/src/errors.ts b/packages/slate-schema/src/errors.ts index ff1059cf7..e0d97a9d3 100644 --- a/packages/slate-schema/src/errors.ts +++ b/packages/slate-schema/src/errors.ts @@ -4,34 +4,24 @@ export interface ChildInvalidError { code: 'child_invalid' node: Descendant path: Path - index: number } export interface ChildMaxInvalidError { code: 'child_max_invalid' node: Descendant path: Path - index: number count: number max: number } export interface ChildMinInvalidError { code: 'child_min_invalid' - node: Descendant + node: Descendant | undefined path: Path - index: number count: number min: number } -export interface ChildUnexpectedError { - code: 'child_unexpected' - node: Descendant - path: Path - index: number -} - export interface FirstChildInvalidError { code: 'first_child_invalid' node: Descendant @@ -102,7 +92,6 @@ export type NodeError = | ChildInvalidError | ChildMaxInvalidError | ChildMinInvalidError - | ChildUnexpectedError | FirstChildInvalidError | LastChildInvalidError | MarkInvalidError diff --git a/packages/slate-schema/src/with-schema.ts b/packages/slate-schema/src/with-schema.ts index 8c519257a..882b66424 100644 --- a/packages/slate-schema/src/with-schema.ts +++ b/packages/slate-schema/src/with-schema.ts @@ -92,12 +92,13 @@ export const withSchema = ( } case 'child_max_invalid': { - const { node, path, index } = error + const { path } = error + const [parent, parentPath] = Editor.parent(editor, path) - if (node.children.length === 1 && path.length !== 0) { - Editor.removeNodes(editor, { at: path }) + if (parent.children.length === 1 && parentPath.length !== 0) { + Editor.removeNodes(editor, { at: parentPath }) } else { - Editor.removeNodes(editor, { at: path.concat(index) }) + Editor.removeNodes(editor, { at: path }) } break @@ -105,22 +106,22 @@ export const withSchema = ( case 'child_min_invalid': { const { path } = error + const [, parentPath] = Editor.parent(editor, path) - if (path.length === 0) { - const range = Editor.range(editor, path) + if (parentPath.length === 0) { + const range = Editor.range(editor, parentPath) Editor.removeNodes(editor, { at: range, match: ([, p]) => p.length === 1, }) } else { - Editor.removeNodes(editor, { at: path }) + Editor.removeNodes(editor, { at: parentPath }) } break } case 'child_invalid': - case 'child_unexpected': case 'next_sibling_invalid': case 'node_property_invalid': case 'node_text_invalid':