@portabletext/block-tools 2.0.8 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,23 +3,42 @@ import {
3
3
  isBlockListObjectField,
4
4
  isBlockSchemaType,
5
5
  isBlockStyleObjectField,
6
- isObjectSchemaType,
7
6
  isTitledListValue,
8
7
  type ArraySchemaType,
9
8
  type BlockSchemaType,
10
9
  type EnumListProps,
11
- type I18nTitledListValue,
12
- type ObjectSchemaType,
13
10
  type SpanSchemaType,
14
- type TitledListValue,
15
11
  } from '@sanity/types'
16
- import type {BlockContentFeatures, ResolvedAnnotationType} from '../types'
17
12
  import {findBlockType} from './findBlockType'
18
13
 
19
- // Helper method for describing a blockContentType's feature set
20
- export default function blockContentFeatures(
14
+ export type PortableTextSchema = {
15
+ block: {
16
+ name: string
17
+ }
18
+ span: {
19
+ name: string
20
+ }
21
+ styles: ReadonlyArray<{
22
+ name: string
23
+ title?: string
24
+ }>
25
+ lists: ReadonlyArray<{
26
+ name: string
27
+ title?: string
28
+ }>
29
+ decorators: ReadonlyArray<{
30
+ name: string
31
+ title?: string
32
+ }>
33
+ annotations: ReadonlyArray<{
34
+ name: string
35
+ title?: string
36
+ }>
37
+ }
38
+
39
+ export function getPortableTextSchema(
21
40
  blockContentType: ArraySchemaType,
22
- ): BlockContentFeatures {
41
+ ): PortableTextSchema {
23
42
  if (!blockContentType) {
24
43
  throw new Error("Parameter 'blockContentType' required")
25
44
  }
@@ -43,33 +62,30 @@ export default function blockContentFeatures(
43
62
  )
44
63
  }
45
64
 
46
- const inlineObjectTypes = ofType.filter(
47
- (inlineType): inlineType is ObjectSchemaType =>
48
- inlineType.name !== 'span' && isObjectSchemaType(inlineType),
49
- )
65
+ const blockName = blockContentType.of.find(findBlockType)?.name
50
66
 
51
- const blockObjectTypes = blockContentType.of.filter(
52
- (memberType): memberType is ObjectSchemaType =>
53
- memberType.name !== blockType.name && isObjectSchemaType(memberType),
54
- )
67
+ if (!blockName) {
68
+ throw new Error('No `block` type found in schema type')
69
+ }
55
70
 
56
71
  return {
57
72
  styles: resolveEnabledStyles(blockType),
58
73
  decorators: resolveEnabledDecorators(spanType),
59
74
  annotations: resolveEnabledAnnotationTypes(spanType),
60
75
  lists: resolveEnabledListItems(blockType),
61
- types: {
62
- block: blockContentType,
63
- span: spanType,
64
- inlineObjects: inlineObjectTypes,
65
- blockObjects: blockObjectTypes,
76
+ block: {
77
+ name: blockName,
78
+ },
79
+ span: {
80
+ name: spanType.name,
66
81
  },
67
82
  }
68
83
  }
69
84
 
70
- function resolveEnabledStyles(
71
- blockType: BlockSchemaType,
72
- ): TitledListValue<string>[] {
85
+ function resolveEnabledStyles(blockType: BlockSchemaType): ReadonlyArray<{
86
+ name: string
87
+ title?: string
88
+ }> {
73
89
  const styleField = blockType.fields.find(isBlockStyleObjectField)
74
90
  if (!styleField) {
75
91
  throw new Error(
@@ -92,24 +108,30 @@ function resolveEnabledStyles(
92
108
 
93
109
  function resolveEnabledAnnotationTypes(
94
110
  spanType: SpanSchemaType,
95
- ): ResolvedAnnotationType[] {
111
+ ): ReadonlyArray<{
112
+ name: string
113
+ title?: string
114
+ }> {
96
115
  return spanType.annotations.map((annotation) => ({
116
+ name: annotation.name,
97
117
  title: annotation.title,
98
- type: annotation,
99
- value: annotation.name,
100
- icon: annotation.icon,
101
118
  }))
102
119
  }
103
120
 
104
- function resolveEnabledDecorators(
105
- spanType: SpanSchemaType,
106
- ): TitledListValue<string>[] {
107
- return spanType.decorators
121
+ function resolveEnabledDecorators(spanType: SpanSchemaType): ReadonlyArray<{
122
+ name: string
123
+ title?: string
124
+ }> {
125
+ return spanType.decorators.map((decorator) => ({
126
+ name: decorator.value,
127
+ title: decorator.title,
128
+ }))
108
129
  }
109
130
 
110
- function resolveEnabledListItems(
111
- blockType: BlockSchemaType,
112
- ): I18nTitledListValue<string>[] {
131
+ function resolveEnabledListItems(blockType: BlockSchemaType): ReadonlyArray<{
132
+ name: string
133
+ title?: string
134
+ }> {
113
135
  const listField = blockType.fields.find(isBlockListObjectField)
114
136
  if (!listField) {
115
137
  throw new Error(
@@ -129,13 +151,24 @@ function resolveEnabledListItems(
129
151
 
130
152
  function getTitledListValuesFromEnumListOptions(
131
153
  options: EnumListProps<string> | undefined,
132
- ): I18nTitledListValue<string>[] {
154
+ ): ReadonlyArray<{
155
+ name: string
156
+ title?: string
157
+ }> {
133
158
  const list = options ? options.list : undefined
134
159
  if (!Array.isArray(list)) {
135
160
  return []
136
161
  }
137
162
 
138
- return list.map((item) =>
139
- isTitledListValue(item) ? item : {title: item, value: item},
140
- )
163
+ return list.map((item) => {
164
+ if (isTitledListValue(item)) {
165
+ return {
166
+ name: item.value ?? item.title,
167
+ title: item.title,
168
+ }
169
+ }
170
+ return {
171
+ name: item,
172
+ }
173
+ })
141
174
  }