-
Notifications
You must be signed in to change notification settings - Fork 394
/
Document.ts
261 lines (217 loc) · 6.82 KB
/
Document.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
import {uniq, camelCase} from 'lodash'
import {SchemaType} from '@sanity/types'
import {ChildResolver} from './ChildResolver'
import {SerializeOptions, Serializable, Child, DocumentNode, EditorNode} from './StructureNodes'
import {SerializeError, HELP_URL} from './SerializeError'
import {validateId} from './util/validateId'
import {ViewBuilder, maybeSerializeView} from './views/View'
import {form} from './views'
import type {StructureContext, View} from './types'
const createDocumentChildResolver =
({resolveDocumentNode}: StructureContext): ChildResolver =>
(itemId, {params, path}) => {
const {type} = params
const parentPath = path.slice(0, path.length - 1)
const currentSegment = path[path.length - 1]
if (!type) {
throw new SerializeError(
`Invalid link. Your link must contain a \`type\`.`,
parentPath,
currentSegment
)
}
return resolveDocumentNode({documentId: itemId, schemaType: type})
}
export interface DocumentOptions {
id: string
type: string
template?: string
templateParameters?: Record<string, unknown>
}
export type PartialDocumentNode = {
id?: string
title?: string
child?: Child
views?: (View | ViewBuilder)[]
options?: Partial<DocumentOptions>
}
export class DocumentBuilder implements Serializable<DocumentNode> {
protected spec: PartialDocumentNode
constructor(protected _context: StructureContext, spec?: PartialDocumentNode) {
this.spec = spec ? spec : {}
}
id(id: string): DocumentBuilder {
return this.clone({id})
}
getId(): PartialDocumentNode['id'] {
return this.spec.id
}
title(title: string): DocumentBuilder {
return this.clone({title, id: this.spec.id || camelCase(title)})
}
getTitle(): PartialDocumentNode['title'] {
return this.spec.title
}
child(child: Child): DocumentBuilder {
return this.clone({child})
}
getChild(): PartialDocumentNode['child'] {
return this.spec.child
}
documentId(documentId: string): DocumentBuilder {
// Let's try to be a bit helpful and assign an ID from document ID if none is specified
const paneId = this.spec.id || documentId
return this.clone({
id: paneId,
options: {
...(this.spec.options || {}),
id: documentId,
},
})
}
getDocumentId(): Partial<DocumentOptions>['id'] {
return this.spec.options?.id
}
schemaType(documentType: SchemaType | string): DocumentBuilder {
return this.clone({
options: {
...(this.spec.options || {}),
type: typeof documentType === 'string' ? documentType : documentType.name,
},
})
}
getSchemaType(): Partial<DocumentOptions>['type'] {
return this.spec.options?.type
}
initialValueTemplate(templateId: string, parameters?: Record<string, unknown>): DocumentBuilder {
return this.clone({
options: {
...(this.spec.options || {}),
template: templateId,
templateParameters: parameters,
},
})
}
getInitialValueTemplate(): Partial<DocumentOptions>['template'] {
return this.spec.options?.template
}
getInitialValueTemplateParameters(): Partial<DocumentOptions>['templateParameters'] {
return this.spec.options?.templateParameters
}
views(views: (View | ViewBuilder)[]): DocumentBuilder {
return this.clone({views})
}
getViews(): (View | ViewBuilder)[] {
return this.spec.views || []
}
serialize({path = [], index, hint}: SerializeOptions = {path: []}): DocumentNode {
const urlId = path[index || path.length - 1]
// Try to grab document ID / editor ID from URL if not defined
const id = this.spec.id || (urlId && `${urlId}`) || ''
const options: Partial<DocumentOptions> = {
id,
type: undefined,
template: undefined,
templateParameters: undefined,
...this.spec.options,
}
if (typeof id !== 'string' || !id) {
throw new SerializeError(
'`id` is required for document nodes',
path,
index,
hint
).withHelpUrl(HELP_URL.ID_REQUIRED)
}
if (!options || !options.id) {
throw new SerializeError(
'document id (`id`) is required for document nodes',
path,
id,
hint
).withHelpUrl(HELP_URL.DOCUMENT_ID_REQUIRED)
}
if (!options || !options.type) {
throw new SerializeError(
'document type (`schemaType`) is required for document nodes',
path,
id,
hint
)
}
const views = (this.spec.views && this.spec.views.length > 0 ? this.spec.views : [form()]).map(
(item, i) => maybeSerializeView(item, i, path)
)
const viewIds = views.map((view) => view.id)
const dupes = uniq(viewIds.filter((viewId, i) => viewIds.includes(viewId, i + 1)))
if (dupes.length > 0) {
throw new SerializeError(
`document node has views with duplicate IDs: ${dupes.join(', ')}`,
path,
id,
hint
)
}
return {
...this.spec,
child: this.spec.child || createDocumentChildResolver(this._context),
id: validateId(id, path, index),
type: 'document',
options: getDocumentOptions(options),
views,
}
}
clone(withSpec: PartialDocumentNode = {}): DocumentBuilder {
const builder = new DocumentBuilder(this._context)
const options = {...(this.spec.options || {}), ...(withSpec.options || {})}
builder.spec = {...this.spec, ...withSpec, options}
return builder
}
}
function getDocumentOptions(spec: Partial<DocumentOptions>): DocumentOptions {
const opts: DocumentOptions = {
id: spec.id || '',
type: spec.type || '*',
}
if (spec.template) {
opts.template = spec.template
}
if (spec.templateParameters) {
opts.templateParameters = spec.templateParameters
}
return opts
}
export function documentFromEditor(context: StructureContext, spec?: EditorNode): DocumentBuilder {
let doc = spec?.type
? // Use user-defined document fragment as base if possible
context.resolveDocumentNode({schemaType: spec.type})
: // Fall back to plain old document builder
new DocumentBuilder(context)
if (!spec) return doc
const {id, type, template, templateParameters} = spec.options
doc = doc.id(spec.id).documentId(id)
if (type) {
doc = doc.schemaType(type)
}
if (template) {
doc = doc.initialValueTemplate(template, templateParameters)
}
if (spec.child) {
doc = doc.child(spec.child)
}
return doc
}
export function documentFromEditorWithInitialValue(
{resolveDocumentNode, templates}: StructureContext,
templateId: string,
parameters?: Record<string, unknown>
): DocumentBuilder {
const template = templates.find((t) => t.id === templateId)
if (!template) {
throw new Error(`Template with ID "${templateId}" not defined`)
}
return resolveDocumentNode({schemaType: template.schemaType}).initialValueTemplate(
templateId,
parameters
)
}