-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
actions.js
753 lines (702 loc) · 18 KB
/
actions.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
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
/**
* External dependencies
*/
import { castArray, pick } from 'lodash';
import { BEGIN, COMMIT, REVERT } from 'redux-optimist';
/**
* Internal dependencies
*/
import {
dispatch,
select,
resolveSelect,
apiFetch,
} from './controls';
import {
STORE_KEY,
POST_UPDATE_TRANSACTION_ID,
SAVE_POST_NOTICE_ID,
TRASH_POST_NOTICE_ID,
} from './constants';
import {
getNotificationArgumentsForSaveSuccess,
getNotificationArgumentsForSaveFail,
getNotificationArgumentsForTrashFail,
} from './utils/notice-builder';
/**
* Returns an action object used in signalling that editor has initialized with
* the specified post object and editor settings.
*
* @param {Object} post Post object.
* @param {Object} edits Initial edited attributes object.
* @param {Array?} template Block Template.
*
* @return {Object} Action object.
*/
export function setupEditor( post, edits, template ) {
return {
type: 'SETUP_EDITOR',
post,
edits,
template,
};
}
/**
* Returns an action object used in signalling that the latest version of the
* post has been received, either by initialization or save.
*
* @param {Object} post Post object.
*
* @return {Object} Action object.
*/
export function resetPost( post ) {
return {
type: 'RESET_POST',
post,
};
}
/**
* Returns an action object used in signalling that the latest autosave of the
* post has been received, by initialization or autosave.
*
* @param {Object} post Autosave post object.
*
* @return {Object} Action object.
*/
export function resetAutosave( post ) {
return {
type: 'RESET_AUTOSAVE',
post,
};
}
/**
* Optimistic action for dispatching that a post update request has started.
*
* @param {Object} options
*
* @return {Object} An action object
*/
export function __experimentalRequestPostUpdateStart( options = {} ) {
return {
type: 'REQUEST_POST_UPDATE_START',
optimist: { type: BEGIN, id: POST_UPDATE_TRANSACTION_ID },
options,
};
}
/**
* Optimistic action for indicating that the request post update has completed
* successfully.
*
* @param {Object} data The data for the action.
* @param {Object} data.previousPost The previous post prior to update.
* @param {Object} data.post The new post after update
* @param {boolean} data.isRevision Whether the post is a revision or not.
* @param {Object} data.options Options passed through from the original
* action dispatch.
* @param {Object} data.postType The post type object.
*
* @return {Object} Action object.
*/
export function __experimentalRequestPostUpdateSuccess( {
previousPost,
post,
isRevision,
options,
postType,
} ) {
return {
type: 'REQUEST_POST_UPDATE_SUCCESS',
previousPost,
post,
optimist: {
// Note: REVERT is not a failure case here. Rather, it
// is simply reversing the assumption that the updates
// were applied to the post proper, such that the post
// treated as having unsaved changes.
type: isRevision ? REVERT : COMMIT,
id: POST_UPDATE_TRANSACTION_ID,
},
options,
postType,
};
}
/**
* Optimistic action for indicating that the request post update has completed
* with a failure.
*
* @param {Object} data The data for the action
* @param {Object} data.post The post that failed updating.
* @param {Object} data.edits The fields that were being updated.
* @param {*} data.error The error from the failed call.
* @param {Object} data.options Options passed through from the original
* action dispatch.
* @return {Object} An action object
*/
export function __experimentalRequestPostUpdateFailure( {
post,
edits,
error,
options,
} ) {
return {
type: 'REQUEST_POST_UPDATE_FAILURE',
optimist: { type: REVERT, id: POST_UPDATE_TRANSACTION_ID },
post,
edits,
error,
options,
};
}
/**
* Returns an action object used in signalling that a patch of updates for the
* latest version of the post have been received.
*
* @param {Object} edits Updated post fields.
*
* @return {Object} Action object.
*/
export function updatePost( edits ) {
return {
type: 'UPDATE_POST',
edits,
};
}
/**
* Returns an action object used to setup the editor state when first opening
* an editor.
*
* @param {Object} post Post object.
*
* @return {Object} Action object.
*/
export function setupEditorState( post ) {
return {
type: 'SETUP_EDITOR_STATE',
post,
};
}
/**
* Returns an action object used in signalling that attributes of the post have
* been edited.
*
* @param {Object} edits Post attributes to edit.
*
* @return {Object} Action object.
*/
export function editPost( edits ) {
return {
type: 'EDIT_POST',
edits,
};
}
/**
* Returns action object produced by the updatePost creator augmented by
* an optimist option that signals optimistically applying updates.
*
* @param {Object} edits Updated post fields.
*
* @return {Object} Action object.
*/
export function __experimentalOptimisticUpdatePost( edits ) {
return {
...updatePost( edits ),
optimist: { id: POST_UPDATE_TRANSACTION_ID },
};
}
/**
* Action generator for saving the current post in the editor.
*
* @param {Object} options
*/
export function* savePost( options = {} ) {
const isEditedPostSaveable = yield select(
STORE_KEY,
'isEditedPostSaveable'
);
if ( ! isEditedPostSaveable ) {
return;
}
let edits = yield select(
STORE_KEY,
'getPostEdits'
);
const isAutosave = !! options.isAutosave;
if ( isAutosave ) {
edits = pick( edits, [ 'title', 'content', 'excerpt' ] );
}
const isEditedPostNew = yield select(
STORE_KEY,
'isEditedPostNew',
);
// New posts (with auto-draft status) must be explicitly assigned draft
// status if there is not already a status assigned in edits (publish).
// Otherwise, they are wrongly left as auto-draft. Status is not always
// respected for autosaves, so it cannot simply be included in the pick
// above. This behavior relies on an assumption that an auto-draft post
// would never be saved by anyone other than the owner of the post, per
// logic within autosaves REST controller to save status field only for
// draft/auto-draft by current user.
//
// See: https://core.trac.wordpress.org/ticket/43316#comment:88
// See: https://core.trac.wordpress.org/ticket/43316#comment:89
if ( isEditedPostNew ) {
edits = { status: 'draft', ...edits };
}
const post = yield select(
STORE_KEY,
'getCurrentPost'
);
const editedPostContent = yield select(
STORE_KEY,
'getEditedPostContent'
);
let toSend = {
...edits,
content: editedPostContent,
id: post.id,
};
const currentPostType = yield select(
STORE_KEY,
'getCurrentPostType'
);
const postType = yield resolveSelect(
'core',
'getPostType',
currentPostType
);
yield dispatch(
STORE_KEY,
'__experimentalRequestPostUpdateStart',
options,
);
// Optimistically apply updates under the assumption that the post
// will be updated. See below logic in success resolution for revert
// if the autosave is applied as a revision.
yield dispatch(
STORE_KEY,
'__experimentalOptimisticUpdatePost',
toSend
);
let path = `/wp/v2/${ postType.rest_base }/${ post.id }`;
let method = 'PUT';
if ( isAutosave ) {
const autoSavePost = yield select(
STORE_KEY,
'getAutosave',
);
// Ensure autosaves contain all expected fields, using autosave or
// post values as fallback if not otherwise included in edits.
toSend = {
...pick( post, [ 'title', 'content', 'excerpt' ] ),
...autoSavePost,
...toSend,
};
path += '/autosaves';
method = 'POST';
} else {
yield dispatch(
'core/notices',
'removeNotice',
SAVE_POST_NOTICE_ID,
);
yield dispatch(
'core/notices',
'removeNotice',
'autosave-exists',
);
}
try {
const newPost = yield apiFetch( {
path,
method,
data: toSend,
} );
const resetAction = isAutosave ? 'resetAutosave' : 'resetPost';
yield dispatch( STORE_KEY, resetAction, newPost );
yield dispatch(
STORE_KEY,
'__experimentalRequestPostUpdateSuccess',
{
previousPost: post,
post: newPost,
options,
postType,
// An autosave may be processed by the server as a regular save
// when its update is requested by the author and the post was
// draft or auto-draft.
isRevision: newPost.id !== post.id,
}
);
const notifySuccessArgs = getNotificationArgumentsForSaveSuccess( {
previousPost: post,
post: newPost,
postType,
options,
} );
if ( notifySuccessArgs.length > 0 ) {
yield dispatch(
'core/notices',
'createSuccessNotice',
...notifySuccessArgs
);
}
} catch ( error ) {
yield dispatch(
STORE_KEY,
'__experimentalRequestPostUpdateFailure',
{ post, edits, error, options }
);
const notifyFailArgs = getNotificationArgumentsForSaveFail( {
post,
edits,
error,
} );
if ( notifyFailArgs.length > 0 ) {
yield dispatch(
'core/notices',
'createErrorNotice',
...notifyFailArgs
);
}
}
}
/**
* Action generator for handling refreshing the current post.
*/
export function* refreshPost() {
const post = yield select(
STORE_KEY,
'getCurrentPost'
);
const postTypeSlug = yield select(
STORE_KEY,
'getCurrentPostType'
);
const postType = yield resolveSelect(
'core',
'getPostType',
postTypeSlug
);
const newPost = yield apiFetch(
{
// Timestamp arg allows caller to bypass browser caching, which is
// expected for this specific function.
path: `/wp/v2/${ postType.rest_base }/${ post.id }` +
`?context=edit&_timestamp=${ Date.now() }`,
}
);
yield dispatch(
STORE_KEY,
'resetPost',
newPost
);
}
/**
* Action generator for trashing the current post in the editor.
*/
export function* trashPost() {
const postTypeSlug = yield select(
STORE_KEY,
'getCurrentPostType'
);
const postType = yield resolveSelect(
'core',
'getPostType',
postTypeSlug
);
yield dispatch(
'core/notices',
'removeNotice',
TRASH_POST_NOTICE_ID
);
try {
const post = yield select(
STORE_KEY,
'getCurrentPost'
);
yield apiFetch(
{
path: `/wp/v2/${ postType.rest_base }/${ post.id }`,
method: 'DELETE',
}
);
// TODO: This should be an updatePost action (updating subsets of post
// properties), but right now editPost is tied with change detection.
yield dispatch(
STORE_KEY,
'resetPost',
{ ...post, status: 'trash' }
);
} catch ( error ) {
yield dispatch(
'core/notices',
'createErrorNotice',
...getNotificationArgumentsForTrashFail( { error } ),
);
}
}
/**
* Action generator used in signalling that the post should autosave.
*
* @param {Object?} options Extra flags to identify the autosave.
*/
export function* autosave( options ) {
yield dispatch(
STORE_KEY,
'savePost',
{ isAutosave: true, ...options }
);
}
/**
* Returns an action object used in signalling that undo history should
* restore last popped state.
*
* @return {Object} Action object.
*/
export function redo() {
return { type: 'REDO' };
}
/**
* Returns an action object used in signalling that undo history should pop.
*
* @return {Object} Action object.
*/
export function undo() {
return { type: 'UNDO' };
}
/**
* Returns an action object used in signalling that undo history record should
* be created.
*
* @return {Object} Action object.
*/
export function createUndoLevel() {
return { type: 'CREATE_UNDO_LEVEL' };
}
/**
* Returns an action object used to lock the editor.
*
* @param {Object} lock Details about the post lock status, user, and nonce.
*
* @return {Object} Action object.
*/
export function updatePostLock( lock ) {
return {
type: 'UPDATE_POST_LOCK',
lock,
};
}
/**
* Returns an action object used to fetch a single reusable block or all
* reusable blocks from the REST API into the store.
*
* @param {?string} id If given, only a single reusable block with this ID will
* be fetched.
*
* @return {Object} Action object.
*/
export function __experimentalFetchReusableBlocks( id ) {
return {
type: 'FETCH_REUSABLE_BLOCKS',
id,
};
}
/**
* Returns an action object used in signalling that reusable blocks have been
* received. `results` is an array of objects containing:
* - `reusableBlock` - Details about how the reusable block is persisted.
* - `parsedBlock` - The original block.
*
* @param {Object[]} results Reusable blocks received.
*
* @return {Object} Action object.
*/
export function __experimentalReceiveReusableBlocks( results ) {
return {
type: 'RECEIVE_REUSABLE_BLOCKS',
results,
};
}
/**
* Returns an action object used to save a reusable block that's in the store to
* the REST API.
*
* @param {Object} id The ID of the reusable block to save.
*
* @return {Object} Action object.
*/
export function __experimentalSaveReusableBlock( id ) {
return {
type: 'SAVE_REUSABLE_BLOCK',
id,
};
}
/**
* Returns an action object used to delete a reusable block via the REST API.
*
* @param {number} id The ID of the reusable block to delete.
*
* @return {Object} Action object.
*/
export function __experimentalDeleteReusableBlock( id ) {
return {
type: 'DELETE_REUSABLE_BLOCK',
id,
};
}
/**
* Returns an action object used in signalling that a reusable block's title is
* to be updated.
*
* @param {number} id The ID of the reusable block to update.
* @param {string} title The new title.
*
* @return {Object} Action object.
*/
export function __experimentalUpdateReusableBlockTitle( id, title ) {
return {
type: 'UPDATE_REUSABLE_BLOCK_TITLE',
id,
title,
};
}
/**
* Returns an action object used to convert a reusable block into a static
* block.
*
* @param {string} clientId The client ID of the block to attach.
*
* @return {Object} Action object.
*/
export function __experimentalConvertBlockToStatic( clientId ) {
return {
type: 'CONVERT_BLOCK_TO_STATIC',
clientId,
};
}
/**
* Returns an action object used to convert a static block into a reusable
* block.
*
* @param {string} clientIds The client IDs of the block to detach.
*
* @return {Object} Action object.
*/
export function __experimentalConvertBlockToReusable( clientIds ) {
return {
type: 'CONVERT_BLOCK_TO_REUSABLE',
clientIds: castArray( clientIds ),
};
}
/**
* Returns an action object used in signalling that the user has enabled the
* publish sidebar.
*
* @return {Object} Action object
*/
export function enablePublishSidebar() {
return {
type: 'ENABLE_PUBLISH_SIDEBAR',
};
}
/**
* Returns an action object used in signalling that the user has disabled the
* publish sidebar.
*
* @return {Object} Action object
*/
export function disablePublishSidebar() {
return {
type: 'DISABLE_PUBLISH_SIDEBAR',
};
}
/**
* Returns an action object used to signal that post saving is locked.
*
* @param {string} lockName The lock name.
*
* @return {Object} Action object
*/
export function lockPostSaving( lockName ) {
return {
type: 'LOCK_POST_SAVING',
lockName,
};
}
/**
* Returns an action object used to signal that post saving is unlocked.
*
* @param {string} lockName The lock name.
*
* @return {Object} Action object
*/
export function unlockPostSaving( lockName ) {
return {
type: 'UNLOCK_POST_SAVING',
lockName,
};
}
/**
* Returns an action object used to signal that the blocks have been updated.
*
* @param {Array} blocks Block Array.
* @param {?Object} options Optional options.
*
* @return {Object} Action object
*/
export function resetEditorBlocks( blocks, options = {} ) {
return {
type: 'RESET_EDITOR_BLOCKS',
blocks,
shouldCreateUndoLevel: options.__unstableShouldCreateUndoLevel !== false,
};
}
/*
* Returns an action object used in signalling that the post editor settings have been updated.
*
* @param {Object} settings Updated settings
*
* @return {Object} Action object
*/
export function updateEditorSettings( settings ) {
return {
type: 'UPDATE_EDITOR_SETTINGS',
settings,
};
}
/**
* Backward compatibility
*/
const getBlockEditorAction = ( name ) => function* ( ...args ) {
yield dispatch( 'core/block-editor', name, ...args );
};
export const resetBlocks = getBlockEditorAction( 'resetBlocks' );
export const receiveBlocks = getBlockEditorAction( 'receiveBlocks' );
export const updateBlock = getBlockEditorAction( 'updateBlock' );
export const updateBlockAttributes = getBlockEditorAction( 'updateBlockAttributes' );
export const selectBlock = getBlockEditorAction( 'selectBlock' );
export const startMultiSelect = getBlockEditorAction( 'startMultiSelect' );
export const stopMultiSelect = getBlockEditorAction( 'stopMultiSelect' );
export const multiSelect = getBlockEditorAction( 'multiSelect' );
export const clearSelectedBlock = getBlockEditorAction( 'clearSelectedBlock' );
export const toggleSelection = getBlockEditorAction( 'toggleSelection' );
export const replaceBlocks = getBlockEditorAction( 'replaceBlocks' );
export const moveBlocksDown = getBlockEditorAction( 'moveBlocksDown' );
export const moveBlocksUp = getBlockEditorAction( 'moveBlocksUp' );
export const moveBlockToPosition = getBlockEditorAction( 'moveBlockToPosition' );
export const insertBlock = getBlockEditorAction( 'insertBlock' );
export const insertBlocks = getBlockEditorAction( 'insertBlocks' );
export const showInsertionPoint = getBlockEditorAction( 'showInsertionPoint' );
export const hideInsertionPoint = getBlockEditorAction( 'hideInsertionPoint' );
export const setTemplateValidity = getBlockEditorAction( 'setTemplateValidity' );
export const synchronizeTemplate = getBlockEditorAction( 'synchronizeTemplate' );
export const mergeBlocks = getBlockEditorAction( 'mergeBlocks' );
export const removeBlocks = getBlockEditorAction( 'removeBlocks' );
export const removeBlock = getBlockEditorAction( 'removeBlock' );
export const toggleBlockMode = getBlockEditorAction( 'toggleBlockMode' );
export const startTyping = getBlockEditorAction( 'startTyping' );
export const stopTyping = getBlockEditorAction( 'stopTyping' );
export const enterFormattedText = getBlockEditorAction( 'enterFormattedText' );
export const exitFormattedText = getBlockEditorAction( 'exitFormattedText' );
export const insertDefaultBlock = getBlockEditorAction( 'insertDefaultBlock' );
export const updateBlockListSettings = getBlockEditorAction( 'updateBlockListSettings' );