-
Notifications
You must be signed in to change notification settings - Fork 395
/
EditorPane.js
603 lines (541 loc) · 17.6 KB
/
EditorPane.js
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
import PropTypes from 'prop-types'
import React from 'react'
import promiseLatest from 'promise-latest'
import {merge, concat, timer, of as observableOf} from 'rxjs'
import {catchError, switchMap, map, mapTo, tap} from 'rxjs/operators'
import {validateDocument} from '@sanity/validation'
import {omit, throttle, debounce} from 'lodash'
import {FormBuilder, checkoutPair} from 'part:@sanity/form-builder'
import {getDraftId, getPublishedId} from 'part:@sanity/base/util/draft-utils'
import schema from 'part:@sanity/base/schema'
import Button from 'part:@sanity/components/buttons/default'
import historyStore from 'part:@sanity/base/datastore/history'
import client from 'part:@sanity/base/client'
import withDocumentType from '../utils/withDocumentType'
import styles from './styles/EditorWrapper.css'
import Editor from './Editor'
import UseState from '../utils/UseState'
import withInitialValue from '../utils/withInitialValue'
const INITIAL_DOCUMENT_STATE = {
isLoading: true,
deletedSnapshot: null,
snapshot: null
}
const INITIAL_STATE = {
isSaving: true,
isReconnecting: false,
isRestoring: false,
isCreatingDraft: false,
transactionResult: null,
validationPending: true,
draft: INITIAL_DOCUMENT_STATE,
published: INITIAL_DOCUMENT_STATE
}
function documentEventToState(event) {
switch (event.type) {
case 'rebase':
case 'create':
case 'createIfNotExists':
case 'snapshot': {
return {
deletedSnapshot: null,
snapshot: event.document
}
}
case 'mutation': {
return {
deletedSnapshot: event.deletedSnapshot,
snapshot: event.document
? {
...event.document,
// todo: The following line is a temporary workaround for a problem with the mutator not
// setting updatedAt on patches applied optimistic when they are received from server
// can be removed when this is fixed
_updatedAt: new Date().toISOString()
}
: event.document
}
}
case 'reconnect': {
return {}
}
case 'committed': {
// note: we *could* use this in conjunction with <document>.commit()
// by setting this.state.isSaving=true before calling <document>.commit and setting to false
// again when we get the 'committed' event back.
// However, calling <document>.commit() doesn't necessarily result in a commit actually being done,
// and thus we are not guaranteed to get a 'committed' event back after a call to
// <document>.commit(), which means we could easily get into a situation where the
// `isSaving` state stays around forever.
return {}
}
case 'error': {
return {}
}
default: {
// eslint-disable-next-line no-console
console.log('Unhandled document event type "%s"', event.type, event)
return {}
}
}
}
function exists(draft, published) {
return draft.snapshot || published.snapshot
}
function isRecoverable(draft, published) {
return !exists(draft, published) && (draft.deletedSnapshot || published.deletedSnapshot)
}
const withWrappers = Pane => withInitialValue(withDocumentType(Pane))
export default withWrappers(
class EditorPane extends React.Component {
static propTypes = {
title: PropTypes.string,
index: PropTypes.number.isRequired,
initialValue: PropTypes.object, // eslint-disable-line react/forbid-prop-types
options: PropTypes.shape({
id: PropTypes.string.isRequired,
type: PropTypes.string.isRequired,
template: PropTypes.string
}).isRequired
}
static defaultProps = {
title: null,
initialValue: undefined
}
state = INITIAL_STATE
patchChannel = FormBuilder.createPatchChannel()
constructor(props) {
super(props)
this.setup(props.options.id)
}
setup(documentId) {
this.dispose()
const publishedId = getPublishedId(documentId)
const draftId = getDraftId(documentId)
const {published, draft} = checkoutPair({publishedId, draftId})
this.published = published
this.draft = draft
this.validateLatestDocument = debounce(promiseLatest(this.validateDocument, 300))
const published$ = this.published.events
const draft$ = this.draft.events.pipe(tap(this.receiveDraftEvent))
this.subscription = merge(
published$.pipe(map(event => ({...event, version: 'published'}))),
draft$.pipe(map(event => ({...event, version: 'draft'})))
)
.pipe(
switchMap(event =>
event.type === 'reconnect' ? timer(500).pipe(mapTo(event)) : observableOf(event)
),
catchError((err, _caught$) => {
// eslint-disable-next-line no-console
console.error(err)
return observableOf({type: 'error', error: err})
})
)
.subscribe(event => {
this.setState(prevState => {
const version = event.version // either 'draft' or 'published'
return {
isReconnecting: event.type === 'reconnect',
validationPending: true,
error: event.type === 'error' ? event.error : null,
[version]: {
...(prevState[version] || {}),
...documentEventToState(event),
isLoading: false
}
}
}, this.validateLatestDocument)
})
}
validateDocument = async () => {
const {draft, published} = this.state
const doc = (draft && draft.snapshot) || (published && published.snapshot)
if (!doc || !doc._type) {
return []
}
const type = schema.get(doc._type)
if (!type) {
// eslint-disable-next-line no-console
console.warn('Schema for document type "%s" not found, skipping validation')
return []
}
const markers = await validateDocument(doc, schema)
this.setStateIfMounted({markers, validationPending: false})
return markers
}
receiveDraftEvent = event => {
if (event.type !== 'mutation') {
return
}
// Broadcast incoming patches to input components that applies patches on their own
// Note: This is *experimental*
this.patchChannel.receivePatches({
patches: event.patches,
snapshot: event.document
})
}
getDraftId() {
return getDraftId(this.props.options.id)
}
getPublishedId() {
return getPublishedId(this.props.options.id)
}
componentDidUpdate(prevProps) {
if (prevProps.options.id !== this.props.options.id) {
this.setup(this.props.options.id)
}
}
componentDidMount() {
this._isMounted = true
}
componentWillUnmount() {
this._isMounted = false
// Cancel throttled commit since draft will be nulled on unmount
this.commit.cancel()
// Instead, explicitly commit
this.draft.commit().subscribe(() => {
// todo: error handling
})
this.dispose()
}
isLiveEditEnabled() {
const selectedSchemaType = schema.get(this.props.options.type)
return selectedSchemaType.liveEdit === true
}
dispose() {
if (this.subscription) {
this.subscription.unsubscribe()
this.subscription = null
}
if (this.validateLatestDocument) {
this.validateLatestDocument.cancel()
this.validateLatestDocument = null
}
this.published = null
this.draft = null
}
handleDelete = () => {
const documentId = this.props.options.id
const tx = client.observable
.transaction()
.delete(getPublishedId(documentId))
.delete(getDraftId(documentId))
tx.commit()
.pipe(
map(result => ({
type: 'success',
result: result
})),
catchError(error =>
observableOf({
type: 'error',
message: `An error occurred while attempting to delete document.
This usually means that you attempted to delete a document that other documents
refers to.`,
error
})
)
)
.subscribe(result => {
this.setStateIfMounted({transactionResult: result})
})
}
handleClearTransactionResult = () => {
this.setStateIfMounted({transactionResult: null})
}
handleUnpublish = () => {
const documentId = this.props.options.id
const {published} = this.state
let tx = client.observable.transaction().delete(getPublishedId(documentId))
if (published.snapshot) {
tx = tx.createIfNotExists({
...omit(published.snapshot, '_updatedAt'),
_id: getDraftId(documentId)
})
}
tx.commit()
.pipe(
map(result => ({
type: 'success',
result: result
})),
catchError(error =>
observableOf({
type: 'error',
message: `An error occurred while attempting to unpublish document.
This usually means that you attempted to unpublish a document that other documents
refers to.`,
error
})
)
)
.subscribe(result => {
this.setStateIfMounted({transactionResult: result})
})
}
handlePublish = () => {
const documentId = this.props.options.id
const {draft, published} = this.state
this.setState({isPublishing: true})
const tx = client.observable.transaction()
if (!published || !published.snapshot) {
// If the document has not been published, we want to create it - if it suddenly exists
// before being created, we don't want to overwrite if, instead we want to yield an error
tx.create({
...omit(draft.snapshot, '_updatedAt'),
_id: getPublishedId(documentId)
})
} else {
// If it exists already, we only want to update it if the revision on the remote server
// matches what our local state thinks it's at
tx.patch(getPublishedId(documentId), {
// Hack until other mutations support revision locking
unset: ['_reserved_prop_'],
ifRevisionID: published.snapshot._rev
}).createOrReplace({
...omit(draft.snapshot, '_updatedAt'),
_id: getPublishedId(documentId)
})
}
tx.delete(getDraftId(documentId))
// @todo add error handling for revision mismatch
tx.commit()
.pipe(
map(result => ({
type: 'success',
result: result
})),
catchError(error =>
observableOf({
type: 'error',
message: 'An error occurred while attempting to publishing document',
error
})
)
)
.subscribe({
next: result => {
this.setState({
transactionResult: result
})
},
complete: () => {
this.setStateIfMounted({isPublishing: false})
}
})
}
handleRestoreRevision = ({id, rev}) => {
const transactionResult$ = historyStore.restore(id, rev).pipe(
map(result => ({
type: 'success',
result: result
})),
catchError(error =>
observableOf({
type: 'error',
message: 'An error occurred while attempting to restore the document',
error
})
),
map(transactionResult => ({transactionResult}))
)
concat(
observableOf({isRestoring: true}),
transactionResult$,
observableOf({isRestoring: false})
).subscribe(nextState => {
this.setStateIfMounted(nextState)
})
}
handleChange = event => {
const {published, draft} = this.state
const initialValue = this.getInitialValue()
if (this.isLiveEditEnabled()) {
// No drafting, patch and commit the published document
this.published.createIfNotExists({
_id: this.getPublishedId(),
...initialValue
})
this.published.patch(event.patches)
} else {
if (!draft.snapshot) {
this.draft.createIfNotExists({
...omit(published.snapshot, '_updatedAt'),
_id: this.getDraftId(),
...initialValue
})
}
this.draft.patch(event.patches)
}
this.commit()
}
setStateIfMounted = (...args) => {
if (!this._isMounted) {
return
}
this.setState(...args)
}
commit = throttle(
() => {
const currentDoc = this.isLiveEditEnabled() ? this.published : this.draft
this.setStateIfMounted({isSaving: true})
currentDoc.commit().subscribe({
next: () => {
// todo
},
error: error => {
// todo
},
complete: () => {
this.setStateIfMounted({isSaving: false})
}
})
},
1000,
{leading: true, trailing: true}
)
handleRestoreDeleted = () => {
const {draft, published} = this.state
const commits = []
if (draft.deletedSnapshot) {
this.draft.createIfNotExists(draft.deletedSnapshot)
commits.push(this.draft.commit())
} else if (published.deletedSnapshot) {
this.published.createIfNotExists(published.deletedSnapshot)
commits.push(this.published.commit())
}
commits.forEach(c => {
c.subscribe({
next: () => {}
})
})
}
renderDeleted() {
return (
<div className={styles.deletedDocument}>
<div className={styles.deletedDocumentInner}>
<h3>This document just got deleted</h3>
<p>You can undo deleting it until you close this window/tab</p>
<Button onClick={this.handleRestoreDeleted}>Undo delete</Button>
</div>
</div>
)
}
renderError(error) {
return (
<div className={styles.error}>
<div className={styles.errorInner}>
<h3>We’re sorry, but your changes could not be applied.</h3>
<UseState startWith={false}>
{([isExpanded, setExpanded]) => (
<>
<Button onClick={() => this.setup(this.props.options.id)}>Reload</Button>
<Button inverted onClick={() => setExpanded(!isExpanded)}>
{isExpanded ? 'Hide' : 'Show'} details
</Button>
<div>
{isExpanded && (
<textarea
className={styles.errorDetails}
onFocus={e => e.currentTarget.select()}
value={error.stack}
/>
)}
</div>
</>
)}
</UseState>
</div>
</div>
)
}
renderUnknownSchemaType() {
const {options} = this.props
const {draft, published} = this.state
const typeName = options.type
const doc = draft.snapshot || published.snapshot
return (
<div className={styles.unknownSchemaType}>
<div className={styles.unknownSchemaTypeInner}>
<h3>Unknown schema type</h3>
<p>
This document has the schema type <code>{typeName}</code>, which is not defined as a
type in the local content studio schema.
</p>
{__DEV__ && doc && (
<div>
<h4>Here is the JSON representation of the document:</h4>
<pre className={styles.jsonDump}>
<code>{JSON.stringify(doc, null, 2)}</code>
</pre>
</div>
)}
</div>
</div>
)
}
getInitialValue() {
const {draft, published} = this.state
const typeName = this.props.options.type
const base = {_type: typeName}
return exists(draft, published) ? base : {...base, ...this.props.initialValue}
}
render() {
const initialValue = this.getInitialValue()
const {options, index, title} = this.props
const typeName = options.type
const schemaType = schema.get(typeName)
const {
draft,
published,
markers,
isCreatingDraft,
isUnpublishing,
transactionResult,
isPublishing,
isRestoring,
isSaving,
error,
validationPending,
isReconnecting
} = this.state
if (!schemaType) {
return this.renderUnknownSchemaType()
}
if (isRecoverable(draft, published)) {
return this.renderDeleted()
}
if (error) {
return this.renderError(error)
}
return (
<Editor
title={title}
paneIndex={index}
patchChannel={this.patchChannel}
type={schemaType}
published={published.snapshot}
draft={draft.snapshot}
markers={markers}
initialValue={initialValue}
validationPending={validationPending}
isLoading={draft.isLoading || published.isLoading}
isRestoring={isRestoring}
isSaving={isSaving}
isReconnecting={isReconnecting}
isPublishing={isPublishing}
isUnpublishing={isUnpublishing}
transactionResult={transactionResult}
isCreatingDraft={isCreatingDraft}
onDelete={this.handleDelete}
onClearTransactionResult={this.handleClearTransactionResult}
onPublish={this.handlePublish}
onRestore={this.handleRestoreRevision}
onUnpublish={this.handleUnpublish}
onChange={this.handleChange}
/>
)
}
}
)