@blocklet/pages-kit 0.2.203 → 0.2.205

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.
@@ -9,16 +9,16 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
9
9
  });
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.getComponents = void 0;
12
+ exports.preloadComponents = void 0;
13
13
  const component_1 = require("@blocklet/sdk/lib/component");
14
- function getComponents({ mode, name } = {}) {
14
+ function preloadComponents({ mode, name, id }) {
15
15
  return __awaiter(this, void 0, void 0, function* () {
16
16
  return (0, component_1.call)({
17
17
  name: 'pages-kit',
18
18
  method: 'GET',
19
- path: '/api/components',
20
- params: { name, mode },
19
+ path: '/api/components/preload',
20
+ params: { mode, id: Array.isArray(id) ? id.join(',') : id, name: Array.isArray(name) ? name.join(',') : name },
21
21
  }).then((res) => res.data);
22
22
  });
23
23
  }
24
- exports.getComponents = getComponents;
24
+ exports.preloadComponents = preloadComponents;
@@ -12,15 +12,23 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
12
12
  return (mod && mod.__esModule) ? mod : { "default": mod };
13
13
  };
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.getComponents = void 0;
15
+ exports.preloadComponents = void 0;
16
16
  const axios_1 = __importDefault(require("axios"));
17
17
  const ufo_1 = require("ufo");
18
18
  const PAGES_KIT_DID = 'z8iZiDFg3vkkrPwsiba1TLXy3H9XHzFERsP8o';
19
- function getComponents({ mode, name } = {}) {
19
+ function preloadComponents({ mode, name, id }) {
20
20
  var _a, _b;
21
21
  return __awaiter(this, void 0, void 0, function* () {
22
22
  const prefix = ((_b = (_a = window.blocklet) === null || _a === void 0 ? void 0 : _a.componentMountPoints.find((i) => i.did === PAGES_KIT_DID)) === null || _b === void 0 ? void 0 : _b.mountPoint) || '/';
23
- return axios_1.default.get((0, ufo_1.joinURL)(prefix, '/api/components'), { params: { name, mode } }).then((res) => res.data);
23
+ return axios_1.default
24
+ .get((0, ufo_1.joinURL)(prefix, '/api/components'), {
25
+ params: {
26
+ mode,
27
+ id: Array.isArray(id) ? id.join(',') : id,
28
+ name: Array.isArray(name) ? name.join(',') : name,
29
+ },
30
+ })
31
+ .then((res) => res.data);
24
32
  });
25
33
  }
26
- exports.getComponents = getComponents;
34
+ exports.preloadComponents = preloadComponents;
@@ -31,178 +31,107 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
31
31
  step((generator = generator.apply(thisArg, _arguments || [])).next());
32
32
  });
33
33
  };
34
- var __importDefault = (this && this.__importDefault) || function (mod) {
35
- return (mod && mod.__esModule) ? mod : { "default": mod };
34
+ var __rest = (this && this.__rest) || function (s, e) {
35
+ var t = {};
36
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
37
+ t[p] = s[p];
38
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
39
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
40
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
41
+ t[p[i]] = s[p[i]];
42
+ }
43
+ return t;
36
44
  };
37
45
  Object.defineProperty(exports, "__esModule", { value: true });
38
46
  const jsx_runtime_1 = require("react/jsx-runtime");
39
- const cache_1 = __importDefault(require("@emotion/cache"));
40
- const react_1 = require("@emotion/react");
41
- const react_2 = require("@iconify/react");
47
+ const react_1 = require("@iconify/react");
42
48
  const material_1 = require("@mui/material");
43
- const react_3 = __importStar(require("react"));
44
- const client_1 = __importDefault(require("react-dom/client"));
49
+ const react_2 = __importStar(require("react"));
45
50
  const react_error_boundary_1 = require("react-error-boundary");
46
- const react_frame_component_1 = require("react-frame-component");
47
51
  const ufo_1 = require("ufo");
48
- const zustand_1 = require("zustand");
49
- const immer_1 = require("zustand/middleware/immer");
52
+ const preload_1 = require("../../types/preload");
53
+ const property_1 = require("../../utils/property");
50
54
  // inject global variables for web-component renderer
51
55
  const win = window;
52
56
  win.joinURL = ufo_1.joinURL;
53
57
  win.withQuery = ufo_1.withQuery;
54
- win.React = react_3.default;
58
+ win.React = react_2.default;
55
59
  win.Box = material_1.Box;
56
60
  win.Stack = material_1.Stack;
57
61
  win.Button = material_1.Button;
