@blocklet/pages-kit-core 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/block.js CHANGED
@@ -22,70 +22,59 @@ var __importStar = (this && this.__importStar) || function (mod) {
22
22
  __setModuleDefault(result, mod);
23
23
  return result;
24
24
  };
25
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
26
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
27
- return new (P || (P = Promise))(function (resolve, reject) {
28
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
29
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
30
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
31
- step((generator = generator.apply(thisArg, _arguments || [])).next());
32
- });
33
- };
34
25
  Object.defineProperty(exports, "__esModule", { value: true });
35
26
  exports.Block = void 0;
36
27
  class Block {
28
+ code;
29
+ // 预编译正则表达式
30
+ ESM_IMPORT_REGEX = /import\s+\w+\s+from/;
31
+ ESM_KEYWORDS = ['export {', 'export default', 'import {', 'import *'];
37
32
  constructor(code) {
38
33
  this.code = code;
39
- // 预编译正则表达式
40
- this.ESM_IMPORT_REGEX = /import\s+\w+\s+from/;
41
- this.ESM_KEYWORDS = ['export {', 'export default', 'import {', 'import *'];
42
34
  }
43
35
  setData(code) {
44
36
  this.code = code;
45
37
  }
46
- render(input, _options) {
47
- return __awaiter(this, void 0, void 0, function* () {
48
- var _a, _b;
49
- if (!this.code) {
50
- return null;
51
- }
52
- try {
53
- const isESM = this.ESM_KEYWORDS.some((keyword) => { var _a; return (_a = this.code) === null || _a === void 0 ? void 0 : _a.includes(keyword); }) ||
54
- this.ESM_IMPORT_REGEX.test(this.code || '');
55
- if (!isESM) {
56
- // UMD 格式处理
57
- if (!((_a = this.code) === null || _a === void 0 ? void 0 : _a.includes('function(')) && !((_b = this.code) === null || _b === void 0 ? void 0 : _b.includes('function ('))) {
58
- throw new Error('Invalid code format: UMD wrapper not found');
59
- }
60
- if (!this.code.includes('Component')) {
61
- throw new Error('Invalid UMD format: Component export not found');
62
- }
63
- const Component = new Function(`${this.code}; return Component;`)();
64
- if (typeof Component !== 'function') {
65
- throw new Error('Invalid UMD format: Component is not a function');
66
- }
67
- return Component(Object.assign({}, input));
38
+ async render(input, _options) {
39
+ if (!this.code) {
40
+ return null;
41
+ }
42
+ try {
43
+ const isESM = this.ESM_KEYWORDS.some((keyword) => this.code?.includes(keyword)) ||
44
+ this.ESM_IMPORT_REGEX.test(this.code || '');
45
+ if (!isESM) {
46
+ // UMD 格式处理
47
+ if (!this.code?.includes('function(') && !this.code?.includes('function (')) {
48
+ throw new Error('Invalid code format: UMD wrapper not found');
68
49
  }
69
- // ESM 格式处理
70
- const blob = new Blob([this.code], { type: 'application/javascript' });
71
- const url = URL.createObjectURL(blob);
72
- try {
73
- const module = yield Promise.resolve(`${url}`).then(s => __importStar(require(s)));
74
- const Component = module.default;
75
- if (typeof Component !== 'function') {
76
- throw new Error('Invalid ESM format: default export is not a function');
77
- }
78
- return Component(Object.assign({}, input));
50
+ if (!this.code.includes('Component')) {
51
+ throw new Error('Invalid UMD format: Component export not found');
79
52
  }
80
- finally {
81
- URL.revokeObjectURL(url);
53
+ const Component = new Function(`${this.code}; return Component;`)();
54
+ if (typeof Component !== 'function') {
55
+ throw new Error('Invalid UMD format: Component is not a function');
56
+ }
57
+ return Component({ ...input });
58
+ }
59
+ // ESM 格式处理
60
+ const blob = new Blob([this.code], { type: 'application/javascript' });
61
+ const url = URL.createObjectURL(blob);
62
+ try {
63
+ const module = await Promise.resolve(`${url}`).then(s => __importStar(require(s)));
64
+ const Component = module.default;
65
+ if (typeof Component !== 'function') {
66
+ throw new Error('Invalid ESM format: default export is not a function');
82
67
  }
68
+ return Component({ ...input });
83
69
  }
84
- catch (error) {
85
- console.error('Failed to render block:', error);
86
- throw new Error(`Failed to execute code: ${error.message}`);
70
+ finally {
71
+ URL.revokeObjectURL(url);
87
72
  }
88
- });
73
+ }
74
+ catch (error) {
75
+ console.error('Failed to render block:', error);
76
+ throw new Error(`Failed to execute code: ${error.message}`);
77
+ }
89
78
  }
90
79
  }
91
80
  exports.Block = Block;
package/lib/cjs/core.js CHANGED
@@ -1,41 +1,36 @@
1
1
  "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
2
  Object.defineProperty(exports, "__esModule", { value: true });
12
3
  exports.Core = void 0;
13
4
  const lodash_1 = require("lodash");
14
5
  class Core {
6
+ blocks;
7
+ datasets;
8
+ pages;
9
+ options;
15
10
  constructor({ blocks = new Map(), datasets = new Map(), pages = new Map(), options = {}, } = {}) {
16
- this.block = {
17
- render: (_input, _options) => __awaiter(this, void 0, void 0, function* () {
18
- const options = this.mergeRenderOptions(_options);
19
- // eslint-disable-next-line no-console
20
- console.log('options', options);
21
- // Implementation here
22
- return Promise.resolve();
23
- }),
24
- };
25
- this.page = {
26
- render: (_input, _options) => __awaiter(this, void 0, void 0, function* () {
27
- const options = this.mergeRenderOptions(_options);
28
- // eslint-disable-next-line no-console
29
- console.log('options', options);
30
- // Implementation here
31
- return Promise.resolve();
32
- }),
33
- };
34
11
  this.blocks = blocks;
35
12
  this.datasets = datasets;
36
13
  this.pages = pages;
37
14
  this.options = options;
38
15
  }
16
+ block = {
17
+ render: async (_input, _options) => {
18
+ const options = this.mergeRenderOptions(_options);
19
+ // eslint-disable-next-line no-console
20
+ console.log('options', options);
21
+ // Implementation here
22
+ return Promise.resolve();
23
+ },
24
+ };
25
+ page = {
26
+ render: async (_input, _options) => {
27
+ const options = this.mergeRenderOptions(_options);
28
+ // eslint-disable-next-line no-console
29
+ console.log('options', options);
30
+ // Implementation here
31
+ return Promise.resolve();
32
+ },
33
+ };
39
34
  mergeRenderOptions(renderOptions) {
40
35
  return (0, lodash_1.merge)({}, this.options, renderOptions);
41
36
  }
@@ -1,34 +1,20 @@
1
1
  "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
2
  Object.defineProperty(exports, "__esModule", { value: true });
12
3
  exports.Dataset = void 0;
13
4
  class Dataset {
5
+ store;
14
6
  constructor(initialData = {}) {
15
7
  this.store = initialData;
16
8
  }
17
- get(key) {
18
- return __awaiter(this, void 0, void 0, function* () {
19
- return this.store[key] || null;
20
- });
9
+ async get(key) {
10
+ return this.store[key] || null;
21
11
  }
22
- set(key, value) {
23
- return __awaiter(this, void 0, void 0, function* () {
24
- this.store[key] = value;
25
- return this.store[key];
26
- });
12
+ async set(key, value) {
13
+ this.store[key] = value;
14
+ return this.store[key];
27
15
  }
28
- clear() {
29
- return __awaiter(this, void 0, void 0, function* () {
30
- this.store = {};
31
- });
16
+ async clear() {
17
+ this.store = {};
32
18
  }
33
19
  }
34
20
  exports.Dataset = Dataset;
package/lib/cjs/page.js CHANGED
@@ -1,19 +1,11 @@
1
1
  "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
2
  Object.defineProperty(exports, "__esModule", { value: true });
12
3
  exports.Page = void 0;
13
4
  const block_1 = require("./block");
14
5
  class Page {
6
+ data;
7
+ blocks = new Map();
15
8
  constructor(data) {
16
- this.blocks = new Map();
17
9
  this.data = data;
18
10
  if (data) {
19
11
  this.initBlocks(data.sections);
@@ -31,7 +23,10 @@ class Page {
31
23
  // 这里实现页面容器组件的创建逻辑
32
24
  return {
33
25
  type: 'div',
34
- props: Object.assign(Object.assign({}, props), { children }),
26
+ props: {
27
+ ...props,
28
+ children,
29
+ },
35
30
  };
36
31
  }
37
32
  setData(data) {
@@ -39,40 +34,47 @@ class Page {
39
34
  this.blocks.clear();
40
35
  this.initBlocks(data.sections);
41
36
  }
42
- render(input, options) {
43
- return __awaiter(this, void 0, void 0, function* () {
44
- if (!this.data) {
45
- return null;
46
- }
47
- try {
48
- // 1. 处理页面级别的配置
49
- const pageProps = Object.assign({ id: this.data.id, backgroundColor: this.data.meta.backgroundColor }, input);
50
- // @FIXME: 这个 map 应该是不需要的
51
- // 使用 Block 实例渲染 sections
52
- const sections = yield Promise.all(this.data.sections.map((section) => __awaiter(this, void 0, void 0, function* () {
53
- try {
54
- const block = this.blocks.get(section.id);
55
- if (!block) {
56
- throw new Error(`Block not found for section ${section.id}`);
57
- }
58
- // 合并配置传入 Block
59
- const sectionProps = Object.assign(Object.assign({ key: section.id, id: section.id }, section.config), section.properties);
60
- const result = yield block.render(sectionProps, options);
61
- return result;
62
- }
63
- catch (error) {
64
- console.error(`Failed to render section ${section.id}:`, error);
65
- return null;
37
+ async render(input, options) {
38
+ if (!this.data) {
39
+ return null;
40
+ }
41
+ try {
42
+ // 1. 处理页面级别的配置
43
+ const pageProps = {
44
+ id: this.data.id,
45
+ backgroundColor: this.data.meta.backgroundColor,
46
+ ...input,
47
+ };
48
+ // @FIXME: 这个 map 应该是不需要的
49
+ // 使用 Block 实例渲染 sections
50
+ const sections = await Promise.all(this.data.sections.map(async (section) => {
51
+ try {
52
+ const block = this.blocks.get(section.id);
53
+ if (!block) {
54
+ throw new Error(`Block not found for section ${section.id}`);
66
55
  }
67
- })));
68
- // 3. 返回页面组件
69
- return this.createPageElement(pageProps, sections.filter(Boolean));
70
- }
71
- catch (error) {
72
- console.error('Failed to render page:', error);
73
- throw new Error(`Failed to render page: ${error.message}`);
74
- }
75
- });
56
+ // 合并配置传入 Block
57
+ const sectionProps = {
58
+ key: section.id,
59
+ id: section.id,
60
+ ...section.config,
61
+ ...section.properties,
62
+ };
63
+ const result = await block.render(sectionProps, options);
64
+ return result;
65
+ }
66
+ catch (error) {
67
+ console.error(`Failed to render section ${section.id}:`, error);
68
+ return null;
69
+ }
70
+ }));
71
+ // 3. 返回页面组件
72
+ return this.createPageElement(pageProps, sections.filter(Boolean));
73
+ }
74
+ catch (error) {
75
+ console.error('Failed to render page:', error);
76
+ throw new Error(`Failed to render page: ${error.message}`);
77
+ }
76
78
  }
77
79
  }
78
80
  exports.Page = Page;