@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.
@@ -49,13 +49,14 @@ function ComponentRenderer({ renderCount = 0, blockletId, blockletTitle, compone
49
49
  const parsedProps = (0, react_1.useMemo)(() => {
50
50
  const result = {
51
51
  ...component?.props,
52
+ // 外部传递进来的 props 优先级最高
52
53
  ...props?.props,
53
54
  };
55
+ const defaultLocale = dev?.defaultLocale ?? 'en';
56
+ const locale = props?.locale;
54
57
  Object.entries(component?.properties ?? {}).forEach(([id, value]) => {
55
58
  const { data: property } = value;
56
59
  const key = property.key ?? property.id ?? id;
57
- const defaultLocale = dev?.defaultLocale ?? 'en';
58
- const locale = props?.locale;
59
60
  const defaultValue = property.locales?.[locale]?.defaultValue ?? property.locales?.[defaultLocale]?.defaultValue;
60
61
  result[key] = (0, property_1.parsePropertyValue)(property, result[key] ?? defaultValue, {
61
62
  locale: props?.locale,
@@ -274,49 +274,53 @@ function useComponent({ instanceId, componentId, properties, locale, dev }) {
274
274
  const transpile = useTranspileComponent({ componentId, locale, properties, dev });
275
275
  const preload = usePreloadComponent({ instanceId, componentId, properties, locale, dev });
276
276
  const componentProperties = transpile?.componentProperties ?? preload?.componentProperties;
277
+ // mode: production 不会有 transpile.props, dev 模式下会有 transpile.props,优先级最低
278
+ const componentProps = transpile?.props ?? preload?.props;
279
+ // 对 properties 进行转译,优先级中等
280
+ const dynamicComponentProps = (0, react_1.useMemo)(() => {
281
+ return Object.fromEntries(Object.entries(properties ?? {})
282
+ .map(([key, { value }]) => {
283
+ const property = componentProperties?.[key]?.data;
284
+ if (!property)
285
+ return undefined;
286
+ // if key is undefined, use id
287
+ const propKey = property.key ?? property.id;
288
+ const propValue = value;
289
+ // keep preload props
290
+ const v = (0, property_1.parsePropertyValue)(property, propValue, {
291
+ locale,
292
+ defaultLocale: dev?.defaultLocale,
293
+ });
294
+ return [propKey, v];
295
+ })
296
+ .filter((i) => !!i));
297
+ }, [JSON.stringify(properties), JSON.stringify(componentProperties), locale, dev?.defaultLocale]);
277
298
  return {
278
299
  Component: transpile?.Component ?? preload?.Component,
279
300
  EditComponent: transpile?.EditComponent,
280
301
  props: {
281
- ...(transpile?.props ?? preload?.props),
282
- ...Object.fromEntries(Object.entries(properties ?? {})
283
- .map(([key, { value }]) => {
284
- const property = componentProperties?.[key]?.data;
285
- if (!property)
286
- return undefined;
287
- // 如果提供了 key 优先使用 key
288
- const propKey = property.key ?? property.id;
289
- const propValue = value;
290
- // keep preload props
291
- let v = (0, property_1.parsePropertyValue)(property, propValue, {
292
- locale,
293
- defaultLocale: dev?.defaultLocale,
294
- });
295
- if (dev?.mode === 'production' &&
296
- property.type === 'component' &&
297
- property.key &&
298
- preload?.props?.[property.key]) {
299
- v = {
300
- ...(preload?.props?.[property.key] ?? {}),
301
- ...v,
302
- };
303
- }
304
- // if key is undefined, use id
305
- return [propKey, v];
306
- })
307
- .filter((i) => !!i)),
302
+ ...componentProps,
303
+ ...dynamicComponentProps,
308
304
  },
309
305
  error: transpile?.error,
310
306
  properties: componentProperties,
311
307
  };
312
308
  }
313
309
  const COMPONENT_LOADER_MAP = {};
314
- function usePreloadComponent({ instanceId, componentId, properties, locale, dev }) {
315
- const previousRef = (0, react_1.useRef)();
310
+ const getRealLocale = (locale) => {
316
311
  const requestLocale = (0, exports.customComponentStates)()((s) => s.state.config.supportedLocales?.some((i) => i.locale === locale) ? locale : undefined);
317
312
  const defaultLocale = (0, exports.customComponentStates)()((s) => s.state.config.defaultLocale);
318
- const realLocale = requestLocale || defaultLocale;
319
- const result = (0, exports.customComponentStates)()((s) => realLocale ? s.getComponent({ instanceId, componentId, locale: realLocale }) : undefined);
313
+ return requestLocale || defaultLocale;
314
+ };
315
+ const getComponentByComponentId = ({ componentId, locale, instanceId, }) => {
316
+ const realLocale = getRealLocale(locale);
317
+ const result = (0, exports.customComponentStates)()((s) => realLocale ? s.getComponent({ componentId, locale: realLocale, instanceId }) : undefined);
318
+ return result;
319
+ };
320
+ function usePreloadComponent({ instanceId, componentId, properties, locale, dev }) {
321
+ const previousRef = (0, react_1.useRef)();
322
+ const realLocale = getRealLocale(locale);
323
+ const result = getComponentByComponentId({ componentId, locale: realLocale, instanceId });
320
324
  if (result) {
321
325
  previousRef.current = result;
322
326
  return result;
@@ -372,7 +376,7 @@ function useTranspileComponent({ componentId, locale, properties, dev: { default
372
376
  error,
373
377
  Component,
374
378
  EditComponent,
375
- props: component?.properties,
379
+ props: component?.props,
376
380
  componentProperties: components?.[componentId]?.data?.properties,
377
381
  };
378
382
  }