@wordpress/edit-widgets 7.1.0 → 7.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/blocks/widget-area/edit/index.cjs +10 -8
- package/build/blocks/widget-area/edit/index.cjs.map +1 -1
- package/build/blocks/widget-area/edit/inner-blocks.cjs +10 -8
- package/build/blocks/widget-area/edit/inner-blocks.cjs.map +1 -1
- package/build/blocks/widget-area/edit/use-is-dragging-within.cjs.map +1 -1
- package/build/blocks/widget-area/index.cjs +9 -7
- package/build/blocks/widget-area/index.cjs.map +1 -1
- package/build/components/error-boundary/index.cjs +1 -1
- package/build/components/error-boundary/index.cjs.map +1 -1
- package/build/components/header/document-tools/index.cjs +20 -10
- package/build/components/header/document-tools/index.cjs.map +1 -1
- package/build/components/header/index.cjs +18 -11
- package/build/components/header/index.cjs.map +1 -1
- package/build/components/keyboard-shortcut-help-modal/config.cjs +5 -0
- package/build/components/keyboard-shortcut-help-modal/config.cjs.map +1 -1
- package/build/components/keyboard-shortcut-help-modal/dynamic-shortcut.cjs +9 -7
- package/build/components/keyboard-shortcut-help-modal/dynamic-shortcut.cjs.map +1 -1
- package/build/components/keyboard-shortcut-help-modal/index.cjs +12 -11
- package/build/components/keyboard-shortcut-help-modal/index.cjs.map +1 -1
- package/build/components/keyboard-shortcut-help-modal/shortcut.cjs +35 -13
- package/build/components/keyboard-shortcut-help-modal/shortcut.cjs.map +1 -1
- package/build/components/keyboard-shortcuts/index.cjs.map +1 -1
- package/build/components/layout/index.cjs +19 -13
- package/build/components/layout/index.cjs.map +1 -1
- package/build/components/layout/interface.cjs +27 -13
- package/build/components/layout/interface.cjs.map +1 -1
- package/build/components/layout/unsaved-changes-warning.cjs +3 -6
- package/build/components/layout/unsaved-changes-warning.cjs.map +1 -1
- package/build/components/more-menu/index.cjs +14 -10
- package/build/components/more-menu/index.cjs.map +1 -1
- package/build/components/secondary-sidebar/index.cjs +10 -8
- package/build/components/secondary-sidebar/index.cjs.map +1 -1
- package/build/components/secondary-sidebar/inserter-sidebar.cjs +9 -7
- package/build/components/secondary-sidebar/inserter-sidebar.cjs.map +1 -1
- package/build/components/sidebar/index.cjs +23 -15
- package/build/components/sidebar/index.cjs.map +1 -1
- package/build/components/welcome-guide/index.cjs +5 -1
- package/build/components/welcome-guide/index.cjs.map +1 -1
- package/build/components/widget-areas-block-editor-content/index.cjs +10 -8
- package/build/components/widget-areas-block-editor-content/index.cjs.map +1 -1
- package/build/components/widget-areas-block-editor-provider/index.cjs +10 -8
- package/build/components/widget-areas-block-editor-provider/index.cjs.map +1 -1
- package/build/constants.cjs +0 -2
- package/build/constants.cjs.map +1 -1
- package/build/index.cjs +12 -10
- package/build/index.cjs.map +1 -1
- package/build/store/actions.cjs +20 -9
- package/build/store/actions.cjs.map +1 -1
- package/build/store/index.cjs +14 -12
- package/build/store/index.cjs.map +1 -1
- package/build/store/selectors.cjs +3 -1
- package/build/store/selectors.cjs.map +1 -1
- package/build-module/blocks/widget-area/index.mjs.map +1 -1
- package/build-module/components/error-boundary/index.mjs +2 -2
- package/build-module/components/error-boundary/index.mjs.map +1 -1
- package/build-module/components/header/document-tools/index.mjs +10 -2
- package/build-module/components/header/document-tools/index.mjs.map +1 -1
- package/build-module/components/header/index.mjs +6 -2
- package/build-module/components/header/index.mjs.map +1 -1
- package/build-module/components/header/undo-redo/redo.mjs.map +1 -1
- package/build-module/components/header/undo-redo/undo.mjs.map +1 -1
- package/build-module/components/keyboard-shortcut-help-modal/config.mjs +5 -0
- package/build-module/components/keyboard-shortcut-help-modal/config.mjs.map +1 -1
- package/build-module/components/keyboard-shortcut-help-modal/index.mjs +1 -2
- package/build-module/components/keyboard-shortcut-help-modal/index.mjs.map +1 -1
- package/build-module/components/keyboard-shortcut-help-modal/shortcut.mjs +35 -13
- package/build-module/components/keyboard-shortcut-help-modal/shortcut.mjs.map +1 -1
- package/build-module/components/layout/index.mjs +5 -1
- package/build-module/components/layout/index.mjs.map +1 -1
- package/build-module/components/layout/interface.mjs +16 -4
- package/build-module/components/layout/interface.mjs.map +1 -1
- package/build-module/components/layout/unsaved-changes-warning.mjs +3 -6
- package/build-module/components/layout/unsaved-changes-warning.mjs.map +1 -1
- package/build-module/components/more-menu/index.mjs +4 -2
- package/build-module/components/more-menu/index.mjs.map +1 -1
- package/build-module/components/sidebar/index.mjs +14 -8
- package/build-module/components/sidebar/index.mjs.map +1 -1
- package/build-module/components/welcome-guide/index.mjs +5 -1
- package/build-module/components/welcome-guide/index.mjs.map +1 -1
- package/build-module/constants.mjs +1 -2
- package/build-module/constants.mjs.map +1 -1
- package/build-module/index.mjs +2 -2
- package/build-module/index.mjs.map +1 -1
- package/build-module/store/actions.mjs +20 -9
- package/build-module/store/actions.mjs.map +1 -1
- package/build-module/store/selectors.mjs +3 -1
- package/build-module/store/selectors.mjs.map +1 -1
- package/build-style/style-rtl.css +72 -46
- package/build-style/style.css +72 -46
- package/build-types/blocks/widget-area/edit/index.d.ts +10 -0
- package/build-types/blocks/widget-area/edit/index.d.ts.map +1 -0
- package/build-types/blocks/widget-area/edit/inner-blocks.d.ts +4 -0
- package/build-types/blocks/widget-area/edit/inner-blocks.d.ts.map +1 -0
- package/build-types/blocks/widget-area/edit/use-is-dragging-within.d.ts +12 -0
- package/build-types/blocks/widget-area/edit/use-is-dragging-within.d.ts.map +1 -0
- package/build-types/blocks/widget-area/index.d.ts +14 -0
- package/build-types/blocks/widget-area/index.d.ts.map +1 -0
- package/build-types/components/error-boundary/index.d.ts +14 -0
- package/build-types/components/error-boundary/index.d.ts.map +1 -0
- package/build-types/components/header/document-tools/index.d.ts +3 -0
- package/build-types/components/header/document-tools/index.d.ts.map +1 -0
- package/build-types/components/header/index.d.ts +3 -0
- package/build-types/components/header/index.d.ts.map +1 -0
- package/build-types/components/header/undo-redo/redo.d.ts +3 -0
- package/build-types/components/header/undo-redo/redo.d.ts.map +1 -0
- package/build-types/components/header/undo-redo/undo.d.ts +3 -0
- package/build-types/components/header/undo-redo/undo.d.ts.map +1 -0
- package/build-types/components/keyboard-shortcut-help-modal/config.d.ts +3 -0
- package/build-types/components/keyboard-shortcut-help-modal/config.d.ts.map +1 -0
- package/build-types/components/keyboard-shortcut-help-modal/dynamic-shortcut.d.ts +5 -0
- package/build-types/components/keyboard-shortcut-help-modal/dynamic-shortcut.d.ts.map +1 -0
- package/build-types/components/keyboard-shortcut-help-modal/index.d.ts +5 -0
- package/build-types/components/keyboard-shortcut-help-modal/index.d.ts.map +1 -0
- package/build-types/components/keyboard-shortcut-help-modal/shortcut.d.ts +25 -0
- package/build-types/components/keyboard-shortcut-help-modal/shortcut.d.ts.map +1 -0
- package/build-types/components/keyboard-shortcuts/index.d.ts +7 -0
- package/build-types/components/keyboard-shortcuts/index.d.ts.map +1 -0
- package/build-types/components/layout/index.d.ts +5 -0
- package/build-types/components/layout/index.d.ts.map +1 -0
- package/build-types/components/layout/interface.d.ts +5 -0
- package/build-types/components/layout/interface.d.ts.map +1 -0
- package/build-types/components/layout/unsaved-changes-warning.d.ts +10 -0
- package/build-types/components/layout/unsaved-changes-warning.d.ts.map +1 -0
- package/build-types/components/more-menu/index.d.ts +2 -0
- package/build-types/components/more-menu/index.d.ts.map +1 -0
- package/build-types/components/more-menu/tools-more-menu-group.d.ts +6 -0
- package/build-types/components/more-menu/tools-more-menu-group.d.ts.map +1 -0
- package/build-types/components/notices/index.d.ts +3 -0
- package/build-types/components/notices/index.d.ts.map +1 -0
- package/build-types/components/save-button/index.d.ts +3 -0
- package/build-types/components/save-button/index.d.ts.map +1 -0
- package/build-types/components/secondary-sidebar/index.d.ts +2 -0
- package/build-types/components/secondary-sidebar/index.d.ts.map +1 -0
- package/build-types/components/secondary-sidebar/inserter-sidebar.d.ts +2 -0
- package/build-types/components/secondary-sidebar/inserter-sidebar.d.ts.map +1 -0
- package/build-types/components/secondary-sidebar/list-view-sidebar.d.ts +2 -0
- package/build-types/components/secondary-sidebar/list-view-sidebar.d.ts.map +1 -0
- package/build-types/components/sidebar/index.d.ts +2 -0
- package/build-types/components/sidebar/index.d.ts.map +1 -0
- package/build-types/components/sidebar/widget-areas.d.ts +4 -0
- package/build-types/components/sidebar/widget-areas.d.ts.map +1 -0
- package/build-types/components/welcome-guide/index.d.ts +2 -0
- package/build-types/components/welcome-guide/index.d.ts.map +1 -0
- package/build-types/components/widget-areas-block-editor-content/index.d.ts +4 -0
- package/build-types/components/widget-areas-block-editor-content/index.d.ts.map +1 -0
- package/build-types/components/widget-areas-block-editor-provider/index.d.ts +6 -0
- package/build-types/components/widget-areas-block-editor-provider/index.d.ts.map +1 -0
- package/build-types/constants.d.ts +2 -0
- package/build-types/constants.d.ts.map +1 -0
- package/build-types/filters/index.d.ts +3 -0
- package/build-types/filters/index.d.ts.map +1 -0
- package/build-types/filters/move-to-widget-area.d.ts +2 -0
- package/build-types/filters/move-to-widget-area.d.ts.map +1 -0
- package/build-types/filters/replace-media-upload.d.ts +2 -0
- package/build-types/filters/replace-media-upload.d.ts.map +1 -0
- package/build-types/hooks/use-last-selected-widget-area.d.ts +9 -0
- package/build-types/hooks/use-last-selected-widget-area.d.ts.map +1 -0
- package/build-types/hooks/use-widget-library-insertion-point.d.ts +3 -0
- package/build-types/hooks/use-widget-library-insertion-point.d.ts.map +1 -0
- package/build-types/index.d.ts +16 -0
- package/build-types/index.d.ts.map +1 -0
- package/build-types/lock-unlock.d.ts +2 -0
- package/build-types/lock-unlock.d.ts.map +1 -0
- package/build-types/store/actions.d.ts +144 -0
- package/build-types/store/actions.d.ts.map +1 -0
- package/build-types/store/constants.d.ts +5 -0
- package/build-types/store/constants.d.ts.map +1 -0
- package/build-types/store/index.d.ts +9 -0
- package/build-types/store/index.d.ts.map +1 -0
- package/build-types/store/private-selectors.d.ts +3 -0
- package/build-types/store/private-selectors.d.ts.map +1 -0
- package/build-types/store/reducer.d.ts +73 -0
- package/build-types/store/reducer.d.ts.map +1 -0
- package/build-types/store/resolvers.d.ts +17 -0
- package/build-types/store/resolvers.d.ts.map +1 -0
- package/build-types/store/selectors.d.ts +159 -0
- package/build-types/store/selectors.d.ts.map +1 -0
- package/build-types/store/transformers.d.ts +16 -0
- package/build-types/store/transformers.d.ts.map +1 -0
- package/build-types/store/utils.d.ts +53 -0
- package/build-types/store/utils.d.ts.map +1 -0
- package/package.json +33 -33
- package/src/components/error-boundary/index.jsx +2 -2
- package/src/components/header/index.jsx +11 -2
- package/src/components/header/style.scss +30 -3
- package/src/components/keyboard-shortcut-help-modal/config.js +3 -0
- package/src/components/keyboard-shortcut-help-modal/index.jsx +0 -2
- package/src/components/keyboard-shortcut-help-modal/shortcut.jsx +44 -29
- package/src/components/keyboard-shortcut-help-modal/style.scss +1 -1
- package/src/components/layout/style.scss +5 -9
- package/src/components/layout/unsaved-changes-warning.js +4 -9
- package/src/components/more-menu/index.jsx +0 -1
- package/src/components/sidebar/index.jsx +1 -1
- package/src/components/sidebar/style.scss +6 -0
- package/src/components/welcome-guide/style.scss +1 -1
- package/src/components/widget-areas-block-editor-content/style.scss +1 -1
- package/src/constants.js +0 -1
- package/src/index.jsx +2 -7
- package/src/store/actions.js +33 -14
- package/src/store/selectors.js +4 -1
- package/src/store/test/actions.jsdom.test.js +275 -0
|
@@ -24,7 +24,7 @@ const { Tabs: WCTabs } = unlock( componentsPrivateApis );
|
|
|
24
24
|
|
|
25
25
|
function SidebarHeader( { selectedWidgetAreaBlock } ) {
|
|
26
26
|
return (
|
|
27
|
-
<WCTabs.TabList>
|
|
27
|
+
<WCTabs.TabList className="edit-widgets-sidebar__panel-tablist">
|
|
28
28
|
<WCTabs.Tab tabId={ WIDGET_AREAS_IDENTIFIER }>
|
|
29
29
|
{ selectedWidgetAreaBlock
|
|
30
30
|
? selectedWidgetAreaBlock.attributes.name
|
|
@@ -4,6 +4,12 @@
|
|
|
4
4
|
padding-left: 0;
|
|
5
5
|
}
|
|
6
6
|
|
|
7
|
+
// Overlap the header's bottom border, so the selected tab's indicator
|
|
8
|
+
// covers it, as in the inserter.
|
|
9
|
+
.edit-widgets-sidebar__panel-tablist {
|
|
10
|
+
margin-bottom: -$border-width;
|
|
11
|
+
}
|
|
12
|
+
|
|
7
13
|
|
|
8
14
|
.edit-widgets-widget-areas__top-container {
|
|
9
15
|
display: flex;
|
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
// This is copied from edit-post/src/components/style.scss but without the padding.
|
|
29
29
|
& .components-button {
|
|
30
30
|
font-family: $default-font;
|
|
31
|
-
font-size:
|
|
31
|
+
font-size: var(--wpds-typography-font-size-md);
|
|
32
32
|
|
|
33
33
|
&.is-tertiary,
|
|
34
34
|
&.has-icon {
|
package/src/constants.js
CHANGED
package/src/index.jsx
CHANGED
|
@@ -23,10 +23,7 @@ import './store';
|
|
|
23
23
|
import './filters';
|
|
24
24
|
import * as widgetArea from './blocks/widget-area';
|
|
25
25
|
import Layout from './components/layout';
|
|
26
|
-
import {
|
|
27
|
-
ALLOW_REUSABLE_BLOCKS,
|
|
28
|
-
ENABLE_EXPERIMENTAL_FSE_BLOCKS,
|
|
29
|
-
} from './constants';
|
|
26
|
+
import { ALLOW_REUSABLE_BLOCKS } from './constants';
|
|
30
27
|
|
|
31
28
|
const disabledBlocks = [
|
|
32
29
|
'core/more',
|
|
@@ -67,9 +64,7 @@ export function initializeEditor( id, settings ) {
|
|
|
67
64
|
registerCoreBlocks( coreBlocks );
|
|
68
65
|
registerLegacyWidgetBlock();
|
|
69
66
|
if ( globalThis.IS_GUTENBERG_PLUGIN ) {
|
|
70
|
-
__experimentalRegisterExperimentalCoreBlocks(
|
|
71
|
-
enableFSEBlocks: ENABLE_EXPERIMENTAL_FSE_BLOCKS,
|
|
72
|
-
} );
|
|
67
|
+
__experimentalRegisterExperimentalCoreBlocks();
|
|
73
68
|
}
|
|
74
69
|
registerLegacyWidgetVariations( settings );
|
|
75
70
|
registerBlock( widgetArea );
|
package/src/store/actions.js
CHANGED
|
@@ -108,7 +108,7 @@ export const saveWidgetAreas =
|
|
|
108
108
|
*/
|
|
109
109
|
export const saveWidgetArea =
|
|
110
110
|
( widgetAreaId ) =>
|
|
111
|
-
async ( {
|
|
111
|
+
async ( { select, registry } ) => {
|
|
112
112
|
const widgets = select.getWidgets();
|
|
113
113
|
|
|
114
114
|
const post = registry
|
|
@@ -155,9 +155,32 @@ export const saveWidgetArea =
|
|
|
155
155
|
const batchMeta = [];
|
|
156
156
|
const batchTasks = [];
|
|
157
157
|
const sidebarWidgetsIds = [];
|
|
158
|
+
const claimedWidgetIds = new Set();
|
|
158
159
|
for ( let i = 0; i < widgetsBlocks.length; i++ ) {
|
|
159
160
|
const block = widgetsBlocks[ i ];
|
|
160
|
-
|
|
161
|
+
let widgetId = getWidgetIdFromBlock( block );
|
|
162
|
+
|
|
163
|
+
// Splitting a block, e.g. pressing Enter in the middle of a
|
|
164
|
+
// paragraph, copies all of its attributes to the new block,
|
|
165
|
+
// including the widget ID, and the new block can then be moved to
|
|
166
|
+
// another widget area. Only the first block with a given ID, in the
|
|
167
|
+
// first widget area that holds it, updates that widget. Any other
|
|
168
|
+
// block with the same ID is saved as a new widget.
|
|
169
|
+
if ( widgetId ) {
|
|
170
|
+
if (
|
|
171
|
+
claimedWidgetIds.has( widgetId ) ||
|
|
172
|
+
select.getWidgetAreaForWidgetId( widgetId )?.id !==
|
|
173
|
+
widgetAreaId
|
|
174
|
+
) {
|
|
175
|
+
widgetId = undefined;
|
|
176
|
+
// Drop the duplicate ID now, so nothing reads it before the
|
|
177
|
+
// new widget's ID is written back after the batch.
|
|
178
|
+
delete block.attributes.__internalWidgetId;
|
|
179
|
+
} else {
|
|
180
|
+
claimedWidgetIds.add( widgetId );
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
161
184
|
const oldWidget = widgets[ widgetId ];
|
|
162
185
|
const widget = transformBlockToWidget( block, oldWidget );
|
|
163
186
|
|
|
@@ -230,7 +253,7 @@ export const saveWidgetArea =
|
|
|
230
253
|
|
|
231
254
|
// Set __internalWidgetId on the block. This will be persisted to the
|
|
232
255
|
// store when we dispatch receiveEntityRecords( post ) below.
|
|
233
|
-
|
|
256
|
+
block.attributes.__internalWidgetId = widget.id;
|
|
234
257
|
|
|
235
258
|
const error = registry
|
|
236
259
|
.select( coreStore )
|
|
@@ -264,17 +287,9 @@ export const saveWidgetArea =
|
|
|
264
287
|
{ undoIgnore: true }
|
|
265
288
|
);
|
|
266
289
|
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
registry
|
|
270
|
-
.dispatch( coreStore )
|
|
271
|
-
.receiveEntityRecords( KIND, POST_TYPE, post, undefined );
|
|
272
|
-
};
|
|
273
|
-
|
|
274
|
-
const trySaveWidgetArea =
|
|
275
|
-
( widgetAreaId ) =>
|
|
276
|
-
( { registry } ) => {
|
|
277
|
-
registry
|
|
290
|
+
// Sidebar and widget requests can both rewrite the sidebar lists. Finish
|
|
291
|
+
// this write before saveWidgetAreas starts saving the next area.
|
|
292
|
+
await registry
|
|
278
293
|
.dispatch( coreStore )
|
|
279
294
|
.saveEditedEntityRecord(
|
|
280
295
|
KIND,
|
|
@@ -284,6 +299,10 @@ const trySaveWidgetArea =
|
|
|
284
299
|
throwOnError: true,
|
|
285
300
|
}
|
|
286
301
|
);
|
|
302
|
+
|
|
303
|
+
registry
|
|
304
|
+
.dispatch( coreStore )
|
|
305
|
+
.receiveEntityRecords( KIND, POST_TYPE, post, undefined );
|
|
287
306
|
};
|
|
288
307
|
|
|
289
308
|
/**
|
package/src/store/selectors.js
CHANGED
|
@@ -12,6 +12,8 @@ import {
|
|
|
12
12
|
} from './utils';
|
|
13
13
|
import { STORE_NAME as editWidgetsStoreName } from './constants';
|
|
14
14
|
|
|
15
|
+
/** @typedef {import('@wordpress/blocks').Block} Block */
|
|
16
|
+
|
|
15
17
|
const EMPTY_INSERTION_POINT = {
|
|
16
18
|
rootClientId: undefined,
|
|
17
19
|
insertionIndex: undefined,
|
|
@@ -108,7 +110,8 @@ export const getWidgetAreaForWidgetId = createRegistrySelector(
|
|
|
108
110
|
*
|
|
109
111
|
* @param {string} clientId The client id of a block in a widget area.
|
|
110
112
|
*
|
|
111
|
-
* @return {
|
|
113
|
+
* @return {Block | null} The widget area block.
|
|
114
|
+
* @type {(state: Record<string, unknown>, clientId: string) => Block | null}
|
|
112
115
|
*/
|
|
113
116
|
export const getParentWidgetAreaBlock = createRegistrySelector(
|
|
114
117
|
( select ) => ( state, clientId ) => {
|
|
@@ -0,0 +1,275 @@
|
|
|
1
|
+
import { describe, expect, it, vi } from 'vitest';
|
|
2
|
+
import { saveWidgetArea, saveWidgetAreas } from '../actions';
|
|
3
|
+
|
|
4
|
+
// Only the store descriptors are imported from these packages; stub them so
|
|
5
|
+
// their browser-only side effects do not run.
|
|
6
|
+
vi.mock( import( '@wordpress/interface' ), () => ( {
|
|
7
|
+
store: 'core/interface',
|
|
8
|
+
} ) );
|
|
9
|
+
vi.mock( import( '@wordpress/block-editor' ), () => ( {
|
|
10
|
+
store: 'core/block-editor',
|
|
11
|
+
} ) );
|
|
12
|
+
vi.mock( import( '@wordpress/notices' ), () => ( { store: 'core/notices' } ) );
|
|
13
|
+
|
|
14
|
+
vi.mock( import( '../transformers' ), () => ( {
|
|
15
|
+
transformBlockToWidget: ( block, relatedWidget = {} ) => ( {
|
|
16
|
+
...relatedWidget,
|
|
17
|
+
id_base: 'block',
|
|
18
|
+
instance: { raw: { content: block.attributes.content } },
|
|
19
|
+
} ),
|
|
20
|
+
} ) );
|
|
21
|
+
|
|
22
|
+
function setup(
|
|
23
|
+
blocks,
|
|
24
|
+
widgets,
|
|
25
|
+
{ area = 'sidebar-1', areaHoldingId = 'sidebar-1' } = {}
|
|
26
|
+
) {
|
|
27
|
+
let nextWidgetNumber = 10;
|
|
28
|
+
const saveEntityRecord = vi.fn( () => ( {
|
|
29
|
+
id: `block-${ nextWidgetNumber++ }`,
|
|
30
|
+
} ) );
|
|
31
|
+
const saveEditedEntityRecord = vi.fn( ( kind, name, id ) => ( { id } ) );
|
|
32
|
+
const coreDispatch = {
|
|
33
|
+
editEntityRecord: vi.fn(),
|
|
34
|
+
receiveEntityRecords: vi.fn(),
|
|
35
|
+
saveEditedEntityRecord: vi.fn(),
|
|
36
|
+
__experimentalBatch: vi.fn( async ( tasks ) =>
|
|
37
|
+
Promise.all(
|
|
38
|
+
tasks.map( ( task ) =>
|
|
39
|
+
task( { saveEntityRecord, saveEditedEntityRecord } )
|
|
40
|
+
)
|
|
41
|
+
)
|
|
42
|
+
),
|
|
43
|
+
};
|
|
44
|
+
const coreSelect = {
|
|
45
|
+
getEditedEntityRecord: () => ( { blocks } ),
|
|
46
|
+
hasEditsForEntityRecord: () => true,
|
|
47
|
+
getLastEntitySaveError: () => undefined,
|
|
48
|
+
};
|
|
49
|
+
const registry = {
|
|
50
|
+
select: () => coreSelect,
|
|
51
|
+
dispatch: () => coreDispatch,
|
|
52
|
+
};
|
|
53
|
+
const select = {
|
|
54
|
+
getWidgets: () => widgets,
|
|
55
|
+
getWidgetAreaForWidgetId: () => ( { id: areaHoldingId } ),
|
|
56
|
+
};
|
|
57
|
+
return {
|
|
58
|
+
run: () =>
|
|
59
|
+
saveWidgetArea( area )( {
|
|
60
|
+
select,
|
|
61
|
+
registry,
|
|
62
|
+
} ),
|
|
63
|
+
coreDispatch,
|
|
64
|
+
saveEntityRecord,
|
|
65
|
+
saveEditedEntityRecord,
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
describe( 'saveWidgetAreas', () => {
|
|
70
|
+
it( 'waits for a widget area’s sidebar save before saving the next area', async () => {
|
|
71
|
+
const firstSidebarSave = Promise.withResolvers();
|
|
72
|
+
const saveSidebar = vi.fn( ( kind, name, id ) =>
|
|
73
|
+
id === 'sidebar-1' ? firstSidebarSave.promise : Promise.resolve()
|
|
74
|
+
);
|
|
75
|
+
const registry = {
|
|
76
|
+
select: () => ( {
|
|
77
|
+
getEditedEntityRecord: () => ( { blocks: [] } ),
|
|
78
|
+
} ),
|
|
79
|
+
dispatch: () => ( {
|
|
80
|
+
__experimentalBatch: async () => [],
|
|
81
|
+
editEntityRecord: vi.fn(),
|
|
82
|
+
finishResolution: vi.fn(),
|
|
83
|
+
receiveEntityRecords: vi.fn(),
|
|
84
|
+
saveEditedEntityRecord: saveSidebar,
|
|
85
|
+
} ),
|
|
86
|
+
};
|
|
87
|
+
const select = { getWidgets: () => ( {} ) };
|
|
88
|
+
const dispatch = Object.assign(
|
|
89
|
+
( thunk ) => thunk( { registry, select, dispatch } ),
|
|
90
|
+
{
|
|
91
|
+
saveWidgetArea: vi.fn( ( id ) =>
|
|
92
|
+
dispatch( saveWidgetArea( id ) )
|
|
93
|
+
),
|
|
94
|
+
}
|
|
95
|
+
);
|
|
96
|
+
const save = dispatch(
|
|
97
|
+
saveWidgetAreas( [ { id: 'sidebar-1' }, { id: 'sidebar-2' } ] )
|
|
98
|
+
);
|
|
99
|
+
try {
|
|
100
|
+
await vi.waitFor( () => expect( saveSidebar ).toHaveBeenCalled() );
|
|
101
|
+
expect( dispatch.saveWidgetArea.mock.calls ).toEqual( [
|
|
102
|
+
[ 'sidebar-1' ],
|
|
103
|
+
] );
|
|
104
|
+
} finally {
|
|
105
|
+
firstSidebarSave.resolve();
|
|
106
|
+
await save;
|
|
107
|
+
}
|
|
108
|
+
expect( saveSidebar.mock.calls.map( ( [ , , id ] ) => id ) ).toEqual( [
|
|
109
|
+
'sidebar-1',
|
|
110
|
+
'sidebar-2',
|
|
111
|
+
] );
|
|
112
|
+
} );
|
|
113
|
+
} );
|
|
114
|
+
|
|
115
|
+
describe( 'saveWidgetArea', () => {
|
|
116
|
+
it( 'saves a block that shares an existing widget ID with an earlier block as a new widget', async () => {
|
|
117
|
+
// Splitting a paragraph widget copies the block's attributes,
|
|
118
|
+
// including `__internalWidgetId`, to the new block.
|
|
119
|
+
const blocks = [
|
|
120
|
+
{
|
|
121
|
+
name: 'core/paragraph',
|
|
122
|
+
clientId: 'head',
|
|
123
|
+
attributes: { content: 'Hello', __internalWidgetId: 'block-2' },
|
|
124
|
+
},
|
|
125
|
+
{
|
|
126
|
+
name: 'core/paragraph',
|
|
127
|
+
clientId: 'tail',
|
|
128
|
+
attributes: { content: 'world', __internalWidgetId: 'block-2' },
|
|
129
|
+
},
|
|
130
|
+
];
|
|
131
|
+
const widgets = {
|
|
132
|
+
'block-2': {
|
|
133
|
+
id: 'block-2',
|
|
134
|
+
id_base: 'block',
|
|
135
|
+
sidebar: 'sidebar-1',
|
|
136
|
+
},
|
|
137
|
+
};
|
|
138
|
+
const { run, coreDispatch, saveEntityRecord, saveEditedEntityRecord } =
|
|
139
|
+
setup( blocks, widgets );
|
|
140
|
+
|
|
141
|
+
await run();
|
|
142
|
+
|
|
143
|
+
const widgetEdits = coreDispatch.editEntityRecord.mock.calls.filter(
|
|
144
|
+
( [ kind, name ] ) => kind === 'root' && name === 'widget'
|
|
145
|
+
);
|
|
146
|
+
expect( widgetEdits ).toHaveLength( 1 );
|
|
147
|
+
expect( widgetEdits[ 0 ][ 3 ].instance.raw.content ).toBe( 'Hello' );
|
|
148
|
+
expect( saveEditedEntityRecord ).toHaveBeenCalledTimes( 1 );
|
|
149
|
+
expect( saveEntityRecord ).toHaveBeenCalledTimes( 1 );
|
|
150
|
+
expect( saveEntityRecord.mock.calls[ 0 ][ 2 ] ).toMatchObject( {
|
|
151
|
+
sidebar: 'sidebar-1',
|
|
152
|
+
instance: { raw: { content: 'world' } },
|
|
153
|
+
} );
|
|
154
|
+
|
|
155
|
+
expect( coreDispatch.editEntityRecord ).toHaveBeenCalledWith(
|
|
156
|
+
'root',
|
|
157
|
+
'sidebar',
|
|
158
|
+
'sidebar-1',
|
|
159
|
+
{ widgets: [ 'block-2', 'block-10' ] },
|
|
160
|
+
{ undoIgnore: true }
|
|
161
|
+
);
|
|
162
|
+
expect( blocks[ 1 ].attributes.__internalWidgetId ).toBe( 'block-10' );
|
|
163
|
+
} );
|
|
164
|
+
|
|
165
|
+
it( 'keeps updating existing widgets when every block has its own widget ID', async () => {
|
|
166
|
+
const blocks = [
|
|
167
|
+
{
|
|
168
|
+
name: 'core/paragraph',
|
|
169
|
+
clientId: 'a',
|
|
170
|
+
attributes: { content: 'A', __internalWidgetId: 'block-2' },
|
|
171
|
+
},
|
|
172
|
+
{
|
|
173
|
+
name: 'core/paragraph',
|
|
174
|
+
clientId: 'b',
|
|
175
|
+
attributes: { content: 'B', __internalWidgetId: 'block-3' },
|
|
176
|
+
},
|
|
177
|
+
];
|
|
178
|
+
const widgets = {
|
|
179
|
+
'block-2': {
|
|
180
|
+
id: 'block-2',
|
|
181
|
+
id_base: 'block',
|
|
182
|
+
sidebar: 'sidebar-1',
|
|
183
|
+
},
|
|
184
|
+
'block-3': {
|
|
185
|
+
id: 'block-3',
|
|
186
|
+
id_base: 'block',
|
|
187
|
+
sidebar: 'sidebar-1',
|
|
188
|
+
},
|
|
189
|
+
};
|
|
190
|
+
const { run, coreDispatch, saveEntityRecord, saveEditedEntityRecord } =
|
|
191
|
+
setup( blocks, widgets );
|
|
192
|
+
|
|
193
|
+
await run();
|
|
194
|
+
|
|
195
|
+
expect( saveEntityRecord ).not.toHaveBeenCalled();
|
|
196
|
+
expect( saveEditedEntityRecord ).toHaveBeenCalledTimes( 2 );
|
|
197
|
+
expect( coreDispatch.editEntityRecord ).toHaveBeenCalledWith(
|
|
198
|
+
'root',
|
|
199
|
+
'sidebar',
|
|
200
|
+
'sidebar-1',
|
|
201
|
+
{ widgets: [ 'block-2', 'block-3' ] },
|
|
202
|
+
{ undoIgnore: true }
|
|
203
|
+
);
|
|
204
|
+
} );
|
|
205
|
+
|
|
206
|
+
it( 'saves a block as a new widget when an earlier widget area holds the same widget ID', async () => {
|
|
207
|
+
// The new block from a split was moved to another widget area, so both
|
|
208
|
+
// areas hold a block with `block-2`. The first area keeps the widget.
|
|
209
|
+
const blocks = [
|
|
210
|
+
{
|
|
211
|
+
name: 'core/paragraph',
|
|
212
|
+
clientId: 'tail',
|
|
213
|
+
attributes: { content: 'world', __internalWidgetId: 'block-2' },
|
|
214
|
+
},
|
|
215
|
+
];
|
|
216
|
+
const widgets = {
|
|
217
|
+
'block-2': {
|
|
218
|
+
id: 'block-2',
|
|
219
|
+
id_base: 'block',
|
|
220
|
+
sidebar: 'sidebar-1',
|
|
221
|
+
},
|
|
222
|
+
};
|
|
223
|
+
const { run, coreDispatch, saveEntityRecord, saveEditedEntityRecord } =
|
|
224
|
+
setup( blocks, widgets, {
|
|
225
|
+
area: 'sidebar-2',
|
|
226
|
+
areaHoldingId: 'sidebar-1',
|
|
227
|
+
} );
|
|
228
|
+
|
|
229
|
+
await run();
|
|
230
|
+
|
|
231
|
+
expect( saveEditedEntityRecord ).not.toHaveBeenCalled();
|
|
232
|
+
expect( saveEntityRecord ).toHaveBeenCalledTimes( 1 );
|
|
233
|
+
expect( saveEntityRecord.mock.calls[ 0 ][ 2 ] ).toMatchObject( {
|
|
234
|
+
sidebar: 'sidebar-2',
|
|
235
|
+
} );
|
|
236
|
+
expect( coreDispatch.editEntityRecord ).toHaveBeenCalledWith(
|
|
237
|
+
'root',
|
|
238
|
+
'sidebar',
|
|
239
|
+
'sidebar-2',
|
|
240
|
+
{ widgets: [ 'block-10' ] },
|
|
241
|
+
{ undoIgnore: true }
|
|
242
|
+
);
|
|
243
|
+
} );
|
|
244
|
+
|
|
245
|
+
it( 'keeps updating a widget that was moved to another widget area', async () => {
|
|
246
|
+
const blocks = [
|
|
247
|
+
{
|
|
248
|
+
name: 'core/paragraph',
|
|
249
|
+
clientId: 'moved',
|
|
250
|
+
attributes: { content: 'Moved', __internalWidgetId: 'block-2' },
|
|
251
|
+
},
|
|
252
|
+
];
|
|
253
|
+
const widgets = {
|
|
254
|
+
'block-2': {
|
|
255
|
+
id: 'block-2',
|
|
256
|
+
id_base: 'block',
|
|
257
|
+
sidebar: 'sidebar-1',
|
|
258
|
+
},
|
|
259
|
+
};
|
|
260
|
+
const { run, saveEntityRecord, saveEditedEntityRecord } = setup(
|
|
261
|
+
blocks,
|
|
262
|
+
widgets,
|
|
263
|
+
{ area: 'sidebar-2', areaHoldingId: 'sidebar-2' }
|
|
264
|
+
);
|
|
265
|
+
|
|
266
|
+
await run();
|
|
267
|
+
|
|
268
|
+
expect( saveEntityRecord ).not.toHaveBeenCalled();
|
|
269
|
+
expect( saveEditedEntityRecord ).toHaveBeenCalledWith(
|
|
270
|
+
'root',
|
|
271
|
+
'widget',
|
|
272
|
+
'block-2'
|
|
273
|
+
);
|
|
274
|
+
} );
|
|
275
|
+
} );
|