@blocklet/pages-kit 0.4.32 → 0.4.34
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 -18
- package/lib/cjs/api/pages-kit.js +5 -17
- package/lib/cjs/builtin/async/react-scroll-to-bottom.js +5 -1
- package/lib/cjs/builtin/components/LanguageField/index.js +6 -18
- package/lib/cjs/builtin/iconify/react.js +1 -1
- package/lib/cjs/builtin/page/header.js +8 -5
- package/lib/cjs/builtin/session.js +6 -0
- package/lib/cjs/builtin/stream.js +1 -1
- package/lib/cjs/builtin/utils.js +3 -5
- package/lib/cjs/components/CustomComponentRenderer/BlockletReactComponentRenderer.js +5 -19
- package/lib/cjs/components/CustomComponentRenderer/DevProvider.js +2 -14
- package/lib/cjs/components/CustomComponentRenderer/ErrorComponent.js +2 -14
- package/lib/cjs/components/CustomComponentRenderer/index.js +12 -27
- package/lib/cjs/components/CustomComponentRenderer/state.js +121 -151
- package/lib/cjs/components/ResponsiveImage/index.js +3 -16
- package/lib/cjs/tsconfig.tsbuildinfo +1 -1
- package/lib/cjs/utils/inject-es-module-shims-options.js +1 -2
- package/lib/cjs/utils/inject-global-components-dump-json.js +1 -1
- package/lib/cjs/utils/inject-global-components.js +29 -20
- package/lib/cjs/utils/preload.js +1 -15
- package/lib/cjs/utils/property.js +31 -25
- package/lib/cjs/utils/typescript/builtin-module-transformer.js +12 -9
- package/lib/esm/api/call.js +7 -18
- package/lib/esm/api/pages-kit.js +5 -17
- package/lib/esm/builtin/async/react-scroll-to-bottom.js +5 -1
- package/lib/esm/builtin/components/LanguageField/index.js +6 -18
- package/lib/esm/builtin/iconify/react.js +1 -1
- package/lib/esm/builtin/page/header.js +8 -5
- package/lib/esm/builtin/session.js +5 -0
- package/lib/esm/builtin/stream.js +1 -1
- package/lib/esm/builtin/utils.js +3 -5
- package/lib/esm/components/CustomComponentRenderer/BlockletReactComponentRenderer.js +5 -19
- package/lib/esm/components/CustomComponentRenderer/DevProvider.js +2 -14
- package/lib/esm/components/CustomComponentRenderer/ErrorComponent.js +2 -14
- package/lib/esm/components/CustomComponentRenderer/index.js +12 -27
- package/lib/esm/components/CustomComponentRenderer/state.js +121 -151
- package/lib/esm/components/ResponsiveImage/index.js +3 -16
- package/lib/esm/tsconfig.tsbuildinfo +1 -1
- package/lib/esm/utils/inject-es-module-shims-options.js +1 -2
- package/lib/esm/utils/inject-global-components-dump-json.js +1 -1
- package/lib/esm/utils/inject-global-components.js +29 -20
- package/lib/esm/utils/preload.js +1 -15
- package/lib/esm/utils/property.js +30 -25
- package/lib/esm/utils/typescript/builtin-module-transformer.js +10 -8
- package/lib/types/builtin/session.d.ts +13 -0
- package/lib/types/tsconfig.tsbuildinfo +1 -1
- package/lib/types/types/core.d.ts +11 -2
- package/lib/types/utils/property.d.ts +1 -0
- package/lib/types/utils/typescript/builtin-module-transformer.d.ts +1 -0
- package/package.json +1 -1
|
@@ -1,14 +1,3 @@
|
|
|
1
|
-
var __rest = (this && this.__rest) || function (s, e) {
|
|
2
|
-
var t = {};
|
|
3
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
4
|
-
t[p] = s[p];
|
|
5
|
-
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
6
|
-
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
7
|
-
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
8
|
-
t[p[i]] = s[p[i]];
|
|
9
|
-
}
|
|
10
|
-
return t;
|
|
11
|
-
};
|
|
12
1
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
13
2
|
import { ErrorBoundary } from 'react-error-boundary';
|
|
14
3
|
import { RenderNestedComponent } from '../../utils/property';
|
|
@@ -20,40 +9,36 @@ import { useComponent } from './state';
|
|
|
20
9
|
export * from './state';
|
|
21
10
|
export * from './context';
|
|
22
11
|
const MAXIMUM_RENDER_STACK_SIZE = 20;
|
|
23
|
-
export default function CustomComponentRenderer(
|
|
24
|
-
var _b;
|
|
25
|
-
var { dev } = _a, props = __rest(_a, ["dev"]);
|
|
12
|
+
export default function CustomComponentRenderer({ dev, ...props }) {
|
|
26
13
|
const inheritedDev = useDev();
|
|
27
14
|
const BuiltinComponent = BuiltinComponents[props.componentId];
|
|
28
|
-
return (_jsx(ErrorBoundary, { FallbackComponent:
|
|
15
|
+
return (_jsx(ErrorBoundary, { FallbackComponent: props?.fallbackRender || ComponentError, resetKeys: [Date.now()], children: _jsx(DevProvider, { dev: dev ?? inheritedDev, children: BuiltinComponent ? (_jsx(BuiltinComponent, { ...props })) : (_jsx(ComponentRenderer, { ...props, instanceId: props.instanceId ?? props.componentId, renderType: props.renderType })) }) }));
|
|
29
16
|
}
|
|
30
17
|
const BuiltinComponents = {
|
|
31
18
|
'blocklet-react-component': BlockletReactComponentRenderer,
|
|
32
19
|
};
|
|
33
|
-
function ComponentRenderer(
|
|
34
|
-
var _b;
|
|
35
|
-
var { renderCount = 0, blockletId, blockletTitle, componentName, renderType = 'view' } = _a, props = __rest(_a, ["renderCount", "blockletId", "blockletTitle", "componentName", "renderType"]);
|
|
20
|
+
function ComponentRenderer({ renderCount = 0, blockletId, blockletTitle, componentName, renderType = 'view', ...props }) {
|
|
36
21
|
const dev = useDev();
|
|
37
22
|
if (renderCount > MAXIMUM_RENDER_STACK_SIZE) {
|
|
38
23
|
throw new Error('Maximum render stack size exceeded');
|
|
39
24
|
}
|
|
40
|
-
const component = useComponent(
|
|
25
|
+
const component = useComponent({ ...props, dev });
|
|
41
26
|
const ctx = useCustomComponentRenderer();
|
|
42
|
-
if (component
|
|
27
|
+
if (component?.error) {
|
|
43
28
|
throw component.error;
|
|
44
29
|
}
|
|
45
30
|
const renderComponentChildren = (Component) => {
|
|
46
|
-
const children = (_jsx(Renderer,
|
|
47
|
-
return
|
|
31
|
+
const children = (_jsx(Renderer, { renderCount: renderCount + 1, ...props, Component: Component, props: { ...component?.props, ...props.props } }));
|
|
32
|
+
return ctx?.customRendererComponent ? (_jsx(ctx.customRendererComponent, { Component: Component, children: children })) : (children);
|
|
48
33
|
};
|
|
49
|
-
if (
|
|
34
|
+
if (component?.Component && renderType === 'view') {
|
|
50
35
|
return renderComponentChildren(component.Component);
|
|
51
36
|
}
|
|
52
|
-
if (
|
|
37
|
+
if (component?.EditComponent && renderType === 'setting') {
|
|
53
38
|
return renderComponentChildren(component.EditComponent);
|
|
54
39
|
}
|
|
55
40
|
// if the component is not in the dev.components, it means the component is not loaded
|
|
56
|
-
if (
|
|
41
|
+
if (dev?.mode === 'draft' && dev && dev.components?.[props.componentId] === undefined) {
|
|
57
42
|
return (_jsx(ComponentError, { componentId: props.componentId, componentName: componentName, blockletId: blockletId, blockletTitle: blockletTitle, message: "Component not found in available components", showHints: true }));
|
|
58
43
|
}
|
|
59
44
|
return null;
|
|
@@ -62,8 +47,8 @@ function Renderer({ renderCount, Component, locale, props, }) {
|
|
|
62
47
|
const componentProps = Object.fromEntries(Object.entries(props).map(([key, val]) => {
|
|
63
48
|
return [
|
|
64
49
|
key,
|
|
65
|
-
|
|
50
|
+
val?.type === RenderNestedComponent ? (_jsx(ComponentRenderer, { locale: locale, renderCount: renderCount, componentId: val.componentId, properties: val.properties, props: val.props })) : (val),
|
|
66
51
|
];
|
|
67
52
|
}));
|
|
68
|
-
return _jsx(Component,
|
|
53
|
+
return _jsx(Component, { locale: locale, ...componentProps });
|
|
69
54
|
}
|
|
@@ -1,23 +1,3 @@
|
|
|
1
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
-
});
|
|
9
|
-
};
|
|
10
|
-
var __rest = (this && this.__rest) || function (s, e) {
|
|
11
|
-
var t = {};
|
|
12
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
13
|
-
t[p] = s[p];
|
|
14
|
-
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
15
|
-
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
16
|
-
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
17
|
-
t[p[i]] = s[p[i]];
|
|
18
|
-
}
|
|
19
|
-
return t;
|
|
20
|
-
};
|
|
21
1
|
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
|
|
22
2
|
import { cx } from '@emotion/css';
|
|
23
3
|
import { Skeleton, Fade, Box } from '@mui/material';
|
|
@@ -46,9 +26,9 @@ function importCustomComponent(m, { componentId }) {
|
|
|
46
26
|
// 只在组件首次加载时执行,避免重复加载
|
|
47
27
|
useEffect(() => {
|
|
48
28
|
let isMounted = true;
|
|
49
|
-
const loadComponent = () =>
|
|
29
|
+
const loadComponent = async () => {
|
|
50
30
|
try {
|
|
51
|
-
const result =
|
|
31
|
+
const result = await m;
|
|
52
32
|
if (!isMounted)
|
|
53
33
|
return;
|
|
54
34
|
if (!result) {
|
|
@@ -76,7 +56,7 @@ function importCustomComponent(m, { componentId }) {
|
|
|
76
56
|
setLoading(false);
|
|
77
57
|
}
|
|
78
58
|
}
|
|
79
|
-
}
|
|
59
|
+
};
|
|
80
60
|
loadComponent();
|
|
81
61
|
return () => {
|
|
82
62
|
isMounted = false;
|
|
@@ -92,7 +72,7 @@ function importCustomComponent(m, { componentId }) {
|
|
|
92
72
|
}
|
|
93
73
|
// 动态渲染组件,但允许props更新传递
|
|
94
74
|
if (ResolvedComponent) {
|
|
95
|
-
return _jsx(ResolvedComponent,
|
|
75
|
+
return _jsx(ResolvedComponent, { ...props });
|
|
96
76
|
}
|
|
97
77
|
return null;
|
|
98
78
|
}, [loading, error, ResolvedComponent, props]);
|
|
@@ -131,7 +111,7 @@ function importCustomComponent(m, { componentId }) {
|
|
|
131
111
|
// if a component is found and has CSS, wrap the component with styles
|
|
132
112
|
if (Component && css) {
|
|
133
113
|
const OriginalComponent = Component;
|
|
134
|
-
Component = (props) => (_jsxs(_Fragment, { children: [_jsx(Helmet, { children: _jsx("style", { children: css }) }), _jsx(OriginalComponent,
|
|
114
|
+
Component = (props) => (_jsxs(_Fragment, { children: [_jsx(Helmet, { children: _jsx("style", { children: css }) }), _jsx(OriginalComponent, { ...props, className: cx(props.className, `CustomComponent_${componentId}`) })] }));
|
|
135
115
|
}
|
|
136
116
|
// keep aigneOutputValueSchema
|
|
137
117
|
if (Component && m.aigneOutputValueSchema) {
|
|
@@ -141,17 +121,15 @@ function importCustomComponent(m, { componentId }) {
|
|
|
141
121
|
}
|
|
142
122
|
export function getPropertiesFromCode(m) {
|
|
143
123
|
let propertiesFromCode = null;
|
|
144
|
-
if (typeof
|
|
124
|
+
if (typeof m?.PROPERTIES_SCHEMA === 'object' && m?.PROPERTIES_SCHEMA.length) {
|
|
145
125
|
propertiesFromCode = {};
|
|
146
126
|
m.PROPERTIES_SCHEMA.forEach((data, index) => {
|
|
147
|
-
var _a, _b, _c;
|
|
148
127
|
// @ts-ignore
|
|
149
|
-
|
|
150
|
-
var _a, _b;
|
|
128
|
+
window?.blocklet?.languages?.forEach?.((item) => {
|
|
151
129
|
const { code: localKey } = item;
|
|
152
|
-
if (!
|
|
130
|
+
if (!data?.locales?.[localKey]) {
|
|
153
131
|
// fallback to en
|
|
154
|
-
set(data, `locales.${localKey}`,
|
|
132
|
+
set(data, `locales.${localKey}`, data.locales?.en);
|
|
155
133
|
}
|
|
156
134
|
});
|
|
157
135
|
propertiesFromCode[data.id] = {
|
|
@@ -163,10 +141,10 @@ export function getPropertiesFromCode(m) {
|
|
|
163
141
|
return propertiesFromCode;
|
|
164
142
|
}
|
|
165
143
|
export const customComponentStates = () => {
|
|
166
|
-
states
|
|
144
|
+
states ??= create()(immer((set, get) => ({
|
|
167
145
|
state: {
|
|
168
|
-
config:
|
|
169
|
-
components: Object.fromEntries(Object.entries(
|
|
146
|
+
config: { ...PRELOAD_COMPONENTS_STATE?.config },
|
|
147
|
+
components: Object.fromEntries(Object.entries({ ...PRELOAD_COMPONENTS_STATE?.components })
|
|
170
148
|
.map(([componentId, preload]) => {
|
|
171
149
|
let Component;
|
|
172
150
|
if (preload.componentModuleGlobalVariable) {
|
|
@@ -186,15 +164,14 @@ export const customComponentStates = () => {
|
|
|
186
164
|
console.warn(`Component global variable ${preload.componentModuleGlobalVariable} is not a function`);
|
|
187
165
|
}
|
|
188
166
|
}
|
|
189
|
-
return [componentId,
|
|
167
|
+
return [componentId, { ...preload, Component }];
|
|
190
168
|
})
|
|
191
169
|
.filter((i) => !!i)),
|
|
192
|
-
instances:
|
|
170
|
+
instances: { ...PRELOAD_COMPONENTS_STATE?.instances },
|
|
193
171
|
},
|
|
194
172
|
getComponent({ instanceId, componentId, locale }) {
|
|
195
|
-
var _a, _b, _c, _d;
|
|
196
173
|
const { config: { supportedLocales }, components, instances, } = get().state;
|
|
197
|
-
if (!
|
|
174
|
+
if (!supportedLocales?.some((i) => i.locale === locale))
|
|
198
175
|
return null;
|
|
199
176
|
const c = components[componentId];
|
|
200
177
|
if (!c)
|
|
@@ -203,92 +180,87 @@ export const customComponentStates = () => {
|
|
|
203
180
|
if (!Component)
|
|
204
181
|
return null;
|
|
205
182
|
if (!instanceId) {
|
|
206
|
-
return { component, Component, componentProperties:
|
|
183
|
+
return { component, Component, componentProperties: components[componentId]?.component.properties };
|
|
207
184
|
}
|
|
208
185
|
const instance = instances[instanceId];
|
|
209
186
|
if (!instance)
|
|
210
187
|
return null;
|
|
211
|
-
const props =
|
|
188
|
+
const props = instance.locales?.[locale]?.props;
|
|
212
189
|
if (!props)
|
|
213
190
|
return null;
|
|
214
|
-
return { component, Component, props, componentProperties:
|
|
191
|
+
return { component, Component, props, componentProperties: components[componentId]?.component.properties };
|
|
215
192
|
},
|
|
216
|
-
loadComponents(input) {
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
(_a = (_c = s.state.instances)[instanceId]) !== null && _a !== void 0 ? _a : (_c[instanceId] = { componentId: instance.componentId });
|
|
227
|
-
(_b = (_d = s.state.instances[instanceId]).locales) !== null && _b !== void 0 ? _b : (_d.locales = {});
|
|
228
|
-
Object.assign(s.state.instances[instanceId].locales, instance.locales);
|
|
229
|
-
});
|
|
193
|
+
async loadComponents(input) {
|
|
194
|
+
const result = await loadComponents(input);
|
|
195
|
+
if (result) {
|
|
196
|
+
set((s) => {
|
|
197
|
+
s.state.config = result.config;
|
|
198
|
+
Object.assign(s.state.components, result.components);
|
|
199
|
+
Object.entries(result.instances).forEach(([instanceId, instance]) => {
|
|
200
|
+
s.state.instances[instanceId] ??= { componentId: instance.componentId };
|
|
201
|
+
s.state.instances[instanceId].locales ??= {};
|
|
202
|
+
Object.assign(s.state.instances[instanceId].locales, instance.locales);
|
|
230
203
|
});
|
|
231
|
-
}
|
|
232
|
-
}
|
|
204
|
+
});
|
|
205
|
+
}
|
|
233
206
|
},
|
|
234
|
-
})))
|
|
207
|
+
})));
|
|
235
208
|
return states;
|
|
236
209
|
};
|
|
237
|
-
function loadComponents(input) {
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
});
|
|
210
|
+
async function loadComponents(input) {
|
|
211
|
+
const result = await preloadComponents({ ...input, module: PreloadComponentScriptModule.ESM });
|
|
212
|
+
if (!result)
|
|
213
|
+
return null;
|
|
214
|
+
const components = Object.fromEntries(await Promise.all(Object.values(result.components).map(async (item) => {
|
|
215
|
+
if (item.script.module !== PreloadComponentScriptModule.ESM) {
|
|
216
|
+
throw new Error('received non esm component script');
|
|
217
|
+
}
|
|
218
|
+
const url = URL.createObjectURL(new Blob([item.script.script], { type: 'application/javascript' }));
|
|
219
|
+
const m = await import(/* @vite-ignore */ url);
|
|
220
|
+
if (typeof m?.default !== 'function') {
|
|
221
|
+
throw new Error(`Invalid custom component module: ${typeof m?.default}`);
|
|
222
|
+
}
|
|
223
|
+
return [
|
|
224
|
+
item.component.id,
|
|
225
|
+
{
|
|
226
|
+
component: item.component,
|
|
227
|
+
Component: importCustomComponent(m, { componentId: item.component.id }),
|
|
228
|
+
},
|
|
229
|
+
];
|
|
230
|
+
})));
|
|
231
|
+
return {
|
|
232
|
+
...result,
|
|
233
|
+
components,
|
|
234
|
+
instances: Object.fromEntries(result.instances.map(({ id, ...instance }) => [id, instance])),
|
|
235
|
+
};
|
|
264
236
|
}
|
|
265
237
|
export function useComponent({ instanceId, componentId, properties, locale, dev }) {
|
|
266
|
-
var _a, _b, _c;
|
|
267
238
|
const transpile = useTranspileComponent({ componentId, locale, properties, dev });
|
|
268
239
|
const preload = usePreloadComponent({ instanceId, componentId, properties, locale, dev });
|
|
269
|
-
const componentProperties =
|
|
240
|
+
const componentProperties = transpile?.componentProperties ?? preload?.componentProperties;
|
|
270
241
|
return {
|
|
271
|
-
Component:
|
|
272
|
-
EditComponent: transpile
|
|
273
|
-
props:
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
242
|
+
Component: transpile?.Component ?? preload?.Component,
|
|
243
|
+
EditComponent: transpile?.EditComponent,
|
|
244
|
+
props: {
|
|
245
|
+
...(transpile?.props ?? preload?.props),
|
|
246
|
+
...Object.fromEntries(Object.entries(properties ?? {})
|
|
247
|
+
.map(([key, { value }]) => {
|
|
248
|
+
const property = componentProperties?.[key]?.data;
|
|
249
|
+
if (!property)
|
|
250
|
+
return undefined;
|
|
251
|
+
const v = parsePropertyValue(property, value, { locale, defaultLocale: dev?.defaultLocale });
|
|
252
|
+
// if key is undefined, use id
|
|
253
|
+
return [property.key || property.id, v];
|
|
254
|
+
})
|
|
255
|
+
.filter((i) => !!i)),
|
|
256
|
+
},
|
|
257
|
+
error: transpile?.error,
|
|
285
258
|
};
|
|
286
259
|
}
|
|
287
260
|
const COMPONENT_LOADER_MAP = {};
|
|
288
261
|
function usePreloadComponent({ instanceId, componentId, properties, locale, dev }) {
|
|
289
|
-
var _a, _b;
|
|
290
262
|
const previousRef = useRef();
|
|
291
|
-
const requestLocale = customComponentStates()((s) =>
|
|
263
|
+
const requestLocale = customComponentStates()((s) => s.state.config.supportedLocales?.some((i) => i.locale === locale) ? locale : undefined);
|
|
292
264
|
const defaultLocale = customComponentStates()((s) => s.state.config.defaultLocale);
|
|
293
265
|
const realLocale = requestLocale || defaultLocale;
|
|
294
266
|
const result = customComponentStates()((s) => realLocale ? s.getComponent({ instanceId, componentId, locale: realLocale }) : undefined);
|
|
@@ -298,37 +270,38 @@ function usePreloadComponent({ instanceId, componentId, properties, locale, dev
|
|
|
298
270
|
}
|
|
299
271
|
if (instanceId) {
|
|
300
272
|
const key = [instanceId, realLocale].join('-');
|
|
301
|
-
|
|
273
|
+
COMPONENT_LOADER_MAP[key] ??= customComponentStates()
|
|
302
274
|
.getState()
|
|
303
275
|
.loadComponents({
|
|
304
|
-
mode: dev
|
|
305
|
-
projectId:
|
|
276
|
+
mode: dev?.mode,
|
|
277
|
+
projectId: dev?.projectId ?? window.__PROJECT_ID__,
|
|
306
278
|
locale: realLocale,
|
|
307
279
|
instances: [{ id: instanceId, component: { id: componentId }, properties }],
|
|
308
|
-
})
|
|
280
|
+
});
|
|
309
281
|
if (previousRef.current)
|
|
310
282
|
return previousRef.current;
|
|
311
283
|
}
|
|
312
284
|
return null;
|
|
313
285
|
}
|
|
314
286
|
function useTranspileComponent({ componentId, locale, properties, dev: { defaultLocale, components } = {}, }) {
|
|
315
|
-
var _a, _b;
|
|
316
287
|
const [{ error, Component, EditComponent, propertiesFromCode }, setComponent] = useState({});
|
|
317
288
|
const component = mergeComponent({
|
|
318
289
|
componentId,
|
|
319
290
|
getComponent: (componentId) => {
|
|
320
|
-
var _a, _b, _c, _d;
|
|
321
291
|
// setting properties from code if it's not set from components props
|
|
322
|
-
if (propertiesFromCode && isEmpty(
|
|
323
|
-
return
|
|
292
|
+
if (propertiesFromCode && isEmpty(components?.[componentId]?.data?.properties)) {
|
|
293
|
+
return {
|
|
294
|
+
...components?.[componentId]?.data,
|
|
295
|
+
properties: propertiesFromCode,
|
|
296
|
+
};
|
|
324
297
|
}
|
|
325
|
-
return
|
|
298
|
+
return components?.[componentId]?.data;
|
|
326
299
|
},
|
|
327
300
|
locale,
|
|
328
301
|
defaultLocale,
|
|
329
302
|
properties,
|
|
330
303
|
});
|
|
331
|
-
const script = useDeferredValue(component
|
|
304
|
+
const script = useDeferredValue(component?.script);
|
|
332
305
|
useEffect(() => {
|
|
333
306
|
if (script) {
|
|
334
307
|
transpileAndLoadScript(script)
|
|
@@ -346,48 +319,45 @@ function useTranspileComponent({ componentId, locale, properties, dev: { default
|
|
|
346
319
|
error,
|
|
347
320
|
Component,
|
|
348
321
|
EditComponent,
|
|
349
|
-
props: component
|
|
350
|
-
componentProperties:
|
|
322
|
+
props: component?.properties,
|
|
323
|
+
componentProperties: components?.[componentId]?.data?.properties,
|
|
351
324
|
};
|
|
352
325
|
}
|
|
353
|
-
export function transpileAndLoadScript(script) {
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
if (m) {
|
|
366
|
-
return m;
|
|
367
|
-
}
|
|
326
|
+
export async function transpileAndLoadScript(script) {
|
|
327
|
+
// @ts-ignore
|
|
328
|
+
if (import.meta.env.DEV && window.__BLOCK_STUDIO__?.getCodeImportPath) {
|
|
329
|
+
try {
|
|
330
|
+
// @ts-ignore
|
|
331
|
+
const filePath = joinURL(window.__BLOCK_STUDIO__?.getCodeImportPath(script));
|
|
332
|
+
if (filePath) {
|
|
333
|
+
// eslint-disable-next-line no-console
|
|
334
|
+
console.info('get code import path from block studio:', filePath);
|
|
335
|
+
const m = await import(/* @vite-ignore */ filePath).catch(() => null);
|
|
336
|
+
if (m) {
|
|
337
|
+
return m;
|
|
368
338
|
}
|
|
369
339
|
}
|
|
370
|
-
catch (e) {
|
|
371
|
-
// ignore error
|
|
372
|
-
}
|
|
373
340
|
}
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
import('../../utils/typescript/builtin-module-transformer'),
|
|
377
|
-
]);
|
|
378
|
-
const compiled = ts.transpileModule(script, {
|
|
379
|
-
compilerOptions: { jsx: ts.JsxEmit.React, module: ts.ModuleKind.ESNext, target: ts.ScriptTarget.ES2016 },
|
|
380
|
-
transformers: {
|
|
381
|
-
before: [createBuiltinModuleTransformer(ts)],
|
|
382
|
-
},
|
|
383
|
-
}).outputText;
|
|
384
|
-
// fallback to the original handling
|
|
385
|
-
const url = URL.createObjectURL(new Blob([compiled], { type: 'application/javascript' }));
|
|
386
|
-
try {
|
|
387
|
-
return yield import(/* @vite-ignore */ url);
|
|
341
|
+
catch (e) {
|
|
342
|
+
// ignore error
|
|
388
343
|
}
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
344
|
+
}
|
|
345
|
+
const [ts, { createBuiltinModuleTransformer }] = await Promise.all([
|
|
346
|
+
import('typescript').then((m) => m.default || m),
|
|
347
|
+
import('../../utils/typescript/builtin-module-transformer'),
|
|
348
|
+
]);
|
|
349
|
+
const compiled = ts.transpileModule(script, {
|
|
350
|
+
compilerOptions: { jsx: ts.JsxEmit.React, module: ts.ModuleKind.ESNext, target: ts.ScriptTarget.ES2016 },
|
|
351
|
+
transformers: {
|
|
352
|
+
before: [createBuiltinModuleTransformer(ts)],
|
|
353
|
+
},
|
|
354
|
+
}).outputText;
|
|
355
|
+
// fallback to the original handling
|
|
356
|
+
const url = URL.createObjectURL(new Blob([compiled], { type: 'application/javascript' }));
|
|
357
|
+
try {
|
|
358
|
+
return await import(/* @vite-ignore */ url);
|
|
359
|
+
}
|
|
360
|
+
finally {
|
|
361
|
+
URL.revokeObjectURL(url);
|
|
362
|
+
}
|
|
393
363
|
}
|
|
@@ -1,30 +1,17 @@
|
|
|
1
|
-
var __rest = (this && this.__rest) || function (s, e) {
|
|
2
|
-
var t = {};
|
|
3
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
4
|
-
t[p] = s[p];
|
|
5
|
-
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
6
|
-
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
7
|
-
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
8
|
-
t[p[i]] = s[p[i]];
|
|
9
|
-
}
|
|
10
|
-
return t;
|
|
11
|
-
};
|
|
12
1
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
13
2
|
import { useTheme } from '@arcblock/ux/lib/Theme';
|
|
14
3
|
import { Box } from '@mui/material';
|
|
15
|
-
export default function ResponsiveImage(
|
|
16
|
-
var { src, sizesAttr, quality = 90, sx, alt } = _a, props = __rest(_a, ["src", "sizesAttr", "quality", "sx", "alt"]);
|
|
4
|
+
export default function ResponsiveImage({ src, sizesAttr, quality = 90, sx, alt, ...props }) {
|
|
17
5
|
const theme = useTheme();
|
|
18
6
|
const breakpoints = theme.breakpoints.values;
|
|
19
7
|
// Generate srcset based on each breakpoint width
|
|
20
8
|
const generateSrcSet = (baseUrl, breakpointWidths) => {
|
|
21
|
-
var _a;
|
|
22
9
|
// Check if URL already has query parameters
|
|
23
10
|
if (baseUrl.includes('?')) {
|
|
24
11
|
return '';
|
|
25
12
|
}
|
|
26
13
|
// Check file extension
|
|
27
|
-
const fileExtension =
|
|
14
|
+
const fileExtension = baseUrl.split('.').pop()?.toLowerCase();
|
|
28
15
|
const supportedExtensions = ['png', 'jpg', 'jpeg', 'webp', 'gif'];
|
|
29
16
|
if (!fileExtension || !supportedExtensions.includes(fileExtension)) {
|
|
30
17
|
return '';
|
|
@@ -61,5 +48,5 @@ export default function ResponsiveImage(_a) {
|
|
|
61
48
|
.filter((size) => size !== null)
|
|
62
49
|
.join(', ');
|
|
63
50
|
};
|
|
64
|
-
return (_jsx(Box,
|
|
51
|
+
return (_jsx(Box, { component: "img", src: src, srcSet: generateSrcSet(src, sizesAttr), sizes: generateSizes(sizesAttr), alt: alt ?? '', loading: "lazy", sx: sx, ...props }));
|
|
65
52
|
}
|