@blocklet/pages-kit 0.4.113 → 0.4.115
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/components/CustomComponentRenderer/index.js +3 -2
- package/lib/cjs/components/CustomComponentRenderer/state.js +36 -32
- package/lib/cjs/tsconfig.tsbuildinfo +1 -1
- package/lib/cjs/utils/property.js +4 -4
- package/lib/esm/components/CustomComponentRenderer/index.js +3 -2
- package/lib/esm/components/CustomComponentRenderer/state.js +36 -32
- package/lib/esm/tsconfig.tsbuildinfo +1 -1
- package/lib/esm/utils/property.js +4 -4
- package/lib/types/tsconfig.tsbuildinfo +1 -1
- package/lib/types/utils/property.d.ts +1 -1
- package/package.json +3 -3
|
@@ -41,7 +41,7 @@ function mergeComponent({ componentId, getComponent, locale, defaultLocale, prop
|
|
|
41
41
|
if (!component)
|
|
42
42
|
return null;
|
|
43
43
|
let script;
|
|
44
|
-
const
|
|
44
|
+
const props = Object.fromEntries(Object.entries(parameters ?? {}).map(([id, { value }]) => {
|
|
45
45
|
const property = component?.properties?.[id]?.data;
|
|
46
46
|
if (!property)
|
|
47
47
|
return [];
|
|
@@ -49,7 +49,7 @@ function mergeComponent({ componentId, getComponent, locale, defaultLocale, prop
|
|
|
49
49
|
return [property.key || property.id, parsePropertyValue(property, value, { locale, defaultLocale })];
|
|
50
50
|
}));
|
|
51
51
|
while (component) {
|
|
52
|
-
assignNullableFields(
|
|
52
|
+
assignNullableFields(props, Object.fromEntries(Object.values(component.properties ?? {}).map(({ data }) => [
|
|
53
53
|
// if key is undefined, use id
|
|
54
54
|
data.key || data.id,
|
|
55
55
|
parsePropertyValue(data, data.locales?.[locale]?.defaultValue ?? data.locales?.[defaultLocale]?.defaultValue, { locale, defaultLocale }),
|
|
@@ -66,7 +66,7 @@ function mergeComponent({ componentId, getComponent, locale, defaultLocale, prop
|
|
|
66
66
|
const { componentId } = component.renderer;
|
|
67
67
|
const next = getComponent(componentId);
|
|
68
68
|
const nextParameters = component.renderer.properties;
|
|
69
|
-
assignNullableFields(
|
|
69
|
+
assignNullableFields(props, Object.fromEntries(Object.entries(next?.properties ?? {}).map(([id, { data }]) => {
|
|
70
70
|
const locales = nextParameters?.[id]?.locales;
|
|
71
71
|
return [
|
|
72
72
|
data.key,
|
|
@@ -84,7 +84,7 @@ function mergeComponent({ componentId, getComponent, locale, defaultLocale, prop
|
|
|
84
84
|
}
|
|
85
85
|
if (!script)
|
|
86
86
|
return null;
|
|
87
|
-
return { script,
|
|
87
|
+
return { script, props };
|
|
88
88
|
}
|
|
89
89
|
exports.RenderNestedComponent = '__RENDER_NESTED_COMPONENT__';
|
|
90
90
|
function safeJSONParse(value) {
|
|
@@ -29,13 +29,14 @@ function ComponentRenderer({ renderCount = 0, blockletId, blockletTitle, compone
|
|
|
29
29
|
const parsedProps = useMemo(() => {
|
|
30
30
|
const result = {
|
|
31
31
|
...component?.props,
|
|
32
|
+
// 外部传递进来的 props 优先级最高
|
|
32
33
|
...props?.props,
|
|
33
34
|
};
|
|
35
|
+
const defaultLocale = dev?.defaultLocale ?? 'en';
|
|
36
|
+
const locale = props?.locale;
|
|
34
37
|
Object.entries(component?.properties ?? {}).forEach(([id, value]) => {
|
|
35
38
|
const { data: property } = value;
|
|
36
39
|
const key = property.key ?? property.id ?? id;
|
|
37
|
-
const defaultLocale = dev?.defaultLocale ?? 'en';
|
|
38
|
-
const locale = props?.locale;
|
|
39
40
|
const defaultValue = property.locales?.[locale]?.defaultValue ?? property.locales?.[defaultLocale]?.defaultValue;
|
|
40
41
|
result[key] = parsePropertyValue(property, result[key] ?? defaultValue, {
|
|
41
42
|
locale: props?.locale,
|
|
@@ -241,49 +241,53 @@ export function useComponent({ instanceId, componentId, properties, locale, dev
|
|
|
241
241
|
const transpile = useTranspileComponent({ componentId, locale, properties, dev });
|
|
242
242
|
const preload = usePreloadComponent({ instanceId, componentId, properties, locale, dev });
|
|
243
243
|
const componentProperties = transpile?.componentProperties ?? preload?.componentProperties;
|
|
244
|
+
// mode: production 不会有 transpile.props, dev 模式下会有 transpile.props,优先级最低
|
|
245
|
+
const componentProps = transpile?.props ?? preload?.props;
|
|
246
|
+
// 对 properties 进行转译,优先级中等
|
|
247
|
+
const dynamicComponentProps = useMemo(() => {
|
|
248
|
+
return Object.fromEntries(Object.entries(properties ?? {})
|
|
249
|
+
.map(([key, { value }]) => {
|
|
250
|
+
const property = componentProperties?.[key]?.data;
|
|
251
|
+
if (!property)
|
|
252
|
+
return undefined;
|
|
253
|
+
// if key is undefined, use id
|
|
254
|
+
const propKey = property.key ?? property.id;
|
|
255
|
+
const propValue = value;
|
|
256
|
+
// keep preload props
|
|
257
|
+
const v = parsePropertyValue(property, propValue, {
|
|
258
|
+
locale,
|
|
259
|
+
defaultLocale: dev?.defaultLocale,
|
|
260
|
+
});
|
|
261
|
+
return [propKey, v];
|
|
262
|
+
})
|
|
263
|
+
.filter((i) => !!i));
|
|
264
|
+
}, [JSON.stringify(properties), JSON.stringify(componentProperties), locale, dev?.defaultLocale]);
|
|
244
265
|
return {
|
|
245
266
|
Component: transpile?.Component ?? preload?.Component,
|
|
246
267
|
EditComponent: transpile?.EditComponent,
|
|
247
268
|
props: {
|
|
248
|
-
...
|
|
249
|
-
...
|
|
250
|
-
.map(([key, { value }]) => {
|
|
251
|
-
const property = componentProperties?.[key]?.data;
|
|
252
|
-
if (!property)
|
|
253
|
-
return undefined;
|
|
254
|
-
// 如果提供了 key 优先使用 key
|
|
255
|
-
const propKey = property.key ?? property.id;
|
|
256
|
-
const propValue = value;
|
|
257
|
-
// keep preload props
|
|
258
|
-
let v = parsePropertyValue(property, propValue, {
|
|
259
|
-
locale,
|
|
260
|
-
defaultLocale: dev?.defaultLocale,
|
|
261
|
-
});
|
|
262
|
-
if (dev?.mode === 'production' &&
|
|
263
|
-
property.type === 'component' &&
|
|
264
|
-
property.key &&
|
|
265
|
-
preload?.props?.[property.key]) {
|
|
266
|
-
v = {
|
|
267
|
-
...(preload?.props?.[property.key] ?? {}),
|
|
268
|
-
...v,
|
|
269
|
-
};
|
|
270
|
-
}
|
|
271
|
-
// if key is undefined, use id
|
|
272
|
-
return [propKey, v];
|
|
273
|
-
})
|
|
274
|
-
.filter((i) => !!i)),
|
|
269
|
+
...componentProps,
|
|
270
|
+
...dynamicComponentProps,
|
|
275
271
|
},
|
|
276
272
|
error: transpile?.error,
|
|
277
273
|
properties: componentProperties,
|
|
278
274
|
};
|
|
279
275
|
}
|
|
280
276
|
const COMPONENT_LOADER_MAP = {};
|
|
281
|
-
|
|
282
|
-
const previousRef = useRef();
|
|
277
|
+
const getRealLocale = (locale) => {
|
|
283
278
|
const requestLocale = customComponentStates()((s) => s.state.config.supportedLocales?.some((i) => i.locale === locale) ? locale : undefined);
|
|
284
279
|
const defaultLocale = customComponentStates()((s) => s.state.config.defaultLocale);
|
|
285
|
-
|
|
286
|
-
|
|
280
|
+
return requestLocale || defaultLocale;
|
|
281
|
+
};
|
|
282
|
+
const getComponentByComponentId = ({ componentId, locale, instanceId, }) => {
|
|
283
|
+
const realLocale = getRealLocale(locale);
|
|
284
|
+
const result = customComponentStates()((s) => realLocale ? s.getComponent({ componentId, locale: realLocale, instanceId }) : undefined);
|
|
285
|
+
return result;
|
|
286
|
+
};
|
|
287
|
+
function usePreloadComponent({ instanceId, componentId, properties, locale, dev }) {
|
|
288
|
+
const previousRef = useRef();
|
|
289
|
+
const realLocale = getRealLocale(locale);
|
|
290
|
+
const result = getComponentByComponentId({ componentId, locale: realLocale, instanceId });
|
|
287
291
|
if (result) {
|
|
288
292
|
previousRef.current = result;
|
|
289
293
|
return result;
|
|
@@ -339,7 +343,7 @@ function useTranspileComponent({ componentId, locale, properties, dev: { default
|
|
|
339
343
|
error,
|
|
340
344
|
Component,
|
|
341
345
|
EditComponent,
|
|
342
|
-
props: component?.
|
|
346
|
+
props: component?.props,
|
|
343
347
|
componentProperties: components?.[componentId]?.data?.properties,
|
|
344
348
|
};
|
|
345
349
|
}
|