@blocklet/pages-kit 0.2.216 → 0.2.218
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/lib/cjs/api/pages-kit.js +1 -1
- package/lib/cjs/components/CustomComponentRenderer/index.js +57 -10
- package/lib/esm/api/pages-kit.js +1 -1
- package/lib/esm/components/CustomComponentRenderer/index.js +57 -10
- package/lib/types/components/CustomComponentRenderer/index.d.ts +2 -0
- package/package.json +1 -1
package/lib/cjs/api/pages-kit.js
CHANGED
|
@@ -21,7 +21,7 @@ function preloadComponents({ mode, name, id, locale, }) {
|
|
|
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
23
|
return axios_1.default
|
|
24
|
-
.get((0, ufo_1.joinURL)(prefix, '/api/components'), {
|
|
24
|
+
.get((0, ufo_1.joinURL)(prefix, '/api/components/preload'), {
|
|
25
25
|
params: {
|
|
26
26
|
mode,
|
|
27
27
|
id: Array.isArray(id) ? id.join(',') : id,
|
|
@@ -50,10 +50,11 @@ const material_1 = require("@mui/material");
|
|
|
50
50
|
const lodash_1 = require("lodash");
|
|
51
51
|
const react_1 = require("react");
|
|
52
52
|
const react_error_boundary_1 = require("react-error-boundary");
|
|
53
|
+
const api_1 = require("../../api");
|
|
53
54
|
const preload_1 = require("../../types/preload");
|
|
54
55
|
const property_1 = require("../../utils/property");
|
|
55
56
|
const builtin_module_transformer_1 = require("../../utils/typescript/builtin-module-transformer");
|
|
56
|
-
const MAXIMUM_RENDER_STACK_SIZE =
|
|
57
|
+
const MAXIMUM_RENDER_STACK_SIZE = 20;
|
|
57
58
|
function CustomComponentRenderer(_a) {
|
|
58
59
|
var _b;
|
|
59
60
|
var props = __rest(_a, []);
|
|
@@ -103,28 +104,74 @@ function AsyncComponentRenderer(_a) {
|
|
|
103
104
|
function E({ error }) {
|
|
104
105
|
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
|
}
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
107
|
+
let PRELOAD_COMPONENTS_STATE = window[preload_1.PreloadComponentsStateGlobalVariableName];
|
|
108
|
+
let PRELOAD_COMPONENTS_STATE_KEY = 0;
|
|
109
|
+
const COMPONENTS_MAP = {};
|
|
110
|
+
function getScriptComponent(componentId) {
|
|
111
|
+
if (!COMPONENTS_MAP[componentId]) {
|
|
110
112
|
const preload = PRELOAD_COMPONENTS_STATE === null || PRELOAD_COMPONENTS_STATE === void 0 ? void 0 : PRELOAD_COMPONENTS_STATE.components[componentId];
|
|
111
113
|
if (preload) {
|
|
112
114
|
const mod = window[preload.preloadComponentModuleName];
|
|
113
115
|
if (typeof mod === 'function') {
|
|
114
|
-
|
|
116
|
+
COMPONENTS_MAP[componentId] = { component: preload.component, Component: mod() };
|
|
115
117
|
}
|
|
116
118
|
}
|
|
117
|
-
|
|
118
|
-
|
|
119
|
+
}
|
|
120
|
+
return COMPONENTS_MAP[componentId];
|
|
121
|
+
}
|
|
122
|
+
const LOAD_REMOTE_COMPONENT_TASKS = {};
|
|
123
|
+
function loadRemoteComponent({ mode, instanceId, componentId, locale, }) {
|
|
124
|
+
var _a;
|
|
125
|
+
const key = [instanceId, locale].join('-');
|
|
126
|
+
(_a = LOAD_REMOTE_COMPONENT_TASKS[key]) !== null && _a !== void 0 ? _a : (LOAD_REMOTE_COMPONENT_TASKS[key] = (0, api_1.preloadComponents)({ mode, id: componentId, locale })
|
|
127
|
+
.then((result) => {
|
|
128
|
+
var _a;
|
|
129
|
+
var _b;
|
|
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 !== null && PRELOAD_COMPONENTS_STATE !== void 0 ? PRELOAD_COMPONENTS_STATE : (PRELOAD_COMPONENTS_STATE = { config: result.config, components: {}, props: {} });
|
|
137
|
+
PRELOAD_COMPONENTS_STATE.components[i.component.id] = {
|
|
138
|
+
preloadComponentModuleName: i.script.umd.moduleName,
|
|
139
|
+
component: i.component,
|
|
140
|
+
};
|
|
141
|
+
(_a = (_b = PRELOAD_COMPONENTS_STATE.props)[instanceId]) !== null && _a !== void 0 ? _a : (_b[instanceId] = {});
|
|
142
|
+
PRELOAD_COMPONENTS_STATE.props[instanceId][locale] = result.props[componentId][locale];
|
|
143
|
+
PRELOAD_COMPONENTS_STATE_KEY += 1;
|
|
144
|
+
}
|
|
145
|
+
})
|
|
146
|
+
.catch((error) => {
|
|
147
|
+
console.error('load remote component error', error);
|
|
148
|
+
LOAD_REMOTE_COMPONENT_TASKS[key] = undefined;
|
|
149
|
+
}));
|
|
150
|
+
return LOAD_REMOTE_COMPONENT_TASKS[key];
|
|
151
|
+
}
|
|
152
|
+
function useComponent({ mode, instanceId, componentId, components, locale, defaultLocale, properties: parameters, props, }) {
|
|
153
|
+
const scriptRef = (0, react_1.useRef)({});
|
|
154
|
+
const preloadComponent = (0, react_1.useMemo)(() => getScriptComponent(componentId), [componentId, PRELOAD_COMPONENTS_STATE_KEY]);
|
|
119
155
|
const preloadState = (0, react_1.useMemo)(() => {
|
|
120
156
|
var _a;
|
|
121
157
|
return (_a = PRELOAD_COMPONENTS_STATE === null || PRELOAD_COMPONENTS_STATE === void 0 ? void 0 : PRELOAD_COMPONENTS_STATE.props[instanceId]) === null || _a === void 0 ? void 0 : _a[locale];
|
|
122
|
-
}, [instanceId, locale]);
|
|
158
|
+
}, [instanceId, locale, PRELOAD_COMPONENTS_STATE_KEY]);
|
|
159
|
+
const previousPreload = (0, react_1.useRef)();
|
|
160
|
+
const [, update] = (0, react_1.useState)(0);
|
|
123
161
|
if ((props || preloadState) && preloadComponent) {
|
|
124
|
-
|
|
162
|
+
previousPreload.current = {
|
|
125
163
|
Component: preloadComponent.Component,
|
|
126
164
|
properties: preloadState,
|
|
127
165
|
};
|
|
166
|
+
return previousPreload.current;
|
|
167
|
+
}
|
|
168
|
+
if (!(components === null || components === void 0 ? void 0 : components.length) && preloadComponent) {
|
|
169
|
+
loadRemoteComponent({ mode, instanceId, componentId, locale }).then(() => {
|
|
170
|
+
update((v) => v + 1);
|
|
171
|
+
});
|
|
172
|
+
if (previousPreload.current) {
|
|
173
|
+
return previousPreload.current;
|
|
174
|
+
}
|
|
128
175
|
}
|
|
129
176
|
const component = (0, property_1.mergeComponent)({
|
|
130
177
|
componentId,
|
package/lib/esm/api/pages-kit.js
CHANGED
|
@@ -15,7 +15,7 @@ export function preloadComponents({ mode, name, id, locale, }) {
|
|
|
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
17
|
return axios
|
|
18
|
-
.get(joinURL(prefix, '/api/components'), {
|
|
18
|
+
.get(joinURL(prefix, '/api/components/preload'), {
|
|
19
19
|
params: {
|
|
20
20
|
mode,
|
|
21
21
|
id: Array.isArray(id) ? id.join(',') : id,
|
|
@@ -24,10 +24,11 @@ import { Alert, Box } from '@mui/material';
|
|
|
24
24
|
import { pick } from 'lodash';
|
|
25
25
|
import { useEffect, useMemo, useRef, useState } from 'react';
|
|
26
26
|
import { ErrorBoundary } from 'react-error-boundary';
|
|
27
|
+
import { preloadComponents } from '../../api';
|
|
27
28
|
import { PreloadComponentsStateGlobalVariableName } from '../../types/preload';
|
|
28
29
|
import { RenderNestedComponent, mergeComponent } from '../../utils/property';
|
|
29
30
|
import { createBuiltinModuleTransformer } from '../../utils/typescript/builtin-module-transformer';
|
|
30
|
-
const MAXIMUM_RENDER_STACK_SIZE =
|
|
31
|
+
const MAXIMUM_RENDER_STACK_SIZE = 20;
|
|
31
32
|
export default function CustomComponentRenderer(_a) {
|
|
32
33
|
var _b;
|
|
33
34
|
var props = __rest(_a, []);
|
|
@@ -76,28 +77,74 @@ function AsyncComponentRenderer(_a) {
|
|
|
76
77
|
function E({ error }) {
|
|
77
78
|
return (_jsx(Box, { children: _jsx(Alert, { severity: "error", children: error.message }) }));
|
|
78
79
|
}
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
80
|
+
let PRELOAD_COMPONENTS_STATE = window[PreloadComponentsStateGlobalVariableName];
|
|
81
|
+
let PRELOAD_COMPONENTS_STATE_KEY = 0;
|
|
82
|
+
const COMPONENTS_MAP = {};
|
|
83
|
+
function getScriptComponent(componentId) {
|
|
84
|
+
if (!COMPONENTS_MAP[componentId]) {
|
|
83
85
|
const preload = PRELOAD_COMPONENTS_STATE === null || PRELOAD_COMPONENTS_STATE === void 0 ? void 0 : PRELOAD_COMPONENTS_STATE.components[componentId];
|
|
84
86
|
if (preload) {
|
|
85
87
|
const mod = window[preload.preloadComponentModuleName];
|
|
86
88
|
if (typeof mod === 'function') {
|
|
87
|
-
|
|
89
|
+
COMPONENTS_MAP[componentId] = { component: preload.component, Component: mod() };
|
|
88
90
|
}
|
|
89
91
|
}
|
|
90
|
-
|
|
91
|
-
|
|
92
|
+
}
|
|
93
|
+
return COMPONENTS_MAP[componentId];
|
|
94
|
+
}
|
|
95
|
+
const LOAD_REMOTE_COMPONENT_TASKS = {};
|
|
96
|
+
function loadRemoteComponent({ mode, instanceId, componentId, locale, }) {
|
|
97
|
+
var _a;
|
|
98
|
+
const key = [instanceId, locale].join('-');
|
|
99
|
+
(_a = LOAD_REMOTE_COMPONENT_TASKS[key]) !== null && _a !== void 0 ? _a : (LOAD_REMOTE_COMPONENT_TASKS[key] = preloadComponents({ mode, id: componentId, locale })
|
|
100
|
+
.then((result) => {
|
|
101
|
+
var _a;
|
|
102
|
+
var _b;
|
|
103
|
+
for (const i of Object.values(result.components)) {
|
|
104
|
+
if (!window[i.script.umd.moduleName]) {
|
|
105
|
+
const script = document.createElement('script');
|
|
106
|
+
script.textContent = i.script.umd.script;
|
|
107
|
+
document.body.appendChild(script);
|
|
108
|
+
}
|
|
109
|
+
PRELOAD_COMPONENTS_STATE !== null && PRELOAD_COMPONENTS_STATE !== void 0 ? PRELOAD_COMPONENTS_STATE : (PRELOAD_COMPONENTS_STATE = { config: result.config, components: {}, props: {} });
|
|
110
|
+
PRELOAD_COMPONENTS_STATE.components[i.component.id] = {
|
|
111
|
+
preloadComponentModuleName: i.script.umd.moduleName,
|
|
112
|
+
component: i.component,
|
|
113
|
+
};
|
|
114
|
+
(_a = (_b = PRELOAD_COMPONENTS_STATE.props)[instanceId]) !== null && _a !== void 0 ? _a : (_b[instanceId] = {});
|
|
115
|
+
PRELOAD_COMPONENTS_STATE.props[instanceId][locale] = result.props[componentId][locale];
|
|
116
|
+
PRELOAD_COMPONENTS_STATE_KEY += 1;
|
|
117
|
+
}
|
|
118
|
+
})
|
|
119
|
+
.catch((error) => {
|
|
120
|
+
console.error('load remote component error', error);
|
|
121
|
+
LOAD_REMOTE_COMPONENT_TASKS[key] = undefined;
|
|
122
|
+
}));
|
|
123
|
+
return LOAD_REMOTE_COMPONENT_TASKS[key];
|
|
124
|
+
}
|
|
125
|
+
function useComponent({ mode, instanceId, componentId, components, locale, defaultLocale, properties: parameters, props, }) {
|
|
126
|
+
const scriptRef = useRef({});
|
|
127
|
+
const preloadComponent = useMemo(() => getScriptComponent(componentId), [componentId, PRELOAD_COMPONENTS_STATE_KEY]);
|
|
92
128
|
const preloadState = useMemo(() => {
|
|
93
129
|
var _a;
|
|
94
130
|
return (_a = PRELOAD_COMPONENTS_STATE === null || PRELOAD_COMPONENTS_STATE === void 0 ? void 0 : PRELOAD_COMPONENTS_STATE.props[instanceId]) === null || _a === void 0 ? void 0 : _a[locale];
|
|
95
|
-
}, [instanceId, locale]);
|
|
131
|
+
}, [instanceId, locale, PRELOAD_COMPONENTS_STATE_KEY]);
|
|
132
|
+
const previousPreload = useRef();
|
|
133
|
+
const [, update] = useState(0);
|
|
96
134
|
if ((props || preloadState) && preloadComponent) {
|
|
97
|
-
|
|
135
|
+
previousPreload.current = {
|
|
98
136
|
Component: preloadComponent.Component,
|
|
99
137
|
properties: preloadState,
|
|
100
138
|
};
|
|
139
|
+
return previousPreload.current;
|
|
140
|
+
}
|
|
141
|
+
if (!(components === null || components === void 0 ? void 0 : components.length) && preloadComponent) {
|
|
142
|
+
loadRemoteComponent({ mode, instanceId, componentId, locale }).then(() => {
|
|
143
|
+
update((v) => v + 1);
|
|
144
|
+
});
|
|
145
|
+
if (previousPreload.current) {
|
|
146
|
+
return previousPreload.current;
|
|
147
|
+
}
|
|
101
148
|
}
|
|
102
149
|
const component = mergeComponent({
|
|
103
150
|
componentId,
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import '../../utils/inject-global-components';
|
|
2
|
+
import { StateMode } from '../../types';
|
|
2
3
|
import { CustomComponent } from '../../types/core';
|
|
3
4
|
export interface CustomComponentRendererProps {
|
|
5
|
+
mode?: StateMode;
|
|
4
6
|
instanceId?: string;
|
|
5
7
|
componentId: string;
|
|
6
8
|
locale?: string;
|