@blocklet/pages-kit-runtime 0.1.21 → 0.1.23

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/client.js CHANGED
@@ -62,6 +62,89 @@ const CenteredContainer = ({ children }) => ((0, jsx_runtime_1.jsx)(Box_1.defaul
62
62
  const api = (0, js_sdk_1.createAxios)({
63
63
  baseURL: (0, ufo_1.joinURL)(((_a = window === null || window === void 0 ? void 0 : window.blocklet) === null || _a === void 0 ? void 0 : _a.prefix) || ''),
64
64
  });
65
+ // convert page data to page init data, use for AIGNE
66
+ function convertPageData(state, page, locale) {
67
+ var _a, _b, _c;
68
+ // 如果页面不存在,返回 null
69
+ if (!page) {
70
+ return null;
71
+ }
72
+ // 获取默认语言
73
+ const defaultLocale = (_a = state.config) === null || _a === void 0 ? void 0 : _a.defaultLocale;
74
+ // 获取页面在指定语言下的数据,如果不存在则使用默认语言的数据
75
+ const pageLocaleData = ((_b = page.locales) === null || _b === void 0 ? void 0 : _b[locale]) || ((_c = page.locales) === null || _c === void 0 ? void 0 : _c[defaultLocale]) || {};
76
+ // 初始化基础页面数据
77
+ const result = {
78
+ title: pageLocaleData.title,
79
+ image: pageLocaleData.image,
80
+ description: pageLocaleData.description,
81
+ sectionsData: {},
82
+ };
83
+ // 处理每个 section 的数据
84
+ page.sectionIds.forEach((sectionId) => {
85
+ var _a, _b, _c, _d, _e, _f, _g;
86
+ const section = page.sections[sectionId];
87
+ if (!section)
88
+ return;
89
+ // 获取 section 在指定语言下的配置,如果不存在则使用默认语言的配置
90
+ const sectionConfig = section.config || {};
91
+ const sectionData = ((_a = section.locales) === null || _a === void 0 ? void 0 : _a[locale]) || ((_b = section.locales) === null || _b === void 0 ? void 0 : _b[defaultLocale]);
92
+ const sectionName = section.name || section.id;
93
+ if (section.component === 'custom-component') {
94
+ // 处理自定义组件
95
+ const componentId = (sectionConfig === null || sectionConfig === void 0 ? void 0 : sectionConfig.componentId) || ((_c = section.config) === null || _c === void 0 ? void 0 : _c.componentId);
96
+ 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);
97
+ if (component) {
98
+ result.sectionsData[sectionName] = {};
99
+ // 遍历组件的属性定义
100
+ Object.entries(component.properties || {}).forEach(([, prop]) => {
101
+ var _a, _b, _c, _d, _e;
102
+ const key = ((_a = prop.data) === null || _a === void 0 ? void 0 : _a.key) || ((_b = prop.data) === null || _b === void 0 ? void 0 : _b.id);
103
+ if (key) {
104
+ 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;
105
+ }
106
+ });
107
+ }
108
+ }
109
+ else if (section.component === 'iframe') {
110
+ // 处理 iframe 组件
111
+ result.sectionsData[sectionName] = {
112
+ src: sectionData === null || sectionData === void 0 ? void 0 : sectionData.src,
113
+ title: sectionData === null || sectionData === void 0 ? void 0 : sectionData.title,
114
+ description: sectionData === null || sectionData === void 0 ? void 0 : sectionData.description,
115
+ };
116
+ }
117
+ else if (section.component === 'section') {
118
+ // 处理普通 section 组件
119
+ result.sectionsData[sectionName] = {
120
+ title: sectionData === null || sectionData === void 0 ? void 0 : sectionData.title,
121
+ description: sectionData === null || sectionData === void 0 ? void 0 : sectionData.description,
122
+ image: sectionData === null || sectionData === void 0 ? void 0 : sectionData.image,
123
+ imageMeta: sectionData === null || sectionData === void 0 ? void 0 : sectionData.imageMeta,
124
+ };
125
+ }
126
+ else if (section.component === 'section-card-list') {
127
+ // 处理卡片列表组件
128
+ result.sectionsData[sectionName] = {
129
+ title: sectionData === null || sectionData === void 0 ? void 0 : sectionData.title,
130
+ description: sectionData === null || sectionData === void 0 ? void 0 : sectionData.description,
131
+ list: (sectionData === null || sectionData === void 0 ? void 0 : sectionData.list) || [],
132
+ };
133
+ }
134
+ else if (section.component === 'toc') {
135
+ // 处理目录组件
136
+ result.sectionsData[sectionName] = {
137
+ title: sectionData === null || sectionData === void 0 ? void 0 : sectionData.title,
138
+ description: sectionData === null || sectionData === void 0 ? void 0 : sectionData.description,
139
+ };
140
+ }
141
+ else {
142
+ // 处理其他类型的组件
143
+ result.sectionsData[sectionName] = sectionData;
144
+ }
145
+ });
146
+ return result;
147
+ }
65
148
  const RuntimeComponent = (props) => {
66
149
  const proxyState = (0, ahooks_1.useReactive)({
67
150
  rawState: props.state,
@@ -90,6 +173,38 @@ const RuntimeComponent = (props) => {
90
173
  listeners.current.add(listener);
91
174
  });
92
175
  },
176
+ getPageInitData: ({ locale }) => {
177
+ return new Promise((resolve) => {
178
+ if (!props.path) {
179
+ resolve(null);
180
+ return;
181
+ }
182
+ const getPageInitData = () => __awaiter(void 0, void 0, void 0, function* () {
183
+ var _a;
184
+ let pageInitData = {};
185
+ if (proxyState.rawState.pages) {
186
+ const page = Object.values(proxyState.rawState.pages).find((page) => (page === null || page === void 0 ? void 0 : page.slug) === props.path);
187
+ if (page) {
188
+ pageInitData = convertPageData(proxyState.rawState, page, locale || ((_a = proxyState.rawState.config) === null || _a === void 0 ? void 0 : _a.defaultLocale));
189
+ }
190
+ }
191
+ resolve(pageInitData);
192
+ });
193
+ // get init data, check if state is ready
194
+ if (proxyState.rawState) {
195
+ getPageInitData();
196
+ }
197
+ else {
198
+ const listener = (state) => {
199
+ if (state) {
200
+ listeners.current.delete(listener);
201
+ getPageInitData();
202
+ }
203
+ };
204
+ listeners.current.add(listener);
205
+ }
206
+ });
207
+ },
93
208
  };
94
209
  });
95
210
  // 当状态更新时通知所有监听器
@@ -136,9 +251,8 @@ const RuntimeComponent = (props) => {
136
251
  }
137
252
  return stateData;
138
253
  }), {
139
- refreshDeps: [props.did, props.state],
254
+ refreshDeps: [props.did, props.state, props.projectId],
140
255
  });
141
- (0, react_1.useEffect)(() => { }, [proxyState.pageData]);
142
256
  if (loading) {
143
257
  return ((0, jsx_runtime_1.jsx)(CenteredContainer, { children: (0, jsx_runtime_1.jsx)(Spinner_1.default, {}) }));
144
258
  }
@@ -168,6 +282,12 @@ class Runtime {
168
282
  var _a;
169
283
  return (_a = this.stateRef.current) === null || _a === void 0 ? void 0 : _a.getPageData();
170
284
  }
285
+ getPageInitData(_a) {
286
+ return __awaiter(this, arguments, void 0, function* ({ locale }) {
287
+ var _b;
288
+ return (_b = this.stateRef.current) === null || _b === void 0 ? void 0 : _b.getPageInitData({ locale });
289
+ });
290
+ }
171
291
  waitForState() {
172
292
  return __awaiter(this, void 0, void 0, function* () {
173
293
  var _a;