58
- win.IconifyIcon = react_2.Icon;
59
- const CUSTOM_COMPONENT_RENDERER = 'custom-component-renderer';
60
- const defineCustomElements = (win) => {
61
- if (!win.customElements.get(CUSTOM_COMPONENT_RENDERER)) {
62
- win.customElements.define(CUSTOM_COMPONENT_RENDERER, class CustomComponentRenderer extends win.HTMLElement {
63
- constructor() {
64
- super();
65
- this.root = this.attachShadow({ mode: 'open' });
66
- this.reactRoot = client_1.default.createRoot(this.root);
67
- }
68
- static get observedAttributes() {
69
- return ['id', 'component-id', 'locale', 'default-locale'];
70
- }
71
- get componentId() {
72
- return this.getAttribute('component-id');
73
- }
74
- get locale() {
75
- return this.getAttribute('locale');
76
- }
77
- get defaultLocale() {
78
- return this.getAttribute('default-locale');
79
- }
80
- connectedCallback() {
81
- this.setup();
82
- }
83
- attributeChangedCallback() {
84
- this.setup();
85
- }
86
- setup() {
87
- const { subscribe } = customComponentState(this.componentId);
88
- const { subscribe: subscribeProperties } = customComponentProperties(this.id);
89
- subscribe(() => this.render());
90
- subscribeProperties(() => this.render());
91
- this.render();
92
- }
93
- get state() {
94
- return Object.assign(Object.assign({}, customComponentState(this.componentId).getState()), { properties: customComponentProperties(this.id).getState().properties });
95
- }
96
- render() {
97
- var _a, _b, _c, _d, _e;
98
- return __awaiter(this, void 0, void 0, function* () {
99
- try {
100
- const { component, properties: parameters } = this.state;
101
- if (!component)
102
- throw new Error('missing required component config');
103
- let script;
104
- let current = component;
105
- const properties = Object.fromEntries(Object.entries(parameters !== null && parameters !== void 0 ? parameters : {}).map(([id, { value }]) => {
106
- var _a, _b;
107
- const property = (_b = (_a = component.properties) === null || _a === void 0 ? void 0 : _a[id]) === null || _b === void 0 ? void 0 : _b.data;
108
- if (!property)
109
- return [];
110
- return [property.key, parsePropertyValue(property.type, value)];
111
- }));
112
- while (current) {
113
- mergeProperties(properties, Object.fromEntries(Object.values((_a = current.properties) !== null && _a !== void 0 ? _a : {}).map(({ data }) => {
114
- var _a, _b, _c, _d, _e;
115
- return [
116
- data.key,
117
- parsePropertyValue(data.type, (_c = (_b = (_a = data.locales) === null || _a === void 0 ? void 0 : _a[this.locale]) === null || _b === void 0 ? void 0 : _b.defaultValue) !== null && _c !== void 0 ? _c : (_e = (_d = data.locales) === null || _d === void 0 ? void 0 : _d[this.defaultLocale]) === null || _e === void 0 ? void 0 : _e.defaultValue),
118
- ];
119
- })));
120
- if (((_b = current.renderer) === null || _b === void 0 ? void 0 : _b.type) === 'web-component') {
121
- script = (_c = current.renderer) === null || _c === void 0 ? void 0 : _c.script;
122
- break;
123
- }
124
- if (((_d = current.renderer) === null || _d === void 0 ? void 0 : _d.type) === 'custom-component' && current.renderer.componentId) {
125
- const c = customComponentState(current.renderer.componentId).getState()
126
- .component;
127
- const rendererProperties = current.renderer.properties;
128
- mergeProperties(properties, Object.fromEntries(Object.entries((_e = c === null || c === void 0 ? void 0 : c.properties) !== null && _e !== void 0 ? _e : {}).map(([id, { data }]) => {
129
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
130
- const locales = (_a = rendererProperties === null || rendererProperties === void 0 ? void 0 : rendererProperties[id]) === null || _a === void 0 ? void 0 : _a.locales;
131
- return [
132
- data.key,
133
- parsePropertyValue(data.type, (_h = (_e = (_c = (_b = locales === null || locales === void 0 ? void 0 : locales[this.locale]) === null || _b === void 0 ? void 0 : _b.value) !== null && _c !== void 0 ? _c : (_d = locales === null || locales === void 0 ? void 0 : locales[this.defaultLocale]) === null || _d === void 0 ? void 0 : _d.value) !== null && _e !== void 0 ? _e : (_g = (_f = data.locales) === null || _f === void 0 ? void 0 : _f[this.locale]) === null || _g === void 0 ? void 0 : _g.defaultValue) !== null && _h !== void 0 ? _h : (_k = (_j = data.locales) === null || _j === void 0 ? void 0 : _j[this.defaultLocale]) === null || _k === void 0 ? void 0 : _k.defaultValue),
134
- ];
135
- })));
136
- current = c;
137
- }
138
- else {
139
- current = undefined;
140
- }
141
- }
142
- if (!script)
143
- throw new Error('Missing required script');
144
- const cache = (0, cache_1.default)({
145
- key: 'css',
146
- prepend: true,
147
- container: this.root,
148
- });
149
- const url = URL.createObjectURL(new Blob([script], { type: 'application/javascript' }));
150
- const { default: C } = yield Promise.resolve(`${url}`).then(s => __importStar(require(s)));
151
- this.reactRoot.render((0, jsx_runtime_1.jsx)(react_1.CacheProvider, { value: cache, children: (0, jsx_runtime_1.jsx)(material_1.CssBaseline, { children: (0, jsx_runtime_1.jsx)(C, Object.assign({}, properties)) }) }));
152
- }
153
- catch (error) {
154
- console.error(error);
155
- this.reactRoot.render((0, jsx_runtime_1.jsx)("div", {}));
156
- }
157
- });
158
- }
159
- });
160
- }
161
- };
162
- const CUSTOM_COMPONENT_STATE_MAP = {};
163
- const customComponentState = (id, initial) => {
164
- var _a;
165
- (_a = CUSTOM_COMPONENT_STATE_MAP[id]) !== null && _a !== void 0 ? _a : (CUSTOM_COMPONENT_STATE_MAP[id] = (0, zustand_1.create)()((0, immer_1.immer)(() => (Object.assign({}, initial)))));
166
- return CUSTOM_COMPONENT_STATE_MAP[id];
167
- };
168
- const CUSTOM_COMPONENT_PROPERTIES_MAP = {};
169
- const customComponentProperties = (id, initial) => {
170
- var _a;
171
- (_a = CUSTOM_COMPONENT_PROPERTIES_MAP[id]) !== null && _a !== void 0 ? _a : (CUSTOM_COMPONENT_PROPERTIES_MAP[id] = (0, zustand_1.create)()((0, immer_1.immer)(() => (Object.assign({}, initial)))));
172
- return CUSTOM_COMPONENT_PROPERTIES_MAP[id];
173
- };
174
- function CustomComponentRenderer({ id, componentId, components, properties, locale, defaultLocale, }) {
175
- const frame = (0, react_frame_component_1.useFrame)();
176
- components === null || components === void 0 ? void 0 : components.forEach((component) => {
177
- customComponentState(component.id, { component }).setState({ component });
178
- });
179
- customComponentProperties(id, { properties }).setState({ properties });
180
- (0, react_3.useEffect)(() => {
181
- defineCustomElements(frame.window || window);
182
- }, [frame]);
183
- return ((0, jsx_runtime_1.jsx)(react_error_boundary_1.ErrorBoundary, { fallbackRender: E, children: (0, jsx_runtime_1.jsx)("custom-component-renderer", { "component-id": componentId, id: id, locale: locale, "default-locale": defaultLocale }) }));
62
+ win.IconifyIcon = react_1.Icon;
63
+ function CustomComponentRenderer(_a) {
64
+ var props = __rest(_a, []);
65
+ return ((0, jsx_runtime_1.jsx)(react_error_boundary_1.ErrorBoundary, { fallbackRender: E, children: (0, jsx_runtime_1.jsx)(Renderer, Object.assign({}, props)) }));
184
66
  }
185
67
  exports.default = CustomComponentRenderer;
68
+ function Renderer(props) {
69
+ const component = loadComponent(props);
70
+ if (!component)
71
+ return null;
72
+ const { Component, AsyncComponent, properties } = component;
73
+ if (Component) {
74
+ return (0, jsx_runtime_1.jsx)(Component, Object.assign({}, properties));
75
+ }
76
+ if (AsyncComponent) {
77
+ return (0, jsx_runtime_1.jsx)(AsyncComponentRenderer, { AsyncComponent: AsyncComponent, properties: properties });
78
+ }
79
+ return null;
80
+ }
81
+ function AsyncComponentRenderer({ AsyncComponent, properties, }) {
82
+ const [{ Component, error, key }, setState] = (0, react_2.useState)({});
83
+ (0, react_2.useEffect)(() => {
84
+ AsyncComponent.then((m) => setState({ Component: m.default, key: Date.now() })).catch((error) => setState({ error, key: Date.now() }));
85
+ }, [AsyncComponent]);
86
+ if (error) {
87
+ return (0, jsx_runtime_1.jsx)(material_1.Alert, { severity: "error", children: error.stack });
88
+ }
89
+ if (Component) {
90
+ return ((0, jsx_runtime_1.jsx)(react_error_boundary_1.ErrorBoundary, { fallbackRender: E, resetKeys: [key], children: (0, jsx_runtime_1.jsx)(Component, Object.assign({}, properties)) }));
91
+ }
92
+ return null;
93
+ }
186
94
  function E({ error }) {
187
95
  return ((0, jsx_runtime_1.jsx)(material_1.Box, { children: (0, jsx_runtime_1.jsx)(material_1.Alert, { severity: "error", children: error.message }) }));
188
96
  }
189
- function mergeProperties(properties, newProperties) {
190
- for (const [key, val] of Object.entries(newProperties)) {
191
- if (properties[key] === undefined || properties[key] === null) {
192
- properties[key] = val;
193
- }
97
+ const PRELOAD_COMPONENTS_STATE = window[preload_1.PreloadComponentsStateGlobalVariableName];
98
+ function loadComponent({ componentId, components, locale, defaultLocale, properties: parameters, }) {
99
+ var _a, _b, _c, _d, _e;
100
+ const preload = PRELOAD_COMPONENTS_STATE === null || PRELOAD_COMPONENTS_STATE === void 0 ? void 0 : PRELOAD_COMPONENTS_STATE.components[componentId];
101
+ if (preload) {
102
+ const properties = Object.fromEntries(Object.entries(parameters !== null && parameters !== void 0 ? parameters : {}).map(([id, { value }]) => {
103
+ var _a, _b;
104
+ const property = (_b = (_a = preload.component.properties) === null || _a === void 0 ? void 0 : _a[id]) === null || _b === void 0 ? void 0 : _b.data;
105
+ if (!property)
106
+ return [];
107
+ return [property.key, (0, property_1.parsePropertyValue)(property.type, value)];
108
+ }));
109
+ return {
110
+ Component: window[preload.preloadComponentModuleName],
111
+ properties: (0, property_1.assignNullableFields)(properties, (_c = (_b = (_a = preload.locales) === null || _a === void 0 ? void 0 : _a[locale]) === null || _b === void 0 ? void 0 : _b.properties) !== null && _c !== void 0 ? _c : (_e = (_d = preload.locales) === null || _d === void 0 ? void 0 : _d[PRELOAD_COMPONENTS_STATE.config.defaultLocale]) === null || _e === void 0 ? void 0 : _e.properties),
112
+ };
194
113
  }
195
- return properties;
114
+ const component = (0, property_1.mergeComponent)({
115
+ componentId,
116
+ getComponent: (componentId) => components === null || components === void 0 ? void 0 : components.find((i) => i.id === componentId),
117
+ locale,
118
+ defaultLocale,
119
+ properties: parameters,
120
+ });
121
+ if (!component)
122
+ return null;
123
+ return {
124
+ AsyncComponent: transpileAndLoadScript(component.script),
125
+ properties: component.properties,
126
+ };
196
127
  }
197
- function parsePropertyValue(type, value) {
198
- if (type === 'json') {
199
- try {
200
- return JSON.parse(value);
201
- }
202
- catch (error) {
203
- console.error('parse json value error', error);
204
- return undefined;
205
- }
206
- }
207
- return value;
128
+ function transpileAndLoadScript(script) {
129
+ return __awaiter(this, void 0, void 0, function* () {
130
+ const ts = yield Promise.resolve().then(() => __importStar(require('typescript')));
131
+ const compiled = ts.transpileModule(script, {
132
+ compilerOptions: { jsx: ts.JsxEmit.React, module: ts.ModuleKind.ESNext },
133
+ }).outputText;
134
+ const url = URL.createObjectURL(new Blob([compiled], { type: 'application/javascript' }));
135
+ return Promise.resolve(`${url}`).then(s => __importStar(require(s)));
136
+ });
208
137
  }
@@ -1,4 +1,18 @@
1
1
  "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
2
16
  var __importDefault = (this && this.__importDefault) || function (mod) {
3
17
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
18
  };
@@ -6,3 +20,4 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
20
  exports.CustomComponentRenderer = void 0;
7
21
  var CustomComponentRenderer_1 = require("./CustomComponentRenderer");
8
22
  Object.defineProperty(exports, "CustomComponentRenderer", { enumerable: true, get: function () { return __importDefault(CustomComponentRenderer_1).default; } });
23
+ __exportStar(require("./CustomComponentRenderer"), exports);
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,2 +1,20 @@
1
1
  "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
2
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./api"), exports);
18
+ __exportStar(require("./core"), exports);
19
+ __exportStar(require("./state"), exports);
20
+ __exportStar(require("./preload"), exports);
@@ -0,0 +1,4 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PreloadComponentsStateGlobalVariableName = void 0;
4
+ exports.PreloadComponentsStateGlobalVariableName = '__PAGES_KIT_PRELOAD_CUSTOM_COMPONENTS_STATE__';
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.injectPreloadComponents = void 0;
4
+ const types_1 = require("../types");
5
+ function injectPreloadComponents({ config, components }) {
6
+ const injectState = {
7
+ config,
8
+ components: Object.fromEntries(components.map(({ script, component, locales }) => [
9
+ component.id,
10
+ {
11
+ preloadComponentModuleName: script.umd.moduleName,
12
+ component,
13
+ locales,
14
+ },
15
+ ])),
16
+ };
17
+ if (!components.length)
18
+ return undefined;
19
+ return {
20
+ html: `\
21
+ ${components
22
+ .map((i) => `\
23
+ <script>
24
+ ${i.script.umd.script}
25
+ </script>
26
+ `)
27
+ .join('\n')}
28
+ <script>
29
+ var ${types_1.PreloadComponentsStateGlobalVariableName} = ${JSON.stringify(injectState)}
30
+ </script>
31
+ `,
32
+ };
33
+ }
34
+ exports.injectPreloadComponents = injectPreloadComponents;
@@ -0,0 +1,83 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.assignNullableFields = exports.parsePropertyValue = exports.mergeComponent = exports.componentUMDName = void 0;
4
+ function componentUMDName({ componentId }) {
5
+ return `PagesCustomComponent${componentId}`;
6
+ }
7
+ exports.componentUMDName = componentUMDName;
8
+ function mergeComponent({ componentId, getComponent, locale, defaultLocale, properties: parameters, }) {
9
+ var _a, _b, _c, _d, _e, _f, _g;
10
+ let component = getComponent(componentId);
11
+ if (!component)
12
+ return null;
13
+ let script;
14
+ const properties = Object.fromEntries(Object.entries(parameters !== null && parameters !== void 0 ? parameters : {}).map(([id, { value }]) => {
15
+ var _a, _b;
16
+ const property = (_b = (_a = component === null || component === void 0 ? void 0 : component.properties) === null || _a === void 0 ? void 0 : _a[id]) === null || _b === void 0 ? void 0 : _b.data;
17
+ if (!property)
18
+ return [];
19
+ return [property.key, parsePropertyValue(property.type, value)];
20
+ }));
21
+ while (component) {
22
+ assignNullableFields(properties, Object.fromEntries(Object.values((_a = component.properties) !== null && _a !== void 0 ? _a : {}).map(({ data }) => {
23
+ var _a, _b, _c, _d, _e;
24
+ return [
25
+ data.key,
26
+ parsePropertyValue(data.type, (_c = (_b = (_a = data.locales) === null || _a === void 0 ? void 0 : _a[locale]) === null || _b === void 0 ? void 0 : _b.defaultValue) !== null && _c !== void 0 ? _c : (_e = (_d = data.locales) === null || _d === void 0 ? void 0 : _d[defaultLocale]) === null || _e === void 0 ? void 0 : _e.defaultValue),
27
+ ];
28
+ })));
29
+ if (((_b = component.renderer) === null || _b === void 0 ? void 0 : _b.type) === 'react-component') {
30
+ script = (_c = component.renderer) === null || _c === void 0 ? void 0 : _c.script;
31
+ break;
32
+ }
33
+ if (((_d = component.renderer) === null || _d === void 0 ? void 0 : _d.type) === 'web-component') {
34
+ script = (_e = component.renderer) === null || _e === void 0 ? void 0 : _e.script;
35
+ break;
36
+ }
37
+ if (((_f = component.renderer) === null || _f === void 0 ? void 0 : _f.type) === 'custom-component' && component.renderer.componentId) {
38
+ const { componentId } = component.renderer;
39
+ const next = getComponent(componentId);
40
+ const nextParameters = component.renderer.properties;
41
+ assignNullableFields(properties, Object.fromEntries(Object.entries((_g = next === null || next === void 0 ? void 0 : next.properties) !== null && _g !== void 0 ? _g : {}).map(([id, { data }]) => {
42
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
43
+ const locales = (_a = nextParameters === null || nextParameters === void 0 ? void 0 : nextParameters[id]) === null || _a === void 0 ? void 0 : _a.locales;
44
+ return [
45
+ data.key,
46
+ parsePropertyValue(data.type, (_h = (_e = (_c = (_b = locales === null || locales === void 0 ? void 0 : locales[locale]) === null || _b === void 0 ? void 0 : _b.value) !== null && _c !== void 0 ? _c : (_d = locales === null || locales === void 0 ? void 0 : locales[defaultLocale]) === null || _d === void 0 ? void 0 : _d.value) !== null && _e !== void 0 ? _e : (_g = (_f = data.locales) === null || _f === void 0 ? void 0 : _f[locale]) === null || _g === void 0 ? void 0 : _g.defaultValue) !== null && _h !== void 0 ? _h : (_k = (_j = data.locales) === null || _j === void 0 ? void 0 : _j[defaultLocale]) === null || _k === void 0 ? void 0 : _k.defaultValue),
47
+ ];
48
+ })));
49
+ component = next;
50
+ }
51
+ else {
52
+ component = undefined;
53
+ }
54
+ }
55
+ if (!script)
56
+ return null;
57
+ return { script, properties };
58
+ }
59
+ exports.mergeComponent = mergeComponent;
60
+ function parsePropertyValue(type, value) {
61
+ if (type === 'json') {
62
+ if (!value)
63
+ return undefined;
64
+ try {
65
+ return JSON.parse(value);
66
+ }
67
+ catch (error) {
68
+ console.error('parse json value error', error);
69
+ return undefined;
70
+ }
71
+ }
72
+ return value;
73
+ }
74
+ exports.parsePropertyValue = parsePropertyValue;
75
+ function assignNullableFields(properties, newProperties) {
76
+ for (const [key, val] of Object.entries(newProperties)) {
77
+ if (properties[key] === undefined || properties[key] === null) {
78
+ properties[key] = val;
79
+ }
80
+ }
81
+ return properties;
82
+ }
83
+ exports.assignNullableFields = assignNullableFields;
@@ -8,13 +8,13 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  });
9
9
  };
10
10
  import { call } from '@blocklet/sdk/lib/component';
11
- export function getComponents({ mode, name } = {}) {
11
+ export function preloadComponents({ mode, name, id }) {
12
12
  return __awaiter(this, void 0, void 0, function* () {
13
13
  return call({
14
14
  name: 'pages-kit',
15
15
  method: 'GET',
16
- path: '/api/components',
17
- params: { name, mode },
16
+ path: '/api/components/preload',
17
+ params: { mode, id: Array.isArray(id) ? id.join(',') : id, name: Array.isArray(name) ? name.join(',') : name },
18
18
  }).then((res) => res.data);
19
19
  });
20
20
  }
@@ -10,10 +10,18 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
10
10
  import axios from 'axios';
11
11
  import { joinURL } from 'ufo';
12
12
  const PAGES_KIT_DID = 'z8iZiDFg3vkkrPwsiba1TLXy3H9XHzFERsP8o';
13
- export function getComponents({ mode, name } = {}) {
13
+ export function preloadComponents({ mode, name, id }) {
14
14
  var _a, _b;
15
15
  return __awaiter(this, void 0, void 0, function* () {
16
16
  const prefix = ((_b = (_a = window.blocklet) === null || _a === void 0 ? void 0 : _a.componentMountPoints.find((i) => i.did === PAGES_KIT_DID)) === null || _b === void 0 ? void 0 : _b.mountPoint) || '/';
17
- return axios.get(joinURL(prefix, '/api/components'), { params: { name, mode } }).then((res) => res.data);
17
+ return axios
18
+ .get(joinURL(prefix, '/api/components'), {
19
+ params: {
20
+ mode,
21
+ id: Array.isArray(id) ? id.join(',') : id,
22
+ name: Array.isArray(name) ? name.join(',') : name,
23
+ },
24
+ })
25
+ .then((res) => res.data);
18
26
  });
19
27
  }
@@ -7,18 +7,25 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
7
7
  step((generator = generator.apply(thisArg, _arguments || [])).next());
8
8
  });
9
9
  };
