-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
actions.js
1106 lines (1030 loc) · 27.7 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
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* External dependencies
*/
import { castArray, first, last, some } from 'lodash';
/**
* WordPress dependencies
*/
import {
cloneBlock,
createBlock,
getDefaultBlockName,
hasBlockSupport,
} from '@wordpress/blocks';
import { speak } from '@wordpress/a11y';
import { __ } from '@wordpress/i18n';
import { controls } from '@wordpress/data';
/**
* Generator which will yield a default block insert action if there
* are no other blocks at the root of the editor. This generator should be used
* in actions which may result in no blocks remaining in the editor (removal,
* replacement, etc).
*/
function* ensureDefaultBlock() {
const count = yield controls.select( 'core/block-editor', 'getBlockCount' );
// To avoid a focus loss when removing the last block, assure there is
// always a default block if the last of the blocks have been removed.
if ( count === 0 ) {
return yield insertDefaultBlock();
}
}
/**
* Returns an action object used in signalling that blocks state should be
* reset to the specified array of blocks, taking precedence over any other
* content reflected as an edit in state.
*
* @param {Array} blocks Array of blocks.
*
* @return {Object} Action object.
*/
export function resetBlocks( blocks ) {
return {
type: 'RESET_BLOCKS',
blocks,
};
}
/**
* A block selection object.
*
* @typedef {Object} WPBlockSelection
*
* @property {string} clientId A block client ID.
* @property {string} attributeKey A block attribute key.
* @property {number} offset An attribute value offset, based on the rich
* text value. See `wp.richText.create`.
*/
/**
* Returns an action object used in signalling that selection state should be
* reset to the specified selection.
*
* @param {WPBlockSelection} selectionStart The selection start.
* @param {WPBlockSelection} selectionEnd The selection end.
*
* @return {Object} Action object.
*/
export function resetSelection( selectionStart, selectionEnd ) {
return {
type: 'RESET_SELECTION',
selectionStart,
selectionEnd,
};
}
/**
* Returns an action object used in signalling that blocks have been received.
* Unlike resetBlocks, these should be appended to the existing known set, not
* replacing.
*
* @param {Object[]} blocks Array of block objects.
*
* @return {Object} Action object.
*/
export function receiveBlocks( blocks ) {
return {
type: 'RECEIVE_BLOCKS',
blocks,
};
}
/**
* Returns an action object used in signalling that the multiple blocks'
* attributes with the specified client IDs have been updated.
*
* @param {string|string[]} clientIds Block client IDs.
* @param {Object} attributes Block attributes to be merged.
*
* @return {Object} Action object.
*/
export function updateBlockAttributes( clientIds, attributes ) {
return {
type: 'UPDATE_BLOCK_ATTRIBUTES',
clientIds: castArray( clientIds ),
attributes,
};
}
/**
* Returns an action object used in signalling that the block with the
* specified client ID has been updated.
*
* @param {string} clientId Block client ID.
* @param {Object} updates Block attributes to be merged.
*
* @return {Object} Action object.
*/
export function updateBlock( clientId, updates ) {
return {
type: 'UPDATE_BLOCK',
clientId,
updates,
};
}
/**
* Returns an action object used in signalling that the block with the
* specified client ID has been selected, optionally accepting a position
* value reflecting its selection directionality. An initialPosition of -1
* reflects a reverse selection.
*
* @param {string} clientId Block client ID.
* @param {?number} initialPosition Optional initial position. Pass as -1 to
* reflect reverse selection.
*
* @return {Object} Action object.
*/
export function selectBlock( clientId, initialPosition = null ) {
return {
type: 'SELECT_BLOCK',
initialPosition,
clientId,
};
}
/**
* Yields action objects used in signalling that the block preceding the given
* clientId should be selected.
*
* @param {string} clientId Block client ID.
*/
export function* selectPreviousBlock( clientId ) {
const previousBlockClientId = yield controls.select(
'core/block-editor',
'getPreviousBlockClientId',
clientId
);
if ( previousBlockClientId ) {
yield selectBlock( previousBlockClientId, -1 );
return [ previousBlockClientId ];
}
}
/**
* Yields action objects used in signalling that the block following the given
* clientId should be selected.
*
* @param {string} clientId Block client ID.
*/
export function* selectNextBlock( clientId ) {
const nextBlockClientId = yield controls.select(
'core/block-editor',
'getNextBlockClientId',
clientId
);
if ( nextBlockClientId ) {
yield selectBlock( nextBlockClientId );
return [ nextBlockClientId ];
}
}
/**
* Returns an action object used in signalling that a block multi-selection has started.
*
* @return {Object} Action object.
*/
export function startMultiSelect() {
return {
type: 'START_MULTI_SELECT',
};
}
/**
* Returns an action object used in signalling that block multi-selection stopped.
*
* @return {Object} Action object.
*/
export function stopMultiSelect() {
return {
type: 'STOP_MULTI_SELECT',
};
}
/**
* Returns an action object used in signalling that block multi-selection changed.
*
* @param {string} start First block of the multi selection.
* @param {string} end Last block of the multiselection.
*
* @return {Object} Action object.
*/
export function multiSelect( start, end ) {
return {
type: 'MULTI_SELECT',
start,
end,
};
}
/**
* Returns an action object used in signalling that the block selection is cleared.
*
* @return {Object} Action object.
*/
export function clearSelectedBlock() {
return {
type: 'CLEAR_SELECTED_BLOCK',
};
}
/**
* Returns an action object that enables or disables block selection.
*
* @param {boolean} [isSelectionEnabled=true] Whether block selection should
* be enabled.
*
* @return {Object} Action object.
*/
export function toggleSelection( isSelectionEnabled = true ) {
return {
type: 'TOGGLE_SELECTION',
isSelectionEnabled,
};
}
function getBlocksWithDefaultStylesApplied( blocks, blockEditorSettings ) {
const preferredStyleVariations =
blockEditorSettings?.__experimentalPreferredStyleVariations?.value ??
{};
return blocks.map( ( block ) => {
const blockName = block.name;
if ( ! hasBlockSupport( blockName, 'defaultStylePicker', true ) ) {
return block;
}
if ( ! preferredStyleVariations[ blockName ] ) {
return block;
}
const className = block.attributes?.className;
if ( className?.includes( 'is-style-' ) ) {
return block;
}
const { attributes = {} } = block;
const blockStyle = preferredStyleVariations[ blockName ];
return {
...block,
attributes: {
...attributes,
className: `${
className || ''
} is-style-${ blockStyle }`.trim(),
},
};
} );
}
/**
* Returns an action object signalling that a blocks should be replaced with
* one or more replacement blocks.
*
* @param {(string|string[])} clientIds Block client ID(s) to replace.
* @param {(Object|Object[])} blocks Replacement block(s).
* @param {number} indexToSelect Index of replacement block to select.
* @param {number} initialPosition Index of caret after in the selected block after the operation.
* @param {?Object} meta Optional Meta values to be passed to the action object.
*
* @yield {Object} Action object.
*/
export function* replaceBlocks(
clientIds,
blocks,
indexToSelect,
initialPosition,
meta
) {
clientIds = castArray( clientIds );
blocks = getBlocksWithDefaultStylesApplied(
castArray( blocks ),
yield controls.select( 'core/block-editor', 'getSettings' )
);
const rootClientId = yield controls.select(
'core/block-editor',
'getBlockRootClientId',
first( clientIds )
);
// Replace is valid if the new blocks can be inserted in the root block.
for ( let index = 0; index < blocks.length; index++ ) {
const block = blocks[ index ];
const canInsertBlock = yield controls.select(
'core/block-editor',
'canInsertBlockType',
block.name,
rootClientId
);
if ( ! canInsertBlock ) {
return;
}
}
yield {
type: 'REPLACE_BLOCKS',
clientIds,
blocks,
time: Date.now(),
indexToSelect,
initialPosition,
meta,
};
yield* ensureDefaultBlock();
}
/**
* Returns an action object signalling that a single block should be replaced
* with one or more replacement blocks.
*
* @param {(string|string[])} clientId Block client ID to replace.
* @param {(Object|Object[])} block Replacement block(s).
*
* @return {Object} Action object.
*/
export function replaceBlock( clientId, block ) {
return replaceBlocks( clientId, block );
}
/**
* Higher-order action creator which, given the action type to dispatch creates
* an action creator for managing block movement.
*
* @param {string} type Action type to dispatch.
*
* @return {Function} Action creator.
*/
function createOnMove( type ) {
return ( clientIds, rootClientId ) => {
return {
clientIds: castArray( clientIds ),
type,
rootClientId,
};
};
}
export const moveBlocksDown = createOnMove( 'MOVE_BLOCKS_DOWN' );
export const moveBlocksUp = createOnMove( 'MOVE_BLOCKS_UP' );
/**
* Returns an action object signalling that the given blocks should be moved to
* a new position.
*
* @param {?string} clientIds The client IDs of the blocks.
* @param {?string} fromRootClientId Root client ID source.
* @param {?string} toRootClientId Root client ID destination.
* @param {number} index The index to move the blocks to.
*
* @yield {Object} Action object.
*/
export function* moveBlocksToPosition(
clientIds,
fromRootClientId = '',
toRootClientId = '',
index
) {
const templateLock = yield controls.select(
'core/block-editor',
'getTemplateLock',
fromRootClientId
);
// If locking is equal to all on the original clientId (fromRootClientId),
// it is not possible to move the block to any other position.
if ( templateLock === 'all' ) {
return;
}
const action = {
type: 'MOVE_BLOCKS_TO_POSITION',
fromRootClientId,
toRootClientId,
clientIds,
index,
};
// If moving inside the same root block the move is always possible.
if ( fromRootClientId === toRootClientId ) {
yield action;
return;
}
// If templateLock is insert we can not remove the block from the parent.
// Given that here we know that we are moving the block to a different
// parent, the move should not be possible if the condition is true.
if ( templateLock === 'insert' ) {
return;
}
const canInsertBlocks = yield controls.select(
'core/block-editor',
'canInsertBlocks',
clientIds,
toRootClientId
);
// If moving to other parent block, the move is possible if we can insert a block of the same type inside the new parent block.
if ( canInsertBlocks ) {
yield action;
}
}
/**
* Returns an action object signalling that the given block should be moved to a
* new position.
*
* @param {?string} clientId The client ID of the block.
* @param {?string} fromRootClientId Root client ID source.
* @param {?string} toRootClientId Root client ID destination.
* @param {number} index The index to move the block to.
*
* @yield {Object} Action object.
*/
export function* moveBlockToPosition(
clientId,
fromRootClientId = '',
toRootClientId = '',
index
) {
yield moveBlocksToPosition(
[ clientId ],
fromRootClientId,
toRootClientId,
index
);
}
/**
* Returns an action object used in signalling that a single block should be
* inserted, optionally at a specific index respective a root block list.
*
* @param {Object} block Block object to insert.
* @param {?number} index Index at which block should be inserted.
* @param {?string} rootClientId Optional root client ID of block list on which to insert.
* @param {?boolean} updateSelection If true block selection will be updated. If false, block selection will not change. Defaults to true.
*
* @return {Object} Action object.
*/
export function insertBlock(
block,
index,
rootClientId,
updateSelection = true
) {
return insertBlocks( [ block ], index, rootClientId, updateSelection );
}
/**
* Returns an action object used in signalling that an array of blocks should
* be inserted, optionally at a specific index respective a root block list.
*
* @param {Object[]} blocks Block objects to insert.
* @param {?number} index Index at which block should be inserted.
* @param {?string} rootClientId Optional root client ID of block list on which to insert.
* @param {?boolean} updateSelection If true block selection will be updated. If false, block selection will not change. Defaults to true.
* @param {?Object} meta Optional Meta values to be passed to the action object.
*
* @return {Object} Action object.
*/
export function* insertBlocks(
blocks,
index,
rootClientId,
updateSelection = true,
meta
) {
blocks = getBlocksWithDefaultStylesApplied(
castArray( blocks ),
yield controls.select( 'core/block-editor', 'getSettings' )
);
const allowedBlocks = [];
for ( const block of blocks ) {
const isValid = yield controls.select(
'core/block-editor',
'canInsertBlockType',
block.name,
rootClientId
);
if ( isValid ) {
allowedBlocks.push( block );
}
}
if ( allowedBlocks.length ) {
return {
type: 'INSERT_BLOCKS',
blocks: allowedBlocks,
index,
rootClientId,
time: Date.now(),
updateSelection,
meta,
};
}
}
/**
* Returns an action object used in signalling that the insertion point should
* be shown.
*
* @param {?string} rootClientId Optional root client ID of block list on
* which to insert.
* @param {?number} index Index at which block should be inserted.
*
* @return {Object} Action object.
*/
export function showInsertionPoint( rootClientId, index ) {
return {
type: 'SHOW_INSERTION_POINT',
rootClientId,
index,
};
}
/**
* Returns an action object hiding the insertion point.
*
* @return {Object} Action object.
*/
export function hideInsertionPoint() {
return {
type: 'HIDE_INSERTION_POINT',
};
}
/**
* Returns an action object resetting the template validity.
*
* @param {boolean} isValid template validity flag.
*
* @return {Object} Action object.
*/
export function setTemplateValidity( isValid ) {
return {
type: 'SET_TEMPLATE_VALIDITY',
isValid,
};
}
/**
* Returns an action object synchronize the template with the list of blocks
*
* @return {Object} Action object.
*/
export function synchronizeTemplate() {
return {
type: 'SYNCHRONIZE_TEMPLATE',
};
}
/**
* Returns an action object used in signalling that two blocks should be merged
*
* @param {string} firstBlockClientId Client ID of the first block to merge.
* @param {string} secondBlockClientId Client ID of the second block to merge.
*
* @return {Object} Action object.
*/
export function mergeBlocks( firstBlockClientId, secondBlockClientId ) {
return {
type: 'MERGE_BLOCKS',
blocks: [ firstBlockClientId, secondBlockClientId ],
};
}
/**
* Yields action objects used in signalling that the blocks corresponding to
* the set of specified client IDs are to be removed.
*
* @param {string|string[]} clientIds Client IDs of blocks to remove.
* @param {boolean} selectPrevious True if the previous block should be
* selected when a block is removed.
*/
export function* removeBlocks( clientIds, selectPrevious = true ) {
if ( ! clientIds || ! clientIds.length ) {
return;
}
clientIds = castArray( clientIds );
const rootClientId = yield controls.select(
'core/block-editor',
'getBlockRootClientId',
clientIds[ 0 ]
);
const isLocked = yield controls.select(
'core/block-editor',
'getTemplateLock',
rootClientId
);
if ( isLocked ) {
return;
}
let previousBlockId;
if ( selectPrevious ) {
previousBlockId = yield selectPreviousBlock( clientIds[ 0 ] );
} else {
previousBlockId = yield controls.select(
'core/block-editor',
'getPreviousBlockClientId',
clientIds[ 0 ]
);
}
yield {
type: 'REMOVE_BLOCKS',
clientIds,
};
// To avoid a focus loss when removing the last block, assure there is
// always a default block if the last of the blocks have been removed.
const defaultBlockId = yield* ensureDefaultBlock();
return [ previousBlockId || defaultBlockId ];
}
/**
* Returns an action object used in signalling that the block with the
* specified client ID is to be removed.
*
* @param {string} clientId Client ID of block to remove.
* @param {boolean} selectPrevious True if the previous block should be
* selected when a block is removed.
*
* @return {Object} Action object.
*/
export function removeBlock( clientId, selectPrevious ) {
return removeBlocks( [ clientId ], selectPrevious );
}
/**
* Returns an action object used in signalling that the inner blocks with the
* specified client ID should be replaced.
*
* @param {string} rootClientId Client ID of the block whose InnerBlocks will re replaced.
* @param {Object[]} blocks Block objects to insert as new InnerBlocks
* @param {?boolean} updateSelection If true block selection will be updated. If false, block selection will not change. Defaults to true.
*
* @return {Object} Action object.
*/
export function replaceInnerBlocks(
rootClientId,
blocks,
updateSelection = true
) {
return {
type: 'REPLACE_INNER_BLOCKS',
rootClientId,
blocks,
updateSelection,
time: Date.now(),
};
}
/**
* Returns an action object used to toggle the block editing mode between
* visual and HTML modes.
*
* @param {string} clientId Block client ID.
*
* @return {Object} Action object.
*/
export function toggleBlockMode( clientId ) {
return {
type: 'TOGGLE_BLOCK_MODE',
clientId,
};
}
/**
* Returns an action object used in signalling that the user has begun to type.
*
* @return {Object} Action object.
*/
export function startTyping() {
return {
type: 'START_TYPING',
};
}
/**
* Returns an action object used in signalling that the user has stopped typing.
*
* @return {Object} Action object.
*/
export function stopTyping() {
return {
type: 'STOP_TYPING',
};
}
/**
* Returns an action object used in signalling that the user has begun to drag blocks.
*
* @param {string[]} clientIds An array of client ids being dragged
*
* @return {Object} Action object.
*/
export function startDraggingBlocks( clientIds = [] ) {
return {
type: 'START_DRAGGING_BLOCKS',
clientIds,
};
}
/**
* Returns an action object used in signalling that the user has stopped dragging blocks.
*
* @return {Object} Action object.
*/
export function stopDraggingBlocks() {
return {
type: 'STOP_DRAGGING_BLOCKS',
};
}
/**
* Returns an action object used in signalling that the caret has entered formatted text.
*
* @return {Object} Action object.
*/
export function enterFormattedText() {
return {
type: 'ENTER_FORMATTED_TEXT',
};
}
/**
* Returns an action object used in signalling that the user caret has exited formatted text.
*
* @return {Object} Action object.
*/
export function exitFormattedText() {
return {
type: 'EXIT_FORMATTED_TEXT',
};
}
/**
* Returns an action object used in signalling that the user caret has changed
* position.
*
* @param {string} clientId The selected block client ID.
* @param {string} attributeKey The selected block attribute key.
* @param {number} startOffset The start offset.
* @param {number} endOffset The end offset.
*
* @return {Object} Action object.
*/
export function selectionChange(
clientId,
attributeKey,
startOffset,
endOffset
) {
return {
type: 'SELECTION_CHANGE',
clientId,
attributeKey,
startOffset,
endOffset,
};
}
/**
* Returns an action object used in signalling that a new block of the default
* type should be added to the block list.
*
* @param {?Object} attributes Optional attributes of the block to assign.
* @param {?string} rootClientId Optional root client ID of block list on which
* to append.
* @param {?number} index Optional index where to insert the default block
*
* @return {Object} Action object
*/
export function insertDefaultBlock( attributes, rootClientId, index ) {
// Abort if there is no default block type (if it has been unregistered).
const defaultBlockName = getDefaultBlockName();
if ( ! defaultBlockName ) {
return;
}
const block = createBlock( defaultBlockName, attributes );
return insertBlock( block, index, rootClientId );
}
/**
* Returns an action object that changes the nested settings of a given block.
*
* @param {string} clientId Client ID of the block whose nested setting are
* being received.
* @param {Object} settings Object with the new settings for the nested block.
*
* @return {Object} Action object
*/
export function updateBlockListSettings( clientId, settings ) {
return {
type: 'UPDATE_BLOCK_LIST_SETTINGS',
clientId,
settings,
};
}
/**
* Returns an action object used in signalling that the block editor settings have been updated.
*
* @param {Object} settings Updated settings
*
* @return {Object} Action object
*/
export function updateSettings( settings ) {
return {
type: 'UPDATE_SETTINGS',
settings,
};
}
/**
* Returns an action object used in signalling that a temporary reusable blocks have been saved
* in order to switch its temporary id with the real id.
*
* @param {string} id Reusable block's id.
* @param {string} updatedId Updated block's id.
*
* @return {Object} Action object.
*/
export function __unstableSaveReusableBlock( id, updatedId ) {
return {
type: 'SAVE_REUSABLE_BLOCK_SUCCESS',
id,
updatedId,
};
}
/**
* Returns an action object used in signalling that the last block change should be marked explicitely as persistent.
*
* @return {Object} Action object.
*/
export function __unstableMarkLastChangeAsPersistent() {
return { type: 'MARK_LAST_CHANGE_AS_PERSISTENT' };
}
/**
* Returns an action object used in signalling that the next block change should be marked explicitly as not persistent.
*
* @return {Object} Action object.
*/
export function __unstableMarkNextChangeAsNotPersistent() {
return { type: 'MARK_NEXT_CHANGE_AS_NOT_PERSISTENT' };
}
/**
* Returns an action object used in signalling that the last block change is
* an automatic change, meaning it was not performed by the user, and can be
* undone using the `Escape` and `Backspace` keys. This action must be called
* after the change was made, and any actions that are a consequence of it, so
* it is recommended to be called at the next idle period to ensure all
* selection changes have been recorded.
*
* @return {Object} Action object.
*/
export function __unstableMarkAutomaticChange() {
return { type: 'MARK_AUTOMATIC_CHANGE' };
}
/**
* Generators that triggers an action used to enable or disable the navigation mode.
*
* @param {string} isNavigationMode Enable/Disable navigation mode.
*/
export function* setNavigationMode( isNavigationMode = true ) {
yield {
type: 'SET_NAVIGATION_MODE',
isNavigationMode,
};
if ( isNavigationMode ) {
speak(
__(
'You are currently in navigation mode. Navigate blocks using the Tab key and Arrow keys. Use Left and Right Arrow keys to move between nesting levels. To exit navigation mode and edit the selected block, press Enter.'
)
);
} else {
speak(
__(
'You are currently in edit mode. To return to the navigation mode, press Escape.'
)
);
}
}
/**
* Generator that triggers an action used to enable or disable the block moving mode.
*
* @param {string|null} hasBlockMovingClientId Enable/Disable block moving mode.
*/
export function* setBlockMovingClientId( hasBlockMovingClientId = null ) {
yield {
type: 'SET_BLOCK_MOVING_MODE',
hasBlockMovingClientId,
};
if ( hasBlockMovingClientId ) {
speak(
__(
'Use the Tab key and Arrow keys to choose new block location. Use Left and Right Arrow keys to move between nesting levels. Once location is selected press Enter or Space to move the block.'
)
);
}
}
/**
* Generator that triggers an action used to duplicate a list of blocks.
*
* @param {string[]} clientIds
* @param {boolean} updateSelection
*/
export function* duplicateBlocks( clientIds, updateSelection = true ) {
if ( ! clientIds && ! clientIds.length ) {
return;
}
const blocks = yield controls.select(
'core/block-editor',
'getBlocksByClientId',
clientIds
);
const rootClientId = yield controls.select(
'core/block-editor',
'getBlockRootClientId',
clientIds[ 0 ]
);
// Return early if blocks don't exist.
if ( some( blocks, ( block ) => ! block ) ) {
return;
}
const blockNames = blocks.map( ( block ) => block.name );
// Return early if blocks don't support multiple usage.
if (
some(
blockNames,
( blockName ) => ! hasBlockSupport( blockName, 'multiple', true )
)
) {
return;
}
const lastSelectedIndex = yield controls.select(
'core/block-editor',
'getBlockIndex',
last( castArray( clientIds ) ),
rootClientId
);
const clonedBlocks = blocks.map( ( block ) => cloneBlock( block ) );
yield insertBlocks(
clonedBlocks,
lastSelectedIndex + 1,
rootClientId,
updateSelection
);
if ( clonedBlocks.length > 1 && updateSelection ) {
yield multiSelect(
first( clonedBlocks ).clientId,
last( clonedBlocks ).clientId
);
}
return clonedBlocks.map( ( block ) => block.clientId );
}
/**
* Generator used to insert an empty block after a given block.
*