-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
selectors.js
1001 lines (901 loc) · 28.4 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 moment from 'moment';
import {
first,
get,
has,
isEqual,
last,
reduce,
some,
values,
keys,
without,
compact,
} from 'lodash';
import createSelector from 'rememo';
/**
* WordPress dependencies
*/
import { serialize, getBlockType } from '@wordpress/blocks';
import { __ } from '@wordpress/i18n';
import { addQueryArgs } from '@wordpress/url';
/***
* Module constants
*/
const MAX_FREQUENT_BLOCKS = 3;
/**
* Returns the current editing mode.
*
* @param {Object} state Global application state
* @return {String} Editing mode
*/
export function getEditorMode( state ) {
return getPreference( state, 'mode', 'visual' );
}
/**
* Returns the state of legacy meta boxes.
*
* @param {Object} state Global application state
* @return {Object} State of meta boxes
*/
export function getMetaBoxes( state ) {
return state.metaBoxes;
}
/**
* Returns the state of legacy meta boxes.
*
* @param {Object} state Global application state
* @param {String} location Location of the meta box.
* @return {Object} State of meta box at specified location.
*/
export function getMetaBox( state, location ) {
return getMetaBoxes( state )[ location ];
}
/**
* Returns a list of dirty meta box locations.
*
* @param {Object} state Global application state
* @return {Array} Array of locations for dirty meta boxes.
*/
export const getDirtyMetaBoxes = createSelector(
( state ) => {
return reduce( getMetaBoxes( state ), ( result, metaBox, location ) => {
return metaBox.isDirty && metaBox.isActive
? [ ...result, location ]
: result;
}, [] );
},
( state ) => state.metaBoxes,
);
/**
* Returns the dirty state of legacy meta boxes.
*
* Checks whether the entire meta box state is dirty. So if a sidebar is dirty,
* but a normal area is not dirty, this will overall return dirty.
*
* @param {Object} state Global application state
* @return {Boolean} Whether state is dirty. True if dirty, false if not.
*/
export const isMetaBoxStateDirty = ( state ) => getDirtyMetaBoxes( state ).length > 0;
/**
* Returns the current active panel for the sidebar.
*
* @param {Object} state Global application state
* @return {String} Active sidebar panel
*/
export function getActivePanel( state ) {
return state.panel;
}
/**
* Returns the preferences (these preferences are persisted locally)
*
* @param {Object} state Global application state
* @return {Object} Preferences Object
*/
export function getPreferences( state ) {
return state.preferences;
}
/**
*
* @param {Object} state Global application state
* @param {String} preferenceKey Preference Key
* @param {Mixed} defaultValue Default Value
* @return {Mixed} Preference Value
*/
export function getPreference( state, preferenceKey, defaultValue ) {
const preferences = getPreferences( state );
const value = preferences[ preferenceKey ];
return value === undefined ? defaultValue : value;
}
/**
* Returns true if the editor sidebar is open, or false otherwise.
*
* @param {Object} state Global application state
* @return {Boolean} Whether sidebar is open
*/
export function isEditorSidebarOpened( state ) {
return getPreference( state, 'isSidebarOpened' );
}
/**
* Returns true if the editor sidebar panel is open, or false otherwise.
*
* @param {Object} state Global application state
* @param {STring} panel Sidebar panel name
* @return {Boolean} Whether sidebar is open
*/
export function isEditorSidebarPanelOpened( state, panel ) {
const panels = getPreference( state, 'panels' );
return panels ? !! panels[ panel ] : false;
}
/**
* Returns true if any past editor history snapshots exist, or false otherwise.
*
* @param {Object} state Global application state
* @return {Boolean} Whether undo history exists
*/
export function hasEditorUndo( state ) {
return state.editor.history.past.length > 0;
}
/**
* Returns true if any future editor history snapshots exist, or false
* otherwise.
*
* @param {Object} state Global application state
* @return {Boolean} Whether redo history exists
*/
export function hasEditorRedo( state ) {
return state.editor.history.future.length > 0;
}
/**
* Returns true if the currently edited post is yet to be saved, or false if
* the post has been saved.
*
* @param {Object} state Global application state
* @return {Boolean} Whether the post is new
*/
export function isEditedPostNew( state ) {
return getCurrentPost( state ).status === 'auto-draft';
}
/**
* Returns true if there are unsaved values for the current edit session, or
* false if the editing state matches the saved or new post.
*
* @param {Object} state Global application state
* @return {Boolean} Whether unsaved values exist
*/
export const isEditedPostDirty = createSelector(
( state ) => {
const edits = getPostEdits( state );
const currentPost = getCurrentPost( state );
const hasEditedAttributes = some( edits, ( value, key ) => {
return ! isEqual( value, currentPost[ key ] );
} );
if ( hasEditedAttributes ) {
return true;
}
if ( isMetaBoxStateDirty( state ) ) {
return true;
}
// This is a cheaper operation that still must occur after checking
// attributes, because a post initialized with attributes different
// from its saved copy should be considered dirty.
if ( ! hasEditorUndo( state ) ) {
return false;
}
// Check whether there are differences between editor from its original
// state (when history was last reset) and currently. Any difference in
// attributes, block type, order should consistute needing save.
const { history } = state.editor;
const originalEditor = history.past[ 0 ];
const currentEditor = history.present;
return some( [
'blocksByUid',
'blockOrder',
], ( key ) => ! isEqual(
originalEditor[ key ],
currentEditor[ key ]
) );
},
( state ) => [
state.editor,
state.currentPost,
state.metaBoxes,
]
);
/**
* Returns true if there are no unsaved values for the current edit session and if
* the currently edited post is new (and has never been saved before).
*
* @param {Object} state Global application state
* @return {Boolean} Whether new post and unsaved values exist
*/
export function isCleanNewPost( state ) {
return ! isEditedPostDirty( state ) && isEditedPostNew( state );
}
/**
* Returns the post currently being edited in its last known saved state, not
* including unsaved edits. Returns an object containing relevant default post
* values if the post has not yet been saved.
*
* @param {Object} state Global application state
* @return {Object} Post object
*/
export function getCurrentPost( state ) {
return state.currentPost;
}
/**
* Returns the post type of the post currently being edited
*
* @param {Object} state Global application state
* @return {String} Post type
*/
export function getCurrentPostType( state ) {
return state.currentPost.type;
}
/**
* Returns the ID of the post currently being edited, or null if the post has
* not yet been saved.
*
* @param {Object} state Global application state
* @return {?Number} ID of current post
*/
export function getCurrentPostId( state ) {
return getCurrentPost( state ).id || null;
}
/**
* Returns any post values which have been changed in the editor but not yet
* been saved.
*
* @param {Object} state Global application state
* @return {Object} Object of key value pairs comprising unsaved edits
*/
export function getPostEdits( state ) {
return state.editor.edits;
}
/**
* Returns a single attribute of the post being edited, preferring the unsaved
* edit if one exists, but falling back to the attribute for the last known
* saved state of the post.
*
* @param {Object} state Global application state
* @param {String} attributeName Post attribute name
* @return {*} Post attribute value
*/
export function getEditedPostAttribute( state, attributeName ) {
return state.editor.edits[ attributeName ] === undefined
? state.currentPost[ attributeName ]
: state.editor.edits[ attributeName ];
}
/**
* Returns the current visibility of the post being edited, preferring the
* unsaved value if different than the saved post. The return value is one of
* "private", "password", or "public".
*
* @param {Object} state Global application state
* @return {String} Post visibility
*/
export function getEditedPostVisibility( state ) {
const status = getEditedPostAttribute( state, 'status' );
const password = getEditedPostAttribute( state, 'password' );
if ( status === 'private' ) {
return 'private';
} else if ( password ) {
return 'password';
}
return 'public';
}
/**
* Return true if the current post has already been published.
*
* @param {Object} state Global application state
* @return {Boolean} Whether the post has been published
*/
export function isCurrentPostPublished( state ) {
const post = getCurrentPost( state );
return [ 'publish', 'private' ].indexOf( post.status ) !== -1 ||
( post.status === 'future' && moment( post.date ).isBefore( moment() ) );
}
/**
* Return true if the post being edited can be published
*
* @param {Object} state Global application state
* @return {Boolean} Whether the post can been published
*/
export function isEditedPostPublishable( state ) {
const post = getCurrentPost( state );
return isEditedPostDirty( state ) || [ 'publish', 'private', 'future' ].indexOf( post.status ) === -1;
}
/**
* Returns true if the post can be saved, or false otherwise. A post must
* contain a title, an excerpt, or non-empty content to be valid for save.
*
* @param {Object} state Global application state
* @return {Boolean} Whether the post can be saved
*/
export function isEditedPostSaveable( state ) {
return (
!! getEditedPostContent( state ) ||
!! getEditedPostTitle( state ) ||
!! getEditedPostExcerpt( state )
);
}
/**
* Return true if the post being edited is being scheduled. Preferring the
* unsaved status values.
*
* @param {Object} state Global application state
* @return {Boolean} Whether the post has been published
*/
export function isEditedPostBeingScheduled( state ) {
const date = getEditedPostAttribute( state, 'date' );
// Adding 1 minute as an error threshold between the server and the client dates.
const now = moment().add( 1, 'minute' );
return moment( date ).isAfter( now );
}
/**
* Returns the raw title of the post being edited, preferring the unsaved value
* if different than the saved post.
*
* @param {Object} state Global application state
* @return {String} Raw post title
*/
export function getEditedPostTitle( state ) {
const editedTitle = getPostEdits( state ).title;
if ( editedTitle !== undefined ) {
return editedTitle;
}
const currentPost = getCurrentPost( state );
if ( currentPost.title && currentPost.title ) {
return currentPost.title;
}
return '';
}
/**
* Gets the document title to be used.
*
* @param {Object} state Global application state
* @return {string} Document title
*/
export function getDocumentTitle( state ) {
let title = getEditedPostTitle( state );
if ( ! title.trim() ) {
title = isCleanNewPost( state ) ? __( 'New post' ) : __( '(Untitled)' );
}
return title;
}
/**
* Returns the raw excerpt of the post being edited, preferring the unsaved
* value if different than the saved post.
*
* @param {Object} state Global application state
* @return {String} Raw post excerpt
*/
export function getEditedPostExcerpt( state ) {
return state.editor.edits.excerpt === undefined
? state.currentPost.excerpt
: state.editor.edits.excerpt;
}
/**
* Returns a URL to preview the post being edited.
*
* @param {Object} state Global application state
* @return {String} Preview URL
*/
export function getEditedPostPreviewLink( state ) {
const link = state.currentPost.link;
if ( ! link ) {
return null;
}
return addQueryArgs( link, { preview: 'true' } );
}
/**
* Returns a block given its unique ID. This is a parsed copy of the block,
* containing its `blockName`, identifier (`uid`), 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 Global application state
* @param {String} uid Block unique ID
* @return {Object} Parsed block object
*/
export const getBlock = createSelector(
( state, uid ) => {
const block = state.editor.blocksByUid[ uid ];
if ( ! block ) {
return null;
}
const type = getBlockType( block.name );
if ( ! type || ! type.attributes ) {
return block;
}
const metaAttributes = reduce( type.attributes, ( result, value, key ) => {
if ( value && 'meta' in value ) {
result[ key ] = getPostMeta( state, value.meta );
}
return result;
}, {} );
if ( ! Object.keys( metaAttributes ).length ) {
return block;
}
return {
...block,
attributes: {
...block.attributes,
...metaAttributes,
},
};
},
( state, uid ) => [
get( state, [ 'editor', 'blocksByUid', uid ] ),
get( state, 'editor.edits.meta' ),
get( state, 'currentPost.meta' ),
]
);
function getPostMeta( state, key ) {
return has( state, [ 'editor', 'edits', 'meta', key ] )
? get( state, [ 'editor', 'edits', 'meta', key ] )
: get( state, [ 'currentPost', 'meta', key ] );
}
/**
* 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 Global application state
* @return {Object[]} Post blocks
*/
export const getBlocks = createSelector(
( state ) => {
return state.editor.blockOrder.map( ( uid ) => getBlock( state, uid ) );
},
( state ) => [
state.editor.blockOrder,
state.editor.blocksByUid,
]
);
/**
* Returns the number of blocks currently present in the post.
*
* @param {Object} state Global application state
* @return {Number} Number of blocks in the post
*/
export function getBlockCount( state ) {
return getBlockUids( state ).length;
}
/**
* 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 = getMultiSelectedBlockUids( state ).length;
if ( multiSelectedBlockCount ) {
return multiSelectedBlockCount;
}
return state.blockSelection.start ? 1 : 0;
}
/**
* 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 { start, end } = state.blockSelection;
if ( start !== end || ! start ) {
return null;
}
return getBlock( state, start );
}
/**
* Returns the current multi-selection set of blocks unique IDs, or an empty
* array if there is no multi-selection.
*
* @param {Object} state Global application state
* @return {Array} Multi-selected block unique UDs
*/
export function getMultiSelectedBlockUids( state ) {
const { blockOrder } = state.editor;
const { start, end } = state.blockSelection;
if ( start === end ) {
return [];
}
const startIndex = blockOrder.indexOf( start );
const endIndex = blockOrder.indexOf( end );
if ( startIndex > endIndex ) {
return blockOrder.slice( endIndex, startIndex + 1 );
}
return blockOrder.slice( startIndex, endIndex + 1 );
}
/**
* Returns the current multi-selection set of blocks, or an empty array if
* there is no multi-selection.
*
* @param {Object} state Global application state
* @return {Array} Multi-selected block objects
*/
export function getMultiSelectedBlocks( state ) {
return getMultiSelectedBlockUids( state ).map( ( uid ) => getBlock( state, uid ) );
}
/**
* Returns the unique ID of the first block in the multi-selection set, or null
* if there is no multi-selection.
*
* @param {Object} state Global application state
* @return {?String} First unique block ID in the multi-selection set
*/
export function getFirstMultiSelectedBlockUid( state ) {
return first( getMultiSelectedBlockUids( state ) ) || null;
}
/**
* Returns the unique ID of the last block in the multi-selection set, or null
* if there is no multi-selection.
*
* @param {Object} state Global application state
* @return {?String} Last unique block ID in the multi-selection set
*/
export function getLastMultiSelectedBlockUid( state ) {
return last( getMultiSelectedBlockUids( state ) ) || null;
}
/**
* Returns true if a multi-selection exists, and the block corresponding to the
* specified unique ID is the first block of the multi-selection set, or false
* otherwise.
*
* @param {Object} state Global application state
* @param {String} uid Block unique ID
* @return {Boolean} Whether block is first in mult-selection
*/
export function isFirstMultiSelectedBlock( state, uid ) {
return getFirstMultiSelectedBlockUid( state ) === uid;
}
/**
* Returns true if the unique ID occurs within the block multi-selection, or
* false otherwise.
*
* @param {Object} state Global application state
* @param {String} uid Block unique ID
* @return {Boolean} Whether block is in multi-selection set
*/
export function isBlockMultiSelected( state, uid ) {
return getMultiSelectedBlockUids( state ).indexOf( uid ) !== -1;
}
/**
* Returns the unique ID of the block which begins the multi-selection set, or
* null if there is no multi-selection.
*
* N.b.: This is not necessarily the first uid in the selection. See
* getFirstMultiSelectedBlockUid().
*
* @param {Object} state Global application state
* @return {?String} Unique ID of block beginning multi-selection
*/
export function getMultiSelectedBlocksStartUid( state ) {
const { start, end } = state.blockSelection;
if ( start === end ) {
return null;
}
return start || null;
}
/**
* Returns the unique ID of the block which ends the multi-selection set, or
* null if there is no multi-selection.
*
* N.b.: This is not necessarily the last uid in the selection. See
* getLastMultiSelectedBlockUid().
*
* @param {Object} state Global application state
* @return {?String} Unique ID of block ending multi-selection
*/
export function getMultiSelectedBlocksEndUid( state ) {
const { start, end } = state.blockSelection;
if ( start === end ) {
return null;
}
return end || null;
}
/**
* Returns an array containing all block unique IDs of the post being edited,
* in the order they appear in the post.
*
* @param {Object} state Global application state
* @return {Array} Ordered unique IDs of post blocks
*/
export function getBlockUids( state ) {
return state.editor.blockOrder;
}
/**
* Returns the index at which the block corresponding to the specified unique
* ID occurs within the post block order, or `-1` if the block does not exist.
*
* @param {Object} state Global application state
* @param {String} uid Block unique ID
* @return {Number} Index at which block exists in order
*/
export function getBlockIndex( state, uid ) {
return state.editor.blockOrder.indexOf( uid );
}
/**
* Returns true if the block corresponding to the specified unique ID is the
* first block of the post, or false otherwise.
*
* @param {Object} state Global application state
* @param {String} uid Block unique ID
* @return {Boolean} Whether block is first in post
*/
export function isFirstBlock( state, uid ) {
return first( state.editor.blockOrder ) === uid;
}
/**
* Returns true if the block corresponding to the specified unique ID is the
* last block of the post, or false otherwise.
*
* @param {Object} state Global application state
* @param {String} uid Block unique ID
* @return {Boolean} Whether block is last in post
*/
export function isLastBlock( state, uid ) {
return last( state.editor.blockOrder ) === uid;
}
/**
* Returns the block object occurring before the one corresponding to the
* specified unique ID.
*
* @param {Object} state Global application state
* @param {String} uid Block unique ID
* @return {Object} Block occurring before specified unique ID
*/
export function getPreviousBlock( state, uid ) {
const order = getBlockIndex( state, uid );
return state.editor.blocksByUid[ state.editor.blockOrder[ order - 1 ] ] || null;
}
/**
* Returns the block object occurring after the one corresponding to the
* specified unique ID.
*
* @param {Object} state Global application state
* @param {String} uid Block unique ID
* @return {Object} Block occurring after specified unique ID
*/
export function getNextBlock( state, uid ) {
const order = getBlockIndex( state, uid );
return state.editor.blocksByUid[ state.editor.blockOrder[ order + 1 ] ] || null;
}
/**
* Returns true if the block corresponding to the specified unique ID is
* currently selected and a multi-selection exists, null if there is no
* multi-selection active, or false if multi-selection exists, but the
* specified unique ID is not the selected block.
*
* @param {Object} state Global application state
* @param {String} uid Block unique ID
* @return {Boolean} Whether block is selected and multi-selection exists
*/
export function isBlockSelected( state, uid ) {
const { start, end } = state.blockSelection;
if ( start !== end ) {
return null;
}
return start === uid;
}
/**
* Returns true if the cursor is hovering the block corresponding to the
* specified unique ID, or false otherwise.
*
* @param {Object} state Global application state
* @param {String} uid Block unique ID
* @return {Boolean} Whether block is hovered
*/
export function isBlockHovered( state, uid ) {
return state.hoveredBlock === uid;
}
/**
* Returns focus state of the block corresponding to the specified unique ID,
* or null if the block is not selected. It is left to a block's implementation
* to manage the content of this object, defaulting to an empty object.
*
* @param {Object} state Global application state
* @param {String} uid Block unique ID
* @return {Object} Block focus state
*/
export function getBlockFocus( state, uid ) {
// If there is multi-selection, keep returning the focus object for the start block.
if ( ! isBlockSelected( state, uid ) && state.blockSelection.start !== uid ) {
return null;
}
return state.blockSelection.focus;
}
/**
* Whether in the process of multi-selecting or not.
*
* @param {Object} state Global application state
* @return {Boolean} True if multi-selecting, false if not.
*/
export function isMultiSelecting( state ) {
return state.blockSelection.isMultiSelecting;
}
/**
* Returns thee block's editing mode
*
* @param {Object} state Global application state
* @param {String} uid Block unique ID
* @return {Object} Block editing mode
*/
export function getBlockMode( state, uid ) {
return state.blocksMode[ uid ] || '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 the insertion point, the index at which the new inserted block would
* be placed. Defaults to the last position
*
* @param {Object} state Global application state
* @return {?String} Unique ID after which insertion will occur
*/
export function getBlockInsertionPoint( state ) {
if ( getEditorMode( state ) !== 'visual' ) {
return state.editor.blockOrder.length;
}
const position = getBlockSiblingInserterPosition( state );
if ( null !== position ) {
return position;
}
const lastMultiSelectedBlock = getLastMultiSelectedBlockUid( state );
if ( lastMultiSelectedBlock ) {
return getBlockIndex( state, lastMultiSelectedBlock ) + 1;
}
const selectedBlock = getSelectedBlock( state );
if ( selectedBlock ) {
return getBlockIndex( state, selectedBlock.uid ) + 1;
}
return state.editor.blockOrder.length;
}
/**
* Returns the position at which the block inserter will insert a new adjacent
* sibling block, or null if the inserter is not actively visible.
*
* @param {Object} state Global application state
* @return {?Number} Whether the inserter is currently visible
*/
export function getBlockSiblingInserterPosition( state ) {
const { position } = state.blockInsertionPoint;
if ( ! Number.isInteger( position ) ) {
return null;
}
return position;
}
/**
* 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.blockInsertionPoint.visible;
}
/**
* Returns true if the post is currently being saved, or false otherwise.
*
* @param {Object} state Global application state
* @return {Boolean} Whether post is being saved
*/
export function isSavingPost( state ) {
return state.saving.requesting;
}
/**
* Returns true if a previous post save was attempted successfully, or false
* otherwise.
*
* @param {Object} state Global application state
* @return {Boolean} Whether the post was saved successfully
*/
export function didPostSaveRequestSucceed( state ) {
return state.saving.successful;
}
/**
* Returns true if a previous post save was attempted but failed, or false
* otherwise.
*
* @param {Object} state Global application state
* @return {Boolean} Whether the post save failed
*/
export function didPostSaveRequestFail( state ) {
return !! state.saving.error;
}
/**
* Returns a suggested post format for the current post, inferred only if there
* is a single block within the post and it is of a type known to match a
* default post format. Returns null if the format cannot be determined.
*
* @param {Object} state Global application state
* @return {?String} Suggested post format
*/
export function getSuggestedPostFormat( state ) {
const blocks = state.editor.blockOrder;
let name;
// If there is only one block in the content of the post grab its name so
// so we can derive a suitable post format from it.
if ( blocks.length === 1 ) {
name = state.editor.blocksByUid[ blocks[ 0 ] ].name;
}
// We only convert to default post formats in core.
switch ( name ) {
case 'core/image':
return 'image';
case 'core/quote':
return 'quote';
}
return null;
}
/**
* Returns the content of the post being edited, preferring raw string edit
* before falling back to serialization of block state.
*
* @param {Object} state Global application state
* @return {String} Post content
*/
export const getEditedPostContent = createSelector(
( state ) => {
const edits = getPostEdits( state );
if ( 'content' in edits ) {
return edits.content;
}
return serialize( getBlocks( state ) );
},
( state ) => [
state.editor.edits.content,
state.editor.blocksByUid,
state.editor.blockOrder,
],
);
/**
* Returns the user notices array
*
* @param {Object} state Global application state
* @return {Array} List of notices
*/
export function getNotices( state ) {
return values( state.notices );
}
/**
* Resolves the list of recently used block names into a list of block type settings.
*
* @param {Object} state Global application state
* @return {Array} List of recently used blocks
*/
export function getRecentlyUsedBlocks( state ) {
// resolves the block names in the state to the block type settings
return compact( state.preferences.recentlyUsedBlocks.map( blockType => getBlockType( blockType ) ) );
}
/**
* Resolves the block usage stats into a list of the most frequently used blocks.
* Memoized so we're not generating block lists every time we render the list
* in the inserter.
*
* @param {Object} state Global application state
* @return {Array} List of block type settings
*/
export const getMostFrequentlyUsedBlocks = createSelector(
( state ) => {
const { blockUsage } = state.preferences;
const orderedByUsage = keys( blockUsage ).sort( ( a, b ) => blockUsage[ b ] - blockUsage[ a ] );
// add in paragraph and image blocks if they're not already in the usage data
return compact(
[ ...orderedByUsage, ...without( [ 'core/paragraph', 'core/image' ], ...orderedByUsage ) ]
.map( blockType => getBlockType( blockType ) )
).slice( 0, MAX_FREQUENT_BLOCKS );
},
( state ) => state.preferences.blockUsage