@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.
@@ -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: { mode, id: Array.isArray(id) ? id.join(',') : id, name: Array.isArray(name) ? name.join(',') : name },
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
  }
@@ -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
- key,
76
- (val === null || val === void 0 ? void 0 : val.type) === property_1.RenderNestedComponent ? ((0, jsx_runtime_1.jsx)(ComponentRenderer, Object.assign({ renderCount: props.renderCount, componentId: val.componentId }, (0, lodash_1.pick)(props, 'components', 'locale', 'defaultLocale'), { properties: val.properties }))) : (val),
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
- const PRELOAD_COMPONENTS_STATE = window[preload_1.PreloadComponentsStateGlobalVariableName];
104
- function useComponent({ componentId, components, locale, defaultLocale, properties: parameters, }) {
105
- var _a, _b, _c, _d, _e;
106
- const preloadRef = (0, react_1.useRef)();
107
- const scriptRef = (0, react_1.useRef)({});
108
- if (preloadRef.current)
109
- return preloadRef.current;
110
- const preload = PRELOAD_COMPONENTS_STATE === null || PRELOAD_COMPONENTS_STATE === void 0 ? void 0 : PRELOAD_COMPONENTS_STATE.components[componentId];
111
- if (preload) {
112
- const properties = Object.fromEntries(Object.entries(parameters !== null && parameters !== void 0 ? parameters : {}).map(([id, { value }]) => {
113
- var _a, _b;
114
- const property = (_b = (_a = preload.component.properties) === null || _a === void 0 ? void 0 : _a[id]) === null || _b === void 0 ? void 0 : _b.data;
115
- if (!property)
116
- return [];
117
- return [property.key, (0, property_1.parsePropertyValue)(property, value, { locale, defaultLocale })];
118
- }));
119
- const mod = window[preload.preloadComponentModuleName];
120
- if (typeof mod === 'function') {
121
- preloadRef.current = {
122
- Component: mod(),
123
- properties: Object.assign(Object.assign({}, (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)), preload.serverSideProps),
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
- return preloadRef.current;
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)({
@@ -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({ config, components }) {
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, locales, serverSideProps }) => [
9
- component.id,
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((i) => `\
26
+ ${Object.values(data.components)
27
+ .map(({ script }) => `\
24
28
  <script>
25
- ${i.script.umd.script}
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 { componentId, locales } = property;
76
- if (!componentId)
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 && _b !== void 0 ? _b : (_c = locales === null || locales === void 0 ? void 0 : locales[defaultLocale]) === null || _c === void 0 ? void 0 : _c.defaultValue,
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;
@@ -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: { mode, id: Array.isArray(id) ? id.join(',') : id, name: Array.isArray(name) ? name.join(',') : name },
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
  }
@@ -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, assignNullableFields, mergeComponent, parsePropertyValue } from '../../utils/property';
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
- key,
49
- (val === null || val === void 0 ? void 0 : val.type) === RenderNestedComponent ? (_jsx(ComponentRenderer, Object.assign({ renderCount: props.renderCount, componentId: val.componentId }, pick(props, 'components', 'locale', 'defaultLocale'), { properties: val.properties }))) : (val),
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
- const PRELOAD_COMPONENTS_STATE = window[PreloadComponentsStateGlobalVariableName];
77
- function useComponent({ componentId, components, locale, defaultLocale, properties: parameters, }) {
78
- var _a, _b, _c, _d, _e;
79
- const preloadRef = useRef();
80
- const scriptRef = useRef({});
81
- if (preloadRef.current)
82
- return preloadRef.current;
83
- const preload = PRELOAD_COMPONENTS_STATE === null || PRELOAD_COMPONENTS_STATE === void 0 ? void 0 : PRELOAD_COMPONENTS_STATE.components[componentId];
84
- if (preload) {
85
- const properties = Object.fromEntries(Object.entries(parameters !== null && parameters !== void 0 ? parameters : {}).map(([id, { value }]) => {
86
- var _a, _b;
87
- const property = (_b = (_a = preload.component.properties) === null || _a === void 0 ? void 0 : _a[id]) === null || _b === void 0 ? void 0 : _b.data;
88
- if (!property)
89
- return [];
90
- return [property.key, parsePropertyValue(property, value, { locale, defaultLocale })];
91
- }));
92
- const mod = window[preload.preloadComponentModuleName];
93
- if (typeof mod === 'function') {
94
- preloadRef.current = {
95
- Component: mod(),
96
- properties: Object.assign(Object.assign({}, 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)), preload.serverSideProps),
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
- return preloadRef.current;
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({
@@ -1,25 +1,29 @@
1
1
  import { PreloadComponentsStateGlobalVariableName } from '../types';
2
- export function injectPreloadComponents({ config, components }) {
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, locales, serverSideProps }) => [
6
- component.id,
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((i) => `\
23
+ ${Object.values(data.components)
24
+ .map(({ script }) => `\
21
25
  <script>
22
- ${i.script.umd.script}
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 { componentId, locales } = property;
71
- if (!componentId)
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 && _b !== void 0 ? _b : (_c = locales === null || locales === void 0 ? void 0 : locales[defaultLocale]) === null || _c === void 0 ? void 0 : _c.defaultValue,
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;
@@ -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;
@@ -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?: string;
11
+ defaultLocale: string;
11
12
  };
12
13
  components: {
13
- component: CustomComponent;
14
- script: {
15
- umd: {
16
- script: string;
17
- moduleName: string;
14
+ [componentId: string]: {
15
+ component: CustomComponent;
16
+ script: {
17
+ umd: {
18
+ script: string;
19
+ moduleName: string;
20
+ };
18
21
  };
19
22
  };
20
- locales: {
23
+ };
24
+ props: {
25
+ [instanceId: string]: {
21
26
  [locale: string]: {
22
- properties: {
23
- [key: string]: any;
24
- };
27
+ [key: string]: any;
25
28
  };
26
29
  };
27
- serverSideProps?: {
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
- [id: string]: PreloadComponent;
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({ config, components }: PreloadComponentsResult): {
2
+ export declare function injectPreloadComponents(...input: PreloadComponentsResult[] | PreloadComponentsResult[][]): {
3
3
  html: string;
4
- } | undefined;
4
+ } | null;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@blocklet/pages-kit",
3
- "version": "0.2.215",
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.19",
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",