10
+ var __rest = (this && this.__rest) || function (s, e) {
11
+ var t = {};
12
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
13
+ t[p] = s[p];
14
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
15
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
16
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
17
+ t[p[i]] = s[p[i]];
18
+ }
19
+ return t;
20
+ };
10
21
  import { jsx as _jsx } from "react/jsx-runtime";
11
- import createCache from '@emotion/cache';
12
- import { CacheProvider } from '@emotion/react';
13
22
  import { Icon as IconifyIcon } from '@iconify/react';
14
- import { Alert, Box, Button, CssBaseline, Stack } from '@mui/material';
15
- import React, { useEffect } from 'react';
16
- import ReactDOM from 'react-dom/client';
23
+ import { Alert, Box, Button, Stack } from '@mui/material';
24
+ import React, { useEffect, useState } from 'react';
17
25
  import { ErrorBoundary } from 'react-error-boundary';
18
- import { useFrame } from 'react-frame-component';
19
26
  import { joinURL, withQuery } from 'ufo';
20
- import { create } from 'zustand';
21
- import { immer } from 'zustand/middleware/immer';
27
+ import { PreloadComponentsStateGlobalVariableName } from '../../types/preload';
28
+ import { assignNullableFields, mergeComponent, parsePropertyValue } from '../../utils/property';
22
29
  // inject global variables for web-component renderer
