-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
selectors.js
1410 lines (1275 loc) · 40.9 KB
/
selectors.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,
flatMap,
first,
get,
includes,
isArray,
isBoolean,
last,
map,
orderBy,
reduce,
some,
find,
} from 'lodash';
import createSelector from 'rememo';
/**
* WordPress dependencies
*/
import {
getBlockType,
getBlockTypes,
hasBlockSupport,
parse,
} from '@wordpress/blocks';
import { SVG, Rect, G, Path } from '@wordpress/components';
// Module constants
/**
* @private
*/
export const INSERTER_UTILITY_HIGH = 3;
/**
* @private
*/
export const INSERTER_UTILITY_MEDIUM = 2;
/**
* @private
*/
export const INSERTER_UTILITY_LOW = 1;
/**
* @private
*/
export const INSERTER_UTILITY_NONE = 0;
const MILLISECONDS_PER_HOUR = 3600 * 1000;
const MILLISECONDS_PER_DAY = 24 * 3600 * 1000;
const MILLISECONDS_PER_WEEK = 7 * 24 * 3600 * 1000;
const templateIcon = <SVG xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><Rect x="0" fill="none" width="24" height="24" /><G><Path d="M19 3H5c-1.105 0-2 .895-2 2v14c0 1.105.895 2 2 2h14c1.105 0 2-.895 2-2V5c0-1.105-.895-2-2-2zM6 6h5v5H6V6zm4.5 13C9.12 19 8 17.88 8 16.5S9.12 14 10.5 14s2.5 1.12 2.5 2.5-1.12 2.5-2.5 2.5zm3-6l3-5 3 5h-6z" /></G></SVG>;
/**
* Shared reference to an empty array for cases where it is important to avoid
* returning a new array reference on every invocation, as in a connected or
* other pure component which performs `shouldComponentUpdate` check on props.
* This should be used as a last resort, since the normalized data should be
* maintained by the reducer result in state.
*
* @type {Array}
*/
const EMPTY_ARRAY = [];
/**
* Returns a block's name given its client ID, or null if no block exists with
* the client ID.
*
* @param {Object} state Editor state.
* @param {string} clientId Block client ID.
*
* @return {string} Block name.
*/
export function getBlockName( state, clientId ) {
const block = state.blocks.byClientId[ clientId ];
return block ? block.name : null;
}
/**
* Returns whether a block is valid or not.
*
* @param {Object} state Editor state.
* @param {string} clientId Block client ID.
*
* @return {boolean} Is Valid.
*/
export function isBlockValid( state, clientId ) {
const block = state.blocks.byClientId[ clientId ];
return !! block && block.isValid;
}
/**
* Returns a block's attributes given its client ID, or null if no block exists with
* the client ID.
*
* @param {Object} state Editor state.
* @param {string} clientId Block client ID.
*
* @return {Object?} Block attributes.
*/
export function getBlockAttributes( state, clientId ) {
const block = state.blocks.byClientId[ clientId ];
if ( ! block ) {
return null;
}
return state.blocks.attributes[ clientId ];
}
/**
* Returns a block given its client ID. This is a parsed copy of the block,
* containing its `blockName`, `clientId`, and current `attributes` state. This
* is not the block's registration settings, which must be retrieved from the
* blocks module registration store.
*
* @param {Object} state Editor state.
* @param {string} clientId Block client ID.
*
* @return {Object} Parsed block object.
*/
export const getBlock = createSelector(
( state, clientId ) => {
const block = state.blocks.byClientId[ clientId ];
if ( ! block ) {
return null;
}
return {
...block,
attributes: getBlockAttributes( state, clientId ),
innerBlocks: getBlocks( state, clientId ),
};
},
( state, clientId ) => [
// Normally, we'd have both `getBlockAttributes` dependancies and
// `getBlocks` (children) dependancies here but for performance reasons
// we use a denormalized cache key computed in the reducer that takes both
// the attributes and inner blocks into account. The value of the cache key
// is being changed whenever one of these dependencies is out of date.
state.blocks.cache[ clientId ],
]
);
export const __unstableGetBlockWithoutInnerBlocks = createSelector(
( state, clientId ) => {
const block = state.blocks.byClientId[ clientId ];
if ( ! block ) {
return null;
}
return {
...block,
attributes: getBlockAttributes( state, clientId ),
};
},
( state, clientId ) => [
state.blocks.byClientId[ clientId ],
state.blocks.attributes[ clientId ],
]
);
/**
* Returns all block objects for the current post being edited as an array in
* the order they appear in the post.
*
* Note: It's important to memoize this selector to avoid return a new instance
* on each call
*
* @param {Object} state Editor state.
* @param {?string} rootClientId Optional root client ID of block list.
*
* @return {Object[]} Post blocks.
*/
export const getBlocks = createSelector(
( state, rootClientId ) => {
return map(
getBlockOrder( state, rootClientId ),
( clientId ) => getBlock( state, clientId )
);
},
( state ) => [
state.blocks.byClientId,
state.blocks.order,
state.blocks.attributes,
]
);
/**
* Returns an array containing the clientIds of all descendants
* of the blocks given.
*
* @param {Object} state Global application state.
* @param {Array} clientIds Array of blocks to inspect.
*
* @return {Array} ids of descendants.
*/
export const getClientIdsOfDescendants = ( state, clientIds ) => flatMap( clientIds, ( clientId ) => {
const descendants = getBlockOrder( state, clientId );
return [ ...descendants, ...getClientIdsOfDescendants( state, descendants ) ];
} );
/**
* Returns an array containing the clientIds of the top-level blocks
* and their descendants of any depth (for nested blocks).
*
* @param {Object} state Global application state.
*
* @return {Array} ids of top-level and descendant blocks.
*/
export const getClientIdsWithDescendants = createSelector(
( state ) => {
const topLevelIds = getBlockOrder( state );
return [ ...topLevelIds, ...getClientIdsOfDescendants( state, topLevelIds ) ];
},
( state ) => [
state.blocks.order,
]
);
/**
* Returns the total number of blocks, or the total number of blocks with a specific name in a post.
* The number returned includes nested blocks.
*
* @param {Object} state Global application state.
* @param {?string} blockName Optional block name, if specified only blocks of that type will be counted.
*
* @return {number} Number of blocks in the post, or number of blocks with name equal to blockName.
*/
export const getGlobalBlockCount = createSelector(
( state, blockName ) => {
const clientIds = getClientIdsWithDescendants( state );
if ( ! blockName ) {
return clientIds.length;
}
return reduce( clientIds, ( count, clientId ) => {
const block = state.blocks.byClientId[ clientId ];
return block.name === blockName ? count + 1 : count;
}, 0 );
},
( state ) => [
state.blocks.order,
state.blocks.byClientId,
]
);
/**
* Given an array of block client IDs, returns the corresponding array of block
* objects.
*
* @param {Object} state Editor state.
* @param {string[]} clientIds Client IDs for which blocks are to be returned.
*
* @return {WPBlock[]} Block objects.
*/
export const getBlocksByClientId = createSelector(
( state, clientIds ) => map(
castArray( clientIds ),
( clientId ) => getBlock( state, clientId )
),
( state ) => [
state.blocks.byClientId,
state.blocks.order,
state.blocks.attributes,
]
);
/**
* Returns the number of blocks currently present in the post.
*
* @param {Object} state Editor state.
* @param {?string} rootClientId Optional root client ID of block list.
*
* @return {number} Number of blocks in the post.
*/
export function getBlockCount( state, rootClientId ) {
return getBlockOrder( state, rootClientId ).length;
}
/**
* @typedef {WPBlockSelection} A block selection object.
*
* @property {string} clientId A block client ID.
* @property {string} attributeKey A block attribute key.
* @property {number} offset A block attribute offset.
*/
/**
* Returns the current selection start block client ID, attribute key and text
* offset.
*
* @param {Object} state Block editor state.
*
* @return {WPBlockSelection} Selection start information.
*/
export function getSelectionStart( state ) {
return state.selectionStart;
}
/**
* Returns the current selection end block client ID, attribute key and text
* offset.
*
* @param {Object} state Block editor state.
*
* @return {WPBlockSelection} Selection end information.
*/
export function getSelectionEnd( state ) {
return state.selectionEnd;
}
/**
* Returns the current block selection start. This value may be null, and it
* may represent either a singular block selection or multi-selection start.
* A selection is singular if its start and end match.
*
* @param {Object} state Global application state.
*
* @return {?string} Client ID of block selection start.
*/
export function getBlockSelectionStart( state ) {
return state.selectionStart.clientId;
}
/**
* Returns the current block selection end. This value may be null, and it
* may represent either a singular block selection or multi-selection end.
* A selection is singular if its start and end match.
*
* @param {Object} state Global application state.
*
* @return {?string} Client ID of block selection end.
*/
export function getBlockSelectionEnd( state ) {
return state.selectionEnd.clientId;
}
/**
* Returns the number of blocks currently selected in the post.
*
* @param {Object} state Global application state.
*
* @return {number} Number of blocks selected in the post.
*/
export function getSelectedBlockCount( state ) {
const multiSelectedBlockCount = getMultiSelectedBlockClientIds( state ).length;
if ( multiSelectedBlockCount ) {
return multiSelectedBlockCount;
}
return state.selectionStart.clientId ? 1 : 0;
}
/**
* Returns true if there is a single selected block, or false otherwise.
*
* @param {Object} state Editor state.
*
* @return {boolean} Whether a single block is selected.
*/
export function hasSelectedBlock( state ) {
const { selectionStart, selectionEnd } = state;
return (
!! selectionStart.clientId &&
selectionStart.clientId === selectionEnd.clientId
);
}
/**
* Returns the currently selected block client ID, or null if there is no
* selected block.
*
* @param {Object} state Editor state.
*
* @return {?string} Selected block client ID.
*/
export function getSelectedBlockClientId( state ) {
const { selectionStart, selectionEnd } = state;
const { clientId } = selectionStart;
if ( ! clientId || clientId !== selectionEnd.clientId ) {
return null;
}
return clientId;
}
/**
* Returns the currently selected block, or null if there is no selected block.
*
* @param {Object} state Global application state.
*
* @return {?Object} Selected block.
*/
export function getSelectedBlock( state ) {
const clientId = getSelectedBlockClientId( state );
return clientId ? getBlock( state, clientId ) : null;
}
/**
* Given a block client ID, returns the root block from which the block is
* nested, an empty string for top-level blocks, or null if the block does not
* exist.
*
* @param {Object} state Editor state.
* @param {string} clientId Block from which to find root client ID.
*
* @return {?string} Root client ID, if exists
*/
export function getBlockRootClientId( state, clientId ) {
return state.blocks.parents[ clientId ] !== undefined ?
state.blocks.parents[ clientId ] :
null;
}
/**
* Given a block client ID, returns the root of the hierarchy from which the block is nested, return the block itself for root level blocks.
*
* @param {Object} state Editor state.
* @param {string} clientId Block from which to find root client ID.
*
* @return {string} Root client ID
*/
export function getBlockHierarchyRootClientId( state, clientId ) {
let current = clientId;
let parent;
do {
parent = current;
current = state.blocks.parents[ current ];
} while ( current );
return parent;
}
/**
* Returns the client ID of the block adjacent one at the given reference
* startClientId and modifier directionality. Defaults start startClientId to
* the selected block, and direction as next block. Returns null if there is no
* adjacent block.
*
* @param {Object} state Editor state.
* @param {?string} startClientId Optional client ID of block from which to
* search.
* @param {?number} modifier Directionality multiplier (1 next, -1
* previous).
*
* @return {?string} Return the client ID of the block, or null if none exists.
*/
export function getAdjacentBlockClientId( state, startClientId, modifier = 1 ) {
// Default to selected block.
if ( startClientId === undefined ) {
startClientId = getSelectedBlockClientId( state );
}
// Try multi-selection starting at extent based on modifier.
if ( startClientId === undefined ) {
if ( modifier < 0 ) {
startClientId = getFirstMultiSelectedBlockClientId( state );
} else {
startClientId = getLastMultiSelectedBlockClientId( state );
}
}
// Validate working start client ID.
if ( ! startClientId ) {
return null;
}
// Retrieve start block root client ID, being careful to allow the falsey
// empty string top-level root by explicitly testing against null.
const rootClientId = getBlockRootClientId( state, startClientId );
if ( rootClientId === null ) {
return null;
}
const { order } = state.blocks;
const orderSet = order[ rootClientId ];
const index = orderSet.indexOf( startClientId );
const nextIndex = ( index + ( 1 * modifier ) );
// Block was first in set and we're attempting to get previous.
if ( nextIndex < 0 ) {
return null;
}
// Block was last in set and we're attempting to get next.
if ( nextIndex === orderSet.length ) {
return null;
}
// Assume incremented index is within the set.
return orderSet[ nextIndex ];
}
/**
* Returns the previous block's client ID from the given reference start ID.
* Defaults start to the selected block. Returns null if there is no previous
* block.
*
* @param {Object} state Editor state.
* @param {?string} startClientId Optional client ID of block from which to
* search.
*
* @return {?string} Adjacent block's client ID, or null if none exists.
*/
export function getPreviousBlockClientId( state, startClientId ) {
return getAdjacentBlockClientId( state, startClientId, -1 );
}
/**
* Returns the next block's client ID from the given reference start ID.
* Defaults start to the selected block. Returns null if there is no next
* block.
*
* @param {Object} state Editor state.
* @param {?string} startClientId Optional client ID of block from which to
* search.
*
* @return {?string} Adjacent block's client ID, or null if none exists.
*/
export function getNextBlockClientId( state, startClientId ) {
return getAdjacentBlockClientId( state, startClientId, 1 );
}
/**
* Returns the initial caret position for the selected block.
* This position is to used to position the caret properly when the selected block changes.
*
* @param {Object} state Global application state.
*
* @return {?Object} Selected block.
*/
export function getSelectedBlocksInitialCaretPosition( state ) {
return state.initialPosition;
}
/**
* Returns the current selection set of block client IDs (multiselection or single selection).
*
* @param {Object} state Editor state.
*
* @return {Array} Multi-selected block client IDs.
*/
export const getSelectedBlockClientIds = createSelector(
( state ) => {
const { selectionStart, selectionEnd } = state;
if (
selectionStart.clientId === undefined ||
selectionEnd.clientId === undefined
) {
return EMPTY_ARRAY;
}
if ( selectionStart.clientId === selectionEnd.clientId ) {
return [ selectionStart.clientId ];
}
// Retrieve root client ID to aid in retrieving relevant nested block
// order, being careful to allow the falsey empty string top-level root
// by explicitly testing against null.
const rootClientId = getBlockRootClientId( state, selectionStart.clientId );
if ( rootClientId === null ) {
return EMPTY_ARRAY;
}
const blockOrder = getBlockOrder( state, rootClientId );
const startIndex = blockOrder.indexOf( selectionStart.clientId );
const endIndex = blockOrder.indexOf( selectionEnd.clientId );
if ( startIndex > endIndex ) {
return blockOrder.slice( endIndex, startIndex + 1 );
}
return blockOrder.slice( startIndex, endIndex + 1 );
},
( state ) => [
state.blocks.order,
state.selectionStart.clientId,
state.selectionEnd.clientId,
],
);
/**
* Returns the current multi-selection set of block client IDs, or an empty
* array if there is no multi-selection.
*
* @param {Object} state Editor state.
*
* @return {Array} Multi-selected block client IDs.
*/
export function getMultiSelectedBlockClientIds( state ) {
const { selectionStart, selectionEnd } = state;
if ( selectionStart.clientId === selectionEnd.clientId ) {
return EMPTY_ARRAY;
}
return getSelectedBlockClientIds( state );
}
/**
* Returns the current multi-selection set of blocks, or an empty array if
* there is no multi-selection.
*
* @param {Object} state Editor state.
*
* @return {Array} Multi-selected block objects.
*/
export const getMultiSelectedBlocks = createSelector(
( state ) => {
const multiSelectedBlockClientIds = getMultiSelectedBlockClientIds( state );
if ( ! multiSelectedBlockClientIds.length ) {
return EMPTY_ARRAY;
}
return multiSelectedBlockClientIds.map( ( clientId ) => getBlock( state, clientId ) );
},
( state ) => [
...getSelectedBlockClientIds.getDependants( state ),
state.blocks.byClientId,
state.blocks.order,
state.blocks.attributes,
]
);
/**
* Returns the client ID of the first block in the multi-selection set, or null
* if there is no multi-selection.
*
* @param {Object} state Editor state.
*
* @return {?string} First block client ID in the multi-selection set.
*/
export function getFirstMultiSelectedBlockClientId( state ) {
return first( getMultiSelectedBlockClientIds( state ) ) || null;
}
/**
* Returns the client ID of the last block in the multi-selection set, or null
* if there is no multi-selection.
*
* @param {Object} state Editor state.
*
* @return {?string} Last block client ID in the multi-selection set.
*/
export function getLastMultiSelectedBlockClientId( state ) {
return last( getMultiSelectedBlockClientIds( state ) ) || null;
}
/**
* Returns true if a multi-selection exists, and the block corresponding to the
* specified client ID is the first block of the multi-selection set, or false
* otherwise.
*
* @param {Object} state Editor state.
* @param {string} clientId Block client ID.
*
* @return {boolean} Whether block is first in multi-selection.
*/
export function isFirstMultiSelectedBlock( state, clientId ) {
return getFirstMultiSelectedBlockClientId( state ) === clientId;
}
/**
* Returns true if the client ID occurs within the block multi-selection, or
* false otherwise.
*
* @param {Object} state Editor state.
* @param {string} clientId Block client ID.
*
* @return {boolean} Whether block is in multi-selection set.
*/
export function isBlockMultiSelected( state, clientId ) {
return getMultiSelectedBlockClientIds( state ).indexOf( clientId ) !== -1;
}
/**
* Returns true if an ancestor of the block is multi-selected, or false
* otherwise.
*
* @param {Object} state Editor state.
* @param {string} clientId Block client ID.
*
* @return {boolean} Whether an ancestor of the block is in multi-selection
* set.
*/
export const isAncestorMultiSelected = createSelector(
( state, clientId ) => {
let ancestorClientId = clientId;
let isMultiSelected = false;
while ( ancestorClientId && ! isMultiSelected ) {
ancestorClientId = getBlockRootClientId( state, ancestorClientId );
isMultiSelected = isBlockMultiSelected( state, ancestorClientId );
}
return isMultiSelected;
},
( state ) => [
state.blocks.order,
state.selectionStart.clientId,
state.selectionEnd.clientId,
],
);
/**
* Returns the client ID of the block which begins the multi-selection set, or
* null if there is no multi-selection.
*
* This is not necessarily the first client ID in the selection.
*
* @see getFirstMultiSelectedBlockClientId
*
* @param {Object} state Editor state.
*
* @return {?string} Client ID of block beginning multi-selection.
*/
export function getMultiSelectedBlocksStartClientId( state ) {
const { selectionStart, selectionEnd } = state;
if ( selectionStart.clientId === selectionEnd.clientId ) {
return null;
}
return selectionStart.clientId || null;
}
/**
* Returns the client ID of the block which ends the multi-selection set, or
* null if there is no multi-selection.
*
* This is not necessarily the last client ID in the selection.
*
* @see getLastMultiSelectedBlockClientId
*
* @param {Object} state Editor state.
*
* @return {?string} Client ID of block ending multi-selection.
*/
export function getMultiSelectedBlocksEndClientId( state ) {
const { selectionStart, selectionEnd } = state;
if ( selectionStart.clientId === selectionEnd.clientId ) {
return null;
}
return selectionEnd.clientId || null;
}
/**
* Returns an array containing all block client IDs in the editor in the order
* they appear. Optionally accepts a root client ID of the block list for which
* the order should be returned, defaulting to the top-level block order.
*
* @param {Object} state Editor state.
* @param {?string} rootClientId Optional root client ID of block list.
*
* @return {Array} Ordered client IDs of editor blocks.
*/
export function getBlockOrder( state, rootClientId ) {
return state.blocks.order[ rootClientId || '' ] || EMPTY_ARRAY;
}
/**
* Returns the index at which the block corresponding to the specified client
* ID occurs within the block order, or `-1` if the block does not exist.
*
* @param {Object} state Editor state.
* @param {string} clientId Block client ID.
* @param {?string} rootClientId Optional root client ID of block list.
*
* @return {number} Index at which block exists in order.
*/
export function getBlockIndex( state, clientId, rootClientId ) {
return getBlockOrder( state, rootClientId ).indexOf( clientId );
}
/**
* Returns true if the block corresponding to the specified client ID is
* currently selected and no multi-selection exists, or false otherwise.
*
* @param {Object} state Editor state.
* @param {string} clientId Block client ID.
*
* @return {boolean} Whether block is selected and multi-selection exists.
*/
export function isBlockSelected( state, clientId ) {
const { selectionStart, selectionEnd } = state;
if ( selectionStart.clientId !== selectionEnd.clientId ) {
return false;
}
return selectionStart.clientId === clientId;
}
/**
* Returns true if one of the block's inner blocks is selected.
*
* @param {Object} state Editor state.
* @param {string} clientId Block client ID.
* @param {boolean} deep Perform a deep check.
*
* @return {boolean} Whether the block as an inner block selected
*/
export function hasSelectedInnerBlock( state, clientId, deep = false ) {
return some(
getBlockOrder( state, clientId ),
( innerClientId ) => (
isBlockSelected( state, innerClientId ) ||
isBlockMultiSelected( state, innerClientId ) ||
( deep && hasSelectedInnerBlock( state, innerClientId, deep ) )
)
);
}
/**
* Returns true if the block corresponding to the specified client ID is
* currently selected but isn't the last of the selected blocks. Here "last"
* refers to the block sequence in the document, _not_ the sequence of
* multi-selection, which is why `state.selectionEnd` isn't used.
*
* @param {Object} state Editor state.
* @param {string} clientId Block client ID.
*
* @return {boolean} Whether block is selected and not the last in the
* selection.
*/
export function isBlockWithinSelection( state, clientId ) {
if ( ! clientId ) {
return false;
}
const clientIds = getMultiSelectedBlockClientIds( state );
const index = clientIds.indexOf( clientId );
return index > -1 && index < clientIds.length - 1;
}
/**
* Returns true if a multi-selection has been made, or false otherwise.
*
* @param {Object} state Editor state.
*
* @return {boolean} Whether multi-selection has been made.
*/
export function hasMultiSelection( state ) {
const { selectionStart, selectionEnd } = state;
return selectionStart.clientId !== selectionEnd.clientId;
}
/**
* Whether in the process of multi-selecting or not. This flag is only true
* while the multi-selection is being selected (by mouse move), and is false
* once the multi-selection has been settled.
*
* @see hasMultiSelection
*
* @param {Object} state Global application state.
*
* @return {boolean} True if multi-selecting, false if not.
*/
export function isMultiSelecting( state ) {
return state.isMultiSelecting;
}
/**
* Selector that returns if multi-selection is enabled or not.
*
* @param {Object} state Global application state.
*
* @return {boolean} True if it should be possible to multi-select blocks, false if multi-selection is disabled.
*/
export function isSelectionEnabled( state ) {
return state.isSelectionEnabled;
}
/**
* Returns the block's editing mode, defaulting to "visual" if not explicitly
* assigned.
*
* @param {Object} state Editor state.
* @param {string} clientId Block client ID.
*
* @return {Object} Block editing mode.
*/
export function getBlockMode( state, clientId ) {
return state.blocksMode[ clientId ] || 'visual';
}
/**
* Returns true if the user is typing, or false otherwise.
*
* @param {Object} state Global application state.
*
* @return {boolean} Whether user is typing.
*/
export function isTyping( state ) {
return state.isTyping;
}
/**
* Returns true if the caret is within formatted text, or false otherwise.
*
* @param {Object} state Global application state.
*
* @return {boolean} Whether the caret is within formatted text.
*/
export function isCaretWithinFormattedText( state ) {
return state.isCaretWithinFormattedText;
}
/**
* Returns the insertion point, the index at which the new inserted block would
* be placed. Defaults to the last index.
*
* @param {Object} state Editor state.
*
* @return {Object} Insertion point object with `rootClientId`, `index`.
*/
export function getBlockInsertionPoint( state ) {
let rootClientId, index;
const { insertionPoint, selectionEnd } = state;
if ( insertionPoint !== null ) {
return insertionPoint;
}
const { clientId } = selectionEnd;
if ( clientId ) {
rootClientId = getBlockRootClientId( state, clientId ) || undefined;
index = getBlockIndex( state, selectionEnd.clientId, rootClientId ) + 1;
} else {
index = getBlockOrder( state ).length;
}
return { rootClientId, index };
}
/**
* Returns true if we should show the block insertion point.
*
* @param {Object} state Global application state.
*
* @return {?boolean} Whether the insertion point is visible or not.
*/
export function isBlockInsertionPointVisible( state ) {
return state.insertionPoint !== null;
}
/**
* Returns whether the blocks matches the template or not.
*
* @param {boolean} state
* @return {?boolean} Whether the template is valid or not.
*/
export function isValidTemplate( state ) {
return state.template.isValid;
}
/**
* Returns the defined block template
*
* @param {boolean} state
* @return {?Array} Block Template
*/
export function getTemplate( state ) {
return state.settings.template;
}
/**
* Returns the defined block template lock. Optionally accepts a root block
* client ID as context, otherwise defaulting to the global context.
*
* @param {Object} state Editor state.
* @param {?string} rootClientId Optional block root client ID.
*
* @return {?string} Block Template Lock
*/
export function getTemplateLock( state, rootClientId ) {
if ( ! rootClientId ) {
return state.settings.templateLock;
}
const blockListSettings = getBlockListSettings( state, rootClientId );
if ( ! blockListSettings ) {
return null;
}
return blockListSettings.templateLock;
}
/**
* Determines if the given block type is allowed to be inserted into the block list.
* This function is not exported and not memoized because using a memoized selector
* inside another memoized selector is just a waste of time.
*
* @param {Object} state Editor state.