@blocklet/pages-kit-core 0.0.1

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 (71) hide show
  1. package/LICENSE +13 -0
  2. package/lib/cjs/block.d.ts +11 -0
  3. package/lib/cjs/block.js +91 -0
  4. package/lib/cjs/core.d.ts +12 -0
  5. package/lib/cjs/core.js +43 -0
  6. package/lib/cjs/dataset.d.ts +8 -0
  7. package/lib/cjs/dataset.js +34 -0
  8. package/lib/cjs/index.d.ts +3 -0
  9. package/lib/cjs/index.js +19 -0
  10. package/lib/cjs/page.d.ts +11 -0
  11. package/lib/cjs/page.js +77 -0
  12. package/lib/cjs/renderable.d.ts +6 -0
  13. package/lib/cjs/renderable.js +6 -0
  14. package/lib/cjs/tsconfig.tsbuildinfo +1 -0
  15. package/lib/cjs/types/block.d.ts +4 -0
  16. package/lib/cjs/types/block.js +2 -0
  17. package/lib/cjs/types/core.d.ts +22 -0
  18. package/lib/cjs/types/core.js +2 -0
  19. package/lib/cjs/types/dataset.d.ts +16 -0
  20. package/lib/cjs/types/dataset.js +2 -0
  21. package/lib/cjs/types/env.d.ts +6 -0
  22. package/lib/cjs/types/env.js +3 -0
  23. package/lib/cjs/types/index.d.ts +6 -0
  24. package/lib/cjs/types/index.js +22 -0
  25. package/lib/cjs/types/page.d.ts +28 -0
  26. package/lib/cjs/types/page.js +2 -0
  27. package/lib/cjs/types/renderable.d.ts +11 -0
  28. package/lib/cjs/types/renderable.js +2 -0
  29. package/lib/esm/block.d.ts +11 -0
  30. package/lib/esm/block.js +64 -0
  31. package/lib/esm/core.d.ts +12 -0
  32. package/lib/esm/core.js +39 -0
  33. package/lib/esm/dataset.d.ts +8 -0
  34. package/lib/esm/dataset.js +30 -0
  35. package/lib/esm/index.d.ts +3 -0
  36. package/lib/esm/index.js +3 -0
  37. package/lib/esm/page.d.ts +11 -0
  38. package/lib/esm/page.js +73 -0
  39. package/lib/esm/renderable.d.ts +6 -0
  40. package/lib/esm/renderable.js +2 -0
  41. package/lib/esm/tsconfig.tsbuildinfo +1 -0
  42. package/lib/esm/types/block.d.ts +4 -0
  43. package/lib/esm/types/block.js +1 -0
  44. package/lib/esm/types/core.d.ts +22 -0
  45. package/lib/esm/types/core.js +1 -0
  46. package/lib/esm/types/dataset.d.ts +16 -0
  47. package/lib/esm/types/dataset.js +1 -0
  48. package/lib/esm/types/env.d.ts +6 -0
  49. package/lib/esm/types/env.js +2 -0
  50. package/lib/esm/types/index.d.ts +6 -0
  51. package/lib/esm/types/index.js +6 -0
  52. package/lib/esm/types/page.d.ts +28 -0
  53. package/lib/esm/types/page.js +1 -0
  54. package/lib/esm/types/renderable.d.ts +11 -0
  55. package/lib/esm/types/renderable.js +1 -0
  56. package/lib/types/block.d.ts +11 -0
  57. package/lib/types/core.d.ts +12 -0
  58. package/lib/types/dataset.d.ts +8 -0
  59. package/lib/types/index.d.ts +3 -0
  60. package/lib/types/page.d.ts +11 -0
  61. package/lib/types/renderable.d.ts +6 -0
  62. package/lib/types/tsconfig.tsbuildinfo +1 -0
  63. package/lib/types/types/block.d.ts +4 -0
  64. package/lib/types/types/core.d.ts +22 -0
  65. package/lib/types/types/dataset.d.ts +16 -0
  66. package/lib/types/types/env.d.ts +6 -0
  67. package/lib/types/types/index.d.ts +6 -0
  68. package/lib/types/types/page.d.ts +28 -0
  69. package/lib/types/types/renderable.d.ts +11 -0
  70. package/package.json +75 -0
  71. package/tsconfig.json +16 -0
