@blocklet/pages-kit 0.4.32 → 0.4.33

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.
Files changed (50) hide show
  1. package/lib/cjs/api/call.js +7 -18
  2. package/lib/cjs/api/pages-kit.js +5 -17
  3. package/lib/cjs/builtin/async/react-scroll-to-bottom.js +5 -1
  4. package/lib/cjs/builtin/components/LanguageField/index.js +6 -18
  5. package/lib/cjs/builtin/iconify/react.js +1 -1
  6. package/lib/cjs/builtin/page/header.js +8 -5
  7. package/lib/cjs/builtin/session.js +6 -0
  8. package/lib/cjs/builtin/stream.js +1 -1
  9. package/lib/cjs/builtin/utils.js +3 -5
  10. package/lib/cjs/components/CustomComponentRenderer/BlockletReactComponentRenderer.js +5 -19
  11. package/lib/cjs/components/CustomComponentRenderer/DevProvider.js +2 -14
  12. package/lib/cjs/components/CustomComponentRenderer/ErrorComponent.js +2 -14
  13. package/lib/cjs/components/CustomComponentRenderer/index.js +12 -27
  14. package/lib/cjs/components/CustomComponentRenderer/state.js +121 -151
  15. package/lib/cjs/components/ResponsiveImage/index.js +3 -16
  16. package/lib/cjs/tsconfig.tsbuildinfo +1 -1
  17. package/lib/cjs/utils/inject-es-module-shims-options.js +1 -2
  18. package/lib/cjs/utils/inject-global-components-dump-json.js +1 -1
  19. package/lib/cjs/utils/inject-global-components.js +29 -20
  20. package/lib/cjs/utils/preload.js +1 -15
  21. package/lib/cjs/utils/property.js +31 -25
  22. package/lib/cjs/utils/typescript/builtin-module-transformer.js +12 -9
  23. package/lib/esm/api/call.js +7 -18
  24. package/lib/esm/api/pages-kit.js +5 -17
  25. package/lib/esm/builtin/async/react-scroll-to-bottom.js +5 -1
  26. package/lib/esm/builtin/components/LanguageField/index.js +6 -18
  27. package/lib/esm/builtin/iconify/react.js +1 -1
  28. package/lib/esm/builtin/page/header.js +8 -5
  29. package/lib/esm/builtin/session.js +5 -0
  30. package/lib/esm/builtin/stream.js +1 -1
  31. package/lib/esm/builtin/utils.js +3 -5
  32. package/lib/esm/components/CustomComponentRenderer/BlockletReactComponentRenderer.js +5 -19
  33. package/lib/esm/components/CustomComponentRenderer/DevProvider.js +2 -14
  34. package/lib/esm/components/CustomComponentRenderer/ErrorComponent.js +2 -14
  35. package/lib/esm/components/CustomComponentRenderer/index.js +12 -27
  36. package/lib/esm/components/CustomComponentRenderer/state.js +121 -151
  37. package/lib/esm/components/ResponsiveImage/index.js +3 -16
  38. package/lib/esm/tsconfig.tsbuildinfo +1 -1
  39. package/lib/esm/utils/inject-es-module-shims-options.js +1 -2
  40. package/lib/esm/utils/inject-global-components-dump-json.js +1 -1
  41. package/lib/esm/utils/inject-global-components.js +29 -20
  42. package/lib/esm/utils/preload.js +1 -15
  43. package/lib/esm/utils/property.js +30 -25
  44. package/lib/esm/utils/typescript/builtin-module-transformer.js +10 -8
  45. package/lib/types/builtin/session.d.ts +13 -0
  46. package/lib/types/tsconfig.tsbuildinfo +1 -1
  47. package/lib/types/types/core.d.ts +11 -2
  48. package/lib/types/utils/property.d.ts +1 -0
  49. package/lib/types/utils/typescript/builtin-module-transformer.d.ts +1 -0
  50. package/package.json +1 -1
