@blocklet/pages-kit 0.4.117 → 0.4.119
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/components/CustomComponentRenderer/state.js +34 -4
- package/lib/cjs/tsconfig.tsbuildinfo +1 -1
- package/lib/cjs/utils/agent.js +5 -0
- package/lib/cjs/utils/builtin.js +1 -0
- package/lib/cjs/utils/data-source.js +70 -0
- package/lib/cjs/utils/inject-global-components.js +2 -0
- package/lib/cjs/utils/property.js +3 -1
- package/lib/cjs/utils/typescript/builtin-module-transformer.js +39 -17
- package/lib/esm/components/CustomComponentRenderer/state.js +34 -4
- package/lib/esm/tsconfig.tsbuildinfo +1 -1
- package/lib/esm/utils/agent.js +2 -0
- package/lib/esm/utils/builtin.js +1 -0
- package/lib/esm/utils/data-source.js +67 -0
- package/lib/esm/utils/inject-global-components.js +2 -0
- package/lib/esm/utils/property.js +3 -1
- package/lib/esm/utils/typescript/builtin-module-transformer.js +36 -16
- package/lib/types/tsconfig.tsbuildinfo +1 -1
- package/lib/types/types/core.d.ts +1 -0
- package/lib/types/types/state.d.ts +36 -1
- package/lib/types/utils/agent.d.ts +5 -0
- package/lib/types/utils/builtin.d.ts +1 -0
- package/lib/types/utils/data-source.d.ts +8 -0
- package/lib/types/utils/property.d.ts +1 -0
- package/lib/types/utils/typescript/builtin-module-transformer.d.ts +2 -0
- package/package.json +1 -1
package/lib/esm/utils/builtin.js
CHANGED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { cloneDeep } from 'lodash';
|
|
2
|
+
export function setPageDataSource(page, state, locale, pageData) {
|
|
3
|
+
try {
|
|
4
|
+
if (!page.locales) {
|
|
5
|
+
page.locales = {};
|
|
6
|
+
}
|
|
7
|
+
page.locales[locale] = cloneDeep(page.locales[locale] || {});
|
|
8
|
+
page.locales[locale].title = pageData.title ?? page.locales[locale].title;
|
|
9
|
+
page.locales[locale].image = pageData.image ?? page.locales[locale].image;
|
|
10
|
+
page.locales[locale].description = pageData.description ?? page.locales[locale].description;
|
|
11
|
+
// 遍历 pageData 的每个 key
|
|
12
|
+
Object.entries(pageData.sectionsData).forEach(([sectionKey, sectionData]) => {
|
|
13
|
+
try {
|
|
14
|
+
// 通过 id 或 name 找到对应的 section
|
|
15
|
+
const section = Object.values(page.sections).find((section) => section.id === sectionKey || section.name === sectionKey);
|
|
16
|
+
if (!section) {
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
if (!section.isTemplateSection) {
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
// 确保 section.locales 存在
|
|
23
|
+
section.locales = cloneDeep(section.locales || {});
|
|
24
|
+
section.locales[locale] = cloneDeep(section.locales[locale] || {});
|
|
25
|
+
if (section.component === 'custom-component') {
|
|
26
|
+
// 处理自定义组件
|
|
27
|
+
const componentId = section.config?.componentId;
|
|
28
|
+
if (!componentId) {
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
// 从 state 中获取组件定义
|
|
32
|
+
const componentDef = state.components[componentId]?.data || state.resources.components?.[componentId]?.component;
|
|
33
|
+
if (!componentDef) {
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
// 确保 properties 存在
|
|
37
|
+
section.locales[locale].properties = cloneDeep(section.locales[locale].properties || {});
|
|
38
|
+
// 遍历 sectionData,设置属性值
|
|
39
|
+
Object.entries(sectionData || {}).forEach(([propKey, propValue]) => {
|
|
40
|
+
// 尝试通过 key 或 id 找到属性定义
|
|
41
|
+
const property = componentDef?.properties?.[propKey] ||
|
|
42
|
+
Object.values(componentDef?.properties || {}).find((prop) => prop.data?.key === propKey);
|
|
43
|
+
if (property) {
|
|
44
|
+
// 设置属性值
|
|
45
|
+
section.locales[locale].properties[property.data?.id] = {
|
|
46
|
+
value: propValue,
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
// 普通组件直接设置 properties
|
|
53
|
+
section.locales[locale] = {
|
|
54
|
+
...cloneDeep(section.locales[locale]),
|
|
55
|
+
...(sectionData || {}),
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
catch (error) {
|
|
60
|
+
console.error('Failed to set section data source:', error);
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
catch (error) {
|
|
65
|
+
console.error('Failed to set page data source:', error);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
@@ -13,6 +13,7 @@ import * as emotionCss from '../builtin/emotion/css';
|
|
|
13
13
|
import * as iconifyReact from '../builtin/iconify/react';
|
|
14
14
|
import * as immer from '../builtin/immer';
|
|
15
15
|
import * as locale from '../builtin/locale';
|
|
16
|
+
import * as markdownRenderer from '../builtin/markdown/index';
|
|
16
17
|
import * as muiLab from '../builtin/mui/lab';
|
|
17
18
|
import * as muiMaterial from '../builtin/mui/material';
|
|
18
19
|
import * as pageHeader from '../builtin/page/header';
|
|
@@ -56,6 +57,7 @@ export function injectGlobalComponents() {
|
|
|
56
57
|
'@blocklet/pages-kit/builtin/react-dom': reactDOM,
|
|
57
58
|
'@blocklet/pages-kit/builtin/uploader': uploader,
|
|
58
59
|
'@blocklet/pages-kit/builtin/color-picker': colorPicker,
|
|
60
|
+
'@blocklet/pages-kit/builtin/markdown/index': markdownRenderer,
|
|
59
61
|
'@blocklet/pages-kit/builtin/emotion/css': emotionCss,
|
|
60
62
|
'@blocklet/pages-kit/builtin/mui/material': muiMaterial,
|
|
61
63
|
'@blocklet/pages-kit/builtin/mui/lab': muiLab,
|
|
@@ -8,6 +8,7 @@ export function mergeComponent({ componentId, getComponent, locale, defaultLocal
|
|
|
8
8
|
if (!component)
|
|
9
9
|
return null;
|
|
10
10
|
let script;
|
|
11
|
+
let editComponent;
|
|
11
12
|
const props = Object.fromEntries(Object.entries(parameters ?? {}).map(([id, { value }]) => {
|
|
12
13
|
const property = component?.properties?.[id]?.data;
|
|
13
14
|
if (!property)
|
|
@@ -23,6 +24,7 @@ export function mergeComponent({ componentId, getComponent, locale, defaultLocal
|
|
|
23
24
|
])));
|
|
24
25
|
if (component.renderer?.type === 'react-component') {
|
|
25
26
|
script = component.renderer?.script;
|
|
27
|
+
editComponent = component.renderer?.editComponent;
|
|
26
28
|
break;
|
|
27
29
|
}
|
|
28
30
|
if (component.renderer?.type === 'web-component') {
|
|
@@ -51,7 +53,7 @@ export function mergeComponent({ componentId, getComponent, locale, defaultLocal
|
|
|
51
53
|
}
|
|
52
54
|
if (!script)
|
|
53
55
|
return null;
|
|
54
|
-
return { script, props };
|
|
56
|
+
return { script, props, editComponent };
|
|
55
57
|
}
|
|
56
58
|
export const RenderNestedComponent = '__RENDER_NESTED_COMPONENT__';
|
|
57
59
|
export function safeJSONParse(value) {
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { basename } from 'path';
|
|
1
2
|
import { BuiltinModulesGlobalVariableName } from '../../types/builtin';
|
|
2
3
|
export const isRelativeModule = (moduleSpecifier) => {
|
|
3
4
|
if (!moduleSpecifier) {
|
|
@@ -11,22 +12,22 @@ export const isAsyncModule = (moduleSpecifier) => {
|
|
|
11
12
|
}
|
|
12
13
|
return ['@blocklet/pages-kit/builtin/async/ai-runtime'].includes(moduleSpecifier);
|
|
13
14
|
};
|
|
15
|
+
// check if the module is a target module
|
|
16
|
+
export const isBuiltinModule = (moduleSpecifier) => {
|
|
17
|
+
if (!moduleSpecifier) {
|
|
18
|
+
return false;
|
|
19
|
+
}
|
|
20
|
+
return moduleSpecifier.startsWith('@blocklet/pages-kit/builtin/');
|
|
21
|
+
};
|
|
14
22
|
export const createBuiltinModuleTransformer = (ts) => (context) => (file) => {
|
|
15
23
|
const { factory } = context;
|
|
16
24
|
// 统一的模块导入收集器
|
|
17
25
|
const imports = [];
|
|
18
|
-
// check if the module is a target module
|
|
19
|
-
const isTargetModule = (moduleSpecifier) => {
|
|
20
|
-
if (!moduleSpecifier) {
|
|
21
|
-
return false;
|
|
22
|
-
}
|
|
23
|
-
return moduleSpecifier.startsWith('@blocklet/pages-kit/builtin/') || isRelativeModule(moduleSpecifier);
|
|
24
|
-
};
|
|
25
26
|
// filter and collect the import statements that need to be processed
|
|
26
27
|
const statements = file.statements.filter((s) => {
|
|
27
28
|
if (ts.isImportDeclaration(s) &&
|
|
28
29
|
ts.isStringLiteral(s.moduleSpecifier) &&
|
|
29
|
-
|
|
30
|
+
(isBuiltinModule(s.moduleSpecifier.text) || isRelativeModule(s.moduleSpecifier.text))) {
|
|
30
31
|
const importInfo = {
|
|
31
32
|
moduleName: s.moduleSpecifier.text,
|
|
32
33
|
name: s.importClause?.name?.text,
|
|
@@ -55,11 +56,11 @@ export const createBuiltinModuleTransformer = (ts) => (context) => (file) => {
|
|
|
55
56
|
statements.unshift(...imports.flatMap((importInfo) => {
|
|
56
57
|
// call inject-global-components require method
|
|
57
58
|
const requireCall = factory.createCallExpression(factory.createPropertyAccessExpression(factory.createIdentifier(BuiltinModulesGlobalVariableName), 'require'), undefined, [factory.createStringLiteral(importInfo.moduleName)]);
|
|
59
|
+
const isAsync = isRelativeModule(importInfo.moduleName) || isAsyncModule(importInfo.moduleName);
|
|
58
60
|
// create await expression if the module is ../ or ./
|
|
59
|
-
const mod =
|
|
60
|
-
? factory.createAwaitExpression(requireCall)
|
|
61
|
-
: requireCall;
|
|
61
|
+
const mod = isAsync ? factory.createAwaitExpression(requireCall) : requireCall;
|
|
62
62
|
return [
|
|
63
|
+
// handle default import
|
|
63
64
|
importInfo.name
|
|
64
65
|
? factory.createVariableStatement([], factory.createVariableDeclarationList([
|
|
65
66
|
factory.createVariableDeclaration(factory.createIdentifier(importInfo.name), undefined, undefined, factory.createPropertyAccessExpression(mod, 'default')),
|
|
@@ -72,12 +73,31 @@ export const createBuiltinModuleTransformer = (ts) => (context) => (file) => {
|
|
|
72
73
|
], ts.NodeFlags.Const))
|
|
73
74
|
: undefined,
|
|
74
75
|
// handle named import
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
factory.createVariableDeclaration(factory.
|
|
78
|
-
|
|
79
|
-
|
|
76
|
+
importInfo.members.length > 0
|
|
77
|
+
? factory.createVariableStatement([], factory.createVariableDeclarationList([
|
|
78
|
+
factory.createVariableDeclaration(factory.createObjectBindingPattern(importInfo.members.map((member) => {
|
|
79
|
+
return factory.createBindingElement(undefined, member.propertyName ? factory.createIdentifier(member.propertyName) : undefined, factory.createIdentifier(member.name), undefined);
|
|
80
|
+
})), undefined, undefined, mod),
|
|
81
|
+
], ts.NodeFlags.Const))
|
|
82
|
+
: undefined,
|
|
80
83
|
].filter((i) => !!i);
|
|
81
84
|
}));
|
|
82
85
|
return factory.updateSourceFile(file, statements);
|
|
83
86
|
};
|
|
87
|
+
export const createWellKnownChunksTransformer = (ts) => (context) => (file) => {
|
|
88
|
+
const { factory } = context;
|
|
89
|
+
// 转换所有导入声明
|
|
90
|
+
const transformedStatements = file.statements.map((node) => {
|
|
91
|
+
if (ts.isImportDeclaration(node) &&
|
|
92
|
+
ts.isStringLiteral(node.moduleSpecifier) &&
|
|
93
|
+
isRelativeModule(node.moduleSpecifier.text)) {
|
|
94
|
+
const moduleSpecifier = node.moduleSpecifier.text;
|
|
95
|
+
const fileName = basename(moduleSpecifier);
|
|
96
|
+
const newPath = `/.well-known/chunks/${fileName}`;
|
|
97
|
+
// 创建新的导入声明
|
|
98
|
+
return factory.createImportDeclaration(node.modifiers, node.importClause, factory.createStringLiteral(newPath));
|
|
99
|
+
}
|
|
100
|
+
return node;
|
|
101
|
+
});
|
|
102
|
+
return factory.updateSourceFile(file, transformedStatements);
|
|
103
|
+
};
|