@blocklet/pages-kit 0.2.222 → 0.2.224

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.
@@ -11,18 +11,13 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.preloadComponents = void 0;
13
13
  const component_1 = require("@blocklet/sdk/lib/component");
14
- function preloadComponents({ mode, name, id, locale, }) {
14
+ function preloadComponents(input) {
15
15
  return __awaiter(this, void 0, void 0, function* () {
16
16
  return (0, component_1.call)({
17
17
  name: 'pages-kit',
18
- method: 'GET',
18
+ method: 'POST',
19
19
  path: '/api/components/preload',
20
- params: {
21
- mode,
22
- id: Array.isArray(id) ? id.join(',') : id,
23
- name: Array.isArray(name) ? name.join(',') : name,
24
- locale,
25
- },
20
+ data: input,
26
21
  }).then((res) => res.data);
27
22
  });
28
23
  }
@@ -16,20 +16,11 @@ 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 preloadComponents({ mode, name, id, locale, }) {
19
+ function preloadComponents(input) {
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
24
- .get((0, ufo_1.joinURL)(prefix, '/api/components/preload'), {
25
- params: {
26
- mode,
27
- id: Array.isArray(id) ? id.join(',') : id,
28
- name: Array.isArray(name) ? name.join(',') : name,
29
- locale,
30
- },
31
- })
32
- .then((res) => res.data);
23
+ return axios_1.default.post((0, ufo_1.joinURL)(prefix, '/api/components/preload'), input).then((res) => res.data);
33
24
  });
34
25
  }
35
26
  exports.preloadComponents = preloadComponents;
@@ -0,0 +1,17 @@
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
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("react-wrap-balancer"), exports);
@@ -10,26 +10,8 @@ var __createBinding = (this && this.__createBinding) || (Object.create ? (functi
10
10
  if (k2 === undefined) k2 = k;
11
11
  o[k2] = m[k];
12
12
  }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || function (mod) {
19
- if (mod && mod.__esModule) return mod;
20
- var result = {};
21
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
- __setModuleDefault(result, mod);
23
- return result;
24
- };
25
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
26
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
27
- return new (P || (P = Promise))(function (resolve, reject) {
28
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
29
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
30
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
31
- step((generator = generator.apply(thisArg, _arguments || [])).next());
32
- });
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);
33
15
  };