23
30
  const win = window;
24
31
  win.joinURL = joinURL;
@@ -28,152 +35,77 @@ win.Box = Box;
28
35
  win.Stack = Stack;
29
36
  win.Button = Button;
30
37
  win.IconifyIcon = IconifyIcon;
31
- const CUSTOM_COMPONENT_RENDERER = 'custom-component-renderer';
32
- const defineCustomElements = (win) => {
33
- if (!win.customElements.get(CUSTOM_COMPONENT_RENDERER)) {
34
- win.customElements.define(CUSTOM_COMPONENT_RENDERER, class CustomComponentRenderer extends win.HTMLElement {
35
- constructor() {
36
- super();
37
- this.root = this.attachShadow({ mode: 'open' });
38
- this.reactRoot = ReactDOM.createRoot(this.root);
39
- }
40
- static get observedAttributes() {
41
- return ['id', 'component-id', 'locale', 'default-locale'];
42
- }
43
- get componentId() {
44
- return this.getAttribute('component-id');
45
- }
46
- get locale() {
47
- return this.getAttribute('locale');
48
- }
49
- get defaultLocale() {
50
- return this.getAttribute('default-locale');
51
- }
52
- connectedCallback() {
53
- this.setup();
54
- }
55
- attributeChangedCallback() {
56
- this.setup();
57
- }
58
- setup() {
59
- const { subscribe } = customComponentState(this.componentId);
60
- const { subscribe: subscribeProperties } = customComponentProperties(this.id);
61
- subscribe(() => this.render());
62
- subscribeProperties(() => this.render());
63
- this.render();
64
- }
65
- get state() {
66
- return Object.assign(Object.assign({}, customComponentState(this.componentId).getState()), { properties: customComponentProperties(this.id).getState().properties });
67
- }
68
- render() {
69
- var _a, _b, _c, _d, _e;
70
- return __awaiter(this, void 0, void 0, function* () {
71
- try {
72
- const { component, properties: parameters } = this.state;
73
- if (!component)
74
- throw new Error('missing required component config');
75
- let script;
76
- let current = component;
77
- const properties = Object.fromEntries(Object.entries(parameters !== null && parameters !== void 0 ? parameters : {}).map(([id, { value }]) => {
78
- var _a, _b;
79
- const property = (_b = (_a = component.properties) === null || _a === void 0 ? void 0 : _a[id]) === null || _b === void 0 ? void 0 : _b.data;
80
- if (!property)
81
- return [];
82
- return [property.key, parsePropertyValue(property.type, value)];
83
- }));
84
- while (current) {
85
- mergeProperties(properties, Object.fromEntries(Object.values((_a = current.properties) !== null && _a !== void 0 ? _a : {}).map(({ data }) => {
86
- var _a, _b, _c, _d, _e;
87
- return [
88
- data.key,
89
- parsePropertyValue(data.type, (_c = (_b = (_a = data.locales) === null || _a === void 0 ? void 0 : _a[this.locale]) === null || _b === void 0 ? void 0 : _b.defaultValue) !== null && _c !== void 0 ? _c : (_e = (_d = data.locales) === null || _d === void 0 ? void 0 : _d[this.defaultLocale]) === null || _e === void 0 ? void 0 : _e.defaultValue),
90
- ];
91
- })));
92
- if (((_b = current.renderer) === null || _b === void 0 ? void 0 : _b.type) === 'web-component') {
93
- script = (_c = current.renderer) === null || _c === void 0 ? void 0 : _c.script;
94
- break;
95
- }
96
- if (((_d = current.renderer) === null || _d === void 0 ? void 0 : _d.type) === 'custom-component' && current.renderer.componentId) {
97
- const c = customComponentState(current.renderer.componentId).getState()
98
- .component;
99
- const rendererProperties = current.renderer.properties;
100
- mergeProperties(properties, Object.fromEntries(Object.entries((_e = c === null || c === void 0 ? void 0 : c.properties) !== null && _e !== void 0 ? _e : {}).map(([id, { data }]) => {
101
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
102
- const locales = (_a = rendererProperties === null || rendererProperties === void 0 ? void 0 : rendererProperties[id]) === null || _a === void 0 ? void 0 : _a.locales;
103
- return [
104
- data.key,
105
- parsePropertyValue(data.type, (_h = (_e = (_c = (_b = locales === null || locales === void 0 ? void 0 : locales[this.locale]) === null || _b === void 0 ? void 0 : _b.value) !== null && _c !== void 0 ? _c : (_d = locales === null || locales === void 0 ? void 0 : locales[this.defaultLocale]) === null || _d === void 0 ? void 0 : _d.value) !== null && _e !== void 0 ? _e : (_g = (_f = data.locales) === null || _f === void 0 ? void 0 : _f[this.locale]) === null || _g === void 0 ? void 0 : _g.defaultValue) !== null && _h !== void 0 ? _h : (_k = (_j = data.locales) === null || _j === void 0 ? void 0 : _j[this.defaultLocale]) === null || _k === void 0 ? void 0 : _k.defaultValue),
106
- ];
107
- })));
108
- current = c;
109
- }
110
- else {
111
- current = undefined;
112
- }
113
- }
114
- if (!script)
115
- throw new Error('Missing required script');
116
- const cache = createCache({
117
- key: 'css',
118
- prepend: true,
119
- container: this.root,
120
- });
121
- const url = URL.createObjectURL(new Blob([script], { type: 'application/javascript' }));
122
- const { default: C } = yield import(/* @vite-ignore */ url);
123
- this.reactRoot.render(_jsx(CacheProvider, { value: cache, children: _jsx(CssBaseline, { children: _jsx(C, Object.assign({}, properties)) }) }));
124
- }
125
- catch (error) {
126
- console.error(error);
127
- this.reactRoot.render(_jsx("div", {}));
128
- }
129
- });
130
- }
131
- });
38
+ export default function CustomComponentRenderer(_a) {
39
+ var props = __rest(_a, []);
40
+ return (_jsx(ErrorBoundary, { fallbackRender: E, children: _jsx(Renderer, Object.assign({}, props)) }));
41
+ }
42
+ function Renderer(props) {
43
+ const component = loadComponent(props);
44
+ if (!component)
45
+ return null;
46
+ const { Component, AsyncComponent, properties } = component;
47
+ if (Component) {
48
+ return _jsx(Component, Object.assign({}, properties));
132
49
  }
133
- };
134
- const CUSTOM_COMPONENT_STATE_MAP = {};
135
- const customComponentState = (id, initial) => {
136
- var _a;
137
- (_a = CUSTOM_COMPONENT_STATE_MAP[id]) !== null && _a !== void 0 ? _a : (CUSTOM_COMPONENT_STATE_MAP[id] = create()(immer(() => (Object.assign({}, initial)))));
138
- return CUSTOM_COMPONENT_STATE_MAP[id];
139
- };
140
- const CUSTOM_COMPONENT_PROPERTIES_MAP = {};
141
- const customComponentProperties = (id, initial) => {
142
- var _a;
143
- (_a = CUSTOM_COMPONENT_PROPERTIES_MAP[id]) !== null && _a !== void 0 ? _a : (CUSTOM_COMPONENT_PROPERTIES_MAP[id] = create()(immer(() => (Object.assign({}, initial)))));
144
- return CUSTOM_COMPONENT_PROPERTIES_MAP[id];
145
- };
146
- export default function CustomComponentRenderer({ id, componentId, components, properties, locale, defaultLocale, }) {
147
- const frame = useFrame();
148
- components === null || components === void 0 ? void 0 : components.forEach((component) => {
149
- customComponentState(component.id, { component }).setState({ component });
150
- });
151
- customComponentProperties(id, { properties }).setState({ properties });
50
+ if (AsyncComponent) {
51
+ return _jsx(AsyncComponentRenderer, { AsyncComponent: AsyncComponent, properties: properties });
52
+ }
53
+ return null;
54
+ }
55
+ function AsyncComponentRenderer({ AsyncComponent, properties, }) {
56
+ const [{ Component, error, key }, setState] = useState({});
152
57
  useEffect(() => {
153
- defineCustomElements(frame.window || window);
154
- }, [frame]);
155
- return (_jsx(ErrorBoundary, { fallbackRender: E, children: _jsx("custom-component-renderer", { "component-id": componentId, id: id, locale: locale, "default-locale": defaultLocale }) }));
58
+ AsyncComponent.then((m) => setState({ Component: m.default, key: Date.now() })).catch((error) => setState({ error, key: Date.now() }));
59
+ }, [AsyncComponent]);
60
+ if (error) {
61
+ return _jsx(Alert, { severity: "error", children: error.stack });
62
+ }
63
+ if (Component) {
64
+ return (_jsx(ErrorBoundary, { fallbackRender: E, resetKeys: [key], children: _jsx(Component, Object.assign({}, properties)) }));
65
+ }
66
+ return null;
156
67
  }
