/
blockContentTypeFeatures.ts
118 lines (101 loc) · 3.61 KB
/
blockContentTypeFeatures.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import {
ArraySchemaType,
BlockSchemaType,
EnumListProps,
isBlockChildrenObjectField,
isBlockListObjectField,
isBlockSchemaType,
isBlockStyleObjectField,
isObjectSchemaType,
isTitledListValue,
ObjectSchemaType,
SpanSchemaType,
TitledListValue,
} from '@sanity/types'
import {BlockContentFeatures, ResolvedAnnotationType} from '../types'
import {findBlockType} from './findBlockType'
// Helper method for describing a blockContentType's feature set
export default function blockContentFeatures(
blockContentType: ArraySchemaType
): BlockContentFeatures {
if (!blockContentType) {
throw new Error("Parameter 'blockContentType' required")
}
const blockType = blockContentType.of.find(findBlockType)
if (!isBlockSchemaType(blockType)) {
throw new Error("'block' type is not defined in this schema (required).")
}
const ofType = blockType.fields.find(isBlockChildrenObjectField)?.type?.of
if (!ofType) {
throw new Error('No `of` declaration found for blocks `children` field')
}
const spanType = ofType.find((member): member is SpanSchemaType => member.name === 'span')
if (!spanType) {
throw new Error('No `span` type found in `block` schema type `children` definition')
}
const inlineObjectTypes = ofType.filter(
(inlineType): inlineType is ObjectSchemaType =>
inlineType.name !== 'span' && isObjectSchemaType(inlineType)
)
const blockObjectTypes = blockContentType.of.filter(
(memberType): memberType is ObjectSchemaType =>
memberType.name !== blockType.name && isObjectSchemaType(memberType)
)
return {
styles: resolveEnabledStyles(blockType),
decorators: resolveEnabledDecorators(spanType),
annotations: resolveEnabledAnnotationTypes(spanType),
lists: resolveEnabledListItems(blockType),
types: {
block: blockContentType,
span: spanType,
inlineObjects: inlineObjectTypes,
blockObjects: blockObjectTypes,
},
}
}
function resolveEnabledStyles(blockType: BlockSchemaType): TitledListValue<string>[] {
const styleField = blockType.fields.find(isBlockStyleObjectField)
if (!styleField) {
throw new Error("A field with name 'style' is not defined in the block type (required).")
}
const textStyles = getTitledListValuesFromEnumListOptions(styleField.type.options)
if (textStyles.length === 0) {
throw new Error(
'The style fields need at least one style ' +
"defined. I.e: {title: 'Normal', value: 'normal'}."
)
}
return textStyles
}
function resolveEnabledAnnotationTypes(spanType: SpanSchemaType): ResolvedAnnotationType[] {
return spanType.annotations.map((annotation) => ({
title: annotation.title,
type: annotation,
value: annotation.name,
icon: annotation.icon,
}))
}
function resolveEnabledDecorators(spanType: SpanSchemaType): TitledListValue<string>[] {
return spanType.decorators
}
function resolveEnabledListItems(blockType: BlockSchemaType): TitledListValue<string>[] {
const listField = blockType.fields.find(isBlockListObjectField)
if (!listField) {
throw new Error("A field with name 'list' is not defined in the block type (required).")
}
const listItems = getTitledListValuesFromEnumListOptions(listField.type.options)
if (!listItems) {
throw new Error('The list field need at least to be an empty array')
}
return listItems
}
function getTitledListValuesFromEnumListOptions(
options: EnumListProps<string> | undefined
): TitledListValue<string>[] {
const list = options ? options.list : undefined
if (!Array.isArray(list)) {
return []
}
return list.map((item) => (isTitledListValue(item) ? item : {title: item, value: item}))
}