-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
selectors.ts
1277 lines (1203 loc) · 31.3 KB
/
selectors.ts
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 createSelector from 'rememo';
import { set, map, get } from 'lodash';
/**
* WordPress dependencies
*/
import { createRegistrySelector } from '@wordpress/data';
import { addQueryArgs } from '@wordpress/url';
import deprecated from '@wordpress/deprecated';
/**
* Internal dependencies
*/
import { STORE_NAME } from './name';
import { getQueriedItems } from './queried-data';
import { DEFAULT_ENTITY_KEY } from './entities';
import { getNormalizedCommaSeparable, isRawAttribute } from './utils';
import type * as ET from './entity-types';
// This is an incomplete, high-level approximation of the State type.
// It makes the selectors slightly more safe, but is intended to evolve
// into a more detailed representation over time.
// See https://github.com/WordPress/gutenberg/pull/40025#discussion_r865410589 for more context.
export interface State {
autosaves: Record< string | number, Array< unknown > >;
blockPatterns: Array< unknown >;
blockPatternCategories: Array< unknown >;
currentGlobalStylesId: string;
currentTheme: string;
currentUser: ET.User< 'edit' >;
embedPreviews: Record< string, { html: string } >;
entities: EntitiesState;
themeBaseGlobalStyles: Record< string, Object >;
themeGlobalStyleVariations: Record< string, string >;
undo: UndoState;
users: UserState;
}
type EntityRecordKey = string | number;
interface EntitiesState {
config: EntityConfig[];
records: Record< string, Record< string, EntityState< ET.EntityRecord > > >;
}
interface EntityState< EntityRecord extends ET.EntityRecord > {
edits: Record< string, Partial< EntityRecord > >;
saving: Record< string, { pending: boolean } >;
}
interface EntityConfig {
name: string;
kind: string;
}
interface UndoState extends Array< Object > {
flattenedUndo: unknown;
offset: number;
}
interface UserState {
queries: Record< string, EntityRecordKey[] >;
byId: Record< EntityRecordKey, ET.User< 'edit' > >;
}
type Optional< T > = T | undefined;
/**
* HTTP Query parameters sent with the API request to fetch the entity records.
*/
type GetRecordsHttpQuery = Record< string, any >;
/**
* Shared reference to an empty object for cases where it is important to avoid
* returning a new object 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.
*/
const EMPTY_OBJECT = {};
/**
* Returns true if a request is in progress for embed preview data, or false
* otherwise.
*
* @param state Data state.
* @param url URL the preview would be for.
*
* @return Whether a request is in progress for an embed preview.
*/
export const isRequestingEmbedPreview = createRegistrySelector(
( select: any ) =>
( state: State, url: string ): boolean => {
return select( STORE_NAME ).isResolving( 'getEmbedPreview', [
url,
] );
}
);
/**
* Returns all available authors.
*
* @deprecated since 11.3. Callers should use `select( 'core' ).getUsers({ who: 'authors' })` instead.
*
* @param state Data state.
* @param query Optional object of query parameters to
* include with request.
* @return Authors list.
*/
export function getAuthors(
state: State,
query?: GetRecordsHttpQuery
): ET.User[] {
deprecated( "select( 'core' ).getAuthors()", {
since: '5.9',
alternative: "select( 'core' ).getUsers({ who: 'authors' })",
} );
const path = addQueryArgs(
'/wp/v2/users/?who=authors&per_page=100',
query
);
return getUserQueryResults( state, path );
}
/**
* Returns the current user.
*
* @param state Data state.
*
* @return Current user object.
*/
export function getCurrentUser( state: State ): ET.User< 'edit' > {
return state.currentUser;
}
/**
* Returns all the users returned by a query ID.
*
* @param state Data state.
* @param queryID Query ID.
*
* @return Users list.
*/
export const getUserQueryResults = createSelector(
( state: State, queryID: string ): ET.User< 'edit' >[] => {
const queryResults = state.users.queries[ queryID ];
return map( queryResults, ( id ) => state.users.byId[ id ] );
},
( state: State, queryID: string ) => [
state.users.queries[ queryID ],
state.users.byId,
]
);
/**
* Returns the loaded entities for the given kind.
*
* @deprecated since WordPress 6.0. Use getEntitiesConfig instead
* @param state Data state.
* @param kind Entity kind.
*
* @return Array of entities with config matching kind.
*/
export function getEntitiesByKind( state: State, kind: string ): Array< any > {
deprecated( "wp.data.select( 'core' ).getEntitiesByKind()", {
since: '6.0',
alternative: "wp.data.select( 'core' ).getEntitiesConfig()",
} );
return getEntitiesConfig( state, kind );
}
/**
* Returns the loaded entities for the given kind.
*
* @param state Data state.
* @param kind Entity kind.
*
* @return Array of entities with config matching kind.
*/
export function getEntitiesConfig( state: State, kind: string ): Array< any > {
return state.entities.config.filter( ( entity ) => entity.kind === kind );
}
/**
* Returns the entity config given its kind and name.
*
* @deprecated since WordPress 6.0. Use getEntityConfig instead
* @param state Data state.
* @param kind Entity kind.
* @param name Entity name.
*
* @return Entity config
*/
export function getEntity( state: State, kind: string, name: string ): any {
deprecated( "wp.data.select( 'core' ).getEntity()", {
since: '6.0',
alternative: "wp.data.select( 'core' ).getEntityConfig()",
} );
return getEntityConfig( state, kind, name );
}
/**
* Returns the entity config given its kind and name.
*
* @param state Data state.
* @param kind Entity kind.
* @param name Entity name.
*
* @return Entity config
*/
export function getEntityConfig(
state: State,
kind: string,
name: string
): any {
return state.entities.config?.find(
( config ) => config.kind === kind && config.name === name
);
}
/**
* GetEntityRecord is declared as a *callable interface* with
* two signatures to work around the fact that TypeScript doesn't
* allow currying generic functions:
*
* ```ts
* type CurriedState = F extends ( state: any, ...args: infer P ) => infer R
* ? ( ...args: P ) => R
* : F;
* type Selector = <K extends string | number>(
* state: any,
* kind: K,
* key: K extends string ? 'string value' : false
* ) => K;
* type BadlyInferredSignature = CurriedState< Selector >
* // BadlyInferredSignature evaluates to:
* // (kind: string number, key: false | "string value") => string number
* ```
*
* The signature without the state parameter shipped as CurriedSignature
* is used in the return value of `select( coreStore )`.
*
* See https://github.com/WordPress/gutenberg/pull/41578 for more details.
*/
export interface GetEntityRecord {
<
EntityRecord extends
| ET.EntityRecord< any >
| Partial< ET.EntityRecord< any > >
>(
state: State,
kind: string,
name: string,
key: EntityRecordKey,
query?: GetRecordsHttpQuery
): EntityRecord | undefined;
CurriedSignature: <
EntityRecord extends
| ET.EntityRecord< any >
| Partial< ET.EntityRecord< any > >
>(
kind: string,
name: string,
key: EntityRecordKey,
query?: GetRecordsHttpQuery
) => EntityRecord | undefined;
}
/**
* Returns the Entity's record object by key. Returns `null` if the value is not
* yet received, undefined if the value entity is known to not exist, or the
* entity object if it exists and is received.
*
* @param state State tree
* @param kind Entity kind.
* @param name Entity name.
* @param key Record's key
* @param query Optional query. If requesting specific
* fields, fields must always include the ID.
*
* @return Record.
*/
export const getEntityRecord = createSelector(
( <
EntityRecord extends
| ET.EntityRecord< any >
| Partial< ET.EntityRecord< any > >
>(
state: State,
kind: string,
name: string,
key: EntityRecordKey,
query?: GetRecordsHttpQuery
): EntityRecord | undefined => {
const queriedState = get( state.entities.records, [
kind,
name,
'queriedData',
] );
if ( ! queriedState ) {
return undefined;
}
const context = query?.context ?? 'default';
if ( query === undefined ) {
// If expecting a complete item, validate that completeness.
if ( ! queriedState.itemIsComplete[ context ]?.[ key ] ) {
return undefined;
}
return queriedState.items[ context ][ key ];
}
const item = queriedState.items[ context ]?.[ key ];
if ( item && query._fields ) {
const filteredItem = {};
const fields = getNormalizedCommaSeparable( query._fields ) ?? [];
for ( let f = 0; f < fields.length; f++ ) {
const field = fields[ f ].split( '.' );
const value = get( item, field );
set( filteredItem, field, value );
}
return filteredItem as EntityRecord;
}
return item;
} ) as GetEntityRecord,
( state: State, kind, name, recordId, query ) => {
const context = query?.context ?? 'default';
return [
get( state.entities.records, [
kind,
name,
'queriedData',
'items',
context,
recordId,
] ),
get( state.entities.records, [
kind,
name,
'queriedData',
'itemIsComplete',
context,
recordId,
] ),
];
}
) as GetEntityRecord;
/**
* Returns the Entity's record object by key. Doesn't trigger a resolver nor requests the entity records from the API if the entity record isn't available in the local state.
*
* @param state State tree
* @param kind Entity kind.
* @param name Entity name.
* @param key Record's key
*
* @return Record.
*/
export function __experimentalGetEntityRecordNoResolver<
EntityRecord extends ET.EntityRecord< any >
>( state: State, kind: string, name: string, key: EntityRecordKey ) {
return getEntityRecord< EntityRecord >( state, kind, name, key );
}
/**
* Returns the entity's record object by key,
* with its attributes mapped to their raw values.
*
* @param state State tree.
* @param kind Entity kind.
* @param name Entity name.
* @param key Record's key.
*
* @return Object with the entity's raw attributes.
*/
export const getRawEntityRecord = createSelector(
< EntityRecord extends ET.EntityRecord< any > >(
state: State,
kind: string,
name: string,
key: EntityRecordKey
): EntityRecord | undefined => {
const record = getEntityRecord< EntityRecord >(
state,
kind,
name,
key
);
return (
record &&
Object.keys( record ).reduce( ( accumulator, _key ) => {
if (
isRawAttribute( getEntityConfig( state, kind, name ), _key )
) {
// Because edits are the "raw" attribute values,
// we return those from record selectors to make rendering,
// comparisons, and joins with edits easier.
accumulator[ _key ] = get(
record[ _key ],
'raw',
record[ _key ]
);
} else {
accumulator[ _key ] = record[ _key ];
}
return accumulator;
}, {} as any )
);
},
(
state: State,
kind: string,
name: string,
recordId: EntityRecordKey,
query?: GetRecordsHttpQuery
) => {
const context = query?.context ?? 'default';
return [
state.entities.config,
get( state.entities.records, [
kind,
name,
'queriedData',
'items',
context,
recordId,
] ),
get( state.entities.records, [
kind,
name,
'queriedData',
'itemIsComplete',
context,
recordId,
] ),
];
}
);
/**
* Returns true if records have been received for the given set of parameters,
* or false otherwise.
*
* @param state State tree
* @param kind Entity kind.
* @param name Entity name.
* @param query Optional terms query.
*
* @return Whether entity records have been received.
*/
export function hasEntityRecords(
state: State,
kind: string,
name: string,
query?: GetRecordsHttpQuery
): boolean {
return Array.isArray( getEntityRecords( state, kind, name, query ) );
}
/**
* GetEntityRecord is declared as a *callable interface* with
* two signatures to work around the fact that TypeScript doesn't
* allow currying generic functions.
*
* @see GetEntityRecord
* @see https://github.com/WordPress/gutenberg/pull/41578
*/
export interface GetEntityRecords {
<
EntityRecord extends
| ET.EntityRecord< any >
| Partial< ET.EntityRecord< any > >
>(
state: State,
kind: string,
name: string,
query?: GetRecordsHttpQuery
): EntityRecord[] | null;
CurriedSignature: <
EntityRecord extends
| ET.EntityRecord< any >
| Partial< ET.EntityRecord< any > >
>(
kind: string,
name: string,
query?: GetRecordsHttpQuery
) => EntityRecord[] | null;
}
/**
* Returns the Entity's records.
*
* @param state State tree
* @param kind Entity kind.
* @param name Entity name.
* @param query Optional terms query. If requesting specific
* fields, fields must always include the ID.
*
* @return Records.
*/
export const getEntityRecords = ( <
EntityRecord extends
| ET.EntityRecord< any >
| Partial< ET.EntityRecord< any > >
>(
state: State,
kind: string,
name: string,
query: GetRecordsHttpQuery
): EntityRecord[] | null => {
// Queried data state is prepopulated for all known entities. If this is not
// assigned for the given parameters, then it is known to not exist.
const queriedState = get( state.entities.records, [
kind,
name,
'queriedData',
] );
if ( ! queriedState ) {
return null;
}
return getQueriedItems( queriedState, query );
} ) as GetEntityRecords;
type DirtyEntityRecord = {
title: string;
key: EntityRecordKey;
name: string;
kind: string;
};
/**
* Returns the list of dirty entity records.
*
* @param state State tree.
*
* @return The list of updated records
*/
export const __experimentalGetDirtyEntityRecords = createSelector(
( state: State ): Array< DirtyEntityRecord > => {
const {
entities: { records },
} = state;
const dirtyRecords: DirtyEntityRecord[] = [];
Object.keys( records ).forEach( ( kind ) => {
Object.keys( records[ kind ] ).forEach( ( name ) => {
const primaryKeys = (
Object.keys( records[ kind ][ name ].edits ) as string[]
).filter(
( primaryKey ) =>
// The entity record must exist (not be deleted),
// and it must have edits.
getEntityRecord( state, kind, name, primaryKey ) &&
hasEditsForEntityRecord( state, kind, name, primaryKey )
);
if ( primaryKeys.length ) {
const entityConfig = getEntityConfig( state, kind, name );
primaryKeys.forEach( ( primaryKey ) => {
const entityRecord = getEditedEntityRecord(
state,
kind,
name,
primaryKey
);
dirtyRecords.push( {
// We avoid using primaryKey because it's transformed into a string
// when it's used as an object key.
key: entityRecord
? entityRecord[
entityConfig.key || DEFAULT_ENTITY_KEY
]
: undefined,
title:
entityConfig?.getTitle?.( entityRecord ) || '',
name,
kind,
} );
} );
}
} );
} );
return dirtyRecords;
},
( state ) => [ state.entities.records ]
);
/**
* Returns the list of entities currently being saved.
*
* @param state State tree.
*
* @return The list of records being saved.
*/
export const __experimentalGetEntitiesBeingSaved = createSelector(
( state: State ): Array< DirtyEntityRecord > => {
const {
entities: { records },
} = state;
const recordsBeingSaved: DirtyEntityRecord[] = [];
Object.keys( records ).forEach( ( kind ) => {
Object.keys( records[ kind ] ).forEach( ( name ) => {
const primaryKeys = (
Object.keys( records[ kind ][ name ].saving ) as string[]
).filter( ( primaryKey ) =>
isSavingEntityRecord( state, kind, name, primaryKey )
);
if ( primaryKeys.length ) {
const entityConfig = getEntityConfig( state, kind, name );
primaryKeys.forEach( ( primaryKey ) => {
const entityRecord = getEditedEntityRecord(
state,
kind,
name,
primaryKey
);
recordsBeingSaved.push( {
// We avoid using primaryKey because it's transformed into a string
// when it's used as an object key.
key: entityRecord
? entityRecord[
entityConfig.key || DEFAULT_ENTITY_KEY
]
: undefined,
title:
entityConfig?.getTitle?.( entityRecord ) || '',
name,
kind,
} );
} );
}
} );
} );
return recordsBeingSaved;
},
( state ) => [ state.entities.records ]
);
/**
* Returns the specified entity record's edits.
*
* @param state State tree.
* @param kind Entity kind.
* @param name Entity name.
* @param recordId Record ID.
*
* @return The entity record's edits.
*/
export function getEntityRecordEdits(
state: State,
kind: string,
name: string,
recordId: EntityRecordKey
): Optional< any > {
return get( state.entities.records, [
kind,
name,
'edits',
recordId as string | number,
] );
}
/**
* Returns the specified entity record's non transient edits.
*
* Transient edits don't create an undo level, and
* are not considered for change detection.
* They are defined in the entity's config.
*
* @param state State tree.
* @param kind Entity kind.
* @param name Entity name.
* @param recordId Record ID.
*
* @return The entity record's non transient edits.
*/
export const getEntityRecordNonTransientEdits = createSelector(
(
state: State,
kind: string,
name: string,
recordId: EntityRecordKey
): Optional< any > => {
const { transientEdits } = getEntityConfig( state, kind, name ) || {};
const edits = getEntityRecordEdits( state, kind, name, recordId ) || {};
if ( ! transientEdits ) {
return edits;
}
return Object.keys( edits ).reduce( ( acc, key ) => {
if ( ! transientEdits[ key ] ) {
acc[ key ] = edits[ key ];
}
return acc;
}, {} );
},
( state: State, kind: string, name: string, recordId: EntityRecordKey ) => [
state.entities.config,
get( state.entities.records, [ kind, name, 'edits', recordId ] ),
]
);
/**
* Returns true if the specified entity record has edits,
* and false otherwise.
*
* @param state State tree.
* @param kind Entity kind.
* @param name Entity name.
* @param recordId Record ID.
*
* @return Whether the entity record has edits or not.
*/
export function hasEditsForEntityRecord(
state: State,
kind: string,
name: string,
recordId: EntityRecordKey
): boolean {
return (
isSavingEntityRecord( state, kind, name, recordId ) ||
Object.keys(
getEntityRecordNonTransientEdits( state, kind, name, recordId )
).length > 0
);
}
/**
* Returns the specified entity record, merged with its edits.
*
* @param state State tree.
* @param kind Entity kind.
* @param name Entity name.
* @param recordId Record ID.
*
* @return The entity record, merged with its edits.
*/
export const getEditedEntityRecord = createSelector(
< EntityRecord extends ET.EntityRecord< any > >(
state: State,
kind: string,
name: string,
recordId: EntityRecordKey
): ET.Updatable< EntityRecord > | undefined => ( {
...getRawEntityRecord( state, kind, name, recordId ),
...getEntityRecordEdits( state, kind, name, recordId ),
} ),
(
state: State,
kind: string,
name: string,
recordId: EntityRecordKey,
query?: GetRecordsHttpQuery
) => {
const context = query?.context ?? 'default';
return [
state.entities.config,
get( state.entities.records, [
kind,
name,
'queriedData',
'items',
context,
recordId,
] ),
get( state.entities.records, [
kind,
name,
'queriedData',
'itemIsComplete',
context,
recordId,
] ),
get( state.entities.records, [ kind, name, 'edits', recordId ] ),
];
}
);
/**
* Returns true if the specified entity record is autosaving, and false otherwise.
*
* @param state State tree.
* @param kind Entity kind.
* @param name Entity name.
* @param recordId Record ID.
*
* @return Whether the entity record is autosaving or not.
*/
export function isAutosavingEntityRecord(
state: State,
kind: string,
name: string,
recordId: EntityRecordKey
): boolean {
const { pending, isAutosave } = get(
state.entities.records,
[ kind, name, 'saving', recordId ],
{}
);
return Boolean( pending && isAutosave );
}
/**
* Returns true if the specified entity record is saving, and false otherwise.
*
* @param state State tree.
* @param kind Entity kind.
* @param name Entity name.
* @param recordId Record ID.
*
* @return Whether the entity record is saving or not.
*/
export function isSavingEntityRecord(
state: State,
kind: string,
name: string,
recordId: EntityRecordKey
): boolean {
return get(
state.entities.records,
[ kind, name, 'saving', recordId as EntityRecordKey, 'pending' ],
false
);
}
/**
* Returns true if the specified entity record is deleting, and false otherwise.
*
* @param state State tree.
* @param kind Entity kind.
* @param name Entity name.
* @param recordId Record ID.
*
* @return Whether the entity record is deleting or not.
*/
export function isDeletingEntityRecord(
state: State,
kind: string,
name: string,
recordId: EntityRecordKey
): boolean {
return get(
state.entities.records,
[ kind, name, 'deleting', recordId, 'pending' ],
false
);
}
/**
* Returns the specified entity record's last save error.
*
* @param state State tree.
* @param kind Entity kind.
* @param name Entity name.
* @param recordId Record ID.
*
* @return The entity record's save error.
*/
export function getLastEntitySaveError(
state: State,
kind: string,
name: string,
recordId: EntityRecordKey
): any {
return get( state.entities.records, [
kind,
name,
'saving',
recordId,
'error',
] );
}
/**
* Returns the specified entity record's last delete error.
*
* @param state State tree.
* @param kind Entity kind.
* @param name Entity name.
* @param recordId Record ID.
*
* @return The entity record's save error.
*/
export function getLastEntityDeleteError(
state: State,
kind: string,
name: string,
recordId: EntityRecordKey
): any {
return get( state.entities.records, [
kind,
name,
'deleting',
recordId,
'error',
] );
}
/**
* Returns the current undo offset for the
* entity records edits history. The offset
* represents how many items from the end
* of the history stack we are at. 0 is the
* last edit, -1 is the second last, and so on.
*
* @param state State tree.
*
* @return The current undo offset.
*/
function getCurrentUndoOffset( state: State ): number {
return state.undo.offset;
}
/**
* Returns the previous edit from the current undo offset
* for the entity records edits history, if any.
*
* @param state State tree.
*
* @return The edit.
*/
export function getUndoEdit( state: State ): Optional< any > {
return state.undo[ state.undo.length - 2 + getCurrentUndoOffset( state ) ];
}
/**
* Returns the next edit from the current undo offset
* for the entity records edits history, if any.
*
* @param state State tree.
*
* @return The edit.
*/
export function getRedoEdit( state: State ): Optional< any > {
return state.undo[ state.undo.length + getCurrentUndoOffset( state ) ];
}
/**
* Returns true if there is a previous edit from the current undo offset
* for the entity records edits history, and false otherwise.
*
* @param state State tree.
*
* @return Whether there is a previous edit or not.
*/
export function hasUndo( state: State ): boolean {
return Boolean( getUndoEdit( state ) );
}
/**
* Returns true if there is a next edit from the current undo offset
* for the entity records edits history, and false otherwise.
*
* @param state State tree.
*
* @return Whether there is a next edit or not.
*/
export function hasRedo( state: State ): boolean {
return Boolean( getRedoEdit( state ) );
}
/**
* Return the current theme.
*
* @param state Data state.
*
* @return The current theme.
*/
export function getCurrentTheme( state: State ): any {
return getEntityRecord( state, 'root', 'theme', state.currentTheme );
}
/**
* Return the ID of the current global styles object.
*
* @param state Data state.
*
* @return The current global styles ID.
*/
export function __experimentalGetCurrentGlobalStylesId( state: State ): string {
return state.currentGlobalStylesId;
}
/**
* Return theme supports data in the index.
*
* @param state Data state.
*
* @return Index data.
*/
export function getThemeSupports( state: State ): any {
return getCurrentTheme( state )?.theme_supports ?? EMPTY_OBJECT;