@wordpress/edit-widgets 7.0.0 → 7.1.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 (188) hide show
  1. package/build/blocks/widget-area/edit/index.cjs +106 -116
  2. package/build/blocks/widget-area/edit/index.cjs.map +1 -7
  3. package/build/blocks/widget-area/edit/inner-blocks.cjs +44 -62
  4. package/build/blocks/widget-area/edit/inner-blocks.cjs.map +1 -7
  5. package/build/blocks/widget-area/edit/use-is-dragging-within.cjs +41 -53
  6. package/build/blocks/widget-area/edit/use-is-dragging-within.cjs.map +1 -7
  7. package/build/blocks/widget-area/index.cjs +40 -44
  8. package/build/blocks/widget-area/index.cjs.map +1 -7
  9. package/build/components/error-boundary/index.cjs +115 -155
  10. package/build/components/error-boundary/index.cjs.map +1 -7
  11. package/build/components/header/document-tools/index.cjs +80 -115
  12. package/build/components/header/document-tools/index.cjs.map +1 -7
  13. package/build/components/header/index.cjs +72 -80
  14. package/build/components/header/index.cjs.map +1 -7
  15. package/build/components/header/undo-redo/redo.cjs +30 -52
  16. package/build/components/header/undo-redo/redo.cjs.map +1 -7
  17. package/build/components/header/undo-redo/undo.cjs +29 -51
  18. package/build/components/header/undo-redo/undo.cjs.map +1 -7
  19. package/build/components/keyboard-shortcut-help-modal/config.cjs +88 -85
  20. package/build/components/keyboard-shortcut-help-modal/config.cjs.map +1 -7
  21. package/build/components/keyboard-shortcut-help-modal/dynamic-shortcut.cjs +40 -57
  22. package/build/components/keyboard-shortcut-help-modal/dynamic-shortcut.cjs.map +1 -7
  23. package/build/components/keyboard-shortcut-help-modal/index.cjs +94 -161
  24. package/build/components/keyboard-shortcut-help-modal/index.cjs.map +1 -7
  25. package/build/components/keyboard-shortcut-help-modal/shortcut.cjs +37 -75
  26. package/build/components/keyboard-shortcut-help-modal/shortcut.cjs.map +1 -7
  27. package/build/components/keyboard-shortcuts/index.cjs +107 -140
  28. package/build/components/keyboard-shortcuts/index.cjs.map +1 -7
  29. package/build/components/layout/index.cjs +65 -74
  30. package/build/components/layout/index.cjs.map +1 -7
  31. package/build/components/layout/interface.cjs +82 -108
  32. package/build/components/layout/interface.cjs.map +1 -7
  33. package/build/components/layout/unsaved-changes-warning.cjs +44 -48
  34. package/build/components/layout/unsaved-changes-warning.cjs.map +1 -7
  35. package/build/components/more-menu/index.cjs +125 -191
  36. package/build/components/more-menu/index.cjs.map +1 -7
  37. package/build/components/more-menu/tools-more-menu-group.cjs +9 -29
  38. package/build/components/more-menu/tools-more-menu-group.cjs.map +1 -7
  39. package/build/components/notices/index.cjs +19 -39
  40. package/build/components/notices/index.cjs.map +1 -7
  41. package/build/components/save-button/index.cjs +31 -58
  42. package/build/components/save-button/index.cjs.map +1 -7
  43. package/build/components/secondary-sidebar/index.cjs +38 -45
  44. package/build/components/secondary-sidebar/index.cjs.map +1 -7
  45. package/build/components/secondary-sidebar/inserter-sidebar.cjs +50 -51
  46. package/build/components/secondary-sidebar/inserter-sidebar.cjs.map +1 -7
  47. package/build/components/secondary-sidebar/list-view-sidebar.cjs +51 -82
  48. package/build/components/secondary-sidebar/list-view-sidebar.cjs.map +1 -7
  49. package/build/components/sidebar/index.cjs +117 -180
  50. package/build/components/sidebar/index.cjs.map +1 -7
  51. package/build/components/sidebar/widget-areas.cjs +43 -85
  52. package/build/components/sidebar/widget-areas.cjs.map +1 -7
  53. package/build/components/welcome-guide/index.cjs +110 -186
  54. package/build/components/welcome-guide/index.cjs.map +1 -7
  55. package/build/components/widget-areas-block-editor-content/index.cjs +59 -64
  56. package/build/components/widget-areas-block-editor-content/index.cjs.map +1 -7
  57. package/build/components/widget-areas-block-editor-provider/index.cjs +105 -140
  58. package/build/components/widget-areas-block-editor-provider/index.cjs.map +1 -7
  59. package/build/constants.cjs +11 -32
  60. package/build/constants.cjs.map +1 -7
  61. package/build/filters/index.cjs +7 -4
  62. package/build/filters/move-to-widget-area.cjs +38 -53
  63. package/build/filters/move-to-widget-area.cjs.map +1 -7
  64. package/build/filters/replace-media-upload.cjs +11 -9
  65. package/build/filters/replace-media-upload.cjs.map +1 -7
  66. package/build/hooks/use-last-selected-widget-area.cjs +28 -49
  67. package/build/hooks/use-last-selected-widget-area.cjs.map +1 -7
  68. package/build/hooks/use-widget-library-insertion-point.cjs +33 -67
  69. package/build/hooks/use-widget-library-insertion-point.cjs.map +1 -7
  70. package/build/index.cjs +96 -97
  71. package/build/index.cjs.map +1 -7
  72. package/build/lock-unlock.cjs +10 -35
  73. package/build/lock-unlock.cjs.map +1 -7
  74. package/build/store/actions.cjs +269 -278
  75. package/build/store/actions.cjs.map +1 -7
  76. package/build/store/constants.cjs +12 -29
  77. package/build/store/constants.cjs.map +1 -7
  78. package/build/store/index.cjs +61 -52
  79. package/build/store/index.cjs.map +1 -7
  80. package/build/store/private-selectors.cjs +11 -32
  81. package/build/store/private-selectors.cjs.map +1 -7
  82. package/build/store/reducer.cjs +107 -88
  83. package/build/store/reducer.cjs.map +1 -7
  84. package/build/store/resolvers.cjs +60 -91
  85. package/build/store/resolvers.cjs.map +1 -7
  86. package/build/store/selectors.cjs +196 -229
  87. package/build/store/selectors.cjs.map +1 -7
  88. package/build/store/transformers.cjs +51 -82
  89. package/build/store/transformers.cjs.map +1 -7
  90. package/build/store/utils.cjs +76 -63
  91. package/build/store/utils.cjs.map +1 -7
  92. package/build-module/blocks/widget-area/edit/index.mjs +82 -94
  93. package/build-module/blocks/widget-area/edit/index.mjs.map +1 -7
  94. package/build-module/blocks/widget-area/edit/inner-blocks.mjs +19 -35
  95. package/build-module/blocks/widget-area/edit/inner-blocks.mjs.map +1 -7
  96. package/build-module/blocks/widget-area/edit/use-is-dragging-within.mjs +36 -33
  97. package/build-module/blocks/widget-area/edit/use-is-dragging-within.mjs.map +1 -7
  98. package/build-module/blocks/widget-area/index.mjs +12 -15
  99. package/build-module/blocks/widget-area/index.mjs.map +1 -7
  100. package/build-module/components/error-boundary/index.mjs +103 -128
  101. package/build-module/components/error-boundary/index.mjs.map +1 -7
  102. package/build-module/components/header/document-tools/index.mjs +51 -84
  103. package/build-module/components/header/document-tools/index.mjs.map +1 -7
  104. package/build-module/components/header/index.mjs +40 -47
  105. package/build-module/components/header/index.mjs.map +1 -7
  106. package/build-module/components/header/undo-redo/redo.mjs +20 -27
  107. package/build-module/components/header/undo-redo/redo.mjs.map +1 -7
  108. package/build-module/components/header/undo-redo/undo.mjs +19 -26
  109. package/build-module/components/header/undo-redo/undo.mjs.map +1 -7
  110. package/build-module/components/keyboard-shortcut-help-modal/config.mjs +83 -61
  111. package/build-module/components/keyboard-shortcut-help-modal/config.mjs.map +1 -7
  112. package/build-module/components/keyboard-shortcut-help-modal/dynamic-shortcut.mjs +19 -33
  113. package/build-module/components/keyboard-shortcut-help-modal/dynamic-shortcut.mjs.map +1 -7
  114. package/build-module/components/keyboard-shortcut-help-modal/index.mjs +67 -136
  115. package/build-module/components/keyboard-shortcut-help-modal/index.mjs.map +1 -7
  116. package/build-module/components/keyboard-shortcut-help-modal/shortcut.mjs +31 -54
  117. package/build-module/components/keyboard-shortcut-help-modal/shortcut.mjs.map +1 -7
  118. package/build-module/components/keyboard-shortcuts/index.mjs +99 -119
  119. package/build-module/components/keyboard-shortcuts/index.mjs.map +1 -7
  120. package/build-module/components/layout/index.mjs +29 -40
  121. package/build-module/components/layout/index.mjs.map +1 -7
  122. package/build-module/components/layout/interface.mjs +53 -82
  123. package/build-module/components/layout/interface.mjs.map +1 -7
  124. package/build-module/components/layout/unsaved-changes-warning.mjs +37 -26
  125. package/build-module/components/layout/unsaved-changes-warning.mjs.map +1 -7
  126. package/build-module/components/more-menu/index.mjs +95 -162
  127. package/build-module/components/more-menu/index.mjs.map +1 -7
  128. package/build-module/components/more-menu/tools-more-menu-group.mjs +5 -9
  129. package/build-module/components/more-menu/tools-more-menu-group.mjs.map +1 -7
  130. package/build-module/components/notices/index.mjs +12 -17
  131. package/build-module/components/notices/index.mjs.map +1 -7
  132. package/build-module/components/save-button/index.mjs +23 -35
  133. package/build-module/components/save-button/index.mjs.map +1 -7
  134. package/build-module/components/secondary-sidebar/index.mjs +15 -20
  135. package/build-module/components/secondary-sidebar/index.mjs.map +1 -7
  136. package/build-module/components/secondary-sidebar/inserter-sidebar.mjs +27 -25
  137. package/build-module/components/secondary-sidebar/inserter-sidebar.mjs.map +1 -7
  138. package/build-module/components/secondary-sidebar/list-view-sidebar.mjs +39 -55
  139. package/build-module/components/secondary-sidebar/list-view-sidebar.mjs.map +1 -7
  140. package/build-module/components/sidebar/index.mjs +96 -162
  141. package/build-module/components/sidebar/index.mjs.map +1 -7
  142. package/build-module/components/sidebar/widget-areas.mjs +30 -57
  143. package/build-module/components/sidebar/widget-areas.mjs.map +1 -7
  144. package/build-module/components/welcome-guide/index.mjs +103 -164
  145. package/build-module/components/welcome-guide/index.mjs.map +1 -7
  146. package/build-module/components/widget-areas-block-editor-content/index.mjs +34 -44
  147. package/build-module/components/widget-areas-block-editor-content/index.mjs.map +1 -7
  148. package/build-module/components/widget-areas-block-editor-provider/index.mjs +76 -109
  149. package/build-module/components/widget-areas-block-editor-provider/index.mjs.map +1 -7
  150. package/build-module/constants.mjs +5 -7
  151. package/build-module/constants.mjs.map +1 -7
  152. package/build-module/filters/index.mjs +0 -2
  153. package/build-module/filters/move-to-widget-area.mjs +28 -48
  154. package/build-module/filters/move-to-widget-area.mjs.map +1 -7
  155. package/build-module/filters/replace-media-upload.mjs +5 -8
  156. package/build-module/filters/replace-media-upload.mjs.map +1 -7
  157. package/build-module/hooks/use-last-selected-widget-area.mjs +23 -29
  158. package/build-module/hooks/use-last-selected-widget-area.mjs.map +1 -7
  159. package/build-module/hooks/use-widget-library-insertion-point.mjs +28 -47
  160. package/build-module/hooks/use-widget-library-insertion-point.mjs.map +1 -7
  161. package/build-module/index.mjs +62 -79
  162. package/build-module/index.mjs.map +1 -7
  163. package/build-module/lock-unlock.mjs +5 -10
  164. package/build-module/lock-unlock.mjs.map +1 -7
  165. package/build-module/store/actions.mjs +250 -247
  166. package/build-module/store/actions.mjs.map +1 -7
  167. package/build-module/store/constants.mjs +7 -5
  168. package/build-module/store/constants.mjs.map +1 -7
  169. package/build-module/store/index.mjs +18 -16
  170. package/build-module/store/index.mjs.map +1 -7
  171. package/build-module/store/private-selectors.mjs +5 -7
  172. package/build-module/store/private-selectors.mjs.map +1 -7
  173. package/build-module/store/reducer.mjs +95 -58
  174. package/build-module/store/reducer.mjs.map +1 -7
  175. package/build-module/store/resolvers.mjs +52 -71
  176. package/build-module/store/resolvers.mjs.map +1 -7
  177. package/build-module/store/selectors.mjs +178 -199
  178. package/build-module/store/selectors.mjs.map +1 -7
  179. package/build-module/store/transformers.mjs +44 -56
  180. package/build-module/store/transformers.mjs.map +1 -7
  181. package/build-module/store/utils.mjs +64 -32
  182. package/build-module/store/utils.mjs.map +1 -7
  183. package/package.json +33 -33
  184. package/src/components/header/index.jsx +2 -2
  185. package/src/components/sidebar/index.jsx +18 -18
  186. package/src/components/welcome-guide/index.jsx +4 -2
  187. package/build/filters/index.cjs.map +0 -7
  188. package/build-module/filters/index.mjs.map +0 -7
