@tinacms/app 1.2.7 → 1.2.9

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.
@@ -1,338 +0,0 @@
1
- /**
2
-
3
- */
4
- import { Client, Field, Form, FormOptions, TinaCMS } from 'tinacms'
5
- import { assign, createMachine, sendParent } from 'xstate'
6
- import { resolveForm, Template, TinaFieldEnriched, TinaSchema } from 'tinacms'
7
-
8
- export type FieldType = Field & TinaFieldEnriched
9
- export type FormValues = Record<string, unknown>
10
- export type FormType = Form<FormValues, FieldType>
11
-
12
- export type DataType = Record<string, unknown>
13
-
14
- export type Data = {
15
- _internalValues: object
16
- _internalSys: {
17
- breadcrumbs: string[]
18
- basename: string
19
- filename: string
20
- path: string
21
- extension: string
22
- relativePath: string
23
- title?: string
24
- template: string
25
- collection: {
26
- name: string
27
- slug: string
28
- label: string
29
- path: string
30
- format: string
31
- matches?: string
32
- templates?: object
33
- fields?: object
34
- __typename: string
35
- }
36
- }
37
- }
38
-
39
- type ContextType = {
40
- id: string
41
- data: null | Data
42
- form: null | FormType
43
- cms: TinaCMS
44
- formifyCallback: (args: any, cms: TinaCMS) => Form
45
- }
46
-
47
- export const documentMachine =
48
- /** @xstate-layout N4IgpgJg5mDOIC5QAoC2BDAxgCwJYDswBKAOgNwBdd0AbXALwKgGIIB7Qs-ANzYGswJNFjyFS5KrQZMEBXpnRUOAbQAMAXUSgADm1iVcHLSAAeiAEwAWSyUsA2VQE4AzAHYArABoQAT0QAORxJHEMdLd1d7d3NXWIBfOO9hHAJiLgMpRnwWMAAnXLZckm0aRQAzQtQhDBSxdMk6LKhZHjYFJXw1TSQQXX0O4zMEKxt7JzcvXwtVd2DQu2d-ZwBGVWX-dwTEkHw2CDhjZNE0iWpGpmM+gyMeoctzbz8EVaDLUNdAtfvIze2j1NIuTA6AgTx0emu+EGiEszlUtlcdncdnMkyey3WtlC-mWjmW7ncTlRCSSNWOpDyBVylwhA1uMLhCKRKLRFlxcxC4UiSJikRJIH+Yhp-UMUPpz38j0Q6y2cSAA */
49
- createMachine(
50
- {
51
- // tsTypes: {} as ,
52
- schema: {
53
- context: {} as ContextType,
54
- services: {} as {
55
- initializer: {
56
- data: {
57
- form: FormType | undefined
58
- data: Data
59
- }
60
- }
61
- scanner: {
62
- data: {
63
- data: object
64
- }
65
- }
66
- },
67
- events: {} as
68
- | {
69
- type: 'ADD_LOCATION'
70
- value: string
71
- }
72
- | {
73
- type: 'INIT'
74
- }
75
- | {
76
- type: 'SCAN'
77
- }
78
- | {
79
- type: 'RESCAN'
80
- },
81
- },
82
- id: '(machine)',
83
- initial: 'initializing',
84
- states: {
85
- initializing: {
86
- invoke: {
87
- src: 'initializer',
88
- onDone: [
89
- {
90
- actions: ['storeFormAndData', 'notifyParent'],
91
- target: 'ready',
92
- },
93
- ],
94
- onError: [
95
- {
96
- actions: 'handleError',
97
- target: 'error',
98
- },
99
- ],
100
- },
101
- },
102
- ready: {},
103
- error: {},
104
- },
105
- },
106
- {
107
- actions: {
108
- notifyParent: sendParent((context) => {
109
- return {
110
- type: 'DOCUMENT_READY',
111
- value: context.id,
112
- }
113
- }),
114
- handleError: (_context, event) => {
115
- console.error(event.data)
116
- },
117
- storeFormAndData: assign((context, event) => {
118
- // context.cms.forms.add(event.data.form)
119
- return { ...context, form: event.data.form, data: event.data.data }
120
- }),
121
- },
122
- services: {
123
- initializer: async (context) => {
124
- const tina = context.cms.api.tina as Client
125
- let node: Data
126
- if (context.data) {
127
- node = context.data
128
- } else {
129
- const response = await tina.request<{
130
- node: Data
131
- }>(
132
- `query GetNode($id: String!) {
133
- node(id: $id) {
134
- ...on Document {
135
- _internalValues: _values
136
- _internalSys: _sys {
137
- breadcrumbs
138
- basename
139
- filename
140
- path
141
- extension
142
- relativePath
143
- title
144
- template
145
- collection {
146
- name
147
- slug
148
- label
149
- path
150
- format
151
- matches
152
- templates
153
- fields
154
- __typename
155
- }
156
- __typename
157
- }
158
- }
159
- }
160
- }`,
161
- { variables: { id: context.id } }
162
- )
163
- node = response.node
164
- }
165
- const schema = context.cms.api.tina.schema as TinaSchema
166
- if (!schema) {
167
- throw new Error(`Schema must be provided`)
168
- }
169
- const collection = schema.getCollection(
170
- node._internalSys.collection.name
171
- )
172
- let template: Template
173
- if (collection.templates) {
174
- template = collection.templates.find((template) => {
175
- if (typeof template === 'string') {
176
- throw new Error(`Global templates not supported`)
177
- }
178
- return template.name === node._internalSys.template
179
- }) as Template
180
- } else {
181
- template = collection
182
- }
183
- if (!template) {
184
- throw new Error(
185
- `Unable to find template for node ${node._internalSys.path}`
186
- )
187
- }
188
- const resolvedForm = resolveForm({
189
- collection,
190
- basename: node._internalSys.filename,
191
- schema,
192
- template,
193
- })
194
- const onSubmit = async (payload: Record<string, unknown>) => {
195
- try {
196
- const mutationString = `#graphql
197
- mutation UpdateDocument($collection: String!, $relativePath: String!, $params: DocumentUpdateMutation!) {
198
- updateDocument(collection: $collection, relativePath: $relativePath, params: $params) {
199
- __typename
200
- }
201
- }
202
- `
203
-
204
- await context.cms.api.tina.request(mutationString, {
205
- variables: {
206
- collection: node._internalSys.collection.name,
207
- relativePath: node._internalSys.relativePath,
208
- params: schema.transformPayload(
209
- node._internalSys.collection.name,
210
- payload
211
- ),
212
- },
213
- })
214
- context.cms.alerts.success('Document saved!')
215
- } catch (e) {
216
- context.cms.alerts.error(
217
- 'There was a problem saving your document'
218
- )
219
- console.error(e)
220
- }
221
- }
222
- const formConfig = {
223
- id: context.id,
224
- label:
225
- node._internalSys.title || node._internalSys.collection.label,
226
- initialValues: node._internalValues,
227
- fields: resolvedForm.fields,
228
- onSubmit,
229
- }
230
- const formifyCallback = context.formifyCallback
231
- const form = buildForm(
232
- formConfig,
233
- context.cms,
234
- (args) => {
235
- if (formifyCallback) {
236
- return formifyCallback(args, context.cms)
237
- } else {
238
- return args.createForm(args.formConfig)
239
- }
240
- },
241
- true,
242
- onSubmit
243
- )
244
- return { form, data: node }
245
- },
246
- },
247
- }
248
- )
249
-
250
- type FormCreator = (formConfig: FormOptions<any>) => Form
251
- interface GlobalFormOptions {
252
- icon?: any
253
- layout: 'fullscreen' | 'popup'
254
- }
255
- type GlobalFormCreator = (
256
- formConfig: FormOptions<any>,
257
- options?: GlobalFormOptions
258
- ) => Form
259
- interface GlobalFormOptions {
260
- icon?: any
261
- layout: 'fullscreen' | 'popup'
262
- }
263
- export interface FormifyArgs {
264
- formConfig: FormOptions<any>
265
- createForm: FormCreator
266
- createGlobalForm: FormCreator
267
- skip?: () => void
268
- }
269
-
270
- export type formifyCallback = (args: FormifyArgs, cms: TinaCMS) => Form | void
271
- export type onSubmitArgs = {
272
- /**
273
- * @deprecated queryString is actually a mutation string, use `mutationString` instead
274
- */
275
- queryString: string
276
- mutationString: string
277
- variables: object
278
- }
279
-
280
- export const generateFormCreators = (cms: TinaCMS, showInSidebar?: boolean) => {
281
- const createForm = (formConfig: FormOptions<any, any>) => {
282
- return new Form(formConfig)
283
- }
284
- const createGlobalForm: GlobalFormCreator = (
285
- formConfig,
286
- options?: { icon?: any; layout: 'fullscreen' | 'popup' }
287
- ) => {
288
- const form = new Form({
289
- ...formConfig,
290
- global: { global: true, ...options },
291
- })
292
- return form
293
- }
294
- return { createForm, createGlobalForm }
295
- }
296
-
297
- export const buildForm = (
298
- formConfig: any,
299
- cms: TinaCMS,
300
- formify: formifyCallback,
301
- showInSidebar: boolean = false,
302
- onSubmit?: (args: any) => void
303
- ): FormType | undefined => {
304
- const { createForm, createGlobalForm } = generateFormCreators(
305
- cms,
306
- showInSidebar
307
- )
308
- const SKIPPED = 'SKIPPED'
309
- let form
310
- let skipped
311
- const skip = () => {
312
- skipped = SKIPPED
313
- }
314
- if (skipped) return
315
-
316
- if (formify) {
317
- form = formify(
318
- {
319
- formConfig,
320
- createForm,
321
- createGlobalForm,
322
- skip,
323
- },
324
- cms
325
- )
326
- } else {
327
- form = createForm(formConfig)
328
- }
329
-
330
- if (!(form instanceof Form)) {
331
- if (skipped === SKIPPED) {
332
- return
333
- }
334
- throw new Error('formify must return a form or skip()')
335
- }
336
-
337
- return form
338
- }