@@ -1,5 +1,4 @@
1
1
  export function injectESModulesShimsOptions(options = {}) {
2
- var _a;
3
2
  // if already initialized, return
4
3
  if (window.esmsInitOptions && !options.force) {
5
4
  return;
@@ -7,7 +6,7 @@ export function injectESModulesShimsOptions(options = {}) {
7
6
  window.esmsInitOptions = {
8
7
  // Notice: 不要修改 shimMode 的默认值,否则可能会影响到其他 blocklet 的正常使用
9
8
  // default is false, do not enable es-module-shims
10
- shimMode: (_a = options.shimMode) !== null && _a !== void 0 ? _a : false,
9
+ shimMode: options.shimMode ?? false,
11
10
  polyfillEnable: ['css-modules', 'json-modules', 'wasm-modules', 'source-phase'],
12
11
  };
13
12
  }
@@ -1128,7 +1128,7 @@ if (cookie && shouldUpdateKnowledge && aiStudioUrl && datasetId) {
1128
1128
  })
1129
1129
  .then((res) => res.json())
1130
1130
  .then(({ items }) => {
1131
- console.info('items count: ', items === null || items === void 0 ? void 0 : items.length);
1131
+ console.info('items count: ', items?.length);
1132
1132
  const successText = (data) => {
1133
1133
  console.info(`${data.name} success`);
1134
1134
  };
@@ -2,7 +2,6 @@ import 'es-module-shims';
2
2
  import React from 'react';
3
3
  import { joinURL } from 'ufo';
4
4
  import * as arcblockUx from '../builtin/arcblock/ux';
5
- import * as aiRuntime from '../builtin/async/ai-runtime';
6
5
  import * as imagePreview from '../builtin/async/image-preview';
7
6
  import * as reactMarkdown from '../builtin/async/react-markdown';
8
7
  import * as reactScrollToBottom from '../builtin/async/react-scroll-to-bottom';
@@ -30,14 +29,13 @@ import { BuiltinModulesGlobalVariableName } from '../types/builtin';
30
29
  import { isRelativeModule } from './typescript/builtin-module-transformer';
31
30
  // Initialize ES Module Shims before any imports
32
31
  export function injectGlobalComponents() {
33
- var _a;
34
32
  const win = window || {};
35
33
  // if already initialized, return
36
34
  if (win[BuiltinModulesGlobalVariableName]) {
37
35
  return;
38
36
  }
39
- const enableShim = !!((_a = window === null || window === void 0 ? void 0 : window.esmsInitOptions) === null || _a === void 0 ? void 0 : _a.shimMode);
40
- if (!enableShim) {
37
+ const enableShim = !!window?.esmsInitOptions?.shimMode && win.importShim;
38
+ if (!enableShim && win.importShim?.addImportMap) {
41
39
  win.importShim.addImportMap = () => {
42
40
  // avoid addImportMap error
43
41
  };
@@ -70,13 +68,12 @@ export function injectGlobalComponents() {
70
68
  '@blocklet/pages-kit/builtin/async/react-markdown': reactMarkdown,
71
69
  '@blocklet/pages-kit/builtin/async/react-syntax-highlighter': reactSyntaxHighlighter,
72
70
  '@blocklet/pages-kit/builtin/async/image-preview': imagePreview,
73
- '@blocklet/pages-kit/builtin/async/ai-runtime': aiRuntime,
71
+ '@blocklet/pages-kit/builtin/async/ai-runtime': import('../builtin/async/ai-runtime'),
74
72
  };
75
73
  // set global variable
76
74
  win[BuiltinModulesGlobalVariableName] = {
77
75
  modules,
78
76
  require(module) {
79
- var _a;
80
77
  // handle builtin module
81
78
  const builtinModule = this.modules[module];
82
79
  if (builtinModule) {
@@ -85,7 +82,7 @@ export function injectGlobalComponents() {
85
82
  // handle relative path import
86
83
  if (isRelativeModule(module)) {
87
84
  const fileName = module.split('/').pop();
88
- const fullUrl = joinURL(window.location.origin, ((_a = window === null || window === void 0 ? void 0 : window.blocklet) === null || _a === void 0 ? void 0 : _a.prefix) || '/', 'chunks', fileName);
85
+ const fullUrl = joinURL(window.location.origin, window?.blocklet?.prefix || '/', 'chunks', fileName);
89
86
  if (enableShim) {
90
87
  const mod = window.importShim(/* @vite-ignore */ fullUrl);
91
88
  return mod;
@@ -97,11 +94,23 @@ export function injectGlobalComponents() {
97
94
  },
98
95
  };
99
96
  // create importmap
100
- const setupImportMap = () => {
97
+ const setupImportMap = async () => {
98
+ const modulesResolved = {};
99
+ // 确保 modules 的 value 中的所有 promise 都加载为 es module
100
+ await Promise.all(Object.entries(modules).map(async ([modulePath, moduleContent]) => {
101
+ if (moduleContent instanceof Promise) {
102
+ const mod = await moduleContent;
103
+ modulesResolved[modulePath] = mod;
104
+ // replace the original moduleContent with the resolved module
105
+ win[BuiltinModulesGlobalVariableName].modules[modulePath] = mod;
106
+ return;
107
+ }
108
+ modulesResolved[modulePath] = moduleContent;
109
+ }));
101
110
  // 计算模块的 hash 值作为缓存版本
102
111
  const calculateModulesHash = () => {
103
112
  // 只提取模块路径和导出的键名用于 hash 计算
104
- const moduleStructure = Object.entries(modules).map(([path, mod]) => ({
113
+ const moduleStructure = Object.entries(modulesResolved).map(([path, mod]) => ({
105
114
  path,
106
115
  exports: Object.keys(mod).sort(),
107
116
  }));
@@ -138,21 +147,21 @@ export function injectGlobalComponents() {
138
147
  return null; // 缓存无效
139
148
  };
140
149
  // 尝试从缓存获取或重新计算
141
- const imports = getImportsFromCache() ||
142
- Object.entries(modules).reduce((acc, [modulePath, moduleContent]) => {
143
- const namedExports = Object.keys(moduleContent).filter((key) => key !== 'default');
144
- const hasDefaultExport = 'default' in moduleContent;
145
- // create module code
146
- const moduleCode = `// GENERATED FILE. DO NOT EDIT.
150
+ const imports = Object.entries(modulesResolved).reduce((acc, [modulePath, moduleContent]) => {
151
+ const moduleContentResolved = moduleContent;
152
+ const namedExports = Object.keys(moduleContentResolved).filter((key) => key !== 'default');
153
+ const hasDefaultExport = 'default' in moduleContentResolved;
154
+ // create module code
155
+ const moduleCode = `// GENERATED FILE. DO NOT EDIT.
147
156
  const moduleSource = window['${BuiltinModulesGlobalVariableName}'].modules['${modulePath}'];
148
157
  ${namedExports.map((name) => `export const ${name} = moduleSource['${name}'];`).join('\n')}
149
158
  export default ${hasDefaultExport ? 'moduleSource.default' : 'moduleSource'};
150
159
  `;
151
- // create base64 code
152
- const base64Code = btoa(moduleCode);
153
- acc[modulePath] = `data:application/javascript;base64,${base64Code}`;
154
- return acc;
155
- }, {});
160
+ // create base64 code
161
+ const base64Code = btoa(moduleCode);
162
+ acc[modulePath] = `data:application/javascript;base64,${base64Code}`;
163
+ return acc;
164
+ }, {});
156
165
  // 如果是新计算的 imports,保存到缓存
157
166
  if (!getImportsFromCache()) {
158
167
  try {
@@ -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 { PreloadComponentScriptModule, PreloadComponentsStateGlobalVariableName, } from '../types';
13
2
  export function injectPreloadComponents(data) {
14
3
  if (!data)
@@ -20,10 +9,7 @@ export function injectPreloadComponents(data) {
20
9
  throw new Error(`Only support inject script with module ${PreloadComponentScriptModule.UMD_FN}`);
21
10
  return [componentId, { componentModuleGlobalVariable: script.moduleName, component }];
22
11
  })),
23
- instances: Object.fromEntries(data.instances.map((_a) => {
24
- var { id } = _a, instance = __rest(_a, ["id"]);
25
- return [id, instance];
26
- })),
12
+ instances: Object.fromEntries(data.instances.map(({ id, ...instance }) => [id, instance])),
27
13
  };
28
14
  return {
29
15
  html: `\
@@ -3,46 +3,43 @@ export function componentUMDName({ componentId }) {
3
3
  return `PagesCustomComponent${componentId}`;
4
4
  }
5
5
  export function mergeComponent({ componentId, getComponent, locale, defaultLocale, properties: parameters, }) {
6
- var _a, _b, _c, _d, _e, _f, _g;
7
6
  let component = getComponent(componentId);
8
7
  if (!component)
9
8
  return null;
10
9
  let script;
11
- const properties = Object.fromEntries(Object.entries(parameters !== null && parameters !== void 0 ? parameters : {}).map(([id, { value }]) => {
12
- var _a, _b;
13
- const property = (_b = (_a = component === null || component === void 0 ? void 0 : component.properties) === null || _a === void 0 ? void 0 : _a[id]) === null || _b === void 0 ? void 0 : _b.data;
10
+ const properties = Object.fromEntries(Object.entries(parameters ?? {}).map(([id, { value }]) => {
11
+ const property = component?.properties?.[id]?.data;
14
12
  if (!property)
15
13
  return [];
16
14
  // // if key is undefined, use id
17
15
  return [property.key || property.id, parsePropertyValue(property, value, { locale, defaultLocale })];
18
16
  }));
19
17
  while (component) {
20
- assignNullableFields(properties, Object.fromEntries(Object.values((_a = component.properties) !== null && _a !== void 0 ? _a : {}).map(({ data }) => {
21
- var _a, _b, _c, _d, _e;
22
- return [
23
- // if key is undefined, use id
24
- data.key || data.id,
25
- parsePropertyValue(data, (_c = (_b = (_a = data.locales) === null || _a === void 0 ? void 0 : _a[locale]) === null || _b === void 0 ? void 0 : _b.defaultValue) !== null && _c !== void 0 ? _c : (_e = (_d = data.locales) === null || _d === void 0 ? void 0 : _d[defaultLocale]) === null || _e === void 0 ? void 0 : _e.defaultValue, { locale, defaultLocale }),
26
- ];
27
- })));
28
- if (((_b = component.renderer) === null || _b === void 0 ? void 0 : _b.type) === 'react-component') {
29
- script = (_c = component.renderer) === null || _c === void 0 ? void 0 : _c.script;
18
+ assignNullableFields(properties, Object.fromEntries(Object.values(component.properties ?? {}).map(({ data }) => [
19
+ // if key is undefined, use id
20
+ data.key || data.id,
21
+ parsePropertyValue(data, data.locales?.[locale]?.defaultValue ?? data.locales?.[defaultLocale]?.defaultValue, { locale, defaultLocale }),
22
+ ])));
23
+ if (component.renderer?.type === 'react-component') {
24
+ script = component.renderer?.script;
30
25
  break;
31
26
  }
32
- if (((_d = component.renderer) === null || _d === void 0 ? void 0 : _d.type) === 'web-component') {
33
- script = (_e = component.renderer) === null || _e === void 0 ? void 0 : _e.script;
27
+ if (component.renderer?.type === 'web-component') {
28
+ script = component.renderer?.script;
34
29
  break;
35
30
  }
36
- if (((_f = component.renderer) === null || _f === void 0 ? void 0 : _f.type) === 'component' && component.renderer.componentId) {
31
+ if (component.renderer?.type === 'component' && component.renderer.componentId) {
37
32
  const { componentId } = component.renderer;
38
33
  const next = getComponent(componentId);
39
34
  const nextParameters = component.renderer.properties;
40
- assignNullableFields(properties, Object.fromEntries(Object.entries((_g = next === null || next === void 0 ? void 0 : next.properties) !== null && _g !== void 0 ? _g : {}).map(([id, { data }]) => {
41
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
42
- const locales = (_a = nextParameters === null || nextParameters === void 0 ? void 0 : nextParameters[id]) === null || _a === void 0 ? void 0 : _a.locales;
35
+ assignNullableFields(properties, Object.fromEntries(Object.entries(next?.properties ?? {}).map(([id, { data }]) => {
36
+ const locales = nextParameters?.[id]?.locales;
43
37
  return [
44
38
  data.key,
45
- parsePropertyValue(data, (_h = (_e = (_c = (_b = locales === null || locales === void 0 ? void 0 : locales[locale]) === null || _b === void 0 ? void 0 : _b.value) !== null && _c !== void 0 ? _c : (_d = locales === null || locales === void 0 ? void 0 : locales[defaultLocale]) === null || _d === void 0 ? void 0 : _d.value) !== null && _e !== void 0 ? _e : (_g = (_f = data.locales) === null || _f === void 0 ? void 0 : _f[locale]) === null || _g === void 0 ? void 0 : _g.defaultValue) !== null && _h !== void 0 ? _h : (_k = (_j = data.locales) === null || _j === void 0 ? void 0 : _j[defaultLocale]) === null || _k === void 0 ? void 0 : _k.defaultValue, { locale, defaultLocale }),
39
+ parsePropertyValue(data, locales?.[locale]?.value ??
40
+ locales?.[defaultLocale]?.value ??
41
+ data.locales?.[locale]?.defaultValue ??
42
+ data.locales?.[defaultLocale]?.defaultValue, { locale, defaultLocale }),
46
43
  ];
47
44
  })));
48
45
  component = next;
@@ -56,13 +53,18 @@ export function mergeComponent({ componentId, getComponent, locale, defaultLocal
56
53
  return { script, properties };
57
54
  }
58
55
  export const RenderNestedComponent = '__RENDER_NESTED_COMPONENT__';
56
+ export function safeJSONParse(value) {
57
+ if (typeof value === 'object' && value !== null) {
58
+ return value;
59
+ }
60
+ return JSON.parse(value);
61
+ }
59
62
  export function parsePropertyValue(property, value, { locale, defaultLocale }) {
60
- var _a, _b, _c, _d, _e;
61
63
  if (property.type === 'json') {
62
64
  if (!value)
63
65
  return undefined;
64
66
  try {
65
- return JSON.parse(value);
67
+ return safeJSONParse(value);
66
68
  }
67
69
  catch (error) {
68
70
  console.error('parse json value error', error);
@@ -81,14 +83,17 @@ export function parsePropertyValue(property, value, { locale, defaultLocale }) {
81
83
  }
82
84
  }
83
85
  if (property.type === 'component') {
84
- const componentId = value === null || value === void 0 ? void 0 : value.componentId;
86
+ const componentId = value?.componentId;
85
87
  if (typeof componentId !== 'string')
86
88
  return undefined;
87
89
  const { locales } = property;
88
90
  return {
89
91
  type: RenderNestedComponent,
90
92
  componentId,
91
- 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),
93
+ properties: {
94
+ ...(locales?.[locale]?.defaultValue?.properties ?? locales?.[defaultLocale]?.defaultValue?.properties),
95
+ ...value?.properties,
96
+ },
92
97
  };
93
98
  }
94
99
  return value;
@@ -2,6 +2,9 @@ import { BuiltinModulesGlobalVariableName } from '../../types/builtin';
2
2
  export const isRelativeModule = (moduleSpecifier) => {
3
3
  return moduleSpecifier.startsWith('./') || moduleSpecifier.startsWith('../');
4
4
  };
5
+ export const isAwaitModule = (moduleSpecifier) => {
6
+ return ['@blocklet/pages-kit/builtin/async/ai-runtime'].includes(moduleSpecifier);
7
+ };
5
8
  export const createBuiltinModuleTransformer = (ts) => (context) => (file) => {
6
9
  const { factory } = context;
7
10
  // 统一的模块导入收集器
@@ -12,27 +15,25 @@ export const createBuiltinModuleTransformer = (ts) => (context) => (file) => {
12
15
  };
13
16
  // filter and collect the import statements that need to be processed
14
17
  const statements = file.statements.filter((s) => {
15
- var _a, _b, _c;
16
18
  if (ts.isImportDeclaration(s) &&
17
19
  ts.isStringLiteral(s.moduleSpecifier) &&
18
20
  isTargetModule(s.moduleSpecifier.text)) {
19
21
  const importInfo = {
20
22
  moduleName: s.moduleSpecifier.text,
21
- name: (_b = (_a = s.importClause) === null || _a === void 0 ? void 0 : _a.name) === null || _b === void 0 ? void 0 : _b.text,
23
+ name: s.importClause?.name?.text,
22
24
  members: [],
23
25
  namespace: '',
24
26
  };
25
- const namedBindings = (_c = s.importClause) === null || _c === void 0 ? void 0 : _c.namedBindings;
27
+ const namedBindings = s.importClause?.namedBindings;
26
28
  if (namedBindings) {
27
29
  if (ts.isNamespaceImport(namedBindings)) {
28
30
  importInfo.namespace = namedBindings.name.text;
29
31
  }
30
32
  namedBindings.forEachChild((node) => {
31
- var _a;
32
33
  if (ts.isImportSpecifier(node)) {
33
34
  importInfo.members.push({
34
35
  name: node.name.text,
35
- propertyName: (_a = node.propertyName) === null || _a === void 0 ? void 0 : _a.text,
36
+ propertyName: node.propertyName?.text,
36
37
  });
37
38
  }
38
39
  });
@@ -46,7 +47,9 @@ export const createBuiltinModuleTransformer = (ts) => (context) => (file) => {
46
47
  // call inject-global-components require method
47
48
  const requireCall = factory.createCallExpression(factory.createPropertyAccessExpression(factory.createIdentifier(BuiltinModulesGlobalVariableName), 'require'), undefined, [factory.createStringLiteral(importInfo.moduleName)]);
48
49
  // create await expression if the module is ../ or ./
49
- const mod = isRelativeModule(importInfo.moduleName) ? factory.createAwaitExpression(requireCall) : requireCall;
50
+ const mod = isRelativeModule(importInfo.moduleName) || isAwaitModule(importInfo.moduleName)
51
+ ? factory.createAwaitExpression(requireCall)
52
+ : requireCall;
50
53
  return [
51
54
  importInfo.name
52
55
  ? factory.createVariableStatement([], factory.createVariableDeclarationList([
@@ -61,9 +64,8 @@ export const createBuiltinModuleTransformer = (ts) => (context) => (file) => {
61
64
  : undefined,
62
65
  // handle named import
63
66
  ...importInfo.members.map((member) => {
64
- var _a;
65
67
  return factory.createVariableStatement([], factory.createVariableDeclarationList([
66
- factory.createVariableDeclaration(factory.createIdentifier(member.name), undefined, undefined, factory.createPropertyAccessExpression(mod, (_a = member.propertyName) !== null && _a !== void 0 ? _a : member.name)),
68
+ factory.createVariableDeclaration(factory.createIdentifier(member.name), undefined, undefined, factory.createPropertyAccessExpression(mod, member.propertyName ?? member.name)),
67
69
  ], ts.NodeFlags.Const));
68
70
  }),
69
71
  ].filter((i) => !!i);
@@ -1 +1,14 @@
1
1
  export * from '@arcblock/did-connect/lib/Session';
2
+ export declare function useSessionContext(): {
3
+ session: {
4
+ user?: {
5
+ did: string;
6
+ fullName: string;
7
+ avatar: string;
8
+ role: string;
9
+ };
10
+ loading?: boolean;
11
+ login: (cb?: () => void) => any;
12
+ };
13
+ events: any;
14
+ };