Skip to content

Commit

Permalink
refactor(types): refactor Block schemas for new component extension api
Browse files Browse the repository at this point in the history
  • Loading branch information
skogsmaskin committed Dec 20, 2022
1 parent d5166e5 commit dfd33ce
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 66 deletions.
29 changes: 18 additions & 11 deletions packages/@sanity/types/src/schema/asserters.ts
@@ -1,18 +1,18 @@
import type {CrossDatasetReferenceSchemaType} from '../crossDatasetReference'
import {TitledListValue} from './definition'
import type {
BlockSchemaType,
ArraySchemaType,
ObjectSchemaType,
ReferenceSchemaType,
SpanSchemaType,
BlockChildrenObjectField,
StyleObjectField,
ListObjectField,
BlockListObjectField,
BlockSchemaType,
BlockStyleObjectField,
BooleanSchemaType,
StringSchemaType,
NumberSchemaType,
ObjectSchemaType,
ReferenceSchemaType,
SchemaType,
SpanSchemaType,
StringSchemaType,
} from './types'

function isRecord(value: unknown): value is Record<string, unknown> {
Expand Down Expand Up @@ -56,6 +56,13 @@ export function isArraySchemaType(type: unknown): type is ArraySchemaType {
return type.jsonType === 'array'
}

/** @internal */
export function isArrayOfBlocksSchemaType(
type: unknown
): type is ArraySchemaType<ObjectSchemaType> {
return isArraySchemaType(type) && type.of.some((memberType) => isBlockSchemaType(memberType))
}

/** @internal */
export function isArrayOfObjectsSchemaType(
type: unknown
Expand Down Expand Up @@ -129,20 +136,20 @@ export function isBlockSchemaType(type: unknown): type is BlockSchemaType {
const [maybeSpanChildren, maybeStyle, maybeList] = type.fields
return (
isBlockChildrenObjectField(maybeSpanChildren) &&
isStyleObjectField(maybeStyle) &&
isListObjectField(maybeList)
isBlockStyleObjectField(maybeStyle) &&
isBlockListObjectField(maybeList)
)
}

/** @internal */
export function isStyleObjectField(field: unknown): field is StyleObjectField {
export function isBlockStyleObjectField(field: unknown): field is BlockStyleObjectField {
if (!isRecord(field)) return false
if (field.name !== 'style') return false
return isRecord(field.type) && field.type.jsonType === 'string'
}

/** @internal */
export function isListObjectField(field: unknown): field is ListObjectField {
export function isBlockListObjectField(field: unknown): field is BlockListObjectField {
if (!isRecord(field)) return false
if (field.name !== 'list') return false
return isRecord(field.type) && field.type.jsonType === 'string'
Expand Down
@@ -1,4 +1,4 @@
import {ComponentType} from 'react'
import {ComponentType, ReactNode} from 'react'
import {PreviewConfig} from '../preview'
import {InitialValueProperty, SchemaValidationValue} from '../types'
import {
Expand All @@ -16,7 +16,6 @@ import {
ObjectDefinition,
ReferenceDefinition,
SlugDefinition,
SpanDefinition,
StringDefinition,
TextDefinition,
UrlDefinition,
Expand Down Expand Up @@ -54,7 +53,6 @@ export interface IntrinsicDefinitions {
reference: ReferenceDefinition
crossDatasetReference: CrossDatasetReferenceDefinition
slug: SlugDefinition
span: SpanDefinition
string: StringDefinition
text: TextDefinition
url: UrlDefinition
Expand Down Expand Up @@ -92,6 +90,7 @@ export type SchemaTypeDefinition<TType extends IntrinsicTypeName = IntrinsicType
export interface TypeReference {
type: string
name?: string
icon?: ComponentType | ReactNode
options?: {[key: string]: unknown}
}

Expand All @@ -115,6 +114,8 @@ export interface TypeAliasDefinition<
preview?: PreviewConfig

components?: {
annotation?: ComponentType<any>
block?: ComponentType<any>
diff?: ComponentType<any>
field?: ComponentType<any>
input?: ComponentType<any>
Expand Down
40 changes: 27 additions & 13 deletions packages/@sanity/types/src/schema/definition/type/block.ts
@@ -1,9 +1,9 @@
import {ComponentType, ReactNode} from 'react'
import {RuleDef, ValidationBuilder} from '../../ruleBuilder'
import {InitialValueProperty} from '../../types'
import {SchemaTypeDefinition, TypeReference} from '../schemaDefinition'
import {ArrayOfType} from './array'
import {BaseSchemaDefinition} from './common'
import {ObjectDefinition} from './object'

/** @public */
export interface BlockOptions {
Expand All @@ -15,28 +15,42 @@ export interface BlockOptions {
export interface BlockRule extends RuleDef<BlockRule, any[]> {}

/** @public */
export interface DecoratorDefinition {
export interface BlockDecoratorDefinition {
title: string
value: string
icon?: ReactNode | ComponentType<any>
blockEditor?: {
icon?: ReactNode | ComponentType<any>
render?: ComponentType<any>
}
icon?: ReactNode | ComponentType
}

/** @public */
export interface MarksDefinition {
decorators?: DecoratorDefinition[]
annotations?: (SchemaTypeDefinition | TypeReference)[]
export interface BlockStyleDefinition {
title: string
value: string
}

/** @public */
export interface BlockListDefinition {
title: string
value: string
icon?: ReactNode | ComponentType
}

/** @public */
export interface BlockAnnotationDefinition extends ObjectDefinition {
icon?: ReactNode | ComponentType
}

/** @public */
export interface BlockMarksDefinition {
decorators?: BlockDecoratorDefinition[]
annotations?: ArrayOfType<'object' | 'reference'>[]
}

/** @public */
export interface BlockDefinition extends BaseSchemaDefinition {
type: 'block'
styles?: Array<{title: string; value: string}>
lists?: Array<{title: string; value: string}>
marks?: MarksDefinition
styles?: BlockStyleDefinition[]
lists?: BlockListDefinition[]
marks?: BlockMarksDefinition
of?: ArrayOfType[]
initialValue?: InitialValueProperty<any, any[]>
options?: BlockOptions
Expand Down
1 change: 0 additions & 1 deletion packages/@sanity/types/src/schema/definition/type/index.ts
Expand Up @@ -12,7 +12,6 @@ export * from './number'
export * from './object'
export * from './reference'
export * from './slug'
export * from './span'
export * from './string'
export * from './text'
export * from './url'
Expand Down
13 changes: 0 additions & 13 deletions packages/@sanity/types/src/schema/definition/type/span.ts

This file was deleted.

26 changes: 13 additions & 13 deletions packages/@sanity/types/src/schema/test/block.test.ts
Expand Up @@ -35,13 +35,19 @@ describe('block types', () => {
{
title: 'Sup',
value: 'sup',
blockEditor: {
icon: () => null,
render: (props) => props,
},
icon: () => null,
},
],
annotations: [
{
name: 'authorInline',
title: 'Author',
type: 'reference',
to: {type: 'author'},
},
{type: 'author', initialValue: {}},
{type: 'object', fields: [{name: 'title', type: 'string'}]},
],
annotations: [{name: 'author', title: 'Author', type: 'reference', to: {type: 'author'}}],
},
of: [{type: 'string'}],
options: {
Expand Down Expand Up @@ -70,10 +76,7 @@ describe('block types', () => {
{
title: 'Sup',
value: 'sup',
blockEditor: {
icon: () => null,
render: (props) => props,
},
icon: () => null,
},
],
annotations: [{name: 'author', title: 'Author', type: 'reference', to: {type: 'author'}}],
Expand All @@ -96,10 +99,7 @@ describe('block types', () => {
{
title: 'Sup',
value: 'sup',
blockEditor: {
icon: () => null,
render: (props) => props,
},
icon: () => null,
},
],
annotations: [{name: 'author', title: 'Author', type: 'reference', to: {type: 'author'}}],
Expand Down
28 changes: 16 additions & 12 deletions packages/@sanity/types/src/schema/types.ts
Expand Up @@ -7,6 +7,7 @@ import type {PreviewConfig} from './preview'
import {SchemaTypeDefinition} from './definition/schemaDefinition'
import {ArrayOptions} from './definition/type/array'
import {
BlockDecoratorDefinition,
BlockOptions,
BooleanOptions,
DateOptions,
Expand All @@ -19,7 +20,6 @@ import {
StringOptions,
TextOptions,
} from './definition/type'
import {TitledListValue} from './definition/type/common'

export {defineType, defineField, defineArrayMember, typed} from './define'

Expand Down Expand Up @@ -183,6 +183,8 @@ export interface BaseSchemaType {

/** @beta */
components?: {
block?: ComponentType<any>
annotation?: ComponentType<any>
diff?: ComponentType<any>
field?: ComponentType<any>
input?: ComponentType<any>
Expand Down Expand Up @@ -248,31 +250,33 @@ export type ArraySchemaTypeOf<TSchemaType extends ArraySchemaType['of'][number]>
*
* @internal
*/
export type MarksObjectField = {name: 'marks'} & ObjectField<ArraySchemaTypeOf<StringSchemaType>>
export type SpanMarksObjectField = {name: 'marks'} & ObjectField<
ArraySchemaTypeOf<StringSchemaType>
>

/**
* A specific `ObjectField` for `text` in `SpanSchemaType`
* @see SpanSchemaType
*
* @internal
*/
export type TextObjectField = {name: 'text'} & ObjectField<TextSchemaType>
export type SpanTextObjectField = {name: 'text'} & ObjectField<TextSchemaType>

/**
* A specific `ObjectField` for `style` in `BlockSchemaType`
* @see BlockSchemaType
*
* @internal
*/
export type StyleObjectField = {name: 'style'} & ObjectField<StringSchemaType>
export type BlockStyleObjectField = {name: 'style'} & ObjectField<StringSchemaType>

/**
* A specific `ObjectField` for `list` in `BlockSchemaType`
* @see BlockSchemaType
*
* @internal
*/
export type ListObjectField = {name: 'list'} & ObjectField<StringSchemaType>
export type BlockListObjectField = {name: 'list'} & ObjectField<StringSchemaType>

/**
* The specific `children` field of a `block` type (`BlockSchemaType`)
Expand All @@ -291,15 +295,15 @@ export type BlockChildrenObjectField = {name: 'children'} & ObjectField<ArraySch
*/
export interface SpanSchemaType extends Omit<ObjectSchemaType, 'fields'> {
annotations: (ObjectSchemaType & {
blockEditor?: {
icon?: string | ComponentType
render?: ComponentType
icon?: string | ComponentType
components?: {
item?: ComponentType
}
})[]
decorators: TitledListValue<string>[]
decorators: BlockDecoratorDefinition[]
// the first field will always be the `marks` field and the second will
// always be the `text` field
fields: [MarksObjectField, TextObjectField]
fields: [SpanMarksObjectField, SpanTextObjectField]
}

/**
Expand All @@ -313,8 +317,8 @@ export interface BlockSchemaType extends ObjectSchemaType {
fields: [
// the first 3 field are always block children, styles, and lists
BlockChildrenObjectField,
StyleObjectField,
ListObjectField,
BlockStyleObjectField,
BlockListObjectField,
// then it could be any additional fields the user could add
...ObjectField[]
]
Expand Down

0 comments on commit dfd33ce

Please sign in to comment.