@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.
Files changed (201) hide show
  1. package/build/blocks/widget-area/edit/index.cjs +10 -8
  2. package/build/blocks/widget-area/edit/index.cjs.map +1 -1
  3. package/build/blocks/widget-area/edit/inner-blocks.cjs +10 -8
  4. package/build/blocks/widget-area/edit/inner-blocks.cjs.map +1 -1
  5. package/build/blocks/widget-area/edit/use-is-dragging-within.cjs.map +1 -1
  6. package/build/blocks/widget-area/index.cjs +9 -7
  7. package/build/blocks/widget-area/index.cjs.map +1 -1
  8. package/build/components/error-boundary/index.cjs +1 -1
  9. package/build/components/error-boundary/index.cjs.map +1 -1
  10. package/build/components/header/document-tools/index.cjs +20 -10
  11. package/build/components/header/document-tools/index.cjs.map +1 -1
  12. package/build/components/header/index.cjs +18 -11
  13. package/build/components/header/index.cjs.map +1 -1
  14. package/build/components/keyboard-shortcut-help-modal/config.cjs +5 -0
  15. package/build/components/keyboard-shortcut-help-modal/config.cjs.map +1 -1
  16. package/build/components/keyboard-shortcut-help-modal/dynamic-shortcut.cjs +9 -7
  17. package/build/components/keyboard-shortcut-help-modal/dynamic-shortcut.cjs.map +1 -1
  18. package/build/components/keyboard-shortcut-help-modal/index.cjs +12 -11
  19. package/build/components/keyboard-shortcut-help-modal/index.cjs.map +1 -1
  20. package/build/components/keyboard-shortcut-help-modal/shortcut.cjs +35 -13
  21. package/build/components/keyboard-shortcut-help-modal/shortcut.cjs.map +1 -1
  22. package/build/components/keyboard-shortcuts/index.cjs.map +1 -1
  23. package/build/components/layout/index.cjs +19 -13
  24. package/build/components/layout/index.cjs.map +1 -1
  25. package/build/components/layout/interface.cjs +27 -13
  26. package/build/components/layout/interface.cjs.map +1 -1
  27. package/build/components/layout/unsaved-changes-warning.cjs +3 -6
  28. package/build/components/layout/unsaved-changes-warning.cjs.map +1 -1
  29. package/build/components/more-menu/index.cjs +14 -10
  30. package/build/components/more-menu/index.cjs.map +1 -1
  31. package/build/components/secondary-sidebar/index.cjs +10 -8
  32. package/build/components/secondary-sidebar/index.cjs.map +1 -1
  33. package/build/components/secondary-sidebar/inserter-sidebar.cjs +9 -7
  34. package/build/components/secondary-sidebar/inserter-sidebar.cjs.map +1 -1
  35. package/build/components/sidebar/index.cjs +23 -15
  36. package/build/components/sidebar/index.cjs.map +1 -1
  37. package/build/components/welcome-guide/index.cjs +5 -1
  38. package/build/components/welcome-guide/index.cjs.map +1 -1
  39. package/build/components/widget-areas-block-editor-content/index.cjs +10 -8
  40. package/build/components/widget-areas-block-editor-content/index.cjs.map +1 -1
  41. package/build/components/widget-areas-block-editor-provider/index.cjs +10 -8
  42. package/build/components/widget-areas-block-editor-provider/index.cjs.map +1 -1
  43. package/build/constants.cjs +0 -2
  44. package/build/constants.cjs.map +1 -1
  45. package/build/index.cjs +12 -10
  46. package/build/index.cjs.map +1 -1
  47. package/build/store/actions.cjs +20 -9
  48. package/build/store/actions.cjs.map +1 -1
  49. package/build/store/index.cjs +14 -12
  50. package/build/store/index.cjs.map +1 -1
  51. package/build/store/selectors.cjs +3 -1
  52. package/build/store/selectors.cjs.map +1 -1
  53. package/build-module/blocks/widget-area/index.mjs.map +1 -1
  54. package/build-module/components/error-boundary/index.mjs +2 -2
  55. package/build-module/components/error-boundary/index.mjs.map +1 -1
  56. package/build-module/components/header/document-tools/index.mjs +10 -2
  57. package/build-module/components/header/document-tools/index.mjs.map +1 -1
  58. package/build-module/components/header/index.mjs +6 -2
  59. package/build-module/components/header/index.mjs.map +1 -1
  60. package/build-module/components/header/undo-redo/redo.mjs.map +1 -1
  61. package/build-module/components/header/undo-redo/undo.mjs.map +1 -1
  62. package/build-module/components/keyboard-shortcut-help-modal/config.mjs +5 -0
  63. package/build-module/components/keyboard-shortcut-help-modal/config.mjs.map +1 -1
  64. package/build-module/components/keyboard-shortcut-help-modal/index.mjs +1 -2
  65. package/build-module/components/keyboard-shortcut-help-modal/index.mjs.map +1 -1
  66. package/build-module/components/keyboard-shortcut-help-modal/shortcut.mjs +35 -13
  67. package/build-module/components/keyboard-shortcut-help-modal/shortcut.mjs.map +1 -1
  68. package/build-module/components/layout/index.mjs +5 -1
  69. package/build-module/components/layout/index.mjs.map +1 -1
  70. package/build-module/components/layout/interface.mjs +16 -4
  71. package/build-module/components/layout/interface.mjs.map +1 -1
  72. package/build-module/components/layout/unsaved-changes-warning.mjs +3 -6
  73. package/build-module/components/layout/unsaved-changes-warning.mjs.map +1 -1
  74. package/build-module/components/more-menu/index.mjs +4 -2
  75. package/build-module/components/more-menu/index.mjs.map +1 -1
  76. package/build-module/components/sidebar/index.mjs +14 -8
  77. package/build-module/components/sidebar/index.mjs.map +1 -1
  78. package/build-module/components/welcome-guide/index.mjs +5 -1
  79. package/build-module/components/welcome-guide/index.mjs.map +1 -1
  80. package/build-module/constants.mjs +1 -2
  81. package/build-module/constants.mjs.map +1 -1
  82. package/build-module/index.mjs +2 -2
  83. package/build-module/index.mjs.map +1 -1
  84. package/build-module/store/actions.mjs +20 -9
  85. package/build-module/store/actions.mjs.map +1 -1
  86. package/build-module/store/selectors.mjs +3 -1
  87. package/build-module/store/selectors.mjs.map +1 -1
  88. package/build-style/style-rtl.css +72 -46
  89. package/build-style/style.css +72 -46
  90. package/build-types/blocks/widget-area/edit/index.d.ts +10 -0
  91. package/build-types/blocks/widget-area/edit/index.d.ts.map +1 -0
  92. package/build-types/blocks/widget-area/edit/inner-blocks.d.ts +4 -0
  93. package/build-types/blocks/widget-area/edit/inner-blocks.d.ts.map +1 -0
  94. package/build-types/blocks/widget-area/edit/use-is-dragging-within.d.ts +12 -0
  95. package/build-types/blocks/widget-area/edit/use-is-dragging-within.d.ts.map +1 -0
  96. package/build-types/blocks/widget-area/index.d.ts +14 -0
  97. package/build-types/blocks/widget-area/index.d.ts.map +1 -0
  98. package/build-types/components/error-boundary/index.d.ts +14 -0
  99. package/build-types/components/error-boundary/index.d.ts.map +1 -0
  100. package/build-types/components/header/document-tools/index.d.ts +3 -0
  101. package/build-types/components/header/document-tools/index.d.ts.map +1 -0
  102. package/build-types/components/header/index.d.ts +3 -0
  103. package/build-types/components/header/index.d.ts.map +1 -0
  104. package/build-types/components/header/undo-redo/redo.d.ts +3 -0
  105. package/build-types/components/header/undo-redo/redo.d.ts.map +1 -0
  106. package/build-types/components/header/undo-redo/undo.d.ts +3 -0
  107. package/build-types/components/header/undo-redo/undo.d.ts.map +1 -0
  108. package/build-types/components/keyboard-shortcut-help-modal/config.d.ts +3 -0
  109. package/build-types/components/keyboard-shortcut-help-modal/config.d.ts.map +1 -0
  110. package/build-types/components/keyboard-shortcut-help-modal/dynamic-shortcut.d.ts +5 -0
  111. package/build-types/components/keyboard-shortcut-help-modal/dynamic-shortcut.d.ts.map +1 -0
  112. package/build-types/components/keyboard-shortcut-help-modal/index.d.ts +5 -0
  113. package/build-types/components/keyboard-shortcut-help-modal/index.d.ts.map +1 -0
  114. package/build-types/components/keyboard-shortcut-help-modal/shortcut.d.ts +25 -0
  115. package/build-types/components/keyboard-shortcut-help-modal/shortcut.d.ts.map +1 -0
  116. package/build-types/components/keyboard-shortcuts/index.d.ts +7 -0
  117. package/build-types/components/keyboard-shortcuts/index.d.ts.map +1 -0
  118. package/build-types/components/layout/index.d.ts +5 -0
  119. package/build-types/components/layout/index.d.ts.map +1 -0
  120. package/build-types/components/layout/interface.d.ts +5 -0
  121. package/build-types/components/layout/interface.d.ts.map +1 -0
  122. package/build-types/components/layout/unsaved-changes-warning.d.ts +10 -0
  123. package/build-types/components/layout/unsaved-changes-warning.d.ts.map +1 -0
  124. package/build-types/components/more-menu/index.d.ts +2 -0
  125. package/build-types/components/more-menu/index.d.ts.map +1 -0
  126. package/build-types/components/more-menu/tools-more-menu-group.d.ts +6 -0
  127. package/build-types/components/more-menu/tools-more-menu-group.d.ts.map +1 -0
  128. package/build-types/components/notices/index.d.ts +3 -0
  129. package/build-types/components/notices/index.d.ts.map +1 -0
  130. package/build-types/components/save-button/index.d.ts +3 -0
  131. package/build-types/components/save-button/index.d.ts.map +1 -0
  132. package/build-types/components/secondary-sidebar/index.d.ts +2 -0
  133. package/build-types/components/secondary-sidebar/index.d.ts.map +1 -0
  134. package/build-types/components/secondary-sidebar/inserter-sidebar.d.ts +2 -0
  135. package/build-types/components/secondary-sidebar/inserter-sidebar.d.ts.map +1 -0
  136. package/build-types/components/secondary-sidebar/list-view-sidebar.d.ts +2 -0
  137. package/build-types/components/secondary-sidebar/list-view-sidebar.d.ts.map +1 -0
  138. package/build-types/components/sidebar/index.d.ts +2 -0
  139. package/build-types/components/sidebar/index.d.ts.map +1 -0
  140. package/build-types/components/sidebar/widget-areas.d.ts +4 -0
  141. package/build-types/components/sidebar/widget-areas.d.ts.map +1 -0
  142. package/build-types/components/welcome-guide/index.d.ts +2 -0
  143. package/build-types/components/welcome-guide/index.d.ts.map +1 -0
  144. package/build-types/components/widget-areas-block-editor-content/index.d.ts +4 -0
  145. package/build-types/components/widget-areas-block-editor-content/index.d.ts.map +1 -0
  146. package/build-types/components/widget-areas-block-editor-provider/index.d.ts +6 -0
  147. package/build-types/components/widget-areas-block-editor-provider/index.d.ts.map +1 -0
  148. package/build-types/constants.d.ts +2 -0
  149. package/build-types/constants.d.ts.map +1 -0
  150. package/build-types/filters/index.d.ts +3 -0
  151. package/build-types/filters/index.d.ts.map +1 -0
  152. package/build-types/filters/move-to-widget-area.d.ts +2 -0
  153. package/build-types/filters/move-to-widget-area.d.ts.map +1 -0
  154. package/build-types/filters/replace-media-upload.d.ts +2 -0
  155. package/build-types/filters/replace-media-upload.d.ts.map +1 -0
  156. package/build-types/hooks/use-last-selected-widget-area.d.ts +9 -0
  157. package/build-types/hooks/use-last-selected-widget-area.d.ts.map +1 -0
  158. package/build-types/hooks/use-widget-library-insertion-point.d.ts +3 -0
  159. package/build-types/hooks/use-widget-library-insertion-point.d.ts.map +1 -0
  160. package/build-types/index.d.ts +16 -0
  161. package/build-types/index.d.ts.map +1 -0
  162. package/build-types/lock-unlock.d.ts +2 -0
  163. package/build-types/lock-unlock.d.ts.map +1 -0
  164. package/build-types/store/actions.d.ts +144 -0
  165. package/build-types/store/actions.d.ts.map +1 -0
  166. package/build-types/store/constants.d.ts +5 -0
  167. package/build-types/store/constants.d.ts.map +1 -0
  168. package/build-types/store/index.d.ts +9 -0
  169. package/build-types/store/index.d.ts.map +1 -0
  170. package/build-types/store/private-selectors.d.ts +3 -0
  171. package/build-types/store/private-selectors.d.ts.map +1 -0
  172. package/build-types/store/reducer.d.ts +73 -0
  173. package/build-types/store/reducer.d.ts.map +1 -0
  174. package/build-types/store/resolvers.d.ts +17 -0
  175. package/build-types/store/resolvers.d.ts.map +1 -0
  176. package/build-types/store/selectors.d.ts +159 -0
  177. package/build-types/store/selectors.d.ts.map +1 -0
  178. package/build-types/store/transformers.d.ts +16 -0
  179. package/build-types/store/transformers.d.ts.map +1 -0
  180. package/build-types/store/utils.d.ts +53 -0
  181. package/build-types/store/utils.d.ts.map +1 -0
  182. package/package.json +33 -33
  183. package/src/components/error-boundary/index.jsx +2 -2
  184. package/src/components/header/index.jsx +11 -2
  185. package/src/components/header/style.scss +30 -3
  186. package/src/components/keyboard-shortcut-help-modal/config.js +3 -0
  187. package/src/components/keyboard-shortcut-help-modal/index.jsx +0 -2
  188. package/src/components/keyboard-shortcut-help-modal/shortcut.jsx +44 -29
  189. package/src/components/keyboard-shortcut-help-modal/style.scss +1 -1
  190. package/src/components/layout/style.scss +5 -9
  191. package/src/components/layout/unsaved-changes-warning.js +4 -9
  192. package/src/components/more-menu/index.jsx +0 -1
  193. package/src/components/sidebar/index.jsx +1 -1
  194. package/src/components/sidebar/style.scss +6 -0
  195. package/src/components/welcome-guide/style.scss +1 -1
  196. package/src/components/widget-areas-block-editor-content/style.scss +1 -1
  197. package/src/constants.js +0 -1
  198. package/src/index.jsx +2 -7
  199. package/src/store/actions.js +33 -14
  200. package/src/store/selectors.js +4 -1
  201. 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;
@@ -23,7 +23,7 @@
23
23
  }
24
24
 
25
25
  &__text {
26
- font-size: $default-font-size;
26
+ font-size: var(--wpds-typography-font-size-md);
27
27
  line-height: 1.4;
28
28
  margin: 0 0 $grid-unit-30 0;
29
29
  padding: 0 $grid-unit-40;
@@ -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: $default-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
@@ -1,2 +1 @@
1
1
  export const ALLOW_REUSABLE_BLOCKS = false;
2
- export const ENABLE_EXPERIMENTAL_FSE_BLOCKS = false;
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 );
@@ -108,7 +108,7 @@ export const saveWidgetAreas =
108
108
  */
109
109
  export const saveWidgetArea =
110
110
  ( widgetAreaId ) =>
111
- async ( { dispatch, select, registry } ) => {
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
- const widgetId = getWidgetIdFromBlock( block );
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
- post.blocks[ position ].attributes.__internalWidgetId = widget.id;
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
- dispatch( trySaveWidgetArea( widgetAreaId ) );
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
  /**
@@ -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 {WPBlock} The widget area block.
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
+ } );