@@ -7,7 +7,7 @@ import {save, restore} from '../lib/persist'
7
7
8
8
import sourceSchemas from '../schemas'
9
9
import Schema from '@sanity/schema'
10
- import { createFormBuilder } from '../../../src'
10
+ import { FormBuilder } from '../../../src'
11
11
import { parseParams , preventDefault } from '../lib/utils'
12
12
13
13
import MyCustomLatLonInput from './custom/MyCustomLatLonInput'
@@ -20,6 +20,7 @@ import resolveReferenceInput from './custom/resolveReferenceInput'
20
20
import { arrayToJSONMatchPath } from '@sanity/mutator'
21
21
import InputWithCustomState from './custom/InputWithCustomState'
22
22
import { set , unset } from '../../../src/utils/patches'
23
+ import { resolvePreviewComponent } from '../../../src/defaultConfig'
23
24
24
25
const SCHEMA_NAMES = Object . keys ( sourceSchemas )
25
26
const params = parseParams ( document . location . pathname )
@@ -42,36 +43,34 @@ function logPatch(patch) {
42
43
)
43
44
}
44
45
45
- const FormBuilder = schema && createFormBuilder ( {
46
- schema : schema ,
47
- resolveInputComponent ( type ) {
48
- if ( type . component ) {
49
- return type . component
50
- }
51
- if ( type . name === 'latlon' ) {
52
- return MyCustomLatLonInput
53
- }
54
- if ( type . name === 'reference' ) {
55
- return resolveReferenceInput ( type )
56
- }
57
- if ( type . name === 'image' ) {
58
- return MyCustomImageInput
59
- }
60
- if ( type . name === 'customPatchHandlingExampleType' ) {
61
- return InputWithCustomState
62
- }
63
- if ( type . name === 'file' ) {
64
- return MyCustomFileInput
65
- }
66
- if ( type . name === 'slug' ) {
67
- return MyCustomSlugInput
68
- }
69
- return type . inputComponent || undefined // signal to use default
70
- } ,
71
- resolveValidationComponent ( ) {
72
- return MyCustomValidationList
46
+ function resolveInputComponent ( type ) {
47
+ if ( type . component ) {
48
+ return type . component
49
+ }
50
+ if ( type . name === 'latlon' ) {
51
+ return MyCustomLatLonInput
52
+ }
53
+ if ( type . name === 'reference' ) {
54
+ return resolveReferenceInput ( type )
55
+ }
56
+ if ( type . name === 'image' ) {
57
+ return MyCustomImageInput
58
+ }
59
+ if ( type . name === 'customPatchHandlingExampleType' ) {
60
+ return InputWithCustomState
73
61
}
74
- } )
62
+ if ( type . name === 'file' ) {
63
+ return MyCustomFileInput
64
+ }
65
+ if ( type . name === 'slug' ) {
66
+ return MyCustomSlugInput
67
+ }
68
+ return type . inputComponent || undefined // signal to use default
69
+ }
70
+
71
+ function resolveValidationComponent ( ) {
72
+ return MyCustomValidationList
73
+ }
75
74
76
75
export default class Main extends React . Component {
77
76
state = {
@@ -80,6 +79,8 @@ export default class Main extends React.Component {
80
79
saved : false
81
80
}
82
81
82
+ patchChannel = FormBuilder . createPatchChannel ( )
83
+
83
84
componentDidUpdate ( prevProps , prevState ) {
84
85
if ( prevState . inspect !== this . state . inspect ) {
85
86
document . body . style . overflow = this . state . inspect === 'fullscreen' ? 'hidden' : ''
@@ -92,7 +93,7 @@ export default class Main extends React.Component {
92
93
93
94
receivePatches ( patches ) {
94
95
const nextValue = patches . reduce ( ( prev , patch ) => applyPatch ( prev , patch ) , this . state . value )
95
- FormBuilder . receivePatches ( { patches, snapshot : nextValue } )
96
+ this . patchChannel . receivePatches ( { patches, snapshot : nextValue } )
96
97
97
98
this . setState ( {
98
99
value : nextValue ,
@@ -105,15 +106,19 @@ export default class Main extends React.Component {
105
106
save ( PERSISTKEY , this . state . value )
106
107
this . setState ( { saved : true } )
107
108
}
109
+
108
110
cmdLog ( event ) {
109
111
console . log ( this . state . value ) // eslint-disable-line no-console
110
112
}
113
+
111
114
cmdClear ( event ) {
112
115
this . receivePatches ( [ unset ( ) ] )
113
116
}
117
+
114
118
cmdRevert ( event ) {
115
119
this . receivePatches ( [ set ( restore ( PERSISTKEY ) ) ] )
116
120
}
121
+
117
122
cmdInspectLive ( event ) {
118
123
this . setState ( { inspect : event . currentTarget . checked ? 'docked' : false } )
119
124
}
@@ -163,10 +168,12 @@ export default class Main extends React.Component {
163
168
< div className = { styles [ inspect === 'docked' ? 'inspectPane' : 'inspectPaneFullScreen' ] } >
164
169
< button className = { styles . closeInspectPaneButton } onClick = { ( ) => this . setState ( { inspect : false } ) } > x</ button >
165
170
{ inspect === 'docked' && (
166
- < button className = { styles . fullscreenInspectPaneButton } onClick = { ( ) => this . setState ( { inspect : 'fullscreen' } ) } > ↑</ button >
171
+ < button className = { styles . fullscreenInspectPaneButton } onClick = { ( ) => this . setState ( { inspect : 'fullscreen' } ) } >
172
+ ↑</ button >
167
173
) }
168
174
{ inspect === 'fullscreen' && (
169
- < button className = { styles . dockedInspectPaneButton } onClick = { ( ) => this . setState ( { inspect : 'docked' } ) } > ↓</ button >
175
+ < button className = { styles . dockedInspectPaneButton } onClick = { ( ) => this . setState ( { inspect : 'docked' } ) } >
176
+ ↓</ button >
170
177
) }
171
178
< div className = { styles [ inspect === 'docked' ? 'inspectPaneInner' : 'inspectPaneInnerFullScreen' ] } >
172
179
< Inspector inspect = { value } />
@@ -190,6 +197,8 @@ export default class Main extends React.Component {
190
197
< form onSubmit = { preventDefault } >
191
198
< FormBuilder
192
199
patchChannel = { this . patchChannel }
200
+ resolveInputComponent = { resolveInputComponent }
201
+ resolvePreviewComponent = { resolvePreviewComponent }
193
202
value = { value }
194
203
type = { schemaType }
195
204
validation = { validation }
0 commit comments