@blocklet/pages-kit 0.2.215 → 0.2.217
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/call.js +7 -2
- package/lib/cjs/api/pages-kit.js +3 -2
- package/lib/cjs/components/CustomComponentRenderer/index.js +75 -27
- package/lib/cjs/utils/preload.js +19 -15
- package/lib/cjs/utils/property.js +5 -4
- package/lib/esm/api/call.js +7 -2
- package/lib/esm/api/pages-kit.js +3 -2
- package/lib/esm/components/CustomComponentRenderer/index.js +77 -29
- package/lib/esm/utils/preload.js +19 -15
- package/lib/esm/utils/property.js +5 -4
- package/lib/types/api/call.d.ts +1 -1
- package/lib/types/api/pages-kit.d.ts +1 -1
- package/lib/types/components/CustomComponentRenderer/index.d.ts +3 -0
- package/lib/types/types/api.d.ts +14 -14
- package/lib/types/types/core.d.ts +13 -0
- package/lib/types/types/preload.d.ts +8 -11
- package/lib/types/utils/preload.d.ts +2 -2
- package/package.json +2 -2
package/lib/cjs/api/call.js
CHANGED
|
@@ -11,13 +11,18 @@ 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 }) {
|
|
14
|
+
function preloadComponents({ mode, name, id, locale, }) {
|
|
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
19
|
path: '/api/components/preload',
|
|
20
|
-
params: {
|
|
20
|
+
params: {
|
|
21
|
+
mode,
|
|
22
|
+
id: Array.isArray(id) ? id.join(',') : id,
|
|
23
|
+
name: Array.isArray(name) ? name.join(',') : name,
|
|
24
|
+
locale,
|
|
25
|
+
},
|
|
21
26
|
}).then((res) => res.data);
|
|
22
27
|
});
|
|
23
28
|
}
|
package/lib/cjs/api/pages-kit.js
CHANGED
|
@@ -16,16 +16,17 @@ 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 }) {
|
|
19
|
+
function preloadComponents({ mode, name, id, locale, }) {
|
|
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
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,
|
|
28
28
|
name: Array.isArray(name) ? name.join(',') : name,
|
|
29
|
+
locale,
|
|
29
30
|
},
|
|
30
31
|
})
|
|
31
32
|
.then((res) => res.data);
|
|
@@ -50,13 +50,15 @@ 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
57
|
const MAXIMUM_RENDER_STACK_SIZE = 50;
|
|
57
58
|
function CustomComponentRenderer(_a) {
|
|
59
|
+
var _b;
|
|
58
60
|
var props = __rest(_a, []);
|
|
59
|
-
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)) }));
|
|
61
|
+
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, { instanceId: (_b = props.instanceId) !== null && _b !== void 0 ? _b : props.componentId })) }));
|
|
60
62
|
}
|
|
61
63
|
exports.default = CustomComponentRenderer;
|
|
62
64
|
function ComponentRenderer(_a) {
|
|
@@ -71,10 +73,12 @@ function Renderer(props) {
|
|
|
71
73
|
if (!component)
|
|
72
74
|
return null;
|
|
73
75
|
const { Component, AsyncComponent, properties } = component;
|
|
74
|
-
const componentProps = Object.fromEntries(Object.entries(Object.assign(Object.assign({}, properties), props.props)).map(([key, val]) =>
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
76
|
+
const componentProps = Object.fromEntries(Object.entries(Object.assign(Object.assign({}, properties), props.props)).map(([key, val]) => {
|
|
77
|
+
return [
|
|
78
|
+
key,
|
|
79
|
+
(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),
|
|
80
|
+
];
|
|
81
|
+
}));
|
|
78
82
|
if (Component) {
|
|
79
83
|
return (0, jsx_runtime_1.jsx)(Component, Object.assign({}, componentProps));
|
|
80
84
|
}
|
|
@@ -100,29 +104,73 @@ function AsyncComponentRenderer(_a) {
|
|
|
100
104
|
function E({ error }) {
|
|
101
105
|
return ((0, jsx_runtime_1.jsx)(material_1.Box, { children: (0, jsx_runtime_1.jsx)(material_1.Alert, { severity: "error", children: error.message }) }));
|
|
102
106
|
}
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
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]) {
|
|
112
|
+
const preload = PRELOAD_COMPONENTS_STATE === null || PRELOAD_COMPONENTS_STATE === void 0 ? void 0 : PRELOAD_COMPONENTS_STATE.components[componentId];
|
|
113
|
+
if (preload) {
|
|
114
|
+
const mod = window[preload.preloadComponentModuleName];
|
|
115
|
+
if (typeof mod === 'function') {
|
|
116
|
+
COMPONENTS_MAP[componentId] = { component: preload.component, Component: mod() };
|
|
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,
|
|
124
140
|
};
|
|
125
|
-
|
|
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]);
|
|
155
|
+
const preloadState = (0, react_1.useMemo)(() => {
|
|
156
|
+
var _a;
|
|
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];
|
|
158
|
+
}, [instanceId, locale, PRELOAD_COMPONENTS_STATE_KEY]);
|
|
159
|
+
const previousPreload = (0, react_1.useRef)();
|
|
160
|
+
const [, update] = (0, react_1.useState)(0);
|
|
161
|
+
if ((props || preloadState) && preloadComponent) {
|
|
162
|
+
previousPreload.current = {
|
|
163
|
+
Component: preloadComponent.Component,
|
|
164
|
+
properties: preloadState,
|
|
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;
|
|
126
174
|
}
|
|
127
175
|
}
|
|
128
176
|
const component = (0, property_1.mergeComponent)({
|
package/lib/cjs/utils/preload.js
CHANGED
|
@@ -2,27 +2,31 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.injectPreloadComponents = void 0;
|
|
4
4
|
const types_1 = require("../types");
|
|
5
|
-
function injectPreloadComponents(
|
|
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));
|
|
6
16
|
const injectState = {
|
|
7
|
-
config,
|
|
8
|
-
components: Object.fromEntries(components.map(({ script, component
|
|
9
|
-
|
|
10
|
-
{
|
|
11
|
-
preloadComponentModuleName: script.umd.moduleName,
|
|
12
|
-
component,
|
|
13
|
-
locales,
|
|
14
|
-
serverSideProps,
|
|
15
|
-
},
|
|
17
|
+
config: data.config,
|
|
18
|
+
components: Object.fromEntries(Object.entries(data.components).map(([componentId, { script, component }]) => [
|
|
19
|
+
componentId,
|
|
20
|
+
{ preloadComponentModuleName: script.umd.moduleName, component },
|
|
16
21
|
])),
|
|
22
|
+
props: data.props,
|
|
17
23
|
};
|
|
18
|
-
if (!components.length)
|
|
19
|
-
return undefined;
|
|
20
24
|
return {
|
|
21
25
|
html: `\
|
|
22
|
-
${components
|
|
23
|
-
.map((
|
|
26
|
+
${Object.values(data.components)
|
|
27
|
+
.map(({ script }) => `\
|
|
24
28
|
<script>
|
|
25
|
-
${
|
|
29
|
+
${script.umd.script}
|
|
26
30
|
</script>
|
|
27
31
|
`)
|
|
28
32
|
.join('\n')}
|
|
@@ -59,7 +59,7 @@ function mergeComponent({ componentId, getComponent, locale, defaultLocale, prop
|
|
|
59
59
|
exports.mergeComponent = mergeComponent;
|
|
60
60
|
exports.RenderNestedComponent = '__RENDER_NESTED_COMPONENT__';
|
|
61
61
|
function parsePropertyValue(property, value, { locale, defaultLocale }) {
|
|
62
|
-
var _a, _b, _c;
|
|
62
|
+
var _a, _b, _c, _d, _e;
|
|
63
63
|
if (property.type === 'json') {
|
|
64
64
|
if (!value)
|
|
65
65
|
return undefined;
|
|
@@ -72,13 +72,14 @@ function parsePropertyValue(property, value, { locale, defaultLocale }) {
|
|
|
72
72
|
}
|
|
73
73
|
}
|
|
74
74
|
if (property.type === 'component') {
|
|
75
|
-
const
|
|
76
|
-
if (
|
|
75
|
+
const componentId = (value === null || value === void 0 ? void 0 : value.componentId) || property.componentId;
|
|
76
|
+
if (typeof componentId !== 'string')
|
|
77
77
|
return undefined;
|
|
78
|
+
const { locales } = property;
|
|
78
79
|
return {
|
|
79
80
|
type: exports.RenderNestedComponent,
|
|
80
81
|
componentId,
|
|
81
|
-
properties: (_b = (_a = locales === null || locales === void 0 ? void 0 : locales[locale]) === null || _a === void 0 ? void 0 : _a.defaultValue) !== null &&
|
|
82
|
+
properties: Object.assign(Object.assign({}, ((_c = (_b = (_a = locales === null || locales === void 0 ? void 0 : locales[locale]) === null || _a === void 0 ? void 0 : _a.defaultValue) === null || _b === void 0 ? void 0 : _b.properties) !== null && _c !== void 0 ? _c : (_e = (_d = locales === null || locales === void 0 ? void 0 : locales[defaultLocale]) === null || _d === void 0 ? void 0 : _d.defaultValue) === null || _e === void 0 ? void 0 : _e.properties)), value === null || value === void 0 ? void 0 : value.properties),
|
|
82
83
|
};
|
|
83
84
|
}
|
|
84
85
|
return value;
|
package/lib/esm/api/call.js
CHANGED
|
@@ -8,13 +8,18 @@ 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 }) {
|
|
11
|
+
export function preloadComponents({ mode, name, id, locale, }) {
|
|
12
12
|
return __awaiter(this, void 0, void 0, function* () {
|
|
13
13
|
return call({
|
|
14
14
|
name: 'pages-kit',
|
|
15
15
|
method: 'GET',
|
|
16
16
|
path: '/api/components/preload',
|
|
17
|
-
params: {
|
|
17
|
+
params: {
|
|
18
|
+
mode,
|
|
19
|
+
id: Array.isArray(id) ? id.join(',') : id,
|
|
20
|
+
name: Array.isArray(name) ? name.join(',') : name,
|
|
21
|
+
locale,
|
|
22
|
+
},
|
|
18
23
|
}).then((res) => res.data);
|
|
19
24
|
});
|
|
20
25
|
}
|
package/lib/esm/api/pages-kit.js
CHANGED
|
@@ -10,16 +10,17 @@ 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 }) {
|
|
13
|
+
export function preloadComponents({ mode, name, id, locale, }) {
|
|
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
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,
|
|
22
22
|
name: Array.isArray(name) ? name.join(',') : name,
|
|
23
|
+
locale,
|
|
23
24
|
},
|
|
24
25
|
})
|
|
25
26
|
.then((res) => res.data);
|
|
@@ -22,15 +22,17 @@ import { jsx as _jsx } from "react/jsx-runtime";
|
|
|
22
22
|
import '../../utils/inject-global-components';
|
|
23
23
|
import { Alert, Box } from '@mui/material';
|
|
24
24
|
import { pick } from 'lodash';
|
|
25
|
-
import { useEffect, useRef, useState } from 'react';
|
|
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
|
-
import { RenderNestedComponent,
|
|
29
|
+
import { RenderNestedComponent, mergeComponent } from '../../utils/property';
|
|
29
30
|
import { createBuiltinModuleTransformer } from '../../utils/typescript/builtin-module-transformer';
|
|
30
31
|
const MAXIMUM_RENDER_STACK_SIZE = 50;
|
|
31
32
|
export default function CustomComponentRenderer(_a) {
|
|
33
|
+
var _b;
|
|
32
34
|
var props = __rest(_a, []);
|
|
33
|
-
return (_jsx(ErrorBoundary, { fallbackRender: E, resetKeys: [Date.now()], children: _jsx(ComponentRenderer, Object.assign({}, props)) }));
|
|
35
|
+
return (_jsx(ErrorBoundary, { fallbackRender: E, resetKeys: [Date.now()], children: _jsx(ComponentRenderer, Object.assign({}, props, { instanceId: (_b = props.instanceId) !== null && _b !== void 0 ? _b : props.componentId })) }));
|
|
34
36
|
}
|
|
35
37
|
function ComponentRenderer(_a) {
|
|
36
38
|
var { renderCount = 0 } = _a, props = __rest(_a, ["renderCount"]);
|
|
@@ -44,10 +46,12 @@ function Renderer(props) {
|
|
|
44
46
|
if (!component)
|
|
45
47
|
return null;
|
|
46
48
|
const { Component, AsyncComponent, properties } = component;
|
|
47
|
-
const componentProps = Object.fromEntries(Object.entries(Object.assign(Object.assign({}, properties), props.props)).map(([key, val]) =>
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
49
|
+
const componentProps = Object.fromEntries(Object.entries(Object.assign(Object.assign({}, properties), props.props)).map(([key, val]) => {
|
|
50
|
+
return [
|
|
51
|
+
key,
|
|
52
|
+
(val === null || val === void 0 ? void 0 : val.type) === RenderNestedComponent ? (_jsx(ComponentRenderer, Object.assign({}, pick(props, 'components', 'locale', 'defaultLocale'), { instanceId: key, renderCount: props.renderCount, componentId: val.componentId, properties: val.properties, props: val.props }))) : (val),
|
|
53
|
+
];
|
|
54
|
+
}));
|
|
51
55
|
if (Component) {
|
|
52
56
|
return _jsx(Component, Object.assign({}, componentProps));
|
|
53
57
|
}
|
|
@@ -73,29 +77,73 @@ function AsyncComponentRenderer(_a) {
|
|
|
73
77
|
function E({ error }) {
|
|
74
78
|
return (_jsx(Box, { children: _jsx(Alert, { severity: "error", children: error.message }) }));
|
|
75
79
|
}
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
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]) {
|
|
85
|
+
const preload = PRELOAD_COMPONENTS_STATE === null || PRELOAD_COMPONENTS_STATE === void 0 ? void 0 : PRELOAD_COMPONENTS_STATE.components[componentId];
|
|
86
|
+
if (preload) {
|
|
87
|
+
const mod = window[preload.preloadComponentModuleName];
|
|
88
|
+
if (typeof mod === 'function') {
|
|
89
|
+
COMPONENTS_MAP[componentId] = { component: preload.component, Component: mod() };
|
|
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,
|
|
97
113
|
};
|
|
98
|
-
|
|
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]);
|
|
128
|
+
const preloadState = useMemo(() => {
|
|
129
|
+
var _a;
|
|
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];
|
|
131
|
+
}, [instanceId, locale, PRELOAD_COMPONENTS_STATE_KEY]);
|
|
132
|
+
const previousPreload = useRef();
|
|
133
|
+
const [, update] = useState(0);
|
|
134
|
+
if ((props || preloadState) && preloadComponent) {
|
|
135
|
+
previousPreload.current = {
|
|
136
|
+
Component: preloadComponent.Component,
|
|
137
|
+
properties: preloadState,
|
|
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;
|
|
99
147
|
}
|
|
100
148
|
}
|
|
101
149
|
const component = mergeComponent({
|
package/lib/esm/utils/preload.js
CHANGED
|
@@ -1,25 +1,29 @@
|
|
|
1
1
|
import { PreloadComponentsStateGlobalVariableName } from '../types';
|
|
2
|
-
export function injectPreloadComponents(
|
|
2
|
+
export function injectPreloadComponents(...input) {
|
|
3
|
+
const list = input.flat();
|
|
4
|
+
const first = list[0];
|
|
5
|
+
if (!first)
|
|
6
|
+
return null;
|
|
7
|
+
const data = list.reduce((res, i) => {
|
|
8
|
+
Object.assign(res.config, i.config);
|
|
9
|
+
Object.assign(res.components, i.components);
|
|
10
|
+
Object.assign(res.props, i.props);
|
|
11
|
+
return res;
|
|
12
|
+
}, Object.assign({}, first));
|
|
3
13
|
const injectState = {
|
|
4
|
-
config,
|
|
5
|
-
components: Object.fromEntries(components.map(({ script, component
|
|
6
|
-
|
|
7
|
-
{
|
|
8
|
-
preloadComponentModuleName: script.umd.moduleName,
|
|
9
|
-
component,
|
|
10
|
-
locales,
|
|
11
|
-
serverSideProps,
|
|
12
|
-
},
|
|
14
|
+
config: data.config,
|
|
15
|
+
components: Object.fromEntries(Object.entries(data.components).map(([componentId, { script, component }]) => [
|
|
16
|
+
componentId,
|
|
17
|
+
{ preloadComponentModuleName: script.umd.moduleName, component },
|
|
13
18
|
])),
|
|
19
|
+
props: data.props,
|
|
14
20
|
};
|
|
15
|
-
if (!components.length)
|
|
16
|
-
return undefined;
|
|
17
21
|
return {
|
|
18
22
|
html: `\
|
|
19
|
-
${components
|
|
20
|
-
.map((
|
|
23
|
+
${Object.values(data.components)
|
|
24
|
+
.map(({ script }) => `\
|
|
21
25
|
<script>
|
|
22
|
-
${
|
|
26
|
+
${script.umd.script}
|
|
23
27
|
</script>
|
|
24
28
|
`)
|
|
25
29
|
.join('\n')}
|
|
@@ -54,7 +54,7 @@ export function mergeComponent({ componentId, getComponent, locale, defaultLocal
|
|
|
54
54
|
}
|
|
55
55
|
export const RenderNestedComponent = '__RENDER_NESTED_COMPONENT__';
|
|
56
56
|
export function parsePropertyValue(property, value, { locale, defaultLocale }) {
|
|
57
|
-
var _a, _b, _c;
|
|
57
|
+
var _a, _b, _c, _d, _e;
|
|
58
58
|
if (property.type === 'json') {
|
|
59
59
|
if (!value)
|
|
60
60
|
return undefined;
|
|
@@ -67,13 +67,14 @@ export function parsePropertyValue(property, value, { locale, defaultLocale }) {
|
|
|
67
67
|
}
|
|
68
68
|
}
|
|
69
69
|
if (property.type === 'component') {
|
|
70
|
-
const
|
|
71
|
-
if (
|
|
70
|
+
const componentId = (value === null || value === void 0 ? void 0 : value.componentId) || property.componentId;
|
|
71
|
+
if (typeof componentId !== 'string')
|
|
72
72
|
return undefined;
|
|
73
|
+
const { locales } = property;
|
|
73
74
|
return {
|
|
74
75
|
type: RenderNestedComponent,
|
|
75
76
|
componentId,
|
|
76
|
-
properties: (_b = (_a = locales === null || locales === void 0 ? void 0 : locales[locale]) === null || _a === void 0 ? void 0 : _a.defaultValue) !== null &&
|
|
77
|
+
properties: Object.assign(Object.assign({}, ((_c = (_b = (_a = locales === null || locales === void 0 ? void 0 : locales[locale]) === null || _a === void 0 ? void 0 : _a.defaultValue) === null || _b === void 0 ? void 0 : _b.properties) !== null && _c !== void 0 ? _c : (_e = (_d = locales === null || locales === void 0 ? void 0 : locales[defaultLocale]) === null || _d === void 0 ? void 0 : _d.defaultValue) === null || _e === void 0 ? void 0 : _e.properties)), value === null || value === void 0 ? void 0 : value.properties),
|
|
77
78
|
};
|
|
78
79
|
}
|
|
79
80
|
return value;
|
package/lib/types/api/call.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import { PreloadComponentQuery, PreloadComponentsResult } from '../types';
|
|
2
|
-
export declare function preloadComponents({ mode, name, id }: PreloadComponentQuery): Promise<PreloadComponentsResult>;
|
|
2
|
+
export declare function preloadComponents({ mode, name, id, locale, }: PreloadComponentQuery): Promise<PreloadComponentsResult>;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import { PreloadComponentQuery, PreloadComponentsResult } from '../types';
|
|
2
|
-
export declare function preloadComponents({ mode, name, id }: PreloadComponentQuery): Promise<PreloadComponentsResult>;
|
|
2
|
+
export declare function preloadComponents({ mode, name, id, locale, }: PreloadComponentQuery): Promise<PreloadComponentsResult>;
|
|
@@ -1,6 +1,9 @@
|
|
|
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;
|
|
6
|
+
instanceId?: string;
|
|
4
7
|
componentId: string;
|
|
5
8
|
locale?: string;
|
|
6
9
|
defaultLocale?: string;
|
package/lib/types/types/api.d.ts
CHANGED
|
@@ -1,31 +1,31 @@
|
|
|
1
1
|
import { CustomComponent } from './core';
|
|
2
2
|
import { StateMode } from './state';
|
|
3
3
|
export interface PreloadComponentQuery {
|
|
4
|
+
locale?: string;
|
|
4
5
|
mode?: StateMode;
|
|
5
6
|
name?: string[] | string;
|
|
6
7
|
id?: string[] | string;
|
|
7
8
|
}
|
|
8
9
|
export interface PreloadComponentsResult {
|
|
9
10
|
config: {
|
|
10
|
-
defaultLocale
|
|
11
|
+
defaultLocale: string;
|
|
11
12
|
};
|
|
12
13
|
components: {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
14
|
+
[componentId: string]: {
|
|
15
|
+
component: CustomComponent;
|
|
16
|
+
script: {
|
|
17
|
+
umd: {
|
|
18
|
+
script: string;
|
|
19
|
+
moduleName: string;
|
|
20
|
+
};
|
|
18
21
|
};
|
|
19
22
|
};
|
|
20
|
-
|
|
23
|
+
};
|
|
24
|
+
props: {
|
|
25
|
+
[instanceId: string]: {
|
|
21
26
|
[locale: string]: {
|
|
22
|
-
|
|
23
|
-
[key: string]: any;
|
|
24
|
-
};
|
|
27
|
+
[key: string]: any;
|
|
25
28
|
};
|
|
26
29
|
};
|
|
27
|
-
|
|
28
|
-
[key: string]: any;
|
|
29
|
-
};
|
|
30
|
-
}[];
|
|
30
|
+
};
|
|
31
31
|
}
|
|
@@ -53,4 +53,17 @@ export interface CustomComponentStringProperty extends CustomComponentPropertyBa
|
|
|
53
53
|
export interface CustomComponentComponentProperty extends CustomComponentPropertyBase {
|
|
54
54
|
type: 'component';
|
|
55
55
|
componentId?: string;
|
|
56
|
+
locales?: {
|
|
57
|
+
[locale: string]: {
|
|
58
|
+
name?: undefined;
|
|
59
|
+
defaultValue?: {
|
|
60
|
+
componentId?: string;
|
|
61
|
+
properties: {
|
|
62
|
+
[id: string]: {
|
|
63
|
+
value: any;
|
|
64
|
+
};
|
|
65
|
+
};
|
|
66
|
+
};
|
|
67
|
+
};
|
|
68
|
+
};
|
|
56
69
|
}
|
|
@@ -3,22 +3,19 @@ export declare const PreloadComponentsStateGlobalVariableName = "__PAGES_KIT_PRE
|
|
|
3
3
|
export interface PreloadComponent {
|
|
4
4
|
preloadComponentModuleName: string;
|
|
5
5
|
component: CustomComponent;
|
|
6
|
-
locales?: {
|
|
7
|
-
[locale: string]: {
|
|
8
|
-
properties?: {
|
|
9
|
-
[key: string]: any;
|
|
10
|
-
};
|
|
11
|
-
};
|
|
12
|
-
};
|
|
13
|
-
serverSideProps?: {
|
|
14
|
-
[key: string]: any;
|
|
15
|
-
};
|
|
16
6
|
}
|
|
17
7
|
export interface PreloadComponentsState {
|
|
18
8
|
config: {
|
|
19
9
|
defaultLocale?: string;
|
|
20
10
|
};
|
|
21
11
|
components: {
|
|
22
|
-
[
|
|
12
|
+
[componentId: string]: PreloadComponent;
|
|
13
|
+
};
|
|
14
|
+
props: {
|
|
15
|
+
[instanceId: string]: {
|
|
16
|
+
[locale: string]: {
|
|
17
|
+
[key: string]: any;
|
|
18
|
+
};
|
|
19
|
+
};
|
|
23
20
|
};
|
|
24
21
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import { PreloadComponentsResult } from '../types';
|
|
2
|
-
export declare function injectPreloadComponents(
|
|
2
|
+
export declare function injectPreloadComponents(...input: PreloadComponentsResult[] | PreloadComponentsResult[][]): {
|
|
3
3
|
html: string;
|
|
4
|
-
} |
|
|
4
|
+
} | null;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@blocklet/pages-kit",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.217",
|
|
4
4
|
"description": "Pages Kit components and utils",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
@@ -120,7 +120,7 @@
|
|
|
120
120
|
"react-router-dom": "^6.16.0"
|
|
121
121
|
},
|
|
122
122
|
"devDependencies": {
|
|
123
|
-
"@arcblock/ux": "^2.9.
|
|
123
|
+
"@arcblock/ux": "^2.9.20",
|
|
124
124
|
"@emotion/cache": "^11.11.0",
|
|
125
125
|
"@emotion/css": "^11.11.2",
|
|
126
126
|
"@emotion/react": "^11.11.3",
|