34
16
  var __rest = (this && this.__rest) || function (s, e) {
35
17
  var t = {};
@@ -43,175 +25,52 @@ var __rest = (this && this.__rest) || function (s, e) {
43
25
  return t;
44
26
  };
45
27
  Object.defineProperty(exports, "__esModule", { value: true });
46
- exports.transpileAndLoadScript = void 0;
47
28
  const jsx_runtime_1 = require("react/jsx-runtime");
48
29
  require("../../utils/inject-global-components");
49
30
  const material_1 = require("@mui/material");
50
- const lodash_1 = require("lodash");
51
- const react_1 = require("react");
52
31
  const react_error_boundary_1 = require("react-error-boundary");
53
- const api_1 = require("../../api");
54
- const preload_1 = require("../../types/preload");
55
32
  const property_1 = require("../../utils/property");
33
+ const state_1 = require("./state");
34
+ __exportStar(require("./state"), exports);
56
35
  const MAXIMUM_RENDER_STACK_SIZE = 20;
57
36
  function CustomComponentRenderer(_a) {
58
37
  var _b;
59
- var { defaultLocale = PRELOAD_COMPONENTS_STATE === null || PRELOAD_COMPONENTS_STATE === void 0 ? void 0 : PRELOAD_COMPONENTS_STATE.config.defaultLocale } = _a, props = __rest(_a, ["defaultLocale"]);
60
- return ((0, jsx_runtime_1.jsx)(react_error_boundary_1.ErrorBoundary, { fallbackRender: E, resetKeys: [Date.now()], children: (0, jsx_runtime_1.jsx)(ComponentRenderer, Object.assign({}, props, { defaultLocale: defaultLocale, instanceId: (_b = props.instanceId) !== null && _b !== void 0 ? _b : props.componentId })) }));
38
+ var props = __rest(_a, []);
39
+ return ((0, jsx_runtime_1.jsx)(react_error_boundary_1.ErrorBoundary, { fallbackRender: ErrorView, resetKeys: [Date.now()], children: (0, jsx_runtime_1.jsx)(ComponentRenderer, Object.assign({}, props, { instanceId: (_b = props.instanceId) !== null && _b !== void 0 ? _b : props.componentId })) }));
61
40
  }
62
41
  exports.default = CustomComponentRenderer;
42
+ function ErrorView({ error }) {
43
+ return ((0, jsx_runtime_1.jsx)(material_1.Box, { children: (0, jsx_runtime_1.jsx)(material_1.Alert, { severity: "error", children: error.message }) }));
44
+ }
63
45
  function ComponentRenderer(_a) {
46
+ var _b;
64
47
  var { renderCount = 0 } = _a, props = __rest(_a, ["renderCount"]);
65
48
  if (renderCount > MAXIMUM_RENDER_STACK_SIZE) {
66
49
  throw new Error('Maximum render stack size exceeded');
67
50
  }
68
- return (0, jsx_runtime_1.jsx)(Renderer, Object.assign({ renderCount: renderCount + 1 }, props));
51
+ if (!((_b = props.dev) === null || _b === void 0 ? void 0 : _b.components)) {
52
+ return (0, jsx_runtime_1.jsx)(PreloadRenderer, Object.assign({ renderCount: renderCount + 1 }, props));
53
+ }
54
+ return (0, jsx_runtime_1.jsx)(DebugRenderer, Object.assign({ renderCount: renderCount + 1 }, props));
69
55
  }
70
- function Renderer(props) {
71
- const component = useComponent(props);
56
+ function PreloadRenderer(props) {
57
+ const component = (0, state_1.usePreloadComponent)(props);
72
58
  if (!component)
73
59
  return null;
74
- const { Component, AsyncComponent, properties } = component;
75
- const componentProps = Object.fromEntries(Object.entries(Object.assign(Object.assign({}, properties), props.props)).map(([key, val]) => {
76
- return [
77
- key,
78
- (val === null || val === void 0 ? void 0 : val.type) === property_1.RenderNestedComponent ? ((0, jsx_runtime_1.jsx)(ComponentRenderer, Object.assign({}, (0, lodash_1.pick)(props, 'components', 'locale', 'defaultLocale'), { instanceId: key, renderCount: props.renderCount, componentId: val.componentId, properties: val.properties, props: val.props }))) : (val),
79
- ];
80
- }));
81
- if (Component) {
82
- return (0, jsx_runtime_1.jsx)(Component, Object.assign({ locale: props.locale }, componentProps));
83
- }
84
- if (AsyncComponent) {
85
- return (0, jsx_runtime_1.jsx)(AsyncComponentRenderer, Object.assign({ AsyncComponent: AsyncComponent, locale: props.locale }, componentProps));
86
- }
87
- return null;
88
- }
89
- function AsyncComponentRenderer(_a) {
90
- var { AsyncComponent } = _a, props = __rest(_a, ["AsyncComponent"]);
91
- const [{ Component, error }, setState] = (0, react_1.useState)({});
92
- (0, react_1.useEffect)(() => {
93
- AsyncComponent.then((m) => setState({ Component: m.default, key: Date.now() })).catch((error) => setState({ error }));
94
- }, [AsyncComponent]);
95
- if (error) {
96
- return (0, jsx_runtime_1.jsx)(material_1.Alert, { severity: "error", children: error.stack });
97
- }
98
- if (Component) {
99
- return (0, jsx_runtime_1.jsx)(Component, Object.assign({}, props));
100
- }
101
- return null;
102
- }
103
- function E({ error }) {
104
- return ((0, jsx_runtime_1.jsx)(material_1.Box, { children: (0, jsx_runtime_1.jsx)(material_1.Alert, { severity: "error", children: error.message }) }));
105
- }
106
- let PRELOAD_COMPONENTS_STATE = window[preload_1.PreloadComponentsStateGlobalVariableName];
107
- let PRELOAD_COMPONENTS_STATE_KEY = 0;
108
- const COMPONENTS_MAP = {};
109
- function getScriptComponent(componentId) {
110
- if (!COMPONENTS_MAP[componentId]) {
111
- const preload = PRELOAD_COMPONENTS_STATE === null || PRELOAD_COMPONENTS_STATE === void 0 ? void 0 : PRELOAD_COMPONENTS_STATE.components[componentId];
112
- if (preload) {
113
- const mod = window[preload.preloadComponentModuleName];
114
- if (typeof mod === 'function') {
115
- COMPONENTS_MAP[componentId] = { component: preload.component, Component: mod() };
116
- }
117
- }
118
- }
119
- return COMPONENTS_MAP[componentId];
60
+ return (0, jsx_runtime_1.jsx)(Renderer, Object.assign({}, props, { Component: component.Component, props: Object.assign(Object.assign({}, component.props), props.props) }));
120
61
  }
121
- const LOAD_REMOTE_COMPONENT_TASKS = {};
122
- function loadRemoteComponent({ mode, instanceId, componentId, locale, }) {
123
- var _a;
124
- const key = [mode, instanceId, locale].join('-');
125
- (_a = LOAD_REMOTE_COMPONENT_TASKS[key]) !== null && _a !== void 0 ? _a : (LOAD_REMOTE_COMPONENT_TASKS[key] = (0, api_1.preloadComponents)({ mode, id: componentId, locale })
126
- .then((result) => {
127
- var _a, _b, _c, _d, _e, _f;
128
- var _g, _h, _j, _k;
129
- PRELOAD_COMPONENTS_STATE !== null && PRELOAD_COMPONENTS_STATE !== void 0 ? PRELOAD_COMPONENTS_STATE : (PRELOAD_COMPONENTS_STATE = { config: result.config, components: {}, props: {} });
130
- for (const i of Object.values(result.components)) {
131
- if (!window[i.script.umd.moduleName]) {
132
- const script = document.createElement('script');
133
- script.textContent = i.script.umd.script;
134
- document.body.appendChild(script);
135
- }
136
- PRELOAD_COMPONENTS_STATE.components[i.component.id] = {
137
- preloadComponentModuleName: i.script.umd.moduleName,
138
- component: i.component,
139
- };
140
- }
141
- (_a = (_g = PRELOAD_COMPONENTS_STATE.props)[instanceId]) !== null && _a !== void 0 ? _a : (_g[instanceId] = {});
142
- (_b = (_h = PRELOAD_COMPONENTS_STATE.props[instanceId]).locales) !== null && _b !== void 0 ? _b : (_h.locales = {});
143
- (_c = (_j = PRELOAD_COMPONENTS_STATE.props[instanceId].locales)[_k = locale]) !== null && _c !== void 0 ? _c : (_j[_k] = {});
144
- PRELOAD_COMPONENTS_STATE.props[instanceId].locales[locale].props =
145
- (_f = (_e = (_d = result.props[componentId]) === null || _d === void 0 ? void 0 : _d.locales) === null || _e === void 0 ? void 0 : _e[locale]) === null || _f === void 0 ? void 0 : _f.props;
146
- PRELOAD_COMPONENTS_STATE_KEY += 1;
147
- })
148
- .catch((error) => {
149
- console.error('load remote component error', error);
150
- LOAD_REMOTE_COMPONENT_TASKS[key] = undefined;
151
- }));
152
- return LOAD_REMOTE_COMPONENT_TASKS[key];
153
- }
154
- function useComponent({ mode, instanceId, componentId, components, locale, defaultLocale, properties: parameters, }) {
155
- var _a;
156
- const scriptRef = (0, react_1.useRef)({});
157
- const preloadComponent = (0, react_1.useMemo)(() => getScriptComponent(componentId), [componentId, PRELOAD_COMPONENTS_STATE_KEY]);
158
- const preloadState = (0, react_1.useMemo)(() => {
159
- var _a, _b;
160
- return (_b = (_a = PRELOAD_COMPONENTS_STATE === null || PRELOAD_COMPONENTS_STATE === void 0 ? void 0 : PRELOAD_COMPONENTS_STATE.props[instanceId]) === null || _a === void 0 ? void 0 : _a.locales) === null || _b === void 0 ? void 0 : _b[locale || defaultLocale];
161
- }, [instanceId, locale, defaultLocale, PRELOAD_COMPONENTS_STATE_KEY]);
162
- const preloadDefaultState = (0, react_1.useMemo)(() => {
163
- var _a, _b;
164
- return (_b = (_a = PRELOAD_COMPONENTS_STATE === null || PRELOAD_COMPONENTS_STATE === void 0 ? void 0 : PRELOAD_COMPONENTS_STATE.props[instanceId]) === null || _a === void 0 ? void 0 : _a.locales) === null || _b === void 0 ? void 0 : _b[defaultLocale];
165
- }, [instanceId, defaultLocale, PRELOAD_COMPONENTS_STATE_KEY]);
166
- const previousPreload = (0, react_1.useRef)();
167
- const [, update] = (0, react_1.useState)(0);
168
- if (!(components === null || components === void 0 ? void 0 : components.length) && !preloadState && preloadComponent) {
169
- loadRemoteComponent({ mode, instanceId, componentId, locale: locale || defaultLocale }).then(() => {
170
- update(PRELOAD_COMPONENTS_STATE_KEY);
171
- });
172
- if (previousPreload.current) {
173
- return previousPreload.current;
174
- }
175
- }
176
- if ((preloadState || preloadDefaultState) && preloadComponent) {
177
- previousPreload.current = {
178
- Component: preloadComponent.Component,
179
- properties: (_a = preloadState === null || preloadState === void 0 ? void 0 : preloadState.props) !== null && _a !== void 0 ? _a : preloadDefaultState === null || preloadDefaultState === void 0 ? void 0 : preloadDefaultState.props,
180
- };
181
- return previousPreload.current;
182
- }
183
- const component = (0, property_1.mergeComponent)({
184
- componentId,
185
- getComponent: (componentId) => components === null || components === void 0 ? void 0 : components.find((i) => i.id === componentId),
186
- locale,
187
- defaultLocale,
188
- properties: parameters,
189
- });
62
+ function DebugRenderer(props) {
63
+ const component = (0, state_1.useTranspileComponent)(props);
190
64
  if (!component)
191
65
  return null;
192
- if (scriptRef.current.script !== component.script) {
193
- scriptRef.current.script = component.script;
194
- scriptRef.current.module = transpileAndLoadScript(component.script);
195
- }
196
- return {
197
- AsyncComponent: scriptRef.current.module,
198
- properties: component.properties,
199
- };
66
+ return (0, jsx_runtime_1.jsx)(Renderer, Object.assign({}, props, { Component: component.Component, props: Object.assign(Object.assign({}, component.props), props.props) }));
200
67
  }
201
- function transpileAndLoadScript(script) {
202
- return __awaiter(this, void 0, void 0, function* () {
203
- const [ts, { createBuiltinModuleTransformer }] = yield Promise.all([
204
- Promise.resolve().then(() => __importStar(require('typescript'))),
205
- Promise.resolve().then(() => __importStar(require('../../utils/typescript/builtin-module-transformer'))),
206
- ]);
207
- const compiled = ts.transpileModule(script, {
208
- compilerOptions: { jsx: ts.JsxEmit.React, module: ts.ModuleKind.ESNext, target: ts.ScriptTarget.ES2016 },
209
- transformers: {
210
- before: [createBuiltinModuleTransformer(ts)],
211
- },
212
- }).outputText;
213
- const url = URL.createObjectURL(new Blob([compiled], { type: 'application/javascript' }));
214
- return Promise.resolve(`${url}`).then(s => __importStar(require(s)));
215
- });
68
+ function Renderer({ renderCount, Component, locale, props, dev, }) {
69
+ const componentProps = Object.fromEntries(Object.entries(props).map(([key, val]) => {
70
+ return [
71
+ key,
72
+ (val === null || val === void 0 ? void 0 : val.type) === property_1.RenderNestedComponent ? ((0, jsx_runtime_1.jsx)(ComponentRenderer, { locale: locale, renderCount: renderCount, componentId: val.componentId, properties: val.properties, props: val.props, dev: dev })) : (val),
73
+ ];
74
+ }));
75
+ return (0, jsx_runtime_1.jsx)(Component, Object.assign({ locale: locale }, componentProps));
216
76
  }
217
- exports.transpileAndLoadScript = transpileAndLoadScript;
@@ -0,0 +1,203 @@
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 __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
26
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
27
+ return new (P || (P = Promise))(function (resolve, reject) {
28
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
29
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
30
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
31
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
32
+ });
33
+ };
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;
44
+ };
45
+ Object.defineProperty(exports, "__esModule", { value: true });
46
+ exports.useTranspileComponent = exports.usePreloadComponent = exports.customComponentStates = void 0;
47
+ const react_1 = require("react");
48
+ const zustand_1 = require("zustand");
49
+ const immer_1 = require("zustand/middleware/immer");
50
+ const api_1 = require("../../api");
51
+ const types_1 = require("../../types");
52
+ const preload_1 = require("../../types/preload");
53
+ const property_1 = require("../../utils/property");
54
+ const PRELOAD_COMPONENTS_STATE = window[preload_1.PreloadComponentsStateGlobalVariableName];
55
+ exports.customComponentStates = (0, zustand_1.create)()((0, immer_1.immer)((set, get) => ({
56
+ state: {
57
+ config: Object.assign({}, PRELOAD_COMPONENTS_STATE === null || PRELOAD_COMPONENTS_STATE === void 0 ? void 0 : PRELOAD_COMPONENTS_STATE.config),
58
+ components: Object.fromEntries(Object.entries(Object.assign({}, PRELOAD_COMPONENTS_STATE === null || PRELOAD_COMPONENTS_STATE === void 0 ? void 0 : PRELOAD_COMPONENTS_STATE.components)).map(([componentId, preload]) => {
59
+ let Component;
60
+ if (preload.componentModuleGlobalVariable) {
61
+ const m = window[preload.componentModuleGlobalVariable];
62
+ if (typeof m === 'function')
63
+ Component = m();
64
+ }
65
+ return [componentId, Object.assign(Object.assign({}, preload), { Component })];
66
+ })),
67
+ instances: Object.assign({}, PRELOAD_COMPONENTS_STATE === null || PRELOAD_COMPONENTS_STATE === void 0 ? void 0 : PRELOAD_COMPONENTS_STATE.instances),
68
+ },
69
+ getComponent({ instanceId, componentId, locale }) {
70
+ var _a, _b;
71
+ const { config: { supportedLocales }, components, instances, } = get().state;
72
+ if (!(supportedLocales === null || supportedLocales === void 0 ? void 0 : supportedLocales.some((i) => i.locale === locale)))
73
+ return null;
74
+ const c = components[componentId];
75
+ if (!c)
76
+ return null;
77
+ const { component, Component } = c;
78
+ if (!Component)
79
+ return null;
80
+ if (!instanceId) {
81
+ return { component, Component };
82
+ }
83
+ const instance = instances[instanceId];
84
+ if (!instance)
85
+ return null;
86
+ const props = (_b = (_a = instance.locales) === null || _a === void 0 ? void 0 : _a[locale]) === null || _b === void 0 ? void 0 : _b.props;
87
+ if (!props)
88
+ return null;
89
+ return { component, Component, props };
90
+ },
91
+ loadComponents(input) {
92
+ return __awaiter(this, void 0, void 0, function* () {
93
+ const result = yield loadComponents(input);
94
+ if (result) {
95
+ set((s) => {
96
+ s.state.config = result.config;
97
+ Object.assign(s.state.components, result.components);
98
+ Object.entries(result.instances).forEach(([instanceId, instance]) => {
99
+ var _a, _b;
100
+ var _c, _d;
101
+ (_a = (_c = s.state.instances)[instanceId]) !== null && _a !== void 0 ? _a : (_c[instanceId] = { componentId: instance.componentId });
102
+ (_b = (_d = s.state.instances[instanceId]).locales) !== null && _b !== void 0 ? _b : (_d.locales = {});
103
+ Object.assign(s.state.instances[instanceId].locales, instance.locales);
104
+ });
105
+ });
106
+ }
107
+ });
108
+ },
109
+ })));
110
+ function loadComponents(input) {
111
+ return __awaiter(this, void 0, void 0, function* () {
112
+ const result = yield (0, api_1.preloadComponents)(Object.assign(Object.assign({}, input), { module: types_1.PreloadComponentScriptModule.ESM }));
113
+ if (!result)
114
+ return null;
115
+ const components = Object.fromEntries(yield Promise.all(Object.values(result.components).map((item) => __awaiter(this, void 0, void 0, function* () {
116
+ if (item.script.module !== types_1.PreloadComponentScriptModule.ESM) {
117
+ throw new Error('received non esm component script');
118
+ }
119
+ const url = URL.createObjectURL(new Blob([item.script.script], { type: 'application/javascript' }));
120
+ const m = yield Promise.resolve(`${url}`).then(s => __importStar(require(s)));
121
+ if (typeof (m === null || m === void 0 ? void 0 : m.default) !== 'function') {
122
+ throw new Error(`Invalid custom component module: ${typeof (m === null || m === void 0 ? void 0 : m.default)}`);
123
+ }
124
+ return [
125
+ item.component.id,
126
+ {
127
+ component: item.component,
128
+ Component: m.default,
129
+ },
130
+ ];
131
+ }))));
132
+ return Object.assign(Object.assign({}, result), { components, instances: Object.fromEntries(result.instances.map((_a) => {
133
+ var { id } = _a, instance = __rest(_a, ["id"]);
134
+ return [id, instance];
135
+ })) });
136
+ });
137
+ }
138
+ const COMPONENT_LOADER_MAP = {};
139
+ function usePreloadComponent({ instanceId, componentId, properties: parameters, locale: requestLocale, dev: { mode } = {}, }) {
140
+ var _a;
141
+ const previousRef = (0, react_1.useRef)();
142
+ const locale = (0, exports.customComponentStates)((s) => { var _a; return ((_a = s.state.config.supportedLocales) === null || _a === void 0 ? void 0 : _a.some((i) => i.locale === requestLocale)) ? requestLocale : undefined; });
143
+ const defaultLocale = (0, exports.customComponentStates)((s) => s.state.config.defaultLocale);
144
+ const realLocale = locale || defaultLocale;
145
+ const result = (0, exports.customComponentStates)((s) => realLocale ? s.getComponent({ instanceId, componentId, locale: realLocale }) : undefined);
146
+ if (result) {
147
+ previousRef.current = result;
148
+ return result;
149
+ }
150
+ if (realLocale && instanceId) {
151
+ const key = [instanceId, realLocale].join('-');
152
+ (_a = COMPONENT_LOADER_MAP[key]) !== null && _a !== void 0 ? _a : (COMPONENT_LOADER_MAP[key] = exports.customComponentStates.getState().loadComponents({
153
+ mode,
154
+ locale: realLocale,
155
+ instances: [{ id: instanceId, component: { id: componentId }, properties: parameters }],
156
+ }));
157
+ if (previousRef.current)
158
+ return previousRef.current;
159
+ }
160
+ return null;
161
+ }
162
+ exports.usePreloadComponent = usePreloadComponent;
163
+ function useTranspileComponent({ componentId, locale, properties, dev: { defaultLocale, components } = {}, }) {
164
+ const [Component, setComponent] = (0, react_1.useState)();
165
+ const component = (0, property_1.mergeComponent)({
166
+ componentId,
167
+ getComponent: (componentId) => { var _a; return (_a = components === null || components === void 0 ? void 0 : components[componentId]) === null || _a === void 0 ? void 0 : _a.data; },
168
+ locale,
169
+ defaultLocale,
170
+ properties,
171
+ });
172
+ const script = (0, react_1.useDeferredValue)(component === null || component === void 0 ? void 0 : component.script);
173
+ (0, react_1.useEffect)(() => {
174
+ if (script) {
175
+ transpileAndLoadScript(script).then((m) => {
176
+ setComponent(() => m.default);
177
+ });
178
+ }
179
+ }, [script]);
180
+ if (!component || !Component)
181
+ return null;
182
+ return {
183
+ Component,
184
+ props: component.properties,
185
+ };
186
+ }
187
+ exports.useTranspileComponent = useTranspileComponent;
188
+ function transpileAndLoadScript(script) {
189
+ return __awaiter(this, void 0, void 0, function* () {
190
+ const [ts, { createBuiltinModuleTransformer }] = yield Promise.all([
191
+ Promise.resolve().then(() => __importStar(require('typescript'))),
192
+ Promise.resolve().then(() => __importStar(require('../../utils/typescript/builtin-module-transformer'))),
193
+ ]);
194
+ const compiled = ts.transpileModule(script, {
195
+ compilerOptions: { jsx: ts.JsxEmit.React, module: ts.ModuleKind.ESNext, target: ts.ScriptTarget.ES2016 },
196
+ transformers: {
197
+ before: [createBuiltinModuleTransformer(ts)],
198
+ },
199
+ }).outputText;
200
+ const url = URL.createObjectURL(new Blob([compiled], { type: 'application/javascript' }));
201
+ return Promise.resolve(`${url}`).then(s => __importStar(require(s)));
202
+ });
203
+ }
@@ -1,2 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PreloadComponentScriptModule = void 0;
4
+ var PreloadComponentScriptModule;
5
+ (function (PreloadComponentScriptModule) {
6
+ PreloadComponentScriptModule["UMD_FN"] = "umd_fn";
7
+ PreloadComponentScriptModule["ESM"] = "esm";
8
+ })(PreloadComponentScriptModule || (exports.PreloadComponentScriptModule = PreloadComponentScriptModule = {}));
@@ -28,6 +28,7 @@ const builtinMuiLab = __importStar(require("../builtin/mui/lab"));
28
28
  const builtinMuiMaterial = __importStar(require("../builtin/mui/material"));
29
29
  const builtinReact = __importStar(require("../builtin/react"));
30
30
  const builtinReactHookForm = __importStar(require("../builtin/react-hook-form"));
31
+ const reactWrapBalancer = __importStar(require("../builtin/react-wrap-balancer"));
31
32
  const builtinUtils = __importStar(require("../builtin/utils"));
32
33
  const builtinZustand = __importStar(require("../builtin/zustand"));
33
34
  const builtinZustandMiddlewareImmer = __importStar(require("../builtin/zustand/middleware/immer"));
@@ -44,6 +45,7 @@ function injectGlobalComponents() {
44
45
  '@blocklet/pages-kit/builtin/iconify/react': builtinIconifyReact,
45
46
  '@blocklet/pages-kit/builtin/zustand': builtinZustand,
46
47
  '@blocklet/pages-kit/builtin/zustand/middleware/immer': builtinZustandMiddlewareImmer,
48
+ '@blocklet/pages-kit/builtin/react-wrap-balancer': reactWrapBalancer,
47
49
  },
48
50
  require(module) {
49
51
  return win[builtin_1.BuiltinModulesGlobalVariableName].modules[module];
@@ -1,32 +1,37 @@
1
1
  "use strict";
2
+ var __rest = (this && this.__rest) || function (s, e) {
3
+ var t = {};
4
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
5
+ t[p] = s[p];
6
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
7
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
8
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
9
+ t[p[i]] = s[p[i]];
10
+ }
11
+ return t;
12
+ };
2
13
  Object.defineProperty(exports, "__esModule", { value: true });
3
14
  exports.injectPreloadComponents = void 0;
4
15
  const types_1 = require("../types");
5
- function injectPreloadComponents(...input) {
6
- const list = input.flat();
7
- const first = list[0];
8
- if (!first)
9
- return null;
10
- const data = list.reduce((res, i) => {
11
- Object.assign(res.config, i.config);
12
- Object.assign(res.components, i.components);
13
- Object.assign(res.props, i.props);
14
- return res;
15
- }, Object.assign({}, first));
16
+ function injectPreloadComponents(data) {
16
17
  const injectState = {
17
18
  config: data.config,
18
- components: Object.fromEntries(Object.entries(data.components).map(([componentId, { script, component }]) => [
19
- componentId,
20
- { preloadComponentModuleName: script.umd.moduleName, component },
21
- ])),
22
- props: data.props,
19
+ components: Object.fromEntries(Object.entries(data.components).map(([componentId, { script, component }]) => {
20
+ if (script.module !== types_1.PreloadComponentScriptModule.UMD_FN)
21
+ throw new Error(`Only support inject script with module ${types_1.PreloadComponentScriptModule.UMD_FN}`);
22
+ return [componentId, { componentModuleGlobalVariable: script.moduleName, component }];
23
+ })),
24
+ instances: Object.fromEntries(data.instances.map((_a) => {
25
+ var { id } = _a, instance = __rest(_a, ["id"]);
26
+ return [id, instance];
27
+ })),
23
28
  };
24
29
  return {
25
30
  html: `\
26
31
  ${Object.values(data.components)
27
- .map(({ script }) => `\
32
+ .map(({ script: { script } }) => `\
28
33
  <script>
29
- ${script.umd.script}
34
+ ${script}
30
35
  </script>
31
36
  `)
32
37
  .join('\n')}
@@ -8,18 +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 preloadComponents({ mode, name, id, locale, }) {
11
+ export function preloadComponents(input) {
12
12
  return __awaiter(this, void 0, void 0, function* () {
13
13
  return call({
14
14
  name: 'pages-kit',
15
- method: 'GET',
15
+ method: 'POST',
16
16
  path: '/api/components/preload',
17
- params: {
18
- mode,
19
- id: Array.isArray(id) ? id.join(',') : id,
20
- name: Array.isArray(name) ? name.join(',') : name,
21
- locale,
22
- },
17
+ data: input,
23
18
  }).then((res) => res.data);
24
19
  });
25
20
  }
@@ -10,19 +10,10 @@ 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 preloadComponents({ mode, name, id, locale, }) {
13
+ export function preloadComponents(input) {
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
18
- .get(joinURL(prefix, '/api/components/preload'), {
19
- params: {
20
- mode,
21
- id: Array.isArray(id) ? id.join(',') : id,
22
- name: Array.isArray(name) ? name.join(',') : name,
23
- locale,
24
- },
25
- })
26
- .then((res) => res.data);
17
+ return axios.post(joinURL(prefix, '/api/components/preload'), input).then((res) => res.data);
27
18
  });
28
19
  }
@@ -0,0 +1 @@
1
+ export * from 'react-wrap-balancer';