-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
actions.js
433 lines (393 loc) · 11.5 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
/**
* WordPress dependencies
*/
import { __, sprintf } from '@wordpress/i18n';
import { store as noticesStore } from '@wordpress/notices';
import { store as interfaceStore } from '@wordpress/interface';
import { getWidgetIdFromBlock } from '@wordpress/widgets';
import { store as coreStore } from '@wordpress/core-data';
import { store as blockEditorStore } from '@wordpress/block-editor';
/**
* Internal dependencies
*/
import { transformBlockToWidget } from './transformers';
import {
buildWidgetAreaPostId,
buildWidgetAreasQuery,
createStubPost,
KIND,
POST_TYPE,
WIDGET_AREA_ENTITY_TYPE,
} from './utils';
import { STORE_NAME as editWidgetsStoreName } from './constants';
/**
* Persists a stub post with given ID to core data store. The post is meant to be in-memory only and
* shouldn't be saved via the API.
*
* @param {string} id Post ID.
* @param {Array} blocks Blocks the post should consist of.
* @return {Object} The post object.
*/
export const persistStubPost =
( id, blocks ) =>
( { registry } ) => {
const stubPost = createStubPost( id, blocks );
registry
.dispatch( coreStore )
.receiveEntityRecords(
KIND,
POST_TYPE,
stubPost,
{ id: stubPost.id },
false
);
return stubPost;
};
/**
* Converts all the blocks from edited widget areas into widgets,
* and submits a batch request to save everything at once.
*
* Creates a snackbar notice on either success or error.
*
* @return {Function} An action creator.
*/
export const saveEditedWidgetAreas =
() =>
async ( { select, dispatch, registry } ) => {
const editedWidgetAreas = select.getEditedWidgetAreas();
if ( ! editedWidgetAreas?.length ) {
return;
}
try {
await dispatch.saveWidgetAreas( editedWidgetAreas );
registry
.dispatch( noticesStore )
.createSuccessNotice( __( 'Widgets saved.' ), {
type: 'snackbar',
} );
} catch ( e ) {
registry.dispatch( noticesStore ).createErrorNotice(
/* translators: %s: The error message. */
sprintf( __( 'There was an error. %s' ), e.message ),
{
type: 'snackbar',
}
);
}
};
/**
* Converts all the blocks from specified widget areas into widgets,
* and submits a batch request to save everything at once.
*
* @param {Object[]} widgetAreas Widget areas to save.
* @return {Function} An action creator.
*/
export const saveWidgetAreas =
( widgetAreas ) =>
async ( { dispatch, registry } ) => {
try {
for ( const widgetArea of widgetAreas ) {
await dispatch.saveWidgetArea( widgetArea.id );
}
} finally {
// saveEditedEntityRecord resets the resolution status, let's fix it manually.
await registry
.dispatch( coreStore )
.finishResolution(
'getEntityRecord',
KIND,
WIDGET_AREA_ENTITY_TYPE,
buildWidgetAreasQuery()
);
}
};
/**
* Converts all the blocks from a widget area specified by ID into widgets,
* and submits a batch request to save everything at once.
*
* @param {string} widgetAreaId ID of the widget area to process.
* @return {Function} An action creator.
*/
export const saveWidgetArea =
( widgetAreaId ) =>
async ( { dispatch, select, registry } ) => {
const widgets = select.getWidgets();
const post = registry
.select( coreStore )
.getEditedEntityRecord(
KIND,
POST_TYPE,
buildWidgetAreaPostId( widgetAreaId )
);
// Get all widgets from this area
const areaWidgets = Object.values( widgets ).filter(
( { sidebar } ) => sidebar === widgetAreaId
);
// Remove all duplicate reference widget instances for legacy widgets.
// Why? We filter out the widgets with duplicate IDs to prevent adding more than one instance of a widget
// implemented using a function. WordPress doesn't support having more than one instance of these, if you try to
// save multiple instances of these in different sidebars you will run into undefined behaviors.
const usedReferenceWidgets = [];
const widgetsBlocks = post.blocks.filter( ( block ) => {
const { id } = block.attributes;
if ( block.name === 'core/legacy-widget' && id ) {
if ( usedReferenceWidgets.includes( id ) ) {
return false;
}
usedReferenceWidgets.push( id );
}
return true;
} );
// Determine which widgets have been deleted. We can tell if a widget is
// deleted and not just moved to a different area by looking to see if
// getWidgetAreaForWidgetId() finds something.
const deletedWidgets = [];
for ( const widget of areaWidgets ) {
const widgetsNewArea = select.getWidgetAreaForWidgetId( widget.id );
if ( ! widgetsNewArea ) {
deletedWidgets.push( widget );
}
}
const batchMeta = [];
const batchTasks = [];
const sidebarWidgetsIds = [];
for ( let i = 0; i < widgetsBlocks.length; i++ ) {
const block = widgetsBlocks[ i ];
const widgetId = getWidgetIdFromBlock( block );
const oldWidget = widgets[ widgetId ];
const widget = transformBlockToWidget( block, oldWidget );
// We'll replace the null widgetId after save, but we track it here
// since order is important.
sidebarWidgetsIds.push( widgetId );
// Check oldWidget as widgetId might refer to an ID which has been
// deleted, e.g. if a deleted block is restored via undo after saving.
if ( oldWidget ) {
// Update an existing widget.
registry.dispatch( coreStore ).editEntityRecord(
'root',
'widget',
widgetId,
{
...widget,
sidebar: widgetAreaId,
},
{ undoIgnore: true }
);
const hasEdits = registry
.select( coreStore )
.hasEditsForEntityRecord( 'root', 'widget', widgetId );
if ( ! hasEdits ) {
continue;
}
batchTasks.push( ( { saveEditedEntityRecord } ) =>
saveEditedEntityRecord( 'root', 'widget', widgetId )
);
} else {
// Create a new widget.
batchTasks.push( ( { saveEntityRecord } ) =>
saveEntityRecord( 'root', 'widget', {
...widget,
sidebar: widgetAreaId,
} )
);
}
batchMeta.push( {
block,
position: i,
clientId: block.clientId,
} );
}
for ( const widget of deletedWidgets ) {
batchTasks.push( ( { deleteEntityRecord } ) =>
deleteEntityRecord( 'root', 'widget', widget.id, {
force: true,
} )
);
}
const records = await registry
.dispatch( coreStore )
.__experimentalBatch( batchTasks );
const preservedRecords = records.filter(
( record ) => ! record.hasOwnProperty( 'deleted' )
);
const failedWidgetNames = [];
for ( let i = 0; i < preservedRecords.length; i++ ) {
const widget = preservedRecords[ i ];
const { block, position } = batchMeta[ i ];
// Set __internalWidgetId on the block. This will be persisted to the
// store when we dispatch receiveEntityRecords( post ) below.
post.blocks[ position ].attributes.__internalWidgetId = widget.id;
const error = registry
.select( coreStore )
.getLastEntitySaveError( 'root', 'widget', widget.id );
if ( error ) {
failedWidgetNames.push( block.attributes?.name || block?.name );
}
if ( ! sidebarWidgetsIds[ position ] ) {
sidebarWidgetsIds[ position ] = widget.id;
}
}
if ( failedWidgetNames.length ) {
throw new Error(
sprintf(
/* translators: %s: List of widget names */
__( 'Could not save the following widgets: %s.' ),
failedWidgetNames.join( ', ' )
)
);
}
registry.dispatch( coreStore ).editEntityRecord(
KIND,
WIDGET_AREA_ENTITY_TYPE,
widgetAreaId,
{
widgets: sidebarWidgetsIds,
},
{ undoIgnore: true }
);
dispatch( trySaveWidgetArea( widgetAreaId ) );
registry
.dispatch( coreStore )
.receiveEntityRecords( KIND, POST_TYPE, post, undefined );
};
const trySaveWidgetArea =
( widgetAreaId ) =>
( { registry } ) => {
registry
.dispatch( coreStore )
.saveEditedEntityRecord(
KIND,
WIDGET_AREA_ENTITY_TYPE,
widgetAreaId,
{
throwOnError: true,
}
);
};
/**
* Sets the clientId stored for a particular widgetId.
*
* @param {number} clientId Client id.
* @param {number} widgetId Widget id.
*
* @return {Object} Action.
*/
export function setWidgetIdForClientId( clientId, widgetId ) {
return {
type: 'SET_WIDGET_ID_FOR_CLIENT_ID',
clientId,
widgetId,
};
}
/**
* Sets the open state of all the widget areas.
*
* @param {Object} widgetAreasOpenState The open states of all the widget areas.
*
* @return {Object} Action.
*/
export function setWidgetAreasOpenState( widgetAreasOpenState ) {
return {
type: 'SET_WIDGET_AREAS_OPEN_STATE',
widgetAreasOpenState,
};
}
/**
* Sets the open state of the widget area.
*
* @param {string} clientId The clientId of the widget area.
* @param {boolean} isOpen Whether the widget area should be opened.
*
* @return {Object} Action.
*/
export function setIsWidgetAreaOpen( clientId, isOpen ) {
return {
type: 'SET_IS_WIDGET_AREA_OPEN',
clientId,
isOpen,
};
}
/**
* Returns an action object used to open/close the inserter.
*
* @param {boolean|Object} value Whether the inserter should be
* opened (true) or closed (false).
* To specify an insertion point,
* use an object.
* @param {string} value.rootClientId The root client ID to insert at.
* @param {number} value.insertionIndex The index to insert at.
*
* @return {Object} Action object.
*/
export function setIsInserterOpened( value ) {
return {
type: 'SET_IS_INSERTER_OPENED',
value,
};
}
/**
* Returns an action object used to open/close the list view.
*
* @param {boolean} isOpen A boolean representing whether the list view should be opened or closed.
* @return {Object} Action object.
*/
export function setIsListViewOpened( isOpen ) {
return {
type: 'SET_IS_LIST_VIEW_OPENED',
isOpen,
};
}
/**
* Returns an action object signalling that the user closed the sidebar.
*
* @return {Object} Action creator.
*/
export const closeGeneralSidebar =
() =>
( { registry } ) => {
registry
.dispatch( interfaceStore )
.disableComplementaryArea( editWidgetsStoreName );
};
/**
* Action that handles moving a block between widget areas
*
* @param {string} clientId The clientId of the block to move.
* @param {string} widgetAreaId The id of the widget area to move the block to.
*/
export const moveBlockToWidgetArea =
( clientId, widgetAreaId ) =>
async ( { dispatch, select, registry } ) => {
const sourceRootClientId = registry
.select( blockEditorStore )
.getBlockRootClientId( clientId );
// Search the top level blocks (widget areas) for the one with the matching
// id attribute. Makes the assumption that all top-level blocks are widget
// areas.
const widgetAreas = registry.select( blockEditorStore ).getBlocks();
const destinationWidgetAreaBlock = widgetAreas.find(
( { attributes } ) => attributes.id === widgetAreaId
);
const destinationRootClientId = destinationWidgetAreaBlock.clientId;
// Get the index for moving to the end of the destination widget area.
const destinationInnerBlocksClientIds = registry
.select( blockEditorStore )
.getBlockOrder( destinationRootClientId );
const destinationIndex = destinationInnerBlocksClientIds.length;
// Reveal the widget area, if it's not open.
const isDestinationWidgetAreaOpen = select.getIsWidgetAreaOpen(
destinationRootClientId
);
if ( ! isDestinationWidgetAreaOpen ) {
dispatch.setIsWidgetAreaOpen( destinationRootClientId, true );
}
// Move the block.
registry
.dispatch( blockEditorStore )
.moveBlocksToPosition(
[ clientId ],
sourceRootClientId,
destinationRootClientId,
destinationIndex
);
};