157
68
  function E({ error }) {
158
69
  return (_jsx(Box, { children: _jsx(Alert, { severity: "error", children: error.message }) }));
159
70
  }
160
- function mergeProperties(properties, newProperties) {
161
- for (const [key, val] of Object.entries(newProperties)) {
162
- if (properties[key] === undefined || properties[key] === null) {
163
- properties[key] = val;
164
- }
71
+ const PRELOAD_COMPONENTS_STATE = window[PreloadComponentsStateGlobalVariableName];
72
+ function loadComponent({ componentId, components, locale, defaultLocale, properties: parameters, }) {
73
+ var _a, _b, _c, _d, _e;
74
+ const preload = PRELOAD_COMPONENTS_STATE === null || PRELOAD_COMPONENTS_STATE === void 0 ? void 0 : PRELOAD_COMPONENTS_STATE.components[componentId];
75
+ if (preload) {
76
+ const properties = Object.fromEntries(Object.entries(parameters !== null && parameters !== void 0 ? parameters : {}).map(([id, { value }]) => {
77
+ var _a, _b;
78
+ const property = (_b = (_a = preload.component.properties) === null || _a === void 0 ? void 0 : _a[id]) === null || _b === void 0 ? void 0 : _b.data;
79
+ if (!property)
80
+ return [];
81
+ return [property.key, parsePropertyValue(property.type, value)];
82
+ }));
83
+ return {
84
+ Component: window[preload.preloadComponentModuleName],
85
+ properties: assignNullableFields(properties, (_c = (_b = (_a = preload.locales) === null || _a === void 0 ? void 0 : _a[locale]) === null || _b === void 0 ? void 0 : _b.properties) !== null && _c !== void 0 ? _c : (_e = (_d = preload.locales) === null || _d === void 0 ? void 0 : _d[PRELOAD_COMPONENTS_STATE.config.defaultLocale]) === null || _e === void 0 ? void 0 : _e.properties),
86
+ };
165
87
  }
166
- return properties;
88
+ const component = mergeComponent({
89
+ componentId,
90
+ getComponent: (componentId) => components === null || components === void 0 ? void 0 : components.find((i) => i.id === componentId),
91
+ locale,
92
+ defaultLocale,
93
+ properties: parameters,
94
+ });
95
+ if (!component)
96
+ return null;
97
+ return {
98
+ AsyncComponent: transpileAndLoadScript(component.script),
99
+ properties: component.properties,
100
+ };
167
101
  }
168
- function parsePropertyValue(type, value) {
169
- if (type === 'json') {
170
- try {
171
- return JSON.parse(value);
172
- }
173
- catch (error) {
174
- console.error('parse json value error', error);
175
- return undefined;
176
- }
177
- }
178
- return value;
102
+ function transpileAndLoadScript(script) {
103
+ return __awaiter(this, void 0, void 0, function* () {
104
+ const ts = yield import('typescript');
105
+ const compiled = ts.transpileModule(script, {
106
+ compilerOptions: { jsx: ts.JsxEmit.React, module: ts.ModuleKind.ESNext },
107
+ }).outputText;
108
+ const url = URL.createObjectURL(new Blob([compiled], { type: 'application/javascript' }));
109
+ return import(/* @vite-ignore */ url);
110
+ });
179
111
  }
@@ -1 +1,2 @@
1
1
  export { default as CustomComponentRenderer } from './CustomComponentRenderer';
2
+ export * from './CustomComponentRenderer';
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -1 +1,4 @@
1
- export {};
1
+ export * from './api';
2
+ export * from './core';
3
+ export * from './state';
4
+ export * from './preload';
@@ -0,0 +1 @@
1
+ export const PreloadComponentsStateGlobalVariableName = '__PAGES_KIT_PRELOAD_CUSTOM_COMPONENTS_STATE__';
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,30 @@
1
+ import { PreloadComponentsStateGlobalVariableName } from '../types';
2
+ export function injectPreloadComponents({ config, components }) {
3
+ const injectState = {
4
+ config,
5
+ components: Object.fromEntries(components.map(({ script, component, locales }) => [
6
+ component.id,
7
+ {
8
+ preloadComponentModuleName: script.umd.moduleName,
9
+ component,
10
+ locales,
11
+ },
12
+ ])),
13
+ };
14
+ if (!components.length)
15
+ return undefined;
16
+ return {
17
+ html: `\
18
+ ${components
19
+ .map((i) => `\
20
+ <script>
21
+ ${i.script.umd.script}
22
+ </script>
23
+ `)
24
+ .join('\n')}
25
+ <script>
26
+ var ${PreloadComponentsStateGlobalVariableName} = ${JSON.stringify(injectState)}
27
+ </script>
28
+ `,
29
+ };
30
+ }
@@ -0,0 +1,76 @@
1
+ export function componentUMDName({ componentId }) {
2
+ return `PagesCustomComponent${componentId}`;
3
+ }
4
+ export function mergeComponent({ componentId, getComponent, locale, defaultLocale, properties: parameters, }) {
5
+ var _a, _b, _c, _d, _e, _f, _g;
6
+ let component = getComponent(componentId);
7
+ if (!component)
8
+ return null;
9
+ let script;
10
+ const properties = Object.fromEntries(Object.entries(parameters !== null && parameters !== void 0 ? parameters : {}).map(([id, { value }]) => {
11
+ var _a, _b;
12
+ const property = (_b = (_a = component === null || component === void 0 ? void 0 : component.properties) === null || _a === void 0 ? void 0 : _a[id]) === null || _b === void 0 ? void 0 : _b.data;
13
+ if (!property)
14
+ return [];
15
+ return [property.key, parsePropertyValue(property.type, value)];
16
+ }));
17
+ while (component) {
18
+ assignNullableFields(properties, Object.fromEntries(Object.values((_a = component.properties) !== null && _a !== void 0 ? _a : {}).map(({ data }) => {
19
+ var _a, _b, _c, _d, _e;
20
+ return [
21
+ data.key,
22
+ parsePropertyValue(data.type, (_c = (_b = (_a = data.locales) === null || _a === void 0 ? void 0 : _a[locale]) === null || _b === void 0 ? void 0 : _b.defaultValue) !== null && _c !== void 0 ? _c : (_e = (_d = data.locales) === null || _d === void 0 ? void 0 : _d[defaultLocale]) === null || _e === void 0 ? void 0 : _e.defaultValue),
23
+ ];
24
+ })));
25
+ if (((_b = component.renderer) === null || _b === void 0 ? void 0 : _b.type) === 'react-component') {
26
+ script = (_c = component.renderer) === null || _c === void 0 ? void 0 : _c.script;
27
+ break;
28
+ }
29
+ if (((_d = component.renderer) === null || _d === void 0 ? void 0 : _d.type) === 'web-component') {
30
+ script = (_e = component.renderer) === null || _e === void 0 ? void 0 : _e.script;
31
+ break;
32
+ }
33
+ if (((_f = component.renderer) === null || _f === void 0 ? void 0 : _f.type) === 'custom-component' && component.renderer.componentId) {
34
+ const { componentId } = component.renderer;
35
+ const next = getComponent(componentId);
36
+ const nextParameters = component.renderer.properties;
37
+ assignNullableFields(properties, Object.fromEntries(Object.entries((_g = next === null || next === void 0 ? void 0 : next.properties) !== null && _g !== void 0 ? _g : {}).map(([id, { data }]) => {
38
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
39
+ const locales = (_a = nextParameters === null || nextParameters === void 0 ? void 0 : nextParameters[id]) === null || _a === void 0 ? void 0 : _a.locales;
40
+ return [
41
+ data.key,
42
+ parsePropertyValue(data.type, (_h = (_e = (_c = (_b = locales === null || locales === void 0 ? void 0 : locales[locale]) === null || _b === void 0 ? void 0 : _b.value) !== null && _c !== void 0 ? _c : (_d = locales === null || locales === void 0 ? void 0 : locales[defaultLocale]) === null || _d === void 0 ? void 0 : _d.value) !== null && _e !== void 0 ? _e : (_g = (_f = data.locales) === null || _f === void 0 ? void 0 : _f[locale]) === null || _g === void 0 ? void 0 : _g.defaultValue) !== null && _h !== void 0 ? _h : (_k = (_j = data.locales) === null || _j === void 0 ? void 0 : _j[defaultLocale]) === null || _k === void 0 ? void 0 : _k.defaultValue),
43
+ ];
44
+ })));
45
+ component = next;
46
+ }
47
+ else {
48
+ component = undefined;
49
+ }
50
+ }
51
+ if (!script)
52
+ return null;
53
+ return { script, properties };
54
+ }
55
+ export function parsePropertyValue(type, value) {
56
+ if (type === 'json') {
57
+ if (!value)
58
+ return undefined;
59
+ try {
60
+ return JSON.parse(value);
61
+ }
62
+ catch (error) {
63
+ console.error('parse json value error', error);
64
+ return undefined;
65
+ }
66
+ }
67
+ return value;
68
+ }
69
+ export function assignNullableFields(properties, newProperties) {
70
+ for (const [key, val] of Object.entries(newProperties)) {
71
+ if (properties[key] === undefined || properties[key] === null) {
72
+ properties[key] = val;
73
+ }
74
+ }
75
+ return properties;
76
+ }
@@ -1,10 +1,2 @@
1
- import { CustomComponent, StateMode } from '../types';
2
- export declare function getComponents({ mode, name }?: {
3
- mode?: StateMode;
4
- name?: string;
5
- }): Promise<{
6
- config: {
7
- defaultLocale?: string;
8
- };
9
- components: CustomComponent[];
10
- }>;
1
+ import { PreloadComponentQuery, PreloadComponentsResult } from '../types';
2
+ export declare function preloadComponents({ mode, name, id }: PreloadComponentQuery): Promise<PreloadComponentsResult>;
@@ -1,10 +1,2 @@
1
- import { CustomComponent, StateMode } from '../types';
2
- export declare function getComponents({ mode, name }?: {
3
- mode?: StateMode;
4
- name?: string;
5
- }): Promise<{
6
- config: {
7
- defaultLocale?: string;
8
- };
9
- components: CustomComponent[];
10
- }>;
1
+ import { PreloadComponentQuery, PreloadComponentsResult } from '../types';
2
+ export declare function preloadComponents({ mode, name, id }: PreloadComponentQuery): Promise<PreloadComponentsResult>;
@@ -1,13 +1,11 @@
1
- import { CustomComponent } from '../../types';
2
- export default function CustomComponentRenderer({ id, componentId, components, properties, locale, defaultLocale, }: {
3
- id: string;
1
+ import { CustomComponent } from '../../types/core';
2
+ export interface CustomComponentRendererProps {
4
3
  componentId: string;
4
+ locale?: string;
5
+ defaultLocale?: string;
5
6
  components?: CustomComponent[];
6
7
  properties?: {
7
- [key: string]: {
8
- value: any;
9
- };
8
+ [key: string]: any;
10
9
  };
11
- locale?: string;
12
- defaultLocale?: string;
13
- }): import("react/jsx-runtime").JSX.Element;
10
+ }
11
+ export default function CustomComponentRenderer({ ...props }: CustomComponentRendererProps): import("react/jsx-runtime").JSX.Element;
@@ -1 +1,2 @@
1
1
  export { default as CustomComponentRenderer } from './CustomComponentRenderer';
2
+ export * from './CustomComponentRenderer';
@@ -0,0 +1,28 @@
1
+ import { CustomComponent } from './core';
2
+ import { StateMode } from './state';
3
+ export interface PreloadComponentQuery {
4
+ mode?: StateMode;
5
+ name?: string[] | string;
6
+ id?: string[] | string;
7
+ }
8
+ export interface PreloadComponentsResult {
9
+ config: {
10
+ defaultLocale?: string;
11
+ };
12
+ components: {
13
+ component: CustomComponent;
14
+ script: {
15
+ umd: {
16
+ script: string;
17
+ moduleName: string;
18
+ };
19
+ };
20
+ locales: {
21
+ [locale: string]: {
22
+ properties: {
23
+ [key: string]: any;
24
+ };
25
+ };
26
+ };
27
+ }[];
28
+ }
@@ -0,0 +1,42 @@
1
+ export type CustomComponent = {
2
+ id: string;
3
+ createdAt: string;
4
+ updatedAt: string;
5
+ name?: string;
6
+ renderer?: {
7
+ type: 'web-component';
8
+ script?: string;
9
+ } | {
10
+ type: 'react-component';
11
+ script?: string;
12
+ } | {
13
+ type: 'custom-component';
14
+ componentId?: string;
15
+ properties?: {
16
+ [id: string]: {
17
+ locales?: {
18
+ [locale: string]: {
19
+ value?: any;
20
+ };
21
+ };
22
+ };
23
+ };
24
+ };
25
+ properties?: {
26
+ [id: string]: {
27
+ index: number;
28
+ data: {
29
+ id: string;
30
+ key?: string;
31
+ type?: 'string' | 'number' | 'boolean' | 'json' | 'custom-component' | 'url';
32
+ multiline?: boolean;
33
+ locales?: {
34
+ [locale: string]: {
35
+ name?: string;
36
+ defaultValue?: any;
37
+ };
38
+ };
39
+ };
40
+ };
41
+ };
42
+ };
@@ -1,43 +1,4 @@
1
- export type StateMode = 'draft' | 'staging' | 'production';
2
- export type CustomComponent = {
3
- id: string;
4
- createdAt: string;
5
- updatedAt: string;
6
- name?: string;
7
- renderer?: {
8
- type: 'web-component';
9
- script?: string;
10
- source?: {
11
- script?: string;
12
- };
13
- } | {
14
- type: 'custom-component';
15
- componentId?: string;
16
- properties?: {
17
- [id: string]: {
18
- locales?: {
19
- [locale: string]: {
20
- value?: any;
21
- };
22
- };
23
- };
24
- };
25
- };
26
- properties?: {
27
- [id: string]: {
28
- index: number;
29
- data: {
30
- id: string;
31
- key?: string;
32
- type?: 'string' | 'number' | 'boolean' | 'json' | 'custom-component' | 'url';
33
- multiline?: boolean;
34
- locales?: {
35
- [locale: string]: {
36
- name?: string;
37
- defaultValue?: any;
38
- };
39
- };
40
- };
41
- };
42
- };
43
- };
1
+ export * from './api';
2
+ export * from './core';
3
+ export * from './state';
4
+ export * from './preload';
@@ -0,0 +1,21 @@
1
+ import { CustomComponent } from './core';
2
+ export declare const PreloadComponentsStateGlobalVariableName = "__PAGES_KIT_PRELOAD_CUSTOM_COMPONENTS_STATE__";
3
+ export interface PreloadComponent {
4
+ preloadComponentModuleName: string;
5
+ component: CustomComponent;
6
+ locales?: {
7
+ [locale: string]: {
8
+ properties?: {
9
+ [key: string]: any;
10
+ };
11
+ };
12
+ };
13
+ }
14
+ export interface PreloadComponentsState {
15
+ config: {
16
+ defaultLocale?: string;
17
+ };
18
+ components: {
19
+ [id: string]: PreloadComponent;
20
+ };
21
+ }
@@ -0,0 +1 @@
1
+ export type StateMode = 'draft' | 'staging' | 'production';
@@ -0,0 +1,4 @@
1
+ import { PreloadComponentsResult } from '../types';
2
+ export declare function injectPreloadComponents({ config, components }: PreloadComponentsResult): {
3
+ html: string;
4
+ } | undefined;
@@ -0,0 +1,22 @@
1
+ import { CustomComponent } from '../types/core';
2
+ export declare function componentUMDName({ componentId }: {
3
+ componentId: string;
4
+ }): string;
5
+ export declare function mergeComponent({ componentId, getComponent, locale, defaultLocale, properties: parameters, }: {
6
+ componentId: string;
7
+ getComponent: (componentId: string) => CustomComponent | null | undefined;
8
+ locale?: string;
9
+ defaultLocale?: string;
10
+ properties?: {
11
+ [id: string]: {
12
+ value: any;
13
+ };
14
+ };
15
+ }): {
16
+ script: string;
17
+ properties: {
18
+ [key: string]: any;
19
+ };
20
+ } | null;
21
+ export declare function parsePropertyValue(type: NonNullable<CustomComponent['properties']>[string]['data']['type'], value: any): any;
22
+ export declare function assignNullableFields(properties: any, newProperties: any): any;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@blocklet/pages-kit",
3
- "version": "0.2.203",
3
+ "version": "0.2.205",
4
4
  "description": "Pages Kit components and utils",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -31,6 +31,10 @@
31
31
  "./types": {
32
32
  "import": "./lib/esm/types/index.js",
33
33
  "require": "./lib/cjs/types/index.js"
34
+ },
35
+ "./utils/*": {
36
+ "import": "./lib/esm/utils/*.js",
37
+ "require": "./lib/cjs/utils/*.js"
34
38
  }
35
39
  },
36
40
  "typesVersions": {
@@ -50,6 +54,10 @@
50
54
  ],
51
55
  "types": [
52
56
  "./lib/types/types/index.d.ts"
57
+ ],
58
+ "utils/*": [
59
+ "./lib/types/utils/*.d.ts",
60
+ "./lib/types/utils/*/index.d.ts"
53
61
  ]
54
62
  }
55
63
  },
