-
Notifications
You must be signed in to change notification settings - Fork 4k
/
actions.js
438 lines (401 loc) · 9.94 KB
/
actions.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
/**
* WordPress dependencies
*/
import { parse } from '@wordpress/blocks';
import deprecated from '@wordpress/deprecated';
import { store as coreStore } from '@wordpress/core-data';
import { store as blockEditorStore } from '@wordpress/block-editor';
import {
store as editorStore,
privateApis as editorPrivateApis,
} from '@wordpress/editor';
import { store as preferencesStore } from '@wordpress/preferences';
/**
* Internal dependencies
*/
import { STORE_NAME as editSiteStoreName } from './constants';
import {
TEMPLATE_POST_TYPE,
TEMPLATE_PART_POST_TYPE,
NAVIGATION_POST_TYPE,
} from '../utils/constants';
import { unlock } from '../lock-unlock';
const { interfaceStore } = unlock( editorPrivateApis );
/**
* Dispatches an action that toggles a feature flag.
*
* @param {string} featureName Feature name.
*/
export function toggleFeature( featureName ) {
return function ( { registry } ) {
deprecated(
"dispatch( 'core/edit-site' ).toggleFeature( featureName )",
{
since: '6.0',
alternative:
"dispatch( 'core/preferences').toggle( 'core/edit-site', featureName )",
}
);
registry
.dispatch( preferencesStore )
.toggle( 'core/edit-site', featureName );
};
}
/**
* Action that changes the width of the editing canvas.
*
* @deprecated
*
* @param {string} deviceType
*
* @return {Object} Action object.
*/
export const __experimentalSetPreviewDeviceType =
( deviceType ) =>
( { registry } ) => {
deprecated(
"dispatch( 'core/edit-site' ).__experimentalSetPreviewDeviceType",
{
since: '6.5',
version: '6.7',
hint: 'registry.dispatch( editorStore ).setDeviceType',
}
);
registry.dispatch( editorStore ).setDeviceType( deviceType );
};
/**
* Action that sets a template, optionally fetching it from REST API.
*
* @return {Object} Action object.
*/
export function setTemplate() {
deprecated( "dispatch( 'core/edit-site' ).setTemplate", {
since: '6.5',
version: '6.8',
hint: 'The setTemplate is not needed anymore, the correct entity is resolved from the URL automatically.',
} );
return {
type: 'NOTHING',
};
}
/**
* Action that adds a new template and sets it as the current template.
*
* @param {Object} template The template.
*
* @deprecated
*
* @return {Object} Action object used to set the current template.
*/
export const addTemplate =
( template ) =>
async ( { dispatch, registry } ) => {
deprecated( "dispatch( 'core/edit-site' ).addTemplate", {
since: '6.5',
version: '6.8',
hint: 'use saveEntityRecord directly',
} );
const newTemplate = await registry
.dispatch( coreStore )
.saveEntityRecord( 'postType', TEMPLATE_POST_TYPE, template );
if ( template.content ) {
registry
.dispatch( coreStore )
.editEntityRecord(
'postType',
TEMPLATE_POST_TYPE,
newTemplate.id,
{ blocks: parse( template.content ) },
{ undoIgnore: true }
);
}
dispatch( {
type: 'SET_EDITED_POST',
postType: TEMPLATE_POST_TYPE,
id: newTemplate.id,
} );
};
/**
* Action that removes a template.
*
* @param {Object} template The template object.
*/
export const removeTemplate =
( template ) =>
( { registry } ) => {
return unlock( registry.dispatch( editorStore ) ).removeTemplates( [
template,
] );
};
/**
* Action that sets a template part.
*
* @param {string} templatePartId The template part ID.
*
* @return {Object} Action object.
*/
export function setTemplatePart( templatePartId ) {
return {
type: 'SET_EDITED_POST',
postType: TEMPLATE_PART_POST_TYPE,
id: templatePartId,
};
}
/**
* Action that sets a navigation menu.
*
* @param {string} navigationMenuId The Navigation Menu Post ID.
*
* @return {Object} Action object.
*/
export function setNavigationMenu( navigationMenuId ) {
return {
type: 'SET_EDITED_POST',
postType: NAVIGATION_POST_TYPE,
id: navigationMenuId,
};
}
/**
* Action that sets an edited entity.
*
* @param {string} postType The entity's post type.
* @param {string} postId The entity's ID.
* @param {Object} context The entity's context.
*
* @return {Object} Action object.
*/
export function setEditedEntity( postType, postId, context ) {
return {
type: 'SET_EDITED_POST',
postType,
id: postId,
context,
};
}
/**
* @deprecated
*/
export function setHomeTemplateId() {
deprecated( "dispatch( 'core/edit-site' ).setHomeTemplateId", {
since: '6.2',
version: '6.4',
} );
return {
type: 'NOTHING',
};
}
/**
* Set's the current block editor context.
*
* @param {Object} context The context object.
*
* @return {Object} Action object.
*/
export function setEditedPostContext( context ) {
return {
type: 'SET_EDITED_POST_CONTEXT',
context,
};
}
/**
* Resolves the template for a page and displays both. If no path is given, attempts
* to use the postId to generate a path like `?p=${ postId }`.
*
* @deprecated
*
* @return {Object} Action object.
*/
export function setPage() {
deprecated( "dispatch( 'core/edit-site' ).setPage", {
since: '6.5',
version: '6.8',
hint: 'The setPage is not needed anymore, the correct entity is resolved from the URL automatically.',
} );
return { type: 'NOTHING' };
}
/**
* Action that sets the active navigation panel menu.
*
* @deprecated
*
* @return {Object} Action object.
*/
export function setNavigationPanelActiveMenu() {
deprecated( "dispatch( 'core/edit-site' ).setNavigationPanelActiveMenu", {
since: '6.2',
version: '6.4',
} );
return { type: 'NOTHING' };
}
/**
* Opens the navigation panel and sets its active menu at the same time.
*
* @deprecated
*/
export function openNavigationPanelToMenu() {
deprecated( "dispatch( 'core/edit-site' ).openNavigationPanelToMenu", {
since: '6.2',
version: '6.4',
} );
return { type: 'NOTHING' };
}
/**
* Sets whether the navigation panel should be open.
*
* @deprecated
*/
export function setIsNavigationPanelOpened() {
deprecated( "dispatch( 'core/edit-site' ).setIsNavigationPanelOpened", {
since: '6.2',
version: '6.4',
} );
return { type: 'NOTHING' };
}
/**
* Returns an action object used to open/close the inserter.
*
* @deprecated
*
* @param {boolean|Object} value Whether the inserter should be opened (true) or closed (false).
*/
export const setIsInserterOpened =
( value ) =>
( { registry } ) => {
deprecated( "dispatch( 'core/edit-site' ).setIsInserterOpened", {
since: '6.5',
alternative: "dispatch( 'core/editor').setIsInserterOpened",
} );
registry.dispatch( editorStore ).setIsInserterOpened( value );
};
/**
* Returns an action object used to open/close the list view.
*
* @deprecated
*
* @param {boolean} isOpen A boolean representing whether the list view should be opened or closed.
*/
export const setIsListViewOpened =
( isOpen ) =>
( { registry } ) => {
deprecated( "dispatch( 'core/edit-site' ).setIsListViewOpened", {
since: '6.5',
alternative: "dispatch( 'core/editor').setIsListViewOpened",
} );
registry.dispatch( editorStore ).setIsListViewOpened( isOpen );
};
/**
* Returns an action object used to update the settings.
*
* @param {Object} settings New settings.
*
* @return {Object} Action object.
*/
export function updateSettings( settings ) {
return {
type: 'UPDATE_SETTINGS',
settings,
};
}
/**
* Sets whether the save view panel should be open.
*
* @param {boolean} isOpen If true, opens the save view. If false, closes it.
* It does not toggle the state, but sets it directly.
*/
export function setIsSaveViewOpened( isOpen ) {
return {
type: 'SET_IS_SAVE_VIEW_OPENED',
isOpen,
};
}
/**
* Reverts a template to its original theme-provided file.
*
* @param {Object} template The template to revert.
* @param {Object} [options]
* @param {boolean} [options.allowUndo] Whether to allow the user to undo
* reverting the template. Default true.
*/
export const revertTemplate =
( template, options ) =>
( { registry } ) => {
return unlock( registry.dispatch( editorStore ) ).revertTemplate(
template,
options
);
};
/**
* Action that opens an editor sidebar.
*
* @param {?string} name Sidebar name to be opened.
*/
export const openGeneralSidebar =
( name ) =>
( { registry } ) => {
registry
.dispatch( interfaceStore )
.enableComplementaryArea( editSiteStoreName, name );
};
/**
* Action that closes the sidebar.
*/
export const closeGeneralSidebar =
() =>
( { registry } ) => {
registry
.dispatch( interfaceStore )
.disableComplementaryArea( editSiteStoreName );
};
/**
* Triggers an action used to switch editor mode.
*
* @deprecated
*
* @param {string} mode The editor mode.
*/
export const switchEditorMode =
( mode ) =>
( { registry } ) => {
deprecated( "dispatch( 'core/edit-site' ).switchEditorMode", {
since: '6.6',
alternative: "dispatch( 'core/editor').switchEditorMode",
} );
registry.dispatch( editorStore ).switchEditorMode( mode );
};
/**
* Sets whether or not the editor allows only page content to be edited.
*
* @param {boolean} hasPageContentFocus True to allow only page content to be
* edited, false to allow template to be
* edited.
*/
export const setHasPageContentFocus =
( hasPageContentFocus ) =>
( { dispatch, registry } ) => {
deprecated( `dispatch( 'core/edit-site' ).setHasPageContentFocus`, {
since: '6.5',
} );
if ( hasPageContentFocus ) {
registry.dispatch( blockEditorStore ).clearSelectedBlock();
}
dispatch( {
type: 'SET_HAS_PAGE_CONTENT_FOCUS',
hasPageContentFocus,
} );
};
/**
* Action that toggles Distraction free mode.
* Distraction free mode expects there are no sidebars, as due to the
* z-index values set, you can't close sidebars.
*
* @deprecated
*/
export const toggleDistractionFree =
() =>
( { registry } ) => {
deprecated( "dispatch( 'core/edit-site' ).toggleDistractionFree", {
since: '6.6',
alternative: "dispatch( 'core/editor').toggleDistractionFree",
} );
registry.dispatch( editorStore ).toggleDistractionFree();
};