@wordpress/widget-primitives 0.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.
- package/CHANGELOG.md +16 -0
- package/LICENSE.md +788 -0
- package/README.md +76 -0
- package/build/components/widget-render/index.cjs +31 -0
- package/build/components/widget-render/index.cjs.map +7 -0
- package/build/components/widget-render/widget-render.cjs +50 -0
- package/build/components/widget-render/widget-render.cjs.map +7 -0
- package/build/hooks/index.cjs +31 -0
- package/build/hooks/index.cjs.map +7 -0
- package/build/hooks/use-widget-types.cjs +84 -0
- package/build/hooks/use-widget-types.cjs.map +7 -0
- package/build/index.cjs +34 -0
- package/build/index.cjs.map +7 -0
- package/build/tools/get-lazy-widget-component/get-lazy-widget-component.cjs +50 -0
- package/build/tools/get-lazy-widget-component/get-lazy-widget-component.cjs.map +7 -0
- package/build/tools/get-lazy-widget-component/index.cjs +31 -0
- package/build/tools/get-lazy-widget-component/index.cjs.map +7 -0
- package/build/types.cjs +19 -0
- package/build/types.cjs.map +7 -0
- package/build-module/components/widget-render/index.mjs +6 -0
- package/build-module/components/widget-render/index.mjs.map +7 -0
- package/build-module/components/widget-render/widget-render.mjs +25 -0
- package/build-module/components/widget-render/widget-render.mjs.map +7 -0
- package/build-module/hooks/index.mjs +6 -0
- package/build-module/hooks/index.mjs.map +7 -0
- package/build-module/hooks/use-widget-types.mjs +59 -0
- package/build-module/hooks/use-widget-types.mjs.map +7 -0
- package/build-module/index.mjs +8 -0
- package/build-module/index.mjs.map +7 -0
- package/build-module/tools/get-lazy-widget-component/get-lazy-widget-component.mjs +25 -0
- package/build-module/tools/get-lazy-widget-component/get-lazy-widget-component.mjs.map +7 -0
- package/build-module/tools/get-lazy-widget-component/index.mjs +6 -0
- package/build-module/tools/get-lazy-widget-component/index.mjs.map +7 -0
- package/build-module/types.mjs +1 -0
- package/build-module/types.mjs.map +7 -0
- package/build-types/components/widget-render/index.d.ts +2 -0
- package/build-types/components/widget-render/index.d.ts.map +1 -0
- package/build-types/components/widget-render/stories/index.story.d.ts +19 -0
- package/build-types/components/widget-render/stories/index.story.d.ts.map +1 -0
- package/build-types/components/widget-render/widget-render.d.ts +13 -0
- package/build-types/components/widget-render/widget-render.d.ts.map +1 -0
- package/build-types/hooks/index.d.ts +2 -0
- package/build-types/hooks/index.d.ts.map +1 -0
- package/build-types/hooks/use-widget-types.d.ts +17 -0
- package/build-types/hooks/use-widget-types.d.ts.map +1 -0
- package/build-types/index.d.ts +13 -0
- package/build-types/index.d.ts.map +1 -0
- package/build-types/tools/get-lazy-widget-component/get-lazy-widget-component.d.ts +12 -0
- package/build-types/tools/get-lazy-widget-component/get-lazy-widget-component.d.ts.map +1 -0
- package/build-types/tools/get-lazy-widget-component/index.d.ts +2 -0
- package/build-types/tools/get-lazy-widget-component/index.d.ts.map +1 -0
- package/build-types/types.d.ts +169 -0
- package/build-types/types.d.ts.map +1 -0
- package/package.json +72 -0
- package/src/components/widget-render/index.ts +1 -0
- package/src/components/widget-render/stories/index.story.tsx +356 -0
- package/src/components/widget-render/widget-render.tsx +44 -0
- package/src/hooks/index.ts +1 -0
- package/src/hooks/use-widget-types.ts +90 -0
- package/src/index.ts +21 -0
- package/src/stories/introduction.mdx +14 -0
- package/src/tools/get-lazy-widget-component/get-lazy-widget-component.ts +62 -0
- package/src/tools/get-lazy-widget-component/index.ts +1 -0
- package/src/types.ts +196 -0
package/README.md
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
# Widget Primitives
|
|
2
|
+
|
|
3
|
+
<div class="callout callout-alert">
|
|
4
|
+
This package is still experimental. “Experimental” means this is an early implementation subject to drastic and breaking changes.
|
|
5
|
+
</div>
|
|
6
|
+
|
|
7
|
+
The host-agnostic toolkit for dashboard widgets: the contract types that define
|
|
8
|
+
what a widget is, plus the runtime to discover the registered widget types and
|
|
9
|
+
resolve their render modules.
|
|
10
|
+
|
|
11
|
+
## Installation
|
|
12
|
+
|
|
13
|
+
Install the module:
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
npm install @wordpress/widget-primitives --save
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
_This package assumes that your code will run in an **ES2015+** environment.
|
|
20
|
+
If you're using an environment that has limited or no support for such
|
|
21
|
+
language features and APIs, you should include [the polyfill shipped in
|
|
22
|
+
`@wordpress/babel-preset-default`](https://github.com/WordPress/gutenberg/tree/HEAD/packages/babel-preset-default#polyfill)
|
|
23
|
+
in your code._
|
|
24
|
+
|
|
25
|
+
## Setup
|
|
26
|
+
|
|
27
|
+
This package ships no stylesheets; there is nothing to enqueue or import.
|
|
28
|
+
|
|
29
|
+
The contract types, `<WidgetRender>`, and `useWidgetTypes()` work in any
|
|
30
|
+
React application. The host fetches the widget-module records however it
|
|
31
|
+
wants and passes them in; `useWidgetTypes( records )` imports each record's
|
|
32
|
+
metadata module and returns the resolved `WidgetType[]`.
|
|
33
|
+
|
|
34
|
+
On a WordPress site the records come from the `/wp/v2/widget-modules` REST
|
|
35
|
+
endpoint, exposed while the `gutenberg-dashboard-widgets` experiment is
|
|
36
|
+
enabled. The dashboard reads it through a `@wordpress/core-data` entity and
|
|
37
|
+
passes the records to the hook.
|
|
38
|
+
|
|
39
|
+
With no records, or an empty list, `useWidgetTypes()` returns an empty list.
|
|
40
|
+
|
|
41
|
+
## Public API
|
|
42
|
+
|
|
43
|
+
- `<WidgetRender>`: canonical entry point for any host that mounts a widget.
|
|
44
|
+
Resolves the widget's render module via a host-provided `resolveWidgetModule`
|
|
45
|
+
and mounts the resulting component with the standard `attributes` plus
|
|
46
|
+
`setAttributes` render contract. Suspense, error handling, and chrome are
|
|
47
|
+
host concerns.
|
|
48
|
+
- `useWidgetTypes( records )` → `[ widgetTypes, isResolvingWidgetTypes ]`:
|
|
49
|
+
takes host-supplied records (`WidgetModuleRecord[]`, or `null` while
|
|
50
|
+
loading), imports each record's metadata, and returns the resolved
|
|
51
|
+
`WidgetType[]` plus a flag that is true while they are still resolving.
|
|
52
|
+
- Contract types: `WidgetType`, `WidgetName`, `WidgetIcon`,
|
|
53
|
+
`WidgetRenderProps`, `ResolveWidgetModule`, `WidgetModuleRecord`.
|
|
54
|
+
`WidgetIcon` is a rendered SVG element; hosts pass it to their icon
|
|
55
|
+
primitive as is.
|
|
56
|
+
|
|
57
|
+
## Architecture
|
|
58
|
+
|
|
59
|
+
For how the full pipeline fits together (authoring, build, server registry, and
|
|
60
|
+
hosts), see the
|
|
61
|
+
[dashboard widget system architecture document](https://github.com/WordPress/gutenberg/blob/HEAD/docs/explanations/architecture/dashboard-widgets.md).
|
|
62
|
+
|
|
63
|
+
## Contributing to this package
|
|
64
|
+
|
|
65
|
+
This is an individual package that's part of the Gutenberg project.
|
|
66
|
+
The project is organized as a monorepo. It's made up of multiple
|
|
67
|
+
self-contained software packages, each with a specific purpose. The
|
|
68
|
+
packages in this monorepo are published to [npm](https://www.npmjs.com/)
|
|
69
|
+
and used by [WordPress](https://make.wordpress.org/core/) as well as
|
|
70
|
+
other software projects.
|
|
71
|
+
|
|
72
|
+
To find out more about contributing to this package or Gutenberg as a
|
|
73
|
+
whole, please read the project's main
|
|
74
|
+
[contributor guide](https://github.com/WordPress/gutenberg/tree/HEAD/CONTRIBUTING.md).
|
|
75
|
+
|
|
76
|
+
<br /><br /><p align="center"><img src="https://s.w.org/style/images/codeispoetry.png?1" alt="Code is Poetry." /></p>
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// packages/widget-primitives/src/components/widget-render/index.ts
|
|
21
|
+
var widget_render_exports = {};
|
|
22
|
+
__export(widget_render_exports, {
|
|
23
|
+
WidgetRender: () => import_widget_render.WidgetRender
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(widget_render_exports);
|
|
26
|
+
var import_widget_render = require("./widget-render.cjs");
|
|
27
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
28
|
+
0 && (module.exports = {
|
|
29
|
+
WidgetRender
|
|
30
|
+
});
|
|
31
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// packages/widget-primitives/src/components/widget-render/widget-render.tsx
|
|
21
|
+
var widget_render_exports = {};
|
|
22
|
+
__export(widget_render_exports, {
|
|
23
|
+
WidgetRender: () => WidgetRender
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(widget_render_exports);
|
|
26
|
+
var import_get_lazy_widget_component = require("../../tools/get-lazy-widget-component/index.cjs");
|
|
27
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
|
28
|
+
function WidgetRender({
|
|
29
|
+
widgetType,
|
|
30
|
+
attributes,
|
|
31
|
+
setAttributes,
|
|
32
|
+
resolveWidgetModule
|
|
33
|
+
}) {
|
|
34
|
+
const WidgetComponent = (0, import_get_lazy_widget_component.getLazyWidgetComponent)(
|
|
35
|
+
widgetType.renderModule,
|
|
36
|
+
resolveWidgetModule
|
|
37
|
+
);
|
|
38
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_jsx_runtime.Fragment, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
39
|
+
WidgetComponent,
|
|
40
|
+
{
|
|
41
|
+
attributes,
|
|
42
|
+
setAttributes
|
|
43
|
+
}
|
|
44
|
+
) });
|
|
45
|
+
}
|
|
46
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
47
|
+
0 && (module.exports = {
|
|
48
|
+
WidgetRender
|
|
49
|
+
});
|
|
50
|
+
//# sourceMappingURL=widget-render.cjs.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../../src/components/widget-render/widget-render.tsx"],
|
|
4
|
+
"sourcesContent": ["/**\n * External dependencies\n */\n\n/**\n * Internal dependencies\n */\nimport { getLazyWidgetComponent } from '../../tools/get-lazy-widget-component';\nimport type { ResolveWidgetModule, WidgetType } from '../../types';\n\ninterface WidgetRenderProps< Item = unknown > {\n\twidgetType: WidgetType< Item >;\n\tattributes?: Item;\n\tsetAttributes?: ( next: Partial< Item > ) => void;\n\tresolveWidgetModule: ResolveWidgetModule;\n}\n\n/*\n * Resolves a widget type's `renderModule` via `resolveWidgetModule` and\n * mounts the resulting component with the `attributes`/`setAttributes`\n * render contract.\n */\nexport function WidgetRender< Item = unknown >( {\n\twidgetType,\n\tattributes,\n\tsetAttributes,\n\tresolveWidgetModule,\n}: WidgetRenderProps< Item > ) {\n\tconst WidgetComponent = getLazyWidgetComponent(\n\t\twidgetType.renderModule,\n\t\tresolveWidgetModule\n\t);\n\n\treturn (\n\t\t<>\n\t\t\t{ /* Cached `lazy()` keyed by renderModule; identity is stable across renders. */ }\n\t\t\t{ /* eslint-disable-next-line react-hooks/static-components */ }\n\t\t\t<WidgetComponent\n\t\t\t\tattributes={ attributes }\n\t\t\t\tsetAttributes={ setAttributes }\n\t\t\t/>\n\t\t</>\n\t);\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAOA,uCAAuC;AA2BrC;AAZK,SAAS,aAAgC;AAAA,EAC/C;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACD,GAA+B;AAC9B,QAAM,sBAAkB;AAAA,IACvB,WAAW;AAAA,IACX;AAAA,EACD;AAEA,SACC,2EAGC;AAAA,IAAC;AAAA;AAAA,MACA;AAAA,MACA;AAAA;AAAA,EACD,GACD;AAEF;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// packages/widget-primitives/src/hooks/index.ts
|
|
21
|
+
var hooks_exports = {};
|
|
22
|
+
__export(hooks_exports, {
|
|
23
|
+
useWidgetTypes: () => import_use_widget_types.useWidgetTypes
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(hooks_exports);
|
|
26
|
+
var import_use_widget_types = require("./use-widget-types.cjs");
|
|
27
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
28
|
+
0 && (module.exports = {
|
|
29
|
+
useWidgetTypes
|
|
30
|
+
});
|
|
31
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// packages/widget-primitives/src/hooks/use-widget-types.ts
|
|
21
|
+
var use_widget_types_exports = {};
|
|
22
|
+
__export(use_widget_types_exports, {
|
|
23
|
+
useWidgetTypes: () => useWidgetTypes
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(use_widget_types_exports);
|
|
26
|
+
var import_element = require("@wordpress/element");
|
|
27
|
+
function useWidgetTypes(records) {
|
|
28
|
+
const [widgetTypes, setWidgetTypes] = (0, import_element.useState)([]);
|
|
29
|
+
const [isResolvingWidgetTypes, setIsResolvingWidgetTypes] = (0, import_element.useState)(true);
|
|
30
|
+
(0, import_element.useEffect)(() => {
|
|
31
|
+
if (records === null || records === void 0) {
|
|
32
|
+
setIsResolvingWidgetTypes(true);
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
if (records.length === 0) {
|
|
36
|
+
setWidgetTypes([]);
|
|
37
|
+
setIsResolvingWidgetTypes(false);
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
let cancelled = false;
|
|
41
|
+
setIsResolvingWidgetTypes(true);
|
|
42
|
+
Promise.all(
|
|
43
|
+
records.map(async (record) => {
|
|
44
|
+
if (!record.widget_module) {
|
|
45
|
+
return null;
|
|
46
|
+
}
|
|
47
|
+
try {
|
|
48
|
+
const module2 = await import(
|
|
49
|
+
/* webpackIgnore: true */
|
|
50
|
+
record.widget_module
|
|
51
|
+
);
|
|
52
|
+
if (!module2?.default) {
|
|
53
|
+
return null;
|
|
54
|
+
}
|
|
55
|
+
return {
|
|
56
|
+
...module2.default,
|
|
57
|
+
name: record.name,
|
|
58
|
+
renderModule: record.render_module ?? "",
|
|
59
|
+
...record.presentation ? { presentation: record.presentation } : {}
|
|
60
|
+
};
|
|
61
|
+
} catch {
|
|
62
|
+
return null;
|
|
63
|
+
}
|
|
64
|
+
})
|
|
65
|
+
).then((results) => {
|
|
66
|
+
if (cancelled) {
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
setWidgetTypes(
|
|
70
|
+
results.filter((t) => t !== null)
|
|
71
|
+
);
|
|
72
|
+
setIsResolvingWidgetTypes(false);
|
|
73
|
+
});
|
|
74
|
+
return () => {
|
|
75
|
+
cancelled = true;
|
|
76
|
+
};
|
|
77
|
+
}, [records]);
|
|
78
|
+
return [widgetTypes, isResolvingWidgetTypes];
|
|
79
|
+
}
|
|
80
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
81
|
+
0 && (module.exports = {
|
|
82
|
+
useWidgetTypes
|
|
83
|
+
});
|
|
84
|
+
//# sourceMappingURL=use-widget-types.cjs.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/hooks/use-widget-types.ts"],
|
|
4
|
+
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { useEffect, useState } from '@wordpress/element';\n\n/**\n * Internal dependencies\n */\nimport type { WidgetModuleRecord, WidgetName, WidgetType } from '../types';\n\n/* `true` while records or their metadata imports are still resolving; hosts\n must not treat a widget instance as missing until it is `false`. */\ntype UseWidgetTypesResult = readonly [ WidgetType[], boolean ];\n\n/**\n * Resolves widget types from host-supplied records.\n *\n * For each record it dynamically imports `widget_module` and merges the\n * module's default export with the runtime fields (`name`, `renderModule`).\n * Pass `null`/`undefined` while records are still loading.\n *\n * @param records Host-supplied records, or `null`/`undefined` while loading.\n */\nexport function useWidgetTypes(\n\trecords: WidgetModuleRecord[] | null | undefined\n): UseWidgetTypesResult {\n\tconst [ widgetTypes, setWidgetTypes ] = useState< WidgetType[] >( [] );\n\tconst [ isResolvingWidgetTypes, setIsResolvingWidgetTypes ] =\n\t\tuseState( true );\n\n\tuseEffect( () => {\n\t\tif ( records === null || records === undefined ) {\n\t\t\tsetIsResolvingWidgetTypes( true );\n\t\t\treturn;\n\t\t}\n\n\t\tif ( records.length === 0 ) {\n\t\t\tsetWidgetTypes( [] );\n\t\t\tsetIsResolvingWidgetTypes( false );\n\t\t\treturn;\n\t\t}\n\n\t\tlet cancelled = false;\n\t\tsetIsResolvingWidgetTypes( true );\n\n\t\tPromise.all(\n\t\t\trecords.map( async ( record ) => {\n\t\t\t\tif ( ! record.widget_module ) {\n\t\t\t\t\treturn null;\n\t\t\t\t}\n\n\t\t\t\ttry {\n\t\t\t\t\tconst module = await import(\n\t\t\t\t\t\t/* webpackIgnore: true */ record.widget_module\n\t\t\t\t\t);\n\n\t\t\t\t\tif ( ! module?.default ) {\n\t\t\t\t\t\treturn null;\n\t\t\t\t\t}\n\n\t\t\t\t\treturn {\n\t\t\t\t\t\t...( module.default as Partial< WidgetType > ),\n\t\t\t\t\t\tname: record.name as WidgetName,\n\t\t\t\t\t\trenderModule: record.render_module ?? '',\n\t\t\t\t\t\t...( record.presentation\n\t\t\t\t\t\t\t? { presentation: record.presentation }\n\t\t\t\t\t\t\t: {} ),\n\t\t\t\t\t} as WidgetType;\n\t\t\t\t} catch {\n\t\t\t\t\treturn null;\n\t\t\t\t}\n\t\t\t} )\n\t\t).then( ( results ) => {\n\t\t\tif ( cancelled ) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tsetWidgetTypes(\n\t\t\t\tresults.filter( ( t ): t is WidgetType => t !== null )\n\t\t\t);\n\t\t\tsetIsResolvingWidgetTypes( false );\n\t\t} );\n\n\t\treturn () => {\n\t\t\tcancelled = true;\n\t\t};\n\t}, [ records ] );\n\n\treturn [ widgetTypes, isResolvingWidgetTypes ];\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,qBAAoC;AAoB7B,SAAS,eACf,SACuB;AACvB,QAAM,CAAE,aAAa,cAAe,QAAI,yBAA0B,CAAC,CAAE;AACrE,QAAM,CAAE,wBAAwB,yBAA0B,QACzD,yBAAU,IAAK;AAEhB,gCAAW,MAAM;AAChB,QAAK,YAAY,QAAQ,YAAY,QAAY;AAChD,gCAA2B,IAAK;AAChC;AAAA,IACD;AAEA,QAAK,QAAQ,WAAW,GAAI;AAC3B,qBAAgB,CAAC,CAAE;AACnB,gCAA2B,KAAM;AACjC;AAAA,IACD;AAEA,QAAI,YAAY;AAChB,8BAA2B,IAAK;AAEhC,YAAQ;AAAA,MACP,QAAQ,IAAK,OAAQ,WAAY;AAChC,YAAK,CAAE,OAAO,eAAgB;AAC7B,iBAAO;AAAA,QACR;AAEA,YAAI;AACH,gBAAMA,UAAS,MAAM;AAAA;AAAA,YACM,OAAO;AAAA;AAGlC,cAAK,CAAEA,SAAQ,SAAU;AACxB,mBAAO;AAAA,UACR;AAEA,iBAAO;AAAA,YACN,GAAKA,QAAO;AAAA,YACZ,MAAM,OAAO;AAAA,YACb,cAAc,OAAO,iBAAiB;AAAA,YACtC,GAAK,OAAO,eACT,EAAE,cAAc,OAAO,aAAa,IACpC,CAAC;AAAA,UACL;AAAA,QACD,QAAQ;AACP,iBAAO;AAAA,QACR;AAAA,MACD,CAAE;AAAA,IACH,EAAE,KAAM,CAAE,YAAa;AACtB,UAAK,WAAY;AAChB;AAAA,MACD;AAEA;AAAA,QACC,QAAQ,OAAQ,CAAE,MAAwB,MAAM,IAAK;AAAA,MACtD;AACA,gCAA2B,KAAM;AAAA,IAClC,CAAE;AAEF,WAAO,MAAM;AACZ,kBAAY;AAAA,IACb;AAAA,EACD,GAAG,CAAE,OAAQ,CAAE;AAEf,SAAO,CAAE,aAAa,sBAAuB;AAC9C;",
|
|
6
|
+
"names": ["module"]
|
|
7
|
+
}
|
package/build/index.cjs
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// packages/widget-primitives/src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
WidgetRender: () => import_widget_render.WidgetRender,
|
|
24
|
+
useWidgetTypes: () => import_hooks.useWidgetTypes
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(index_exports);
|
|
27
|
+
var import_widget_render = require("./components/widget-render/index.cjs");
|
|
28
|
+
var import_hooks = require("./hooks/index.cjs");
|
|
29
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
30
|
+
0 && (module.exports = {
|
|
31
|
+
WidgetRender,
|
|
32
|
+
useWidgetTypes
|
|
33
|
+
});
|
|
34
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../src/index.ts"],
|
|
4
|
+
"sourcesContent": ["/**\n * Components\n */\nexport { WidgetRender } from './components/widget-render';\n\n/**\n * Hooks\n */\nexport { useWidgetTypes } from './hooks';\n\n/**\n * Types\n */\nexport type {\n\tWidgetName,\n\tWidgetIcon,\n\tWidgetType,\n\tWidgetRenderProps,\n\tResolveWidgetModule,\n\tWidgetModuleRecord,\n} from './types';\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,2BAA6B;AAK7B,mBAA+B;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// packages/widget-primitives/src/tools/get-lazy-widget-component/get-lazy-widget-component.ts
|
|
21
|
+
var get_lazy_widget_component_exports = {};
|
|
22
|
+
__export(get_lazy_widget_component_exports, {
|
|
23
|
+
getLazyWidgetComponent: () => getLazyWidgetComponent
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(get_lazy_widget_component_exports);
|
|
26
|
+
var import_element = require("@wordpress/element");
|
|
27
|
+
function isValidWidgetModule(module2) {
|
|
28
|
+
return typeof module2 === "object" && module2 !== null && "default" in module2 && typeof module2.default === "function";
|
|
29
|
+
}
|
|
30
|
+
var componentCache = /* @__PURE__ */ new Map();
|
|
31
|
+
function getLazyWidgetComponent(renderModule, resolveWidgetModule) {
|
|
32
|
+
const cached = componentCache.get(renderModule);
|
|
33
|
+
if (cached) {
|
|
34
|
+
return cached;
|
|
35
|
+
}
|
|
36
|
+
const lazyComponent = (0, import_element.lazy)(async () => {
|
|
37
|
+
const module2 = await resolveWidgetModule(renderModule);
|
|
38
|
+
if (!isValidWidgetModule(module2)) {
|
|
39
|
+
throw new Error(`Invalid widget module: ${renderModule}`);
|
|
40
|
+
}
|
|
41
|
+
return module2;
|
|
42
|
+
});
|
|
43
|
+
componentCache.set(renderModule, lazyComponent);
|
|
44
|
+
return lazyComponent;
|
|
45
|
+
}
|
|
46
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
47
|
+
0 && (module.exports = {
|
|
48
|
+
getLazyWidgetComponent
|
|
49
|
+
});
|
|
50
|
+
//# sourceMappingURL=get-lazy-widget-component.cjs.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../../src/tools/get-lazy-widget-component/get-lazy-widget-component.ts"],
|
|
4
|
+
"sourcesContent": ["/**\n * External dependencies\n */\nimport type { ComponentType } from 'react';\n\n/**\n * WordPress dependencies\n */\nimport { lazy } from '@wordpress/element';\n\n/**\n * Internal dependencies\n */\nimport type {\n\tResolveWidgetModule,\n\tWidgetModule,\n\tWidgetRenderProps,\n} from '../../types';\n\ntype LazyWidgetComponent = ComponentType< WidgetRenderProps< unknown > >;\n\nfunction isValidWidgetModule( module: unknown ): module is WidgetModule {\n\treturn (\n\t\ttypeof module === 'object' &&\n\t\tmodule !== null &&\n\t\t'default' in module &&\n\t\ttypeof ( module as { default: unknown } ).default === 'function'\n\t);\n}\n\n/*\n * Cache keyed by `renderModule`. The lazy component must keep a stable\n * identity across renders; rebuilding it inline (e.g. via `useMemo`) resets\n * the Suspense boundary and the resolved module.\n */\nconst componentCache = new Map< string, LazyWidgetComponent >();\n\n/*\n * Resolve a widget render module to a `lazy()` React component, cached by\n * `renderModule` id so repeated calls return the same instance.\n */\nexport function getLazyWidgetComponent(\n\trenderModule: string,\n\tresolveWidgetModule: ResolveWidgetModule\n): LazyWidgetComponent {\n\tconst cached = componentCache.get( renderModule );\n\tif ( cached ) {\n\t\treturn cached;\n\t}\n\n\tconst lazyComponent = lazy< LazyWidgetComponent >( async () => {\n\t\tconst module: unknown = await resolveWidgetModule( renderModule );\n\t\tif ( ! isValidWidgetModule( module ) ) {\n\t\t\tthrow new Error( `Invalid widget module: ${ renderModule }` );\n\t\t}\n\n\t\treturn module;\n\t} );\n\n\tcomponentCache.set( renderModule, lazyComponent );\n\treturn lazyComponent;\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAQA,qBAAqB;AAarB,SAAS,oBAAqBA,SAA0C;AACvE,SACC,OAAOA,YAAW,YAClBA,YAAW,QACX,aAAaA,WACb,OAASA,QAAiC,YAAY;AAExD;AAOA,IAAM,iBAAiB,oBAAI,IAAmC;AAMvD,SAAS,uBACf,cACA,qBACsB;AACtB,QAAM,SAAS,eAAe,IAAK,YAAa;AAChD,MAAK,QAAS;AACb,WAAO;AAAA,EACR;AAEA,QAAM,oBAAgB,qBAA6B,YAAY;AAC9D,UAAMA,UAAkB,MAAM,oBAAqB,YAAa;AAChE,QAAK,CAAE,oBAAqBA,OAAO,GAAI;AACtC,YAAM,IAAI,MAAO,0BAA2B,YAAa,EAAG;AAAA,IAC7D;AAEA,WAAOA;AAAA,EACR,CAAE;AAEF,iBAAe,IAAK,cAAc,aAAc;AAChD,SAAO;AACR;",
|
|
6
|
+
"names": ["module"]
|
|
7
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// packages/widget-primitives/src/tools/get-lazy-widget-component/index.ts
|
|
21
|
+
var get_lazy_widget_component_exports = {};
|
|
22
|
+
__export(get_lazy_widget_component_exports, {
|
|
23
|
+
getLazyWidgetComponent: () => import_get_lazy_widget_component.getLazyWidgetComponent
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(get_lazy_widget_component_exports);
|
|
26
|
+
var import_get_lazy_widget_component = require("./get-lazy-widget-component.cjs");
|
|
27
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
28
|
+
0 && (module.exports = {
|
|
29
|
+
getLazyWidgetComponent
|
|
30
|
+
});
|
|
31
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../../src/tools/get-lazy-widget-component/index.ts"],
|
|
4
|
+
"sourcesContent": ["export { getLazyWidgetComponent } from './get-lazy-widget-component';\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,uCAAuC;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
package/build/types.cjs
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __copyProps = (to, from, except, desc) => {
|
|
7
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
8
|
+
for (let key of __getOwnPropNames(from))
|
|
9
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
10
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
11
|
+
}
|
|
12
|
+
return to;
|
|
13
|
+
};
|
|
14
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
15
|
+
|
|
16
|
+
// packages/widget-primitives/src/types.ts
|
|
17
|
+
var types_exports = {};
|
|
18
|
+
module.exports = __toCommonJS(types_exports);
|
|
19
|
+
//# sourceMappingURL=types.cjs.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../src/types.ts"],
|
|
4
|
+
"sourcesContent": ["/**\n * Widget type definitions.\n *\n * Canonical home for widget identity types consumed by the registry and\n * hosts that render widgets.\n *\n * Each type is generic over the widget's attribute object (`Item`), so a\n * widget binds its attribute shape once and gets typed `attributes`,\n * `example`, and `setAttributes`.\n */\n\n/**\n * External dependencies\n */\nimport type { ComponentProps, ComponentType, ReactElement } from 'react';\nimport type { Field } from '@wordpress/dataviews';\n\n/**\n * Widget type identifier, structured as `<widget-namespace>/<widget-name>`.\n * Both segments are lowercase, kebab-case.\n */\nexport type WidgetName = `${ string }/${ string }`;\n\n/**\n * Icon for a widget type: a rendered SVG element, typically one from\n * `@wordpress/icons`.\n */\nexport type WidgetIcon = ReactElement< ComponentProps< 'svg' > >;\n\n/**\n * Literal contents of a widget's `widget.json` metadata file.\n *\n * Captures the *authoring* shape only; module entry points and style\n * assets are discovered by convention from the widget directory, not\n * declared here.\n */\nexport interface WidgetTypeMetadata< Item = unknown > {\n\t/**\n\t * Version of the Widget API used by the widget.\n\t */\n\tapiVersion: number;\n\n\t/**\n\t * Stable type identifier.\n\t */\n\tname: WidgetName;\n\n\t/**\n\t * Display title; hosts surface it in pickers and chrome.\n\t */\n\ttitle: string;\n\n\t/**\n\t * Short description; hosts surface it in pickers and help panels.\n\t */\n\tdescription?: string;\n\n\t/**\n\t * Visual identifier for the widget type; hosts decide where, and\n\t * whether, to render it.\n\t */\n\ticon?: WidgetIcon;\n\n\t/**\n\t * Grouping category. Core provides `dashboard`; plugins and themes may\n\t * register custom categories.\n\t */\n\tcategory?: string;\n\n\t/**\n\t * Authoring intent about how the widget renders. Not a user-editable\n\t * attribute.\n\t *\n\t * - `'framed'` (default when absent): the widget renders its\n\t * content only.\n\t * - `'content-bleed'`: the host's chrome stays visible while the\n\t * content fills the content area edge-to-edge, with no padding.\n\t * - `'full-bleed'`: the widget renders edge-to-edge with no\n\t * surrounding chrome.\n\t */\n\tpresentation?: 'framed' | 'content-bleed' | 'full-bleed';\n\n\t/**\n\t * Search aliases hosts use to match the widget in their pickers.\n\t */\n\tkeywords?: string[];\n\n\t/**\n\t * Widget version, used for asset cache invalidation.\n\t */\n\tversion?: string;\n\n\t/**\n\t * Gettext text domain for translations.\n\t */\n\ttextdomain?: string;\n\n\t/**\n\t * Experiment gate; boolean `true`, or a specific experiment name.\n\t */\n\t__experimental?: string | boolean;\n\n\t/**\n\t * Declarative attribute schema, bound to the widget's attribute\n\t * object via `Item`. Hosts render forms straight from this list\n\t * via `DataForm`, with no per-widget form wiring.\n\t */\n\tattributes?: Field< Item >[];\n\n\t/**\n\t * Structured example data hosts use for previews, and the default\n\t * attributes applied when a new instance is created without initial\n\t * attributes.\n\t */\n\texample?: {\n\t\tattributes?: Partial< Item >;\n\t};\n}\n\n/**\n * Runtime widget type consumed by hosts.\n *\n * Extends `WidgetTypeMetadata` with runtime-only fields, notably\n * `renderModule`. Hosts supply the raw records in snake_case\n * (`WidgetModuleRecord`); `useWidgetTypes` is the single boundary that\n * resolves them into this camelCase shape.\n */\nexport interface WidgetType< Item = unknown >\n\textends WidgetTypeMetadata< Item > {\n\t/**\n\t * Script-module identifier resolved to a React component at render\n\t * time, produced from the conventional `render.*` entry point.\n\t */\n\trenderModule: string;\n}\n\n/**\n * Props passed to a widget's render component by the host, bound over\n * `Item` so `attributes` and `setAttributes` are typed against the\n * widget's attribute object.\n */\nexport interface WidgetRenderProps< Item = unknown > {\n\t/**\n\t * User-configured attributes for this widget instance.\n\t */\n\tattributes: Item;\n\n\t/**\n\t * Updates the attributes of this instance. Optional because some\n\t * hosts render widgets in read-only contexts.\n\t */\n\tsetAttributes?: ( next: Partial< Item > ) => void;\n}\n\n/**\n * Widget render module shape returned by the module resolver.\n */\nexport interface WidgetModule {\n\tdefault: ComponentType< WidgetRenderProps< unknown > >;\n}\n\n/**\n * Resolver function: maps a `WidgetType.renderModule` id to a React\n * component. Override for tests, Storybook, or to load from a non-URL\n * source.\n */\nexport type ResolveWidgetModule = (\n\tmoduleId: string\n) => Promise< WidgetModule >;\n\n/**\n * Per-widget record a host feeds to `useWidgetTypes`, in snake_case wire\n * format. The host fetches these however it likes; only the field shape is\n * part of the contract.\n */\nexport interface WidgetModuleRecord {\n\t/**\n\t * Stable widget type identifier.\n\t */\n\tname: string;\n\n\t/**\n\t * Script-module id resolved to the render component at render time.\n\t */\n\trender_module?: string | null;\n\n\t/**\n\t * Script-module id dynamically imported for the widget's live metadata.\n\t */\n\twidget_module?: string | null;\n\n\t/**\n\t * Authoring presentation hint; overrides the metadata module's value.\n\t */\n\tpresentation?: WidgetTypeMetadata[ 'presentation' ] | null;\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;AAAA;AAAA;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
// packages/widget-primitives/src/components/widget-render/widget-render.tsx
|
|
2
|
+
import { getLazyWidgetComponent } from "../../tools/get-lazy-widget-component/index.mjs";
|
|
3
|
+
import { Fragment, jsx } from "react/jsx-runtime";
|
|
4
|
+
function WidgetRender({
|
|
5
|
+
widgetType,
|
|
6
|
+
attributes,
|
|
7
|
+
setAttributes,
|
|
8
|
+
resolveWidgetModule
|
|
9
|
+
}) {
|
|
10
|
+
const WidgetComponent = getLazyWidgetComponent(
|
|
11
|
+
widgetType.renderModule,
|
|
12
|
+
resolveWidgetModule
|
|
13
|
+
);
|
|
14
|
+
return /* @__PURE__ */ jsx(Fragment, { children: /* @__PURE__ */ jsx(
|
|
15
|
+
WidgetComponent,
|
|
16
|
+
{
|
|
17
|
+
attributes,
|
|
18
|
+
setAttributes
|
|
19
|
+
}
|
|
20
|
+
) });
|
|
21
|
+
}
|
|
22
|
+
export {
|
|
23
|
+
WidgetRender
|
|
24
|
+
};
|
|
25
|
+
//# sourceMappingURL=widget-render.mjs.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../../src/components/widget-render/widget-render.tsx"],
|
|
4
|
+
"sourcesContent": ["/**\n * External dependencies\n */\n\n/**\n * Internal dependencies\n */\nimport { getLazyWidgetComponent } from '../../tools/get-lazy-widget-component';\nimport type { ResolveWidgetModule, WidgetType } from '../../types';\n\ninterface WidgetRenderProps< Item = unknown > {\n\twidgetType: WidgetType< Item >;\n\tattributes?: Item;\n\tsetAttributes?: ( next: Partial< Item > ) => void;\n\tresolveWidgetModule: ResolveWidgetModule;\n}\n\n/*\n * Resolves a widget type's `renderModule` via `resolveWidgetModule` and\n * mounts the resulting component with the `attributes`/`setAttributes`\n * render contract.\n */\nexport function WidgetRender< Item = unknown >( {\n\twidgetType,\n\tattributes,\n\tsetAttributes,\n\tresolveWidgetModule,\n}: WidgetRenderProps< Item > ) {\n\tconst WidgetComponent = getLazyWidgetComponent(\n\t\twidgetType.renderModule,\n\t\tresolveWidgetModule\n\t);\n\n\treturn (\n\t\t<>\n\t\t\t{ /* Cached `lazy()` keyed by renderModule; identity is stable across renders. */ }\n\t\t\t{ /* eslint-disable-next-line react-hooks/static-components */ }\n\t\t\t<WidgetComponent\n\t\t\t\tattributes={ attributes }\n\t\t\t\tsetAttributes={ setAttributes }\n\t\t\t/>\n\t\t</>\n\t);\n}\n"],
|
|
5
|
+
"mappings": ";AAOA,SAAS,8BAA8B;AA2BrC,mBAGC,WAHD;AAZK,SAAS,aAAgC;AAAA,EAC/C;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACD,GAA+B;AAC9B,QAAM,kBAAkB;AAAA,IACvB,WAAW;AAAA,IACX;AAAA,EACD;AAEA,SACC,gCAGC;AAAA,IAAC;AAAA;AAAA,MACA;AAAA,MACA;AAAA;AAAA,EACD,GACD;AAEF;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
// packages/widget-primitives/src/hooks/use-widget-types.ts
|
|
2
|
+
import { useEffect, useState } from "@wordpress/element";
|
|
3
|
+
function useWidgetTypes(records) {
|
|
4
|
+
const [widgetTypes, setWidgetTypes] = useState([]);
|
|
5
|
+
const [isResolvingWidgetTypes, setIsResolvingWidgetTypes] = useState(true);
|
|
6
|
+
useEffect(() => {
|
|
7
|
+
if (records === null || records === void 0) {
|
|
8
|
+
setIsResolvingWidgetTypes(true);
|
|
9
|
+
return;
|
|
10
|
+
}
|
|
11
|
+
if (records.length === 0) {
|
|
12
|
+
setWidgetTypes([]);
|
|
13
|
+
setIsResolvingWidgetTypes(false);
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
16
|
+
let cancelled = false;
|
|
17
|
+
setIsResolvingWidgetTypes(true);
|
|
18
|
+
Promise.all(
|
|
19
|
+
records.map(async (record) => {
|
|
20
|
+
if (!record.widget_module) {
|
|
21
|
+
return null;
|
|
22
|
+
}
|
|
23
|
+
try {
|
|
24
|
+
const module = await import(
|
|
25
|
+
/* webpackIgnore: true */
|
|
26
|
+
record.widget_module
|
|
27
|
+
);
|
|
28
|
+
if (!module?.default) {
|
|
29
|
+
return null;
|
|
30
|
+
}
|
|
31
|
+
return {
|
|
32
|
+
...module.default,
|
|
33
|
+
name: record.name,
|
|
34
|
+
renderModule: record.render_module ?? "",
|
|
35
|
+
...record.presentation ? { presentation: record.presentation } : {}
|
|
36
|
+
};
|
|
37
|
+
} catch {
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
})
|
|
41
|
+
).then((results) => {
|
|
42
|
+
if (cancelled) {
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
setWidgetTypes(
|
|
46
|
+
results.filter((t) => t !== null)
|
|
47
|
+
);
|
|
48
|
+
setIsResolvingWidgetTypes(false);
|
|
49
|
+
});
|
|
50
|
+
return () => {
|
|
51
|
+
cancelled = true;
|
|
52
|
+
};
|
|
53
|
+
}, [records]);
|
|
54
|
+
return [widgetTypes, isResolvingWidgetTypes];
|
|
55
|
+
}
|
|
56
|
+
export {
|
|
57
|
+
useWidgetTypes
|
|
58
|
+
};
|
|
59
|
+
//# sourceMappingURL=use-widget-types.mjs.map
|