@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
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AGENT_OPTIONS = exports.PAGE_CONTENT_WRITER_AGENT_ID = void 0;
|
|
4
|
+
exports.PAGE_CONTENT_WRITER_AGENT_ID = 'page-content-writer';
|
|
5
|
+
exports.AGENT_OPTIONS = [{ id: exports.PAGE_CONTENT_WRITER_AGENT_ID, name: 'Page Content Writer' }];
|
package/lib/cjs/utils/builtin.js
CHANGED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.setPageDataSource = setPageDataSource;
|
|
4
|
+
const lodash_1 = require("lodash");
|
|
5
|
+
function setPageDataSource(page, state, locale, pageData) {
|
|
6
|
+
try {
|
|
7
|
+
if (!page.locales) {
|
|
8
|
+
page.locales = {};
|
|
9
|
+
}
|
|
10
|
+
page.locales[locale] = (0, lodash_1.cloneDeep)(page.locales[locale] || {});
|
|
11
|
+
page.locales[locale].title = pageData.title ?? page.locales[locale].title;
|
|
12
|
+
page.locales[locale].image = pageData.image ?? page.locales[locale].image;
|
|
13
|
+
page.locales[locale].description = pageData.description ?? page.locales[locale].description;
|
|
14
|
+
// 遍历 pageData 的每个 key
|
|
15
|
+
Object.entries(pageData.sectionsData).forEach(([sectionKey, sectionData]) => {
|
|
16
|
+
try {
|
|
17
|
+
// 通过 id 或 name 找到对应的 section
|
|
18
|
+
const section = Object.values(page.sections).find((section) => section.id === sectionKey || section.name === sectionKey);
|
|
19
|
+
if (!section) {
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
if (!section.isTemplateSection) {
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
// 确保 section.locales 存在
|
|
26
|
+
section.locales = (0, lodash_1.cloneDeep)(section.locales || {});
|
|
27
|
+
section.locales[locale] = (0, lodash_1.cloneDeep)(section.locales[locale] || {});
|
|
28
|
+
if (section.component === 'custom-component') {
|
|
29
|
+
// 处理自定义组件
|
|
30
|
+
const componentId = section.config?.componentId;
|
|
31
|
+
if (!componentId) {
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
// 从 state 中获取组件定义
|
|
35
|
+
const componentDef = state.components[componentId]?.data || state.resources.components?.[componentId]?.component;
|
|
36
|
+
if (!componentDef) {
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
// 确保 properties 存在
|
|
40
|
+
section.locales[locale].properties = (0, lodash_1.cloneDeep)(section.locales[locale].properties || {});
|
|
41
|
+
// 遍历 sectionData,设置属性值
|
|
42
|
+
Object.entries(sectionData || {}).forEach(([propKey, propValue]) => {
|
|
43
|
+
// 尝试通过 key 或 id 找到属性定义
|
|
44
|
+
const property = componentDef?.properties?.[propKey] ||
|
|
45
|
+
Object.values(componentDef?.properties || {}).find((prop) => prop.data?.key === propKey);
|
|
46
|
+
if (property) {
|
|
47
|
+
// 设置属性值
|
|
48
|
+
section.locales[locale].properties[property.data?.id] = {
|
|
49
|
+
value: propValue,
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
// 普通组件直接设置 properties
|
|
56
|
+
section.locales[locale] = {
|
|
57
|
+
...(0, lodash_1.cloneDeep)(section.locales[locale]),
|
|
58
|
+
...(sectionData || {}),
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
catch (error) {
|
|
63
|
+
console.error('Failed to set section data source:', error);
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
catch (error) {
|
|
68
|
+
console.error('Failed to set page data source:', error);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
@@ -42,6 +42,7 @@ const emotionCss = __importStar(require("../builtin/emotion/css"));
|
|
|
42
42
|
const iconifyReact = __importStar(require("../builtin/iconify/react"));
|
|
43
43
|
const immer = __importStar(require("../builtin/immer"));
|
|
44
44
|
const locale = __importStar(require("../builtin/locale"));
|
|
45
|
+
const markdownRenderer = __importStar(require("../builtin/markdown/index"));
|
|
45
46
|
const muiLab = __importStar(require("../builtin/mui/lab"));
|
|
46
47
|
const muiMaterial = __importStar(require("../builtin/mui/material"));
|
|
47
48
|
const pageHeader = __importStar(require("../builtin/page/header"));
|
|
@@ -85,6 +86,7 @@ function injectGlobalComponents() {
|
|
|
85
86
|
'@blocklet/pages-kit/builtin/react-dom': reactDOM,
|
|
86
87
|
'@blocklet/pages-kit/builtin/uploader': uploader,
|
|
87
88
|
'@blocklet/pages-kit/builtin/color-picker': colorPicker,
|
|
89
|
+
'@blocklet/pages-kit/builtin/markdown/index': markdownRenderer,
|
|
88
90
|
'@blocklet/pages-kit/builtin/emotion/css': emotionCss,
|
|
89
91
|
'@blocklet/pages-kit/builtin/mui/material': muiMaterial,
|
|
90
92
|
'@blocklet/pages-kit/builtin/mui/lab': muiLab,
|
|
@@ -41,6 +41,7 @@ function mergeComponent({ componentId, getComponent, locale, defaultLocale, prop
|
|
|
41
41
|
if (!component)
|
|
42
42
|
return null;
|
|
43
43
|
let script;
|
|
44
|
+
let editComponent;
|
|
44
45
|
const props = Object.fromEntries(Object.entries(parameters ?? {}).map(([id, { value }]) => {
|
|
45
46
|
const property = component?.properties?.[id]?.data;
|
|
46
47
|
if (!property)
|
|
@@ -56,6 +57,7 @@ function mergeComponent({ componentId, getComponent, locale, defaultLocale, prop
|
|
|
56
57
|
])));
|
|
57
58
|
if (component.renderer?.type === 'react-component') {
|
|
58
59
|
script = component.renderer?.script;
|
|
60
|
+
editComponent = component.renderer?.editComponent;
|
|
59
61
|
break;
|
|
60
62
|
}
|
|
61
63
|
if (component.renderer?.type === 'web-component') {
|
|
@@ -84,7 +86,7 @@ function mergeComponent({ componentId, getComponent, locale, defaultLocale, prop
|
|
|
84
86
|
}
|
|
85
87
|
if (!script)
|
|
86
88
|
return null;
|
|
87
|
-
return { script, props };
|
|
89
|
+
return { script, props, editComponent };
|
|
88
90
|
}
|
|
89
91
|
exports.RenderNestedComponent = '__RENDER_NESTED_COMPONENT__';
|
|
90
92
|
function safeJSONParse(value) {
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.createBuiltinModuleTransformer = exports.isAsyncModule = exports.isRelativeModule = void 0;
|
|
3
|
+
exports.createWellKnownChunksTransformer = exports.createBuiltinModuleTransformer = exports.isBuiltinModule = exports.isAsyncModule = exports.isRelativeModule = void 0;
|
|
4
|
+
const path_1 = require("path");
|
|
4
5
|
const builtin_1 = require("../../types/builtin");
|
|
5
6
|
const isRelativeModule = (moduleSpecifier) => {
|
|
6
7
|
if (!moduleSpecifier) {
|
|
@@ -16,22 +17,23 @@ const isAsyncModule = (moduleSpecifier) => {
|
|
|
16
17
|
return ['@blocklet/pages-kit/builtin/async/ai-runtime'].includes(moduleSpecifier);
|
|
17
18
|
};
|
|
18
19
|
exports.isAsyncModule = isAsyncModule;
|
|
20
|
+
// check if the module is a target module
|
|
21
|
+
const isBuiltinModule = (moduleSpecifier) => {
|
|
22
|
+
if (!moduleSpecifier) {
|
|
23
|
+
return false;
|
|
24
|
+
}
|
|
25
|
+
return moduleSpecifier.startsWith('@blocklet/pages-kit/builtin/');
|
|
26
|
+
};
|
|
27
|
+
exports.isBuiltinModule = isBuiltinModule;
|
|
19
28
|
const createBuiltinModuleTransformer = (ts) => (context) => (file) => {
|
|
20
29
|
const { factory } = context;
|
|
21
30
|
// 统一的模块导入收集器
|
|
22
31
|
const imports = [];
|
|
23
|
-
// check if the module is a target module
|
|
24
|
-
const isTargetModule = (moduleSpecifier) => {
|
|
25
|
-
if (!moduleSpecifier) {
|
|
26
|
-
return false;
|
|
27
|
-
}
|
|
28
|
-
return moduleSpecifier.startsWith('@blocklet/pages-kit/builtin/') || (0, exports.isRelativeModule)(moduleSpecifier);
|
|
29
|
-
};
|
|
30
32
|
// filter and collect the import statements that need to be processed
|
|
31
33
|
const statements = file.statements.filter((s) => {
|
|
32
34
|
if (ts.isImportDeclaration(s) &&
|
|
33
35
|
ts.isStringLiteral(s.moduleSpecifier) &&
|
|
34
|
-
|
|
36
|
+
((0, exports.isBuiltinModule)(s.moduleSpecifier.text) || (0, exports.isRelativeModule)(s.moduleSpecifier.text))) {
|
|
35
37
|
const importInfo = {
|
|
36
38
|
moduleName: s.moduleSpecifier.text,
|
|
37
39
|
name: s.importClause?.name?.text,
|
|
@@ -60,11 +62,11 @@ const createBuiltinModuleTransformer = (ts) => (context) => (file) => {
|
|
|
60
62
|
statements.unshift(...imports.flatMap((importInfo) => {
|
|
61
63
|
// call inject-global-components require method
|
|
62
64
|
const requireCall = factory.createCallExpression(factory.createPropertyAccessExpression(factory.createIdentifier(builtin_1.BuiltinModulesGlobalVariableName), 'require'), undefined, [factory.createStringLiteral(importInfo.moduleName)]);
|
|
65
|
+
const isAsync = (0, exports.isRelativeModule)(importInfo.moduleName) || (0, exports.isAsyncModule)(importInfo.moduleName);
|
|
63
66
|
// create await expression if the module is ../ or ./
|
|
64
|
-
const mod =
|
|
65
|
-
? factory.createAwaitExpression(requireCall)
|
|
66
|
-
: requireCall;
|
|
67
|
+
const mod = isAsync ? factory.createAwaitExpression(requireCall) : requireCall;
|
|
67
68
|
return [
|
|
69
|
+
// handle default import
|
|
68
70
|
importInfo.name
|
|
69
71
|
? factory.createVariableStatement([], factory.createVariableDeclarationList([
|
|
70
72
|
factory.createVariableDeclaration(factory.createIdentifier(importInfo.name), undefined, undefined, factory.createPropertyAccessExpression(mod, 'default')),
|
|
@@ -77,13 +79,33 @@ const createBuiltinModuleTransformer = (ts) => (context) => (file) => {
|
|
|
77
79
|
], ts.NodeFlags.Const))
|
|
78
80
|
: undefined,
|
|
79
81
|
// handle named import
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
factory.createVariableDeclaration(factory.
|
|
83
|
-
|
|
84
|
-
|
|
82
|
+
importInfo.members.length > 0
|
|
83
|
+
? factory.createVariableStatement([], factory.createVariableDeclarationList([
|
|
84
|
+
factory.createVariableDeclaration(factory.createObjectBindingPattern(importInfo.members.map((member) => {
|
|
85
|
+
return factory.createBindingElement(undefined, member.propertyName ? factory.createIdentifier(member.propertyName) : undefined, factory.createIdentifier(member.name), undefined);
|
|
86
|
+
})), undefined, undefined, mod),
|
|
87
|
+
], ts.NodeFlags.Const))
|
|
88
|
+
: undefined,
|
|
85
89
|
].filter((i) => !!i);
|
|
86
90
|
}));
|
|
87
91
|
return factory.updateSourceFile(file, statements);
|
|
88
92
|
};
|
|
89
93
|
exports.createBuiltinModuleTransformer = createBuiltinModuleTransformer;
|
|
94
|
+
const createWellKnownChunksTransformer = (ts) => (context) => (file) => {
|
|
95
|
+
const { factory } = context;
|
|
96
|
+
// 转换所有导入声明
|
|
97
|
+
const transformedStatements = file.statements.map((node) => {
|
|
98
|
+
if (ts.isImportDeclaration(node) &&
|
|
99
|
+
ts.isStringLiteral(node.moduleSpecifier) &&
|
|
100
|
+
(0, exports.isRelativeModule)(node.moduleSpecifier.text)) {
|
|
101
|
+
const moduleSpecifier = node.moduleSpecifier.text;
|
|
102
|
+
const fileName = (0, path_1.basename)(moduleSpecifier);
|
|
103
|
+
const newPath = `/.well-known/chunks/${fileName}`;
|
|
104
|
+
// 创建新的导入声明
|
|
105
|
+
return factory.createImportDeclaration(node.modifiers, node.importClause, factory.createStringLiteral(newPath));
|
|
106
|
+
}
|
|
107
|
+
return node;
|
|
108
|
+
});
|
|
109
|
+
return factory.updateSourceFile(file, transformedStatements);
|
|
110
|
+
};
|
|
111
|
+
exports.createWellKnownChunksTransformer = createWellKnownChunksTransformer;
|
|
@@ -155,9 +155,20 @@ export const customComponentStates = () => {
|
|
|
155
155
|
// handle the global variable
|
|
156
156
|
if (typeof m === 'function') {
|
|
157
157
|
try {
|
|
158
|
-
// call m() to get the component module
|
|
159
158
|
const modulePromiseOrObject = m();
|
|
160
|
-
|
|
159
|
+
// if the module is a promise, we need to wait for it to resolve
|
|
160
|
+
if (modulePromiseOrObject instanceof Promise) {
|
|
161
|
+
modulePromiseOrObject
|
|
162
|
+
.then((m) => {
|
|
163
|
+
Component = importCustomComponent(m, { componentId });
|
|
164
|
+
})
|
|
165
|
+
.catch((err) => {
|
|
166
|
+
console.error(`Failed to initialize component ${componentId}:`, err);
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
else {
|
|
170
|
+
Component = importCustomComponent(modulePromiseOrObject, { componentId });
|
|
171
|
+
}
|
|
161
172
|
}
|
|
162
173
|
catch (err) {
|
|
163
174
|
console.error(`Failed to initialize component ${componentId}:`, err);
|
|
@@ -165,6 +176,7 @@ export const customComponentStates = () => {
|
|
|
165
176
|
}
|
|
166
177
|
else {
|
|
167
178
|
console.warn(`Component global variable ${preload.componentModuleGlobalVariable} is not a function`);
|
|
179
|
+
return null;
|
|
168
180
|
}
|
|
169
181
|
}
|
|
170
182
|
return [componentId, { ...preload, Component }];
|
|
@@ -326,19 +338,37 @@ function useTranspileComponent({ componentId, locale, properties, dev: { default
|
|
|
326
338
|
properties,
|
|
327
339
|
});
|
|
328
340
|
const script = useDeferredValue(component?.script);
|
|
341
|
+
const editComponent = useDeferredValue(component?.editComponent);
|
|
329
342
|
useEffect(() => {
|
|
330
343
|
if (script) {
|
|
344
|
+
// transpile view component
|
|
331
345
|
transpileAndLoadScript(script)
|
|
332
346
|
.then((m) => {
|
|
333
347
|
// get properties from code
|
|
334
348
|
const propertiesFromCode = getPropertiesFromCode(m);
|
|
335
|
-
|
|
349
|
+
const extraComponentInfo = m.EditComponent && !editComponent ? { EditComponent: m.EditComponent } : {};
|
|
350
|
+
setComponent((prev) => ({
|
|
351
|
+
...prev,
|
|
352
|
+
...extraComponentInfo,
|
|
353
|
+
Component: m.default,
|
|
354
|
+
propertiesFromCode,
|
|
355
|
+
}));
|
|
336
356
|
})
|
|
337
357
|
.catch((error) => {
|
|
338
358
|
setComponent({ error });
|
|
339
359
|
});
|
|
340
360
|
}
|
|
341
|
-
|
|
361
|
+
// transpile edit component
|
|
362
|
+
if (editComponent) {
|
|
363
|
+
transpileAndLoadScript(editComponent)
|
|
364
|
+
.then((m) => {
|
|
365
|
+
setComponent((prev) => ({ ...prev, EditComponent: m.default || m.EditComponent || undefined }));
|
|
366
|
+
})
|
|
367
|
+
.catch(() => {
|
|
368
|
+
// ignore error when transpile edit component
|
|
369
|
+
});
|
|
370
|
+
}
|
|
371
|
+
}, [script, editComponent]);
|
|
342
372
|
return {
|
|
343
373
|
error,
|
|
344
374
|
Component,
|