package/LICENSE ADDED
@@ -0,0 +1,13 @@
1
+ Copyright 2018-2020 ArcBlock
2
+
3
+ Licensed under the Apache License, Version 2.0 (the "License");
4
+ you may not use this file except in compliance with the License.
5
+ You may obtain a copy of the License at
6
+
7
+ http://www.apache.org/licenses/LICENSE-2.0
8
+
9
+ Unless required by applicable law or agreed to in writing, software
10
+ distributed under the License is distributed on an "AS IS" BASIS,
11
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ See the License for the specific language governing permissions and
13
+ limitations under the License.
@@ -0,0 +1,11 @@
1
+ import { Block as BlockType } from '@types';
2
+ import { ReactElement } from 'react';
3
+ import { Renderable, RenderOptions } from './types/renderable';
4
+ export declare class Block implements BlockType, Renderable<Record<string, any>, ReactElement | null> {
5
+ code?: string | undefined;
6
+ private ESM_IMPORT_REGEX;
7
+ private ESM_KEYWORDS;
8
+ constructor(code?: string | undefined);
9
+ setData(code: string): void;
10
+ render(input?: Record<string, any>, _options?: RenderOptions): Promise<ReactElement | null>;
11
+ }
@@ -0,0 +1,91 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
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
+ Object.defineProperty(exports, "__esModule", { value: true });
35
+ exports.Block = void 0;
36
+ class Block {
37
+ constructor(code) {
38
+ this.code = code;
39
+ // 预编译正则表达式
40
+ this.ESM_IMPORT_REGEX = /import\s+\w+\s+from/;
41
+ this.ESM_KEYWORDS = ['export {', 'export default', 'import {', 'import *'];
42
+ }
43
+ setData(code) {
44
+ this.code = code;
45
+ }
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));
68
+ }
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));
79
+ }
80
+ finally {
81
+ URL.revokeObjectURL(url);
82
+ }
83
+ }
84
+ catch (error) {
85
+ console.error('Failed to render block:', error);
86
+ throw new Error(`Failed to execute code: ${error.message}`);
87
+ }
88
+ });
89
+ }
90
+ }
91
+ exports.Block = Block;
@@ -0,0 +1,12 @@
1
+ import { Block, CoreConstructor, CoreOptions, ICore, Page } from '@types';
2
+ import { Dataset } from './dataset';
3
+ export declare class Core<B extends Block = Block, D extends Dataset = Dataset, P extends Page = Page> implements ICore<B, D, P> {
4
+ readonly blocks: Map<string, B>;
5
+ readonly datasets: Map<string, D>;
6
+ readonly pages: Map<string, P>;
7
+ readonly options: CoreOptions;
8
+ constructor({ blocks, datasets, pages, options, }?: CoreConstructor<B, D, P>);
9
+ readonly block: Block;
10
+ readonly page: Page;
11
+ private mergeRenderOptions;
12
+ }
@@ -0,0 +1,43 @@
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
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.Core = void 0;
13
+ const lodash_1 = require("lodash");
14
+ class Core {
15
+ 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
+ this.blocks = blocks;
35
+ this.datasets = datasets;
36
+ this.pages = pages;
37
+ this.options = options;
38
+ }
39
+ mergeRenderOptions(renderOptions) {
40
+ return (0, lodash_1.merge)({}, this.options, renderOptions);
41
+ }
42
+ }
43
+ exports.Core = Core;
@@ -0,0 +1,8 @@
1
+ import { DatasetStructure, IDataset } from '@types';
2
+ export declare class Dataset implements IDataset<DatasetStructure> {
3
+ private store;
4
+ constructor(initialData?: DatasetStructure);
5
+ get(key: string): Promise<DatasetStructure | null>;
6
+ set(key: string, value: DatasetStructure): Promise<DatasetStructure | null>;
7
+ clear(): Promise<void>;
8
+ }
@@ -0,0 +1,34 @@
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
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.Dataset = void 0;
13
+ class Dataset {
14
+ constructor(initialData = {}) {
15
+ this.store = initialData;
16
+ }
17
+ get(key) {
18
+ return __awaiter(this, void 0, void 0, function* () {
19
+ return this.store[key] || null;
20
+ });
21
+ }
22
+ set(key, value) {
23
+ return __awaiter(this, void 0, void 0, function* () {
24
+ this.store[key] = value;
25
+ return this.store[key];
26
+ });
27
+ }
28
+ clear() {
29
+ return __awaiter(this, void 0, void 0, function* () {
30
+ this.store = {};
31
+ });
32
+ }
33
+ }
34
+ exports.Dataset = Dataset;
@@ -0,0 +1,3 @@
1
+ export * from './types';
2
+ export * from './core';
3
+ export * from './dataset';
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./types"), exports);
18
+ __exportStar(require("./core"), exports);
19
+ __exportStar(require("./dataset"), exports);
@@ -0,0 +1,11 @@
1
+ import { Page as PageType, PageData, PageBlocks, RenderOptions } from '@types';
2
+ import { ReactElement } from 'react';
3
+ export declare class Page implements PageType {
4
+ private data?;
5
+ blocks: PageBlocks;
6
+ constructor(data?: PageData);
7
+ private initBlocks;
8
+ private createPageElement;
9
+ setData(data: PageData): void;
10
+ render(input?: Record<string, any>, options?: RenderOptions): Promise<ReactElement | null>;
11
+ }
@@ -0,0 +1,77 @@
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
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.Page = void 0;
13
+ const block_1 = require("./block");
14
+ class Page {
15
+ constructor(data) {
16
+ this.blocks = new Map();
17
+ this.data = data;
18
+ if (data) {
19
+ this.initBlocks(data.sections);
20
+ }
21
+ }
22
+ initBlocks(sections) {
23
+ sections.forEach((section) => {
24
+ if (section.code) {
25
+ const block = new block_1.Block(section.code);
26
+ this.blocks.set(section.id, block);
27
+ }
28
+ });
29
+ }
30
+ createPageElement(props, children) {
31
+ // 这里实现页面容器组件的创建逻辑
32
+ return {
33
+ type: 'div',
34
+ props: Object.assign(Object.assign({}, props), { children }),
35
+ };
36
+ }
37
+ setData(data) {
38
+ this.data = data;
39
+ this.blocks.clear();
40
+ this.initBlocks(data.sections);
41
+ }
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
+ return block.render(sectionProps, options);
61
+ }
62
+ catch (error) {
63
+ console.error(`Failed to render section ${section.id}:`, error);
64
+ return null;
65
+ }
66
+ })));
67
+ // 3. 返回页面组件
68
+ return this.createPageElement(pageProps, sections.filter(Boolean));
69
+ }
70
+ catch (error) {
71
+ console.error('Failed to render page:', error);
72
+ throw new Error(`Failed to render page: ${error.message}`);
73
+ }
74
+ });
75
+ }
76
+ }
77
+ exports.Page = Page;
@@ -0,0 +1,6 @@
1
+ export declare abstract class Renderable {
2
+ abstract render(): {
3
+ type: string;
4
+ props: Record<string, any>;
5
+ };
6
+ }
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Renderable = void 0;
4
+ class Renderable {
5
+ }
6
+ exports.Renderable = Renderable;