From c323b12816175654d3e42cdbeabedaf6f8307473 Mon Sep 17 00:00:00 2001 From: Per-Kristian Nordnes Date: Tue, 20 Dec 2022 11:38:46 +0100 Subject: [PATCH] feature(schema): backward compatibility for breaking changes in block schema --- .../src/sanity/validation/types/block.ts | 44 ++++++++++++++++++- 1 file changed, 42 insertions(+), 2 deletions(-) diff --git a/packages/@sanity/schema/src/sanity/validation/types/block.ts b/packages/@sanity/schema/src/sanity/validation/types/block.ts index 54c6e14a62a..59efb9bf187 100644 --- a/packages/@sanity/schema/src/sanity/validation/types/block.ts +++ b/packages/@sanity/schema/src/sanity/validation/types/block.ts @@ -18,8 +18,8 @@ const allowedKeys = [ 'validation', ] const allowedMarkKeys = ['decorators', 'annotations'] -const allowedStyleKeys = ['title', 'value', 'component'] -const allowedDecoratorKeys = ['title', 'value', 'icon', 'component'] +const allowedStyleKeys = ['blockEditor', 'title', 'value', 'component'] +const allowedDecoratorKeys = ['blockEditor', 'title', 'value', 'icon', 'component'] const allowedListKeys = ['title', 'value', 'icon', 'component'] export default function validateBlockType(typeDef, visitorContext) { @@ -94,6 +94,12 @@ function validateMarks(marks, visitorContext, problems) { error(`"marks.decorators" declaration should be an array, got ${getTypeOf(decorators)}`) ) } else if (decorators) { + decorators + .filter((dec) => !!dec.blockEditor) + .forEach((dec) => { + dec.icon = dec.blockEditor.icon + dec.component = dec.blockEditor.render + }) decorators = validateDecorators(decorators, visitorContext, problems) } @@ -188,6 +194,15 @@ function validateStyles(styles, visitorContext, problems) { } else if (!style.title) { problems.push(warning(`Style ${name} is missing recommended "title" property`)) } + if (typeof style.blockEditor !== 'undefined') { + problems.push( + warning( + `Style has deprecated key "blockEditor", please refer to the documentation on how to configure the block type for version 3.` + ) + ) + // TODO remove this backward compatibility at some point. + style.component = style.component || style.blockEditor.render + } }) return styles } @@ -227,6 +242,16 @@ function validateDecorators(decorators, visitorContext, problems) { } else if (!decorator.title) { problems.push(warning(`Decorator ${name} is missing recommended "title" property`)) } + if (typeof decorator.blockEditor !== 'undefined') { + problems.push( + warning( + `Decorator "${name}" has deprecated key "blockEditor", please refer to the documentation on how to configure the block type for version 3.` + ) + ) + // TODO remove this backward compatibility at some point. + decorator.icon = decorator.icon || decorator.blockEditor.icon + decorator.component = decorator.component || decorator.blockEditor.render + } }) return decorators } @@ -250,6 +275,21 @@ function validateAnnotations(annotations, visitorContext, problems) { ) } + if (typeof annotation.blockEditor !== 'undefined') { + problems.push( + warning( + `Annotation has deprecated key "blockEditor", please refer to the documentation on how to configure the block type for version 3.` + ) + ) + // TODO remove this backward compatibility at some point. + annotation.icon = annotation.icon || annotation.blockEditor.icon + if (annotation.blockEditor?.render && !annotation.components?.annotation) { + annotation.components = annotation.components || {} + annotation.components.annotation = + annotation.components.annotation || annotation.blockEditor.render + } + } + return {...annotation, _problems} }) }