| 1 |
var __create = Object.create; |
| 2 |
var __defProp = Object.defineProperty; |
| 3 |
var __getOwnPropDesc = Object.getOwnPropertyDescriptor; |
| 4 |
var __getOwnPropNames = Object.getOwnPropertyNames; |
| 5 |
var __getProtoOf = Object.getPrototypeOf; |
| 6 |
var __hasOwnProp = Object.prototype.hasOwnProperty; |
| 7 |
var __commonJS = (cb, mod) => function __require() { |
| 8 |
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports; |
| 9 |
}; |
| 10 |
var __copyProps = (to, from, except, desc) => { |
| 11 |
if (from && typeof from === "object" || typeof from === "function") { |
| 12 |
for (let key of __getOwnPropNames(from)) |
| 13 |
if (!__hasOwnProp.call(to, key) && key !== except) |
| 14 |
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); |
| 15 |
} |
| 16 |
return to; |
| 17 |
}; |
| 18 |
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( |
| 19 |
// If the importer is in node compatibility mode or this is not an ESM |
| 20 |
// file that has been converted to a CommonJS file using a Babel- |
| 21 |
// compatible transform (i.e. "__esModule" has not been set), then set |
| 22 |
// "default" to the CommonJS "module.exports" for node compatibility. |
| 23 |
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, |
| 24 |
mod |
| 25 |
)); |
| 26 |
|
| 27 |
// package-external:@wordpress/element |
| 28 |
var require_element = __commonJS({ |
| 29 |
"package-external:@wordpress/element"(exports, module) { |
| 30 |
module.exports = window.wp.element; |
| 31 |
} |
| 32 |
}); |
| 33 |
|
| 34 |
// vendor-external:react/jsx-runtime |
| 35 |
var require_jsx_runtime = __commonJS({ |
| 36 |
"vendor-external:react/jsx-runtime"(exports, module) { |
| 37 |
module.exports = window.ReactJSXRuntime; |
| 38 |
} |
| 39 |
}); |
| 40 |
|
| 41 |
// packages/widget-primitives/build-module/tools/get-lazy-widget-component/get-lazy-widget-component.mjs |
| 42 |
var import_element = __toESM(require_element(), 1); |
| 43 |
function isValidWidgetModule(module) { |
| 44 |
return typeof module === "object" && module !== null && "default" in module && typeof module.default === "function"; |
| 45 |
} |
| 46 |
var componentCache = /* @__PURE__ */ new Map(); |
| 47 |
function getLazyWidgetComponent(renderModule, resolveWidgetModule) { |
| 48 |
const cached = componentCache.get(renderModule); |
| 49 |
if (cached) { |
| 50 |
return cached; |
| 51 |
} |
| 52 |
const lazyComponent = (0, import_element.lazy)(async () => { |
| 53 |
const module = await resolveWidgetModule(renderModule); |
| 54 |
if (!isValidWidgetModule(module)) { |
| 55 |
throw new Error(`Invalid widget module: ${renderModule}`); |
| 56 |
} |
| 57 |
return module; |
| 58 |
}); |
| 59 |
componentCache.set(renderModule, lazyComponent); |
| 60 |
return lazyComponent; |
| 61 |
} |
| 62 |
|
| 63 |
// packages/widget-primitives/build-module/components/widget-render/widget-render.mjs |
| 64 |
var import_jsx_runtime = __toESM(require_jsx_runtime(), 1); |
| 65 |
function WidgetRender({ |
| 66 |
widgetType, |
| 67 |
attributes, |
| 68 |
setAttributes, |
| 69 |
resolveWidgetModule |
| 70 |
}) { |
| 71 |
const WidgetComponent = getLazyWidgetComponent( |
| 72 |
widgetType.renderModule, |
| 73 |
resolveWidgetModule |
| 74 |
); |
| 75 |
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_jsx_runtime.Fragment, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)( |
| 76 |
WidgetComponent, |
| 77 |
{ |
| 78 |
attributes, |
| 79 |
setAttributes |
| 80 |
} |
| 81 |
) }); |
| 82 |
} |
| 83 |
|
| 84 |
// packages/widget-primitives/build-module/hooks/use-widget-types.mjs |
| 85 |
var import_element2 = __toESM(require_element(), 1); |
| 86 |
|
| 87 |
// packages/widget-primitives/build-module/field-types/field-types.mjs |
| 88 |
var FIELD_TYPE_NAME_PATTERN = /^[a-z][a-z0-9-]*(\/[a-z][a-z0-9-]*)?$/; |
| 89 |
var fieldTypes = /* @__PURE__ */ new Map(); |
| 90 |
function registerFieldType(fieldType) { |
| 91 |
if (!FIELD_TYPE_NAME_PATTERN.test(fieldType.name) || fieldTypes.has(fieldType.name)) { |
| 92 |
return void 0; |
| 93 |
} |
| 94 |
fieldTypes.set(fieldType.name, fieldType); |
| 95 |
return fieldType; |
| 96 |
} |
| 97 |
function resolveFields(fields) { |
| 98 |
return fields.map((field) => { |
| 99 |
const fieldType = field.type ? fieldTypes.get(field.type) : void 0; |
| 100 |
if (!fieldType) { |
| 101 |
return field; |
| 102 |
} |
| 103 |
const { name, baseType, isValid, ...fieldDefaults } = fieldType; |
| 104 |
const { type, isValid: fieldIsValid, ...rest } = field; |
| 105 |
return { |
| 106 |
...fieldDefaults, |
| 107 |
...rest, |
| 108 |
type: baseType, |
| 109 |
...isValid || fieldIsValid ? { isValid: { ...isValid, ...fieldIsValid } } : {} |
| 110 |
}; |
| 111 |
}); |
| 112 |
} |
| 113 |
|
| 114 |
// packages/widget-primitives/build-module/icon-resolver/icon-resolver.mjs |
| 115 |
var iconResolver; |
| 116 |
function registerIconResolver(resolver) { |
| 117 |
if (iconResolver) { |
| 118 |
return void 0; |
| 119 |
} |
| 120 |
iconResolver = resolver; |
| 121 |
return resolver; |
| 122 |
} |
| 123 |
async function resolveIcon(reference) { |
| 124 |
if (!iconResolver) { |
| 125 |
return null; |
| 126 |
} |
| 127 |
try { |
| 128 |
return await iconResolver(reference); |
| 129 |
} catch { |
| 130 |
return null; |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
// packages/widget-primitives/build-module/hooks/use-widget-types.mjs |
| 135 |
var pendingIcon = (0, import_element2.createElement)("svg", { |
| 136 |
viewBox: "0 0 24 24" |
| 137 |
}); |
| 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 |
}); |
| 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 |
} |
| 193 |
function useWidgetTypes(records) { |
| 194 |
const [widgetTypes, setWidgetTypes] = (0, import_element2.useState)([]); |
| 195 |
const [isResolvingWidgetTypes, setIsResolvingWidgetTypes] = (0, import_element2.useState)(true); |
| 196 |
(0, import_element2.useEffect)(() => { |
| 197 |
if (records === null || records === void 0) { |
| 198 |
setIsResolvingWidgetTypes(true); |
| 199 |
return; |
| 200 |
} |
| 201 |
if (records.length === 0) { |
| 202 |
setWidgetTypes([]); |
| 203 |
setIsResolvingWidgetTypes(false); |
| 204 |
return; |
| 205 |
} |
| 206 |
let cancelled = false; |
| 207 |
setIsResolvingWidgetTypes(true); |
| 208 |
Promise.all( |
| 209 |
records.map(async (record) => { |
| 210 |
if (!record.widget_module) { |
| 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 |
}; |
| 231 |
} |
| 232 |
try { |
| 233 |
const module = await import( |
| 234 |
/* webpackIgnore: true */ |
| 235 |
record.widget_module |
| 236 |
); |
| 237 |
if (!module?.default) { |
| 238 |
return null; |
| 239 |
} |
| 240 |
const metadata = module.default; |
| 241 |
const moduleIcon = (0, import_element2.isValidElement)(metadata.icon) ? metadata.icon : void 0; |
| 242 |
const icon = moduleIcon ?? (record.icon ? pendingIcon : void 0); |
| 243 |
const actions = record.actions ?? metadata.actions; |
| 244 |
const attributes = mergeAttributes( |
| 245 |
record.attributes, |
| 246 |
metadata.attributes |
| 247 |
); |
| 248 |
return { |
| 249 |
apiVersion: DEFAULT_API_VERSION, |
| 250 |
...metadata, |
| 251 |
...attributes ? { attributes: resolveFields(attributes) } : {}, |
| 252 |
icon, |
| 253 |
/* |
| 254 |
* `title` is required: |
| 255 |
* - Server-side title wins |
| 256 |
* - Then the module's title |
| 257 |
* - Then the record's name as fallback |
| 258 |
*/ |
| 259 |
title: record.title ?? metadata.title ?? record.name, |
| 260 |
...actions ? { |
| 261 |
actions: withRenderableIcons( |
| 262 |
actions, |
| 263 |
actions === record.actions |
| 264 |
) |
| 265 |
} : {}, |
| 266 |
...recordOverlay(record) |
| 267 |
}; |
| 268 |
} catch { |
| 269 |
return null; |
| 270 |
} |
| 271 |
}) |
| 272 |
).then((results) => { |
| 273 |
if (cancelled) { |
| 274 |
return; |
| 275 |
} |
| 276 |
setWidgetTypes( |
| 277 |
results.filter((t) => t !== null) |
| 278 |
); |
| 279 |
setIsResolvingWidgetTypes(false); |
| 280 |
for (const record of records) { |
| 281 |
if (!record.icon) { |
| 282 |
continue; |
| 283 |
} |
| 284 |
void resolveIcon(record.icon).then((resolved) => { |
| 285 |
if (cancelled) { |
| 286 |
return; |
| 287 |
} |
| 288 |
setWidgetTypes( |
| 289 |
(prev) => prev.map((widgetType) => { |
| 290 |
if (widgetType.name !== record.name) { |
| 291 |
return widgetType; |
| 292 |
} |
| 293 |
if (resolved) { |
| 294 |
return { ...widgetType, icon: resolved }; |
| 295 |
} |
| 296 |
return widgetType.icon === pendingIcon ? { ...widgetType, icon: void 0 } : widgetType; |
| 297 |
}) |
| 298 |
); |
| 299 |
}); |
| 300 |
} |
| 301 |
for (const record of records) { |
| 302 |
for (const action of record.actions ?? []) { |
| 303 |
if (typeof action.icon !== "string") { |
| 304 |
continue; |
| 305 |
} |
| 306 |
void resolveIcon(action.icon).then((resolved) => { |
| 307 |
if (cancelled) { |
| 308 |
return; |
| 309 |
} |
| 310 |
setWidgetTypes( |
| 311 |
(prev) => prev.map((type) => { |
| 312 |
if (type.name !== record.name) { |
| 313 |
return type; |
| 314 |
} |
| 315 |
return { |
| 316 |
...type, |
| 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 |
}) |
| 329 |
}; |
| 330 |
}) |
| 331 |
); |
| 332 |
}); |
| 333 |
} |
| 334 |
} |
| 335 |
}); |
| 336 |
return () => { |
| 337 |
cancelled = true; |
| 338 |
}; |
| 339 |
}, [records]); |
| 340 |
return [widgetTypes, isResolvingWidgetTypes]; |
| 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 |
); |
| 377 |
export { |
| 378 |
HostLink, |
| 379 |
WidgetHostProvider, |
| 380 |
WidgetRender, |
| 381 |
registerFieldType, |
| 382 |
registerIconResolver, |
| 383 |
useWidgetHost, |
| 384 |
useWidgetTypes |
| 385 |
}; |
| 386 |
//# sourceMappingURL=index.js.map |
| 387 |
|