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