@@ -81,7 +89,11 @@
81
89
  "axios": "^1.6.5",
82
90
  "lodash": "^4.17.21",
83
91
  "nanoid": "^3.3.7",
84
- "ufo": "^1.3.2"
92
+ "react-error-boundary": "^4.0.12",
93
+ "react-frame-component": "^5.2.6",
94
+ "typescript": "^5.3.3",
95
+ "ufo": "^1.3.2",
96
+ "zustand": "^4.4.7"
85
97
  },
86
98
  "peerDependencies": {
87
99
  "@arcblock/ux": "^2.8.17",
@@ -94,10 +106,7 @@
94
106
  "axios": "^1.6.5",
95
107
  "react": "^18.2.0",
96
108
  "react-dom": "^18.2.0",
97
- "react-error-boundary": "^4.0.12",
98
- "react-frame-component": "^5.2.6",
99
- "react-router-dom": "^6.16.0",
100
- "zustand": "^4.4.7"
109
+ "react-router-dom": "^6.16.0"
101
110
  },
102
111
  "devDependencies": {
103
112
  "@arcblock/ux": "^2.9.13",
@@ -115,11 +124,7 @@
115
124
  "npm-run-all": "^4.1.5",
116
125
  "react": "^18.2.0",
117
126
  "react-dom": "^18.2.0",
118
- "react-error-boundary": "^4.0.12",
119
- "react-frame-component": "^5.2.6",
120
127
  "react-router-dom": "^6.21.2",
121
- "rimraf": "^5.0.5",
122
- "typescript": "^5.3.3",
123
- "zustand": "^4.4.7"
128
+ "rimraf": "^5.0.5"
124
129
  }
125
130
  }