@@ -1,300 +1,291 @@
1
- var __defProp = Object.defineProperty;
2
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
- var __getOwnPropNames = Object.getOwnPropertyNames;
4
- var __hasOwnProp = Object.prototype.hasOwnProperty;
5
- var __export = (target, all) => {
6
- for (var name in all)
7
- __defProp(target, name, { get: all[name], enumerable: true });
8
- };
9
- var __copyProps = (to, from, except, desc) => {
10
- if (from && typeof from === "object" || typeof from === "function") {
11
- for (let key of __getOwnPropNames(from))
12
- if (!__hasOwnProp.call(to, key) && key !== except)
13
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
- }
15
- return to;
16
- };
17
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
-
19
- // packages/edit-widgets/src/store/actions.js
20
- var actions_exports = {};
21
- __export(actions_exports, {
22
- closeGeneralSidebar: () => closeGeneralSidebar,
23
- lockWidgetSaving: () => lockWidgetSaving,
24
- moveBlockToWidgetArea: () => moveBlockToWidgetArea,
25
- persistStubPost: () => persistStubPost,
26
- saveEditedWidgetAreas: () => saveEditedWidgetAreas,
27
- saveWidgetArea: () => saveWidgetArea,
28
- saveWidgetAreas: () => saveWidgetAreas,
29
- setIsInserterOpened: () => setIsInserterOpened,
30
- setIsListViewOpened: () => setIsListViewOpened,
31
- setIsWidgetAreaOpen: () => setIsWidgetAreaOpen,
32
- setWidgetAreasOpenState: () => setWidgetAreasOpenState,
33
- setWidgetIdForClientId: () => setWidgetIdForClientId,
34
- unlockWidgetSaving: () => unlockWidgetSaving
1
+ "use strict";
2
+ Object.defineProperties(exports, {
3
+ __esModule: { value: true },
4
+ [Symbol.toStringTag]: { value: "Module" }
35
5
  });
36
- module.exports = __toCommonJS(actions_exports);
37
- var import_i18n = require("@wordpress/i18n");
38
- var import_notices = require("@wordpress/notices");
39
- var import_interface = require("@wordpress/interface");
40
- var import_widgets = require("@wordpress/widgets");
41
- var import_core_data = require("@wordpress/core-data");
42
- var import_block_editor = require("@wordpress/block-editor");
43
- var import_transformers = require("./transformers.cjs");
44
- var import_utils = require("./utils.cjs");
45
- var import_constants = require("./constants.cjs");
46
- var persistStubPost = (id, blocks) => ({ registry }) => {
47
- const stubPost = (0, import_utils.createStubPost)(id, blocks);
48
- registry.dispatch(import_core_data.store).receiveEntityRecords(
49
- import_utils.KIND,
50
- import_utils.POST_TYPE,
51
- stubPost,
52
- { id: stubPost.id },
53
- false
54
- );
55
- return stubPost;
6
+ let _wordpress_i18n = require("@wordpress/i18n");
7
+ let _wordpress_notices = require("@wordpress/notices");
8
+ let _wordpress_interface = require("@wordpress/interface");
9
+ let _wordpress_widgets = require("@wordpress/widgets");
10
+ let _wordpress_core_data = require("@wordpress/core-data");
11
+ let _wordpress_block_editor = require("@wordpress/block-editor");
12
+ let __transformers_js = require("./transformers.cjs");
13
+ let __utils_js = require("./utils.cjs");
14
+ let __constants_js = require("./constants.cjs");
15
+ // packages/edit-widgets/src/store/actions.js
16
+ /**
17
+ * Persists a stub post with given ID to core data store. The post is meant to be in-memory only and
18
+ * shouldn't be saved via the API.
19
+ *
20
+ * @param {string} id Post ID.
21
+ * @param {Array} blocks Blocks the post should consist of.
22
+ * @return {Object} The post object.
23
+ */
24
+ const persistStubPost = (id, blocks) => ({ registry }) => {
25
+ const stubPost = (0, __utils_js.createStubPost)(id, blocks);
26
+ registry.dispatch(_wordpress_core_data.store).receiveEntityRecords(__utils_js.KIND, __utils_js.POST_TYPE, stubPost, { id: stubPost.id }, false);
27
+ return stubPost;
56
28
  };
57
- var saveEditedWidgetAreas = () => async ({ select, dispatch, registry }) => {
58
- const editedWidgetAreas = select.getEditedWidgetAreas();
59
- if (!editedWidgetAreas?.length) {
60
- return;
61
- }
62
- try {
63
- await dispatch.saveWidgetAreas(editedWidgetAreas);
64
- registry.dispatch(import_notices.store).createSuccessNotice((0, import_i18n.__)("Widgets saved."), {
65
- type: "snackbar"
66
- });
67
- } catch (e) {
68
- registry.dispatch(import_notices.store).createErrorNotice(
69
- /* translators: %s: The error message. */
70
- (0, import_i18n.sprintf)((0, import_i18n.__)("There was an error. %s"), e.message),
71
- {
72
- type: "snackbar"
73
- }
74
- );
75
- }
29
+ /**
30
+ * Converts all the blocks from edited widget areas into widgets,
31
+ * and submits a batch request to save everything at once.
32
+ *
33
+ * Creates a snackbar notice on either success or error.
34
+ *
35
+ * @return {Function} An action creator.
36
+ */
37
+ const saveEditedWidgetAreas = () => async ({ select, dispatch, registry }) => {
38
+ const editedWidgetAreas = select.getEditedWidgetAreas();
39
+ if (!editedWidgetAreas?.length) return;
40
+ try {
41
+ await dispatch.saveWidgetAreas(editedWidgetAreas);
42
+ registry.dispatch(_wordpress_notices.store).createSuccessNotice((0, _wordpress_i18n.__)("Widgets saved."), { type: "snackbar" });
43
+ } catch (e) {
44
+ registry.dispatch(_wordpress_notices.store).createErrorNotice((0, _wordpress_i18n.sprintf)((0, _wordpress_i18n.__)("There was an error. %s"), e.message), { type: "snackbar" });
45
+ }
76
46
  };
77
- var saveWidgetAreas = (widgetAreas) => async ({ dispatch, registry }) => {
78
- try {
79
- for (const widgetArea of widgetAreas) {
80
- await dispatch.saveWidgetArea(widgetArea.id);
81
- }
82
- } finally {
83
- await registry.dispatch(import_core_data.store).finishResolution(
84
- "getEntityRecord",
85
- import_utils.KIND,
86
- import_utils.WIDGET_AREA_ENTITY_TYPE,
87
- (0, import_utils.buildWidgetAreasQuery)()
88
- );
89
- }
47
+ /**
48
+ * Converts all the blocks from specified widget areas into widgets,
49
+ * and submits a batch request to save everything at once.
50
+ *
51
+ * @param {Object[]} widgetAreas Widget areas to save.
52
+ * @return {Function} An action creator.
53
+ */
54
+ const saveWidgetAreas = (widgetAreas) => async ({ dispatch, registry }) => {
55
+ try {
56
+ for (const widgetArea of widgetAreas) await dispatch.saveWidgetArea(widgetArea.id);
57
+ } finally {
58
+ await registry.dispatch(_wordpress_core_data.store).finishResolution("getEntityRecord", __utils_js.KIND, __utils_js.WIDGET_AREA_ENTITY_TYPE, (0, __utils_js.buildWidgetAreasQuery)());
59
+ }
90
60
  };
91
- var saveWidgetArea = (widgetAreaId) => async ({ dispatch, select, registry }) => {
92
- const widgets = select.getWidgets();
93
- const post = registry.select(import_core_data.store).getEditedEntityRecord(
94
- import_utils.KIND,
95
- import_utils.POST_TYPE,
96
- (0, import_utils.buildWidgetAreaPostId)(widgetAreaId)
97
- );
98
- const areaWidgets = Object.values(widgets).filter(
99
- ({ sidebar }) => sidebar === widgetAreaId
100
- );
101
- const usedReferenceWidgets = [];
102
- const widgetsBlocks = post.blocks.filter((block) => {
103
- const { id } = block.attributes;
104
- if (block.name === "core/legacy-widget" && id) {
105
- if (usedReferenceWidgets.includes(id)) {
106
- return false;
107
- }
108
- usedReferenceWidgets.push(id);
109
- }
110
- return true;
111
- });
112
- const deletedWidgets = [];
113
- for (const widget of areaWidgets) {
114
- const widgetsNewArea = select.getWidgetAreaForWidgetId(widget.id);
115
- if (!widgetsNewArea) {
116
- deletedWidgets.push(widget);
117
- }
118
- }
119
- const batchMeta = [];
120
- const batchTasks = [];
121
- const sidebarWidgetsIds = [];
122
- for (let i = 0; i < widgetsBlocks.length; i++) {
123
- const block = widgetsBlocks[i];
124
- const widgetId = (0, import_widgets.getWidgetIdFromBlock)(block);
125
- const oldWidget = widgets[widgetId];
126
- const widget = (0, import_transformers.transformBlockToWidget)(block, oldWidget);
127
- sidebarWidgetsIds.push(widgetId);
128
- if (oldWidget) {
129
- registry.dispatch(import_core_data.store).editEntityRecord(
130
- "root",
131
- "widget",
132
- widgetId,
133
- {
134
- ...widget,
135
- sidebar: widgetAreaId
136
- },
137
- { undoIgnore: true }
138
- );
139
- const hasEdits = registry.select(import_core_data.store).hasEditsForEntityRecord("root", "widget", widgetId);
140
- if (!hasEdits) {
141
- continue;
142
- }
143
- batchTasks.push(
144
- ({ saveEditedEntityRecord }) => saveEditedEntityRecord("root", "widget", widgetId)
145
- );
146
- } else {
147
- batchTasks.push(
148
- ({ saveEntityRecord }) => saveEntityRecord("root", "widget", {
149
- ...widget,
150
- sidebar: widgetAreaId
151
- })
152
- );
153
- }
154
- batchMeta.push({
155
- block,
156
- position: i,
157
- clientId: block.clientId
158
- });
159
- }
160
- for (const widget of deletedWidgets) {
161
- batchTasks.push(
162
- ({ deleteEntityRecord }) => deleteEntityRecord("root", "widget", widget.id, {
163
- force: true
164
- })
165
- );
166
- }
167
- const records = await registry.dispatch(import_core_data.store).__experimentalBatch(batchTasks);
168
- const preservedRecords = records.filter(
169
- (record) => !record.hasOwnProperty("deleted")
170
- );
171
- const failedWidgetNames = [];
172
- for (let i = 0; i < preservedRecords.length; i++) {
173
- const widget = preservedRecords[i];
174
- const { block, position } = batchMeta[i];
175
- post.blocks[position].attributes.__internalWidgetId = widget.id;
176
- const error = registry.select(import_core_data.store).getLastEntitySaveError("root", "widget", widget.id);
177
- if (error) {
178
- failedWidgetNames.push(block.attributes?.name || block?.name);
179
- }
180
- if (!sidebarWidgetsIds[position]) {
181
- sidebarWidgetsIds[position] = widget.id;
182
- }
183
- }
184
- if (failedWidgetNames.length) {
185
- throw new Error(
186
- (0, import_i18n.sprintf)(
187
- /* translators: %s: List of widget names */
188
- (0, import_i18n.__)("Could not save the following widgets: %s."),
189
- failedWidgetNames.join(", ")
190
- )
191
- );
192
- }
193
- registry.dispatch(import_core_data.store).editEntityRecord(
194
- import_utils.KIND,
195
- import_utils.WIDGET_AREA_ENTITY_TYPE,
196
- widgetAreaId,
197
- {
198
- widgets: sidebarWidgetsIds
199
- },
200
- { undoIgnore: true }
201
- );
202
- dispatch(trySaveWidgetArea(widgetAreaId));
203
- registry.dispatch(import_core_data.store).receiveEntityRecords(import_utils.KIND, import_utils.POST_TYPE, post, void 0);
61
+ /**
62
+ * Converts all the blocks from a widget area specified by ID into widgets,
63
+ * and submits a batch request to save everything at once.
64
+ *
65
+ * @param {string} widgetAreaId ID of the widget area to process.
66
+ * @return {Function} An action creator.
67
+ */
68
+ const saveWidgetArea = (widgetAreaId) => async ({ dispatch, select, registry }) => {
69
+ const widgets = select.getWidgets();
70
+ const post = registry.select(_wordpress_core_data.store).getEditedEntityRecord(__utils_js.KIND, __utils_js.POST_TYPE, (0, __utils_js.buildWidgetAreaPostId)(widgetAreaId));
71
+ const areaWidgets = Object.values(widgets).filter(({ sidebar }) => sidebar === widgetAreaId);
72
+ const usedReferenceWidgets = [];
73
+ const widgetsBlocks = post.blocks.filter((block) => {
74
+ const { id } = block.attributes;
75
+ if (block.name === "core/legacy-widget" && id) {
76
+ if (usedReferenceWidgets.includes(id)) return false;
77
+ usedReferenceWidgets.push(id);
78
+ }
79
+ return true;
80
+ });
81
+ const deletedWidgets = [];
82
+ for (const widget of areaWidgets) if (!select.getWidgetAreaForWidgetId(widget.id)) deletedWidgets.push(widget);
83
+ const batchMeta = [];
84
+ const batchTasks = [];
85
+ const sidebarWidgetsIds = [];
86
+ for (let i = 0; i < widgetsBlocks.length; i++) {
87
+ const block = widgetsBlocks[i];
88
+ const widgetId = (0, _wordpress_widgets.getWidgetIdFromBlock)(block);
89
+ const oldWidget = widgets[widgetId];
90
+ const widget = (0, __transformers_js.transformBlockToWidget)(block, oldWidget);
91
+ sidebarWidgetsIds.push(widgetId);
92
+ if (oldWidget) {
93
+ registry.dispatch(_wordpress_core_data.store).editEntityRecord("root", "widget", widgetId, {
94
+ ...widget,
95
+ sidebar: widgetAreaId
96
+ }, { undoIgnore: true });
97
+ if (!registry.select(_wordpress_core_data.store).hasEditsForEntityRecord("root", "widget", widgetId)) continue;
98
+ batchTasks.push(({ saveEditedEntityRecord }) => saveEditedEntityRecord("root", "widget", widgetId));
99
+ } else batchTasks.push(({ saveEntityRecord }) => saveEntityRecord("root", "widget", {
100
+ ...widget,
101
+ sidebar: widgetAreaId
102
+ }));
103
+ batchMeta.push({
104
+ block,
105
+ position: i,
106
+ clientId: block.clientId
107
+ });
108
+ }
109
+ for (const widget of deletedWidgets) batchTasks.push(({ deleteEntityRecord }) => deleteEntityRecord("root", "widget", widget.id, { force: true }));
110
+ const preservedRecords = (await registry.dispatch(_wordpress_core_data.store).__experimentalBatch(batchTasks)).filter((record) => !record.hasOwnProperty("deleted"));
111
+ const failedWidgetNames = [];
112
+ for (let i = 0; i < preservedRecords.length; i++) {
113
+ const widget = preservedRecords[i];
114
+ const { block, position } = batchMeta[i];
115
+ post.blocks[position].attributes.__internalWidgetId = widget.id;
116
+ if (registry.select(_wordpress_core_data.store).getLastEntitySaveError("root", "widget", widget.id)) failedWidgetNames.push(block.attributes?.name || block?.name);
117
+ if (!sidebarWidgetsIds[position]) sidebarWidgetsIds[position] = widget.id;
118
+ }
119
+ if (failedWidgetNames.length) throw new Error((0, _wordpress_i18n.sprintf)((0, _wordpress_i18n.__)("Could not save the following widgets: %s."), failedWidgetNames.join(", ")));
120
+ registry.dispatch(_wordpress_core_data.store).editEntityRecord(__utils_js.KIND, __utils_js.WIDGET_AREA_ENTITY_TYPE, widgetAreaId, { widgets: sidebarWidgetsIds }, { undoIgnore: true });
121
+ dispatch(trySaveWidgetArea(widgetAreaId));
122
+ registry.dispatch(_wordpress_core_data.store).receiveEntityRecords(__utils_js.KIND, __utils_js.POST_TYPE, post, void 0);
204
123
  };
205
- var trySaveWidgetArea = (widgetAreaId) => ({ registry }) => {
206
- registry.dispatch(import_core_data.store).saveEditedEntityRecord(
207
- import_utils.KIND,
208
- import_utils.WIDGET_AREA_ENTITY_TYPE,
209
- widgetAreaId,
210
- {
211
- throwOnError: true
212
- }
213
- );
124
+ const trySaveWidgetArea = (widgetAreaId) => ({ registry }) => {
125
+ registry.dispatch(_wordpress_core_data.store).saveEditedEntityRecord(__utils_js.KIND, __utils_js.WIDGET_AREA_ENTITY_TYPE, widgetAreaId, { throwOnError: true });
214
126
  };
127
+ /**
128
+ * Sets the clientId stored for a particular widgetId.
129
+ *
130
+ * @param {number} clientId Client id.
131
+ * @param {number} widgetId Widget id.
132
+ *
133
+ * @return {Object} Action.
134
+ */
215
135
  function setWidgetIdForClientId(clientId, widgetId) {
216
- return {
217
- type: "SET_WIDGET_ID_FOR_CLIENT_ID",
218
- clientId,
219
- widgetId
220
- };
136
+ return {
137
+ type: "SET_WIDGET_ID_FOR_CLIENT_ID",
138
+ clientId,
139
+ widgetId
140
+ };
221
141
  }
142
+ /**
143
+ * Sets the open state of all the widget areas.
144
+ *
145
+ * @param {Object} widgetAreasOpenState The open states of all the widget areas.
146
+ *
147
+ * @return {Object} Action.
148
+ */
222
149
  function setWidgetAreasOpenState(widgetAreasOpenState) {
223
- return {
224
- type: "SET_WIDGET_AREAS_OPEN_STATE",
225
- widgetAreasOpenState
226
- };
150
+ return {
151
+ type: "SET_WIDGET_AREAS_OPEN_STATE",
152
+ widgetAreasOpenState
153
+ };
227
154
  }
155
+ /**
156
+ * Sets the open state of the widget area.
157
+ *
158
+ * @param {string} clientId The clientId of the widget area.
159
+ * @param {boolean} isOpen Whether the widget area should be opened.
160
+ *
161
+ * @return {Object} Action.
162
+ */
228
163
  function setIsWidgetAreaOpen(clientId, isOpen) {
229
- return {
230
- type: "SET_IS_WIDGET_AREA_OPEN",
231
- clientId,
232
- isOpen
233
- };
164
+ return {
165
+ type: "SET_IS_WIDGET_AREA_OPEN",
166
+ clientId,
167
+ isOpen
168
+ };
234
169
  }
170
+ /**
171
+ * Returns an action object used to open/close the inserter.
172
+ *
173
+ * @param {boolean|Object} value Whether the inserter should be
174
+ * opened (true) or closed (false).
175
+ * To specify an insertion point,
176
+ * use an object.
177
+ * @param {string} value.rootClientId The root client ID to insert at.
178
+ * @param {number} value.insertionIndex The index to insert at.
179
+ *
180
+ * @return {Object} Action object.
181
+ */
235
182
  function setIsInserterOpened(value) {
236
- return {
237
- type: "SET_IS_INSERTER_OPENED",
238
- value
239
- };
183
+ return {
184
+ type: "SET_IS_INSERTER_OPENED",
185
+ value
186
+ };
240
187
  }
188
+ /**
189
+ * Returns an action object used to open/close the list view.
190
+ *
191
+ * @param {boolean} isOpen A boolean representing whether the list view should be opened or closed.
192
+ * @return {Object} Action object.
193
+ */
241
194
  function setIsListViewOpened(isOpen) {
242
- return {
243
- type: "SET_IS_LIST_VIEW_OPENED",
244
- isOpen
245
- };
195
+ return {
196
+ type: "SET_IS_LIST_VIEW_OPENED",
197
+ isOpen
198
+ };
246
199
  }
247
- var closeGeneralSidebar = () => ({ registry }) => {
248
- registry.dispatch(import_interface.store).disableComplementaryArea(import_constants.STORE_NAME);
200
+ /**
201
+ * Returns an action object signalling that the user closed the sidebar.
202
+ *
203
+ * @return {Object} Action creator.
204
+ */
205
+ const closeGeneralSidebar = () => ({ registry }) => {
206
+ registry.dispatch(_wordpress_interface.store).disableComplementaryArea(__constants_js.STORE_NAME);
249
207
  };
250
- var moveBlockToWidgetArea = (clientId, widgetAreaId) => async ({ dispatch, select, registry }) => {
251
- const sourceRootClientId = registry.select(import_block_editor.store).getBlockRootClientId(clientId);
252
- const widgetAreas = registry.select(import_block_editor.store).getBlocks();
253
- const destinationWidgetAreaBlock = widgetAreas.find(
254
- ({ attributes }) => attributes.id === widgetAreaId
255
- );
256
- const destinationRootClientId = destinationWidgetAreaBlock.clientId;
257
- const destinationInnerBlocksClientIds = registry.select(import_block_editor.store).getBlockOrder(destinationRootClientId);
258
- const destinationIndex = destinationInnerBlocksClientIds.length;
259
- const isDestinationWidgetAreaOpen = select.getIsWidgetAreaOpen(
260
- destinationRootClientId
261
- );
262
- if (!isDestinationWidgetAreaOpen) {
263
- dispatch.setIsWidgetAreaOpen(destinationRootClientId, true);
264
- }
265
- registry.dispatch(import_block_editor.store).moveBlocksToPosition(
266
- [clientId],
267
- sourceRootClientId,
268
- destinationRootClientId,
269
- destinationIndex
270
- );
208
+ /**
209
+ * Action that handles moving a block between widget areas
210
+ *
211
+ * @param {string} clientId The clientId of the block to move.
212
+ * @param {string} widgetAreaId The id of the widget area to move the block to.
213
+ */
214
+ const moveBlockToWidgetArea = (clientId, widgetAreaId) => async ({ dispatch, select, registry }) => {
215
+ const sourceRootClientId = registry.select(_wordpress_block_editor.store).getBlockRootClientId(clientId);
216
+ const destinationRootClientId = registry.select(_wordpress_block_editor.store).getBlocks().find(({ attributes }) => attributes.id === widgetAreaId).clientId;
217
+ const destinationIndex = registry.select(_wordpress_block_editor.store).getBlockOrder(destinationRootClientId).length;
218
+ if (!select.getIsWidgetAreaOpen(destinationRootClientId)) dispatch.setIsWidgetAreaOpen(destinationRootClientId, true);
219
+ registry.dispatch(_wordpress_block_editor.store).moveBlocksToPosition([clientId], sourceRootClientId, destinationRootClientId, destinationIndex);
271
220
  };
221
+ /**
222
+ * Returns an action object used to signal that widget saving is unlocked.
223
+ *
224
+ * @param {string} lockName The lock name.
225
+ *
226
+ * @example
227
+ * ```js
228
+ * import { store as widgetStore } from '@wordpress/edit-widgets';
229
+ * import { useDispatch } from '@wordpress/data';
230
+ *
231
+ * const ExampleComponent = () => {
232
+ * const { unlockWidgetSaving } = useDispatch( widgetStore );
233
+ * return (
234
+ * <Button onClick={ () => unlockWidgetSaving( 'lockName' ) }>
235
+ * { __( 'Unlock Widget Saving' ) }
236
+ * </Button>
237
+ * );
238
+ * };
239
+ * ```
240
+ *
241
+ * @return {Object} Action object
242
+ */
272
243
  function unlockWidgetSaving(lockName) {
273
- return {
274
- type: "UNLOCK_WIDGET_SAVING",
275
- lockName
276
- };
244
+ return {
245
+ type: "UNLOCK_WIDGET_SAVING",
246
+ lockName
247
+ };
277
248
  }
249
+ /**
250
+ * Returns an action object used to signal that widget saving is locked.
251
+ *
252
+ * @param {string} lockName The lock name.
253
+ *
254
+ * @example
255
+ * ```js
256
+ * import { store as widgetStore } from '@wordpress/edit-widgets';
257
+ * import { useDispatch } from '@wordpress/data';
258
+ *
259
+ * const ExampleComponent = () => {
260
+ * const { lockWidgetSaving } = useDispatch( widgetStore );
261
+ * return (
262
+ * <Button onClick={ () => lockWidgetSaving( 'lockName' ) }>
263
+ * { __( 'Lock Widget Saving' ) }
264
+ * </Button>
265
+ * );
266
+ * };
267
+ * ```
268
+ *
269
+ * @return {Object} Action object
270
+ */
278
271
  function lockWidgetSaving(lockName) {
279
- return {
280
- type: "LOCK_WIDGET_SAVING",
281
- lockName
282
- };
272
+ return {
273
+ type: "LOCK_WIDGET_SAVING",
274
+ lockName
275
+ };
283
276
  }
284
- // Annotate the CommonJS export names for ESM import in node:
285
- 0 && (module.exports = {
286
- closeGeneralSidebar,
287
- lockWidgetSaving,
288
- moveBlockToWidgetArea,
289
- persistStubPost,
290
- saveEditedWidgetAreas,
291
- saveWidgetArea,
292
- saveWidgetAreas,
293
- setIsInserterOpened,
294
- setIsListViewOpened,
295
- setIsWidgetAreaOpen,
296
- setWidgetAreasOpenState,
297
- setWidgetIdForClientId,
298
- unlockWidgetSaving
299
- });
300
- //# sourceMappingURL=actions.cjs.map
277
+ exports.closeGeneralSidebar = closeGeneralSidebar;
278
+ exports.lockWidgetSaving = lockWidgetSaving;
279
+ exports.moveBlockToWidgetArea = moveBlockToWidgetArea;
280
+ exports.persistStubPost = persistStubPost;
281
+ exports.saveEditedWidgetAreas = saveEditedWidgetAreas;
282
+ exports.saveWidgetArea = saveWidgetArea;
283
+ exports.saveWidgetAreas = saveWidgetAreas;
284
+ exports.setIsInserterOpened = setIsInserterOpened;
285
+ exports.setIsListViewOpened = setIsListViewOpened;
286
+ exports.setIsWidgetAreaOpen = setIsWidgetAreaOpen;
287
+ exports.setWidgetAreasOpenState = setWidgetAreasOpenState;
288
+ exports.setWidgetIdForClientId = setWidgetIdForClientId;
289
+ exports.unlockWidgetSaving = unlockWidgetSaving;
290
+
291
+ //# sourceMappingURL=actions.cjs.map