PluginProbe
Gutenberg / trunk
Gutenberg vtrunk
24.1.0 24.0.0 23.9.1 23.9.0 23.8.0 23.7.2 23.7.1 23.7.0 23.6.1 23.6.2 23.6.0 23.5.3 23.5.2 23.5.1 23.5.0 23.4.0 23.3.2 23.3.1 23.3.0 23.2.0 23.2.1 23.2.2 23.1.1 23.1.0 23.0.1 All 404 releases
← All changes | build/modules/widget-primitives/index.js +138 -23 23.8.0 → trunk View file →
@@ -134,14 +134,63 @@
134 134 // packages/widget-primitives/build-module/hooks/use-widget-types.mjs
135 135 var pendingIcon = (0, import_element2.createElement)("svg", {
136 136 viewBox: "0 0 24 24"
137 137 });
138 -function withRenderableIcons(actions) {
139 - return actions.map(({ icon, ...action }) => ({
140 - ...action,
141 - ...(0, import_element2.isValidElement)(icon) ? { icon } : {}
142 - }));
138 +function withRenderableIcons(actions, holdPending) {
139 + return actions.map(({ icon, ...action }) => {
140 + if ((0, import_element2.isValidElement)(icon)) {
141 + return { ...action, icon };
142 + }
143 + if (holdPending && typeof icon === "string") {
144 + return { ...action, icon: pendingIcon };
145 + }
146 + return action;
147 + });
143 148 }
149 +function mergeAttributes(recordAttributes, moduleAttributes) {
150 + if (!recordAttributes) {
151 + return moduleAttributes;
152 + }
153 + if (!moduleAttributes) {
154 + return recordAttributes;
155 + }
156 + const moduleById = new Map(
157 + moduleAttributes.map(
158 + (attribute) => [attribute.id, attribute]
159 + )
160 + );
161 + const recordIds = new Set(
162 + recordAttributes.map((attribute) => attribute.id)
163 + );
164 + return [
165 + ...recordAttributes.map((attribute) => {
166 + const moduleAttribute = moduleById.get(attribute.id);
167 + if (!moduleAttribute?.isValid || !attribute.isValid) {
168 + return { ...moduleAttribute, ...attribute };
169 + }
170 + return {
171 + ...moduleAttribute,
172 + ...attribute,
173 + isValid: { ...moduleAttribute.isValid, ...attribute.isValid }
174 + };
175 + }),
176 + ...moduleAttributes.filter(
177 + (attribute) => !recordIds.has(attribute.id)
178 + )
179 + ];
180 +}
181 +var DEFAULT_API_VERSION = 1;
182 +function recordOverlay(record) {
183 + return {
184 + name: record.name,
185 + renderModule: record.render_module ?? "",
186 + ...record.presentation ? { presentation: record.presentation } : {},
187 + ...record.category ? { category: record.category } : {},
188 + ...record.description ? { description: record.description } : {},
189 + ...record.help ? { help: record.help } : {},
190 + ...record.keywords ? { keywords: record.keywords } : {}
191 + };
192 +}
144 193 function useWidgetTypes(records) {
145 194 const [widgetTypes, setWidgetTypes] = (0, import_element2.useState)([]);
146 195 const [isResolvingWidgetTypes, setIsResolvingWidgetTypes] = (0, import_element2.useState)(true);
147 196 (0, import_element2.useEffect)(() => {
@@ -158,9 +207,28 @@
158 207 setIsResolvingWidgetTypes(true);
159 208 Promise.all(
160 209 records.map(async (record) => {
161 210 if (!record.widget_module) {
162 - return null;
211 + if (!record.render_module) {
212 + return null;
213 + }
214 + return {
215 + apiVersion: DEFAULT_API_VERSION,
216 + title: record.title ?? record.name,
217 + ...record.attributes ? {
218 + attributes: resolveFields(
219 + record.attributes
220 + )
221 + } : {},
222 + ...record.icon ? { icon: pendingIcon } : {},
223 + ...record.actions ? {
224 + actions: withRenderableIcons(
225 + record.actions,
226 + true
227 + )
228 + } : {},
229 + ...recordOverlay(record)
230 + };
163 231 }
164 232 try {
165 233 const module = await import(
166 234 /* webpackIgnore: true */
@@ -172,17 +240,16 @@
172 240 const metadata = module.default;
173 241 const moduleIcon = (0, import_element2.isValidElement)(metadata.icon) ? metadata.icon : void 0;
174 242 const icon = moduleIcon ?? (record.icon ? pendingIcon : void 0);
175 243 const actions = record.actions ?? metadata.actions;
244 + const attributes = mergeAttributes(
245 + record.attributes,
246 + metadata.attributes
247 + );
176 248 return {
249 + apiVersion: DEFAULT_API_VERSION,
177 250 ...metadata,
178 - ...metadata.attributes ? {
179 - attributes: resolveFields(
180 - metadata.attributes
181 - )
182 - } : {},
183 - name: record.name,
184 - renderModule: record.render_module ?? "",
251 + ...attributes ? { attributes: resolveFields(attributes) } : {},
185 252 icon,
186 253 /*
187 254 * `title` is required:
188 255 * - Server-side title wins
@@ -189,14 +256,15 @@
189 256 * - Then the module's title
190 257 * - Then the record's name as fallback
191 258 */
192 259 title: record.title ?? metadata.title ?? record.name,
193 - ...record.presentation ? { presentation: record.presentation } : {},
194 - ...record.category ? { category: record.category } : {},
195 - ...record.description ? { description: record.description } : {},
196 - ...record.help ? { help: record.help } : {},
197 - ...record.keywords ? { keywords: record.keywords } : {},
198 - ...actions ? { actions: withRenderableIcons(actions) } : {}
260 + ...actions ? {
261 + actions: withRenderableIcons(
262 + actions,
263 + actions === record.actions
264 + )
265 + } : {},
266 + ...recordOverlay(record)
199 267 };
200 268 } catch {
201 269 return null;
202 270 }
@@ -235,9 +303,9 @@
235 303 if (typeof action.icon !== "string") {
236 304 continue;
237 305 }
238 306 void resolveIcon(action.icon).then((resolved) => {
239 - if (cancelled || !resolved) {
307 + if (cancelled) {
240 308 return;
241 309 }
242 310 setWidgetTypes(
243 311 (prev) => prev.map((type) => {
@@ -245,11 +313,20 @@
245 313 return type;
246 314 }
247 315 return {
248 316 ...type,
249 - actions: type.actions?.map(
250 - (entry) => entry.id === action.id ? { ...entry, icon: resolved } : entry
251 - )
317 + actions: type.actions?.map((entry) => {
318 + if (entry.id !== action.id) {
319 + return entry;
320 + }
321 + if (resolved) {
322 + return {
323 + ...entry,
324 + icon: resolved
325 + };
326 + }
327 + return entry.icon === pendingIcon ? { ...entry, icon: void 0 } : entry;
328 + })
252 329 };
253 330 })
254 331 );
255 332 });
@@ -261,11 +338,49 @@
261 338 };
262 339 }, [records]);
263 340 return [widgetTypes, isResolvingWidgetTypes];
264 341 }
342 +
343 +// packages/widget-primitives/build-module/widget-host/widget-host.mjs
344 +var import_element3 = __toESM(require_element(), 1);
345 +var import_jsx_runtime2 = __toESM(require_jsx_runtime(), 1);
346 +var WidgetHostContext = (0, import_element3.createContext)({});
347 +function WidgetHostProvider({
348 + value,
349 + children
350 +}) {
351 + const inherited = (0, import_element3.useContext)(WidgetHostContext);
352 + const merged = (0, import_element3.useMemo)(
353 + () => ({ ...inherited, ...value }),
354 + [inherited, value]
355 + );
356 + return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(WidgetHostContext.Provider, { value: merged, children });
357 +}
358 +function useWidgetHost() {
359 + return (0, import_element3.useContext)(WidgetHostContext);
360 +}
361 +
362 +// packages/widget-primitives/build-module/widget-host/host-link.mjs
363 +var import_element4 = __toESM(require_element(), 1);
364 +var import_jsx_runtime3 = __toESM(require_jsx_runtime(), 1);
365 +var HostLink = (0, import_element4.forwardRef)(
366 + function HostLink2({ href, children, ...props }, ref) {
367 + const { links } = useWidgetHost();
368 + const { download, target } = props;
369 + const opensNewDocument = download !== void 0 && download !== false || /^_blank$/i.test(target ?? "");
370 + const path = links && !opensNewDocument ? links.match(href) : null;
371 + if (links && path !== null) {
372 + return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(links.Link, { ref, path, ...props, children });
373 + }
374 + return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("a", { ref, href, ...props, children });
375 + }
376 +);
265 377 export {
378 + HostLink,
379 + WidgetHostProvider,
266 380 WidgetRender,
267 381 registerFieldType,
268 382 registerIconResolver,
383 + useWidgetHost,
269 384 useWidgetTypes
270 385 };
271 386 //# sourceMappingURL=index.js.map