-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathentities.js
205 lines (193 loc) · 4.29 KB
/
entities.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
/**
* External dependencies
*/
import { upperFirst, camelCase, map, find, get, startCase } from 'lodash';
/**
* WordPress dependencies
*/
import { apiFetch, syncSelect } from '@wordpress/data-controls';
import { __ } from '@wordpress/i18n';
/**
* Internal dependencies
*/
import { addEntities } from './actions';
export const DEFAULT_ENTITY_KEY = 'id';
export const defaultEntities = [
{
label: __( 'Base' ),
name: '__unstableBase',
kind: 'root',
baseURL: '',
},
{
label: __( 'Site' ),
name: 'site',
kind: 'root',
baseURL: '/wp/v2/settings',
getTitle: ( record ) => {
return get( record, [ 'title' ], __( 'Site Title' ) );
},
},
{
label: __( 'Post Type' ),
name: 'postType',
kind: 'root',
key: 'slug',
baseURL: '/wp/v2/types',
},
{
name: 'media',
kind: 'root',
baseURL: '/wp/v2/media',
plural: 'mediaItems',
label: __( 'Media' ),
},
{
name: 'taxonomy',
kind: 'root',
key: 'slug',
baseURL: '/wp/v2/taxonomies',
plural: 'taxonomies',
label: __( 'Taxonomy' ),
},
{
name: 'sidebar',
kind: 'root',
baseURL: '/__experimental/sidebars',
plural: 'sidebars',
transientEdits: { blocks: true },
label: __( 'Widget areas' ),
},
{
label: __( 'User' ),
name: 'user',
kind: 'root',
baseURL: '/wp/v2/users',
plural: 'users',
},
{
name: 'comment',
kind: 'root',
baseURL: '/wp/v2/comments',
plural: 'comments',
label: __( 'Comment' ),
},
{
name: 'menu',
kind: 'root',
baseURL: '/__experimental/menus',
plural: 'menus',
label: __( 'Menu' ),
},
{
name: 'menuItem',
kind: 'root',
baseURL: '/__experimental/menu-items',
plural: 'menuItems',
label: __( 'Menu Item' ),
},
{
name: 'menuLocation',
kind: 'root',
baseURL: '/__experimental/menu-locations',
plural: 'menuLocations',
label: __( 'Menu Location' ),
key: 'name',
},
];
export const kinds = [
{ name: 'postType', loadEntities: loadPostTypeEntities },
{ name: 'taxonomy', loadEntities: loadTaxonomyEntities },
];
/**
* Returns the list of post type entities.
*
* @return {Promise} Entities promise
*/
function* loadPostTypeEntities() {
const postTypes = yield apiFetch( { path: '/wp/v2/types?context=edit' } );
return map( postTypes, ( postType, name ) => {
return {
kind: 'postType',
baseURL: '/wp/v2/' + postType.rest_base,
name,
label: postType.labels.singular_name,
transientEdits: {
blocks: true,
selectionStart: true,
selectionEnd: true,
},
mergedEdits: { meta: true },
getTitle( record ) {
if ( name === 'wp_template_part' || name === 'wp_template' ) {
return startCase( record.slug );
}
return get( record, [ 'title', 'rendered' ], record.id );
},
};
} );
}
/**
* Returns the list of the taxonomies entities.
*
* @return {Promise} Entities promise
*/
function* loadTaxonomyEntities() {
const taxonomies = yield apiFetch( {
path: '/wp/v2/taxonomies?context=edit',
} );
return map( taxonomies, ( taxonomy, name ) => {
return {
kind: 'taxonomy',
baseURL: '/wp/v2/' + taxonomy.rest_base,
name,
label: taxonomy.labels.singular_name,
};
} );
}
/**
* Returns the entity's getter method name given its kind and name.
*
* @param {string} kind Entity kind.
* @param {string} name Entity name.
* @param {string} prefix Function prefix.
* @param {boolean} usePlural Whether to use the plural form or not.
*
* @return {string} Method name
*/
export const getMethodName = (
kind,
name,
prefix = 'get',
usePlural = false
) => {
const entity = find( defaultEntities, { kind, name } );
const kindPrefix = kind === 'root' ? '' : upperFirst( camelCase( kind ) );
const nameSuffix =
upperFirst( camelCase( name ) ) + ( usePlural ? 's' : '' );
const suffix =
usePlural && entity.plural
? upperFirst( camelCase( entity.plural ) )
: nameSuffix;
return `${ prefix }${ kindPrefix }${ suffix }`;
};
/**
* Loads the kind entities into the store.
*
* @param {string} kind Kind
*
* @return {Array} Entities
*/
export function* getKindEntities( kind ) {
let entities = yield syncSelect( 'core', 'getEntitiesByKind', kind );
if ( entities && entities.length !== 0 ) {
return entities;
}
const kindConfig = find( kinds, { name: kind } );
if ( ! kindConfig ) {
return [];
}
entities = yield kindConfig.loadEntities();
yield addEntities( entities );
return entities;
}