@blocklet/pages-kit-runtime 0.1.21 → 0.1.22
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/esm/client.js
CHANGED
|
@@ -23,6 +23,89 @@ const CenteredContainer = ({ children }) => (_jsx(Box, { display: "flex", justif
|
|
|
23
23
|
const api = createAxios({
|
|
24
24
|
baseURL: joinURL(((_a = window === null || window === void 0 ? void 0 : window.blocklet) === null || _a === void 0 ? void 0 : _a.prefix) || ''),
|
|
25
25
|
});
|
|
26
|
+
// convert page data to page init data, use for AIGNE
|
|
27
|
+
function convertPageData(state, page, locale) {
|
|
28
|
+
var _a, _b, _c;
|
|
29
|
+
// 如果页面不存在,返回 null
|
|
30
|
+
if (!page) {
|
|
31
|
+
return null;
|
|
32
|
+
}
|
|
33
|
+
// 获取默认语言
|
|
34
|
+
const defaultLocale = (_a = state.config) === null || _a === void 0 ? void 0 : _a.defaultLocale;
|
|
35
|
+
// 获取页面在指定语言下的数据,如果不存在则使用默认语言的数据
|
|
36
|
+
const pageLocaleData = ((_b = page.locales) === null || _b === void 0 ? void 0 : _b[locale]) || ((_c = page.locales) === null || _c === void 0 ? void 0 : _c[defaultLocale]) || {};
|
|
37
|
+
// 初始化基础页面数据
|
|
38
|
+
const result = {
|
|
39
|
+
title: pageLocaleData.title,
|
|
40
|
+
image: pageLocaleData.image,
|
|
41
|
+
description: pageLocaleData.description,
|
|
42
|
+
sectionsData: {},
|
|
43
|
+
};
|
|
44
|
+
// 处理每个 section 的数据
|
|
45
|
+
page.sectionIds.forEach((sectionId) => {
|
|
46
|
+
var _a, _b, _c, _d, _e, _f, _g;
|
|
47
|
+
const section = page.sections[sectionId];
|
|
48
|
+
if (!section)
|
|
49
|
+
return;
|
|
50
|
+
// 获取 section 在指定语言下的配置,如果不存在则使用默认语言的配置
|
|
51
|
+
const sectionConfig = section.config || {};
|
|
52
|
+
const sectionData = ((_a = section.locales) === null || _a === void 0 ? void 0 : _a[locale]) || ((_b = section.locales) === null || _b === void 0 ? void 0 : _b[defaultLocale]);
|
|
53
|
+
const sectionName = section.name || section.id;
|
|
54
|
+
if (section.component === 'custom-component') {
|
|
55
|
+
// 处理自定义组件
|
|
56
|
+
const componentId = (sectionConfig === null || sectionConfig === void 0 ? void 0 : sectionConfig.componentId) || ((_c = section.config) === null || _c === void 0 ? void 0 : _c.componentId);
|
|
57
|
+
const component = ((_d = state.components[componentId]) === null || _d === void 0 ? void 0 : _d.data) || ((_g = (_f = (_e = state.resources) === null || _e === void 0 ? void 0 : _e.components) === null || _f === void 0 ? void 0 : _f[componentId]) === null || _g === void 0 ? void 0 : _g.component);
|
|
58
|
+
if (component) {
|
|
59
|
+
result.sectionsData[sectionName] = {};
|
|
60
|
+
// 遍历组件的属性定义
|
|
61
|
+
Object.entries(component.properties || {}).forEach(([, prop]) => {
|
|
62
|
+
var _a, _b, _c, _d, _e;
|
|
63
|
+
const key = ((_a = prop.data) === null || _a === void 0 ? void 0 : _a.key) || ((_b = prop.data) === null || _b === void 0 ? void 0 : _b.id);
|
|
64
|
+
if (key) {
|
|
65
|
+
result.sectionsData[sectionName][key] = (_e = (_c = sectionData === null || sectionData === void 0 ? void 0 : sectionData.properties) === null || _c === void 0 ? void 0 : _c[(_d = prop.data) === null || _d === void 0 ? void 0 : _d.id]) === null || _e === void 0 ? void 0 : _e.value;
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
else if (section.component === 'iframe') {
|
|
71
|
+
// 处理 iframe 组件
|
|
72
|
+
result.sectionsData[sectionName] = {
|
|
73
|
+
src: sectionData === null || sectionData === void 0 ? void 0 : sectionData.src,
|
|
74
|
+
title: sectionData === null || sectionData === void 0 ? void 0 : sectionData.title,
|
|
75
|
+
description: sectionData === null || sectionData === void 0 ? void 0 : sectionData.description,
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
else if (section.component === 'section') {
|
|
79
|
+
// 处理普通 section 组件
|
|
80
|
+
result.sectionsData[sectionName] = {
|
|
81
|
+
title: sectionData === null || sectionData === void 0 ? void 0 : sectionData.title,
|
|
82
|
+
description: sectionData === null || sectionData === void 0 ? void 0 : sectionData.description,
|
|
83
|
+
image: sectionData === null || sectionData === void 0 ? void 0 : sectionData.image,
|
|
84
|
+
imageMeta: sectionData === null || sectionData === void 0 ? void 0 : sectionData.imageMeta,
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
else if (section.component === 'section-card-list') {
|
|
88
|
+
// 处理卡片列表组件
|
|
89
|
+
result.sectionsData[sectionName] = {
|
|
90
|
+
title: sectionData === null || sectionData === void 0 ? void 0 : sectionData.title,
|
|
91
|
+
description: sectionData === null || sectionData === void 0 ? void 0 : sectionData.description,
|
|
92
|
+
list: (sectionData === null || sectionData === void 0 ? void 0 : sectionData.list) || [],
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
else if (section.component === 'toc') {
|
|
96
|
+
// 处理目录组件
|
|
97
|
+
result.sectionsData[sectionName] = {
|
|
98
|
+
title: sectionData === null || sectionData === void 0 ? void 0 : sectionData.title,
|
|
99
|
+
description: sectionData === null || sectionData === void 0 ? void 0 : sectionData.description,
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
else {
|
|
103
|
+
// 处理其他类型的组件
|
|
104
|
+
result.sectionsData[sectionName] = sectionData;
|
|
105
|
+
}
|
|
106
|
+
});
|
|
107
|
+
return result;
|
|
108
|
+
}
|
|
26
109
|
export const RuntimeComponent = (props) => {
|
|
27
110
|
const proxyState = useReactive({
|
|
28
111
|
rawState: props.state,
|
|
@@ -51,6 +134,38 @@ export const RuntimeComponent = (props) => {
|
|
|
51
134
|
listeners.current.add(listener);
|
|
52
135
|
});
|
|
53
136
|
},
|
|
137
|
+
getPageInitData: ({ locale }) => {
|
|
138
|
+
return new Promise((resolve) => {
|
|
139
|
+
if (!props.path) {
|
|
140
|
+
resolve(null);
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
const getPageInitData = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
144
|
+
var _a;
|
|
145
|
+
let pageInitData = {};
|
|
146
|
+
if (proxyState.rawState.pages) {
|
|
147
|
+
const page = Object.values(proxyState.rawState.pages).find((page) => (page === null || page === void 0 ? void 0 : page.slug) === props.path);
|
|
148
|
+
if (page) {
|
|
149
|
+
pageInitData = convertPageData(proxyState.rawState, page, locale || ((_a = proxyState.rawState.config) === null || _a === void 0 ? void 0 : _a.defaultLocale));
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
resolve(pageInitData);
|
|
153
|
+
});
|
|
154
|
+
// get init data, check if state is ready
|
|
155
|
+
if (proxyState.rawState) {
|
|
156
|
+
getPageInitData();
|
|
157
|
+
}
|
|
158
|
+
else {
|
|
159
|
+
const listener = (state) => {
|
|
160
|
+
if (state) {
|
|
161
|
+
listeners.current.delete(listener);
|
|
162
|
+
getPageInitData();
|
|
163
|
+
}
|
|
164
|
+
};
|
|
165
|
+
listeners.current.add(listener);
|
|
166
|
+
}
|
|
167
|
+
});
|
|
168
|
+
},
|
|
54
169
|
};
|
|
55
170
|
});
|
|
56
171
|
// 当状态更新时通知所有监听器
|
|
@@ -97,9 +212,8 @@ export const RuntimeComponent = (props) => {
|
|
|
97
212
|
}
|
|
98
213
|
return stateData;
|
|
99
214
|
}), {
|
|
100
|
-
refreshDeps: [props.did, props.state],
|
|
215
|
+
refreshDeps: [props.did, props.state, props.projectId],
|
|
101
216
|
});
|
|
102
|
-
useEffect(() => { }, [proxyState.pageData]);
|
|
103
217
|
if (loading) {
|
|
104
218
|
return (_jsx(CenteredContainer, { children: _jsx(Spinner, {}) }));
|
|
105
219
|
}
|
|
@@ -128,6 +242,12 @@ export default class Runtime {
|
|
|
128
242
|
var _a;
|
|
129
243
|
return (_a = this.stateRef.current) === null || _a === void 0 ? void 0 : _a.getPageData();
|
|
130
244
|
}
|
|
245
|
+
getPageInitData(_a) {
|
|
246
|
+
return __awaiter(this, arguments, void 0, function* ({ locale }) {
|
|
247
|
+
var _b;
|
|
248
|
+
return (_b = this.stateRef.current) === null || _b === void 0 ? void 0 : _b.getPageInitData({ locale });
|
|
249
|
+
});
|
|
250
|
+
}
|
|
131
251
|
waitForState() {
|
|
132
252
|
return __awaiter(this, void 0, void 0, function* () {
|
|
133
253
|
var _a;
|