@blocklet/pages-kit-runtime 0.4.32 → 0.4.34

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
@@ -1,13 +1,3 @@
1
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
- return new (P || (P = Promise))(function (resolve, reject) {
4
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
- step((generator = generator.apply(thisArg, _arguments || [])).next());
8
- });
9
- };
10
- var _a;
11
1
  import { jsx as _jsx } from "react/jsx-runtime";
12
2
  import Result from '@arcblock/ux/lib/Result';
13
3
  import Spinner from '@arcblock/ux/lib/Spinner';
@@ -20,19 +10,18 @@ import React, { useImperativeHandle, useEffect, useRef } from 'react';
20
10
  import { joinURL } from 'ufo';
21
11
  const CenteredContainer = ({ children }) => (_jsx(Box, { display: "flex", justifyContent: "center", alignItems: "center", minHeight: "100vh", children: children }));
22
12
  const api = createAxios({
23
- baseURL: joinURL(((_a = window === null || window === void 0 ? void 0 : window.blocklet) === null || _a === void 0 ? void 0 : _a.prefix) || ''),
13
+ baseURL: joinURL(window?.blocklet?.prefix || ''),
24
14
  });
25
15
  // convert page data to page init data, use for AIGNE
26
16
  function convertPageData(state, page, locale) {
27
- var _a, _b, _c;
28
17
  // 如果页面不存在,返回 null
29
18
  if (!page) {
30
19
  return null;
31
20
  }
32
21
  // 获取默认语言
33
- const defaultLocale = (_a = state.config) === null || _a === void 0 ? void 0 : _a.defaultLocale;
22
+ const defaultLocale = state.config?.defaultLocale;
34
23
  // 获取页面在指定语言下的数据,如果不存在则使用默认语言的数据
35
- const pageLocaleData = ((_b = page.locales) === null || _b === void 0 ? void 0 : _b[locale]) || ((_c = page.locales) === null || _c === void 0 ? void 0 : _c[defaultLocale]) || {};
24
+ const pageLocaleData = page.locales?.[locale] || page.locales?.[defaultLocale] || {};
36
25
  // 初始化基础页面数据
37
26
  const result = {
38
27
  title: pageLocaleData.title,
@@ -42,26 +31,24 @@ function convertPageData(state, page, locale) {
42
31
  };
43
32
  // 处理每个 section 的数据
44
33
  page.sectionIds.forEach((sectionId) => {
45
- var _a, _b, _c, _d, _e, _f, _g;
46
34
  const section = page.sections[sectionId];
47
35
  if (!section)
48
36
  return;
49
37
  // 获取 section 在指定语言下的配置,如果不存在则使用默认语言的配置
50
38
  const sectionConfig = section.config || {};
51
- const sectionData = ((_a = section.locales) === null || _a === void 0 ? void 0 : _a[locale]) || ((_b = section.locales) === null || _b === void 0 ? void 0 : _b[defaultLocale]);
39
+ const sectionData = section.locales?.[locale] || section.locales?.[defaultLocale];
52
40
  const sectionName = section.name || section.id;
53
41
  if (section.component === 'custom-component') {
54
42
  // 处理自定义组件
55
- const componentId = (sectionConfig === null || sectionConfig === void 0 ? void 0 : sectionConfig.componentId) || ((_c = section.config) === null || _c === void 0 ? void 0 : _c.componentId);
56
- 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);
43
+ const componentId = sectionConfig?.componentId || section.config?.componentId;
44
+ const component = state.components[componentId]?.data || state.resources?.components?.[componentId]?.component;
57
45
  if (component) {
58
46
  result.sectionsData[sectionName] = {};
59
47
  // 遍历组件的属性定义
60
48
  Object.entries(component.properties || {}).forEach(([, prop]) => {
61
- var _a, _b, _c, _d, _e;
62
- const key = ((_a = prop.data) === null || _a === void 0 ? void 0 : _a.key) || ((_b = prop.data) === null || _b === void 0 ? void 0 : _b.id);
49
+ const key = prop.data?.key || prop.data?.id;
63
50
  if (key) {
64
- 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;
51
+ result.sectionsData[sectionName][key] = sectionData?.properties?.[prop.data?.id]?.value;
65
52
  }
66
53
  });
67
54
  }
@@ -69,33 +56,33 @@ function convertPageData(state, page, locale) {
69
56
  else if (section.component === 'iframe') {
70
57
  // 处理 iframe 组件
71
58
  result.sectionsData[sectionName] = {
72
- src: sectionData === null || sectionData === void 0 ? void 0 : sectionData.src,
73
- title: sectionData === null || sectionData === void 0 ? void 0 : sectionData.title,
74
- description: sectionData === null || sectionData === void 0 ? void 0 : sectionData.description,
59
+ src: sectionData?.src,
60
+ title: sectionData?.title,
61
+ description: sectionData?.description,
75
62
  };
76
63
  }
77
64
  else if (section.component === 'section') {
78
65
  // 处理普通 section 组件
79
66
  result.sectionsData[sectionName] = {
80
- title: sectionData === null || sectionData === void 0 ? void 0 : sectionData.title,
81
- description: sectionData === null || sectionData === void 0 ? void 0 : sectionData.description,
82
- image: sectionData === null || sectionData === void 0 ? void 0 : sectionData.image,
83
- imageMeta: sectionData === null || sectionData === void 0 ? void 0 : sectionData.imageMeta,
67
+ title: sectionData?.title,
68
+ description: sectionData?.description,
69
+ image: sectionData?.image,
70
+ imageMeta: sectionData?.imageMeta,
84
71
  };
85
72
  }
86
73
  else if (section.component === 'section-card-list') {
87
74
  // 处理卡片列表组件
88
75
  result.sectionsData[sectionName] = {
89
- title: sectionData === null || sectionData === void 0 ? void 0 : sectionData.title,
90
- description: sectionData === null || sectionData === void 0 ? void 0 : sectionData.description,
91
- list: (sectionData === null || sectionData === void 0 ? void 0 : sectionData.list) || [],
76
+ title: sectionData?.title,
77
+ description: sectionData?.description,
78
+ list: sectionData?.list || [],
92
79
  };
93
80
  }
94
81
  else if (section.component === 'toc') {
95
82
  // 处理目录组件
96
83
  result.sectionsData[sectionName] = {
97
- title: sectionData === null || sectionData === void 0 ? void 0 : sectionData.title,
98
- description: sectionData === null || sectionData === void 0 ? void 0 : sectionData.description,
84
+ title: sectionData?.title,
85
+ description: sectionData?.description,
99
86
  };
100
87
  }
101
88
  else {
@@ -139,17 +126,16 @@ export const RuntimeComponent = (props) => {
139
126
  resolve(null);
140
127
  return;
141
128
  }
142
- const getPageInitData = () => __awaiter(void 0, void 0, void 0, function* () {
143
- var _a;
129
+ const getPageInitData = async () => {
144
130
  let pageInitData = {};
145
131
  if (proxyState.rawState.pages) {
146
- const page = Object.values(proxyState.rawState.pages).find((page) => (page === null || page === void 0 ? void 0 : page.slug) === props.path);
132
+ const page = Object.values(proxyState.rawState.pages).find((page) => page?.slug === props.path);
147
133
  if (page) {
148
- pageInitData = convertPageData(proxyState.rawState, page, locale || ((_a = proxyState.rawState.config) === null || _a === void 0 ? void 0 : _a.defaultLocale));
134
+ pageInitData = convertPageData(proxyState.rawState, page, locale || proxyState.rawState.config?.defaultLocale);
149
135
  }
150
136
  }
151
137
  resolve(pageInitData);
152
- });
138
+ };
153
139
  // get init data, check if state is ready
154
140
  if (proxyState.rawState) {
155
141
  getPageInitData();
@@ -169,17 +155,16 @@ export const RuntimeComponent = (props) => {
169
155
  });
170
156
  // 当状态更新时通知所有监听器
171
157
  useEffect(() => {
172
- var _a;
173
- if ((_a = props.stateRef) === null || _a === void 0 ? void 0 : _a.current) {
158
+ if (props.stateRef?.current) {
174
159
  listeners.current.forEach((listener) => listener(proxyState.rawState));
175
160
  }
176
161
  }, [proxyState.rawState]);
177
- const { loading } = useRequest(() => __awaiter(void 0, void 0, void 0, function* () {
162
+ const { loading } = useRequest(async () => {
178
163
  if (props.state) {
179
164
  return null;
180
165
  }
181
166
  // 统一使用 /api/pages 接口
182
- const { data } = yield api.get('/api/pages', {
167
+ const { data } = await api.get('/api/pages', {
183
168
  params: {
184
169
  did: props.did,
185
170
  projectId: props.projectId,
@@ -188,7 +173,7 @@ export const RuntimeComponent = (props) => {
188
173
  });
189
174
  proxyState.rawState = data.state || data;
190
175
  return data;
191
- }), {
176
+ }, {
192
177
  refreshDeps: [props.did, props.state, props.projectId],
193
178
  });
194
179
  if (loading) {
@@ -203,35 +188,27 @@ export const RuntimeComponent = (props) => {
203
188
  return _jsx(PageRoutes, { mode: "production", state: proxyState.rawState });
204
189
  };
205
190
  export default class Runtime {
191
+ stateRef = React.createRef();
192
+ props;
206
193
  constructor(props) {
207
- this.stateRef = React.createRef();
208
194
  this.props = props;
209
195
  }
210
196
  get rawState() {
211
- var _a;
212
- return (_a = this.stateRef.current) === null || _a === void 0 ? void 0 : _a.getState();
197
+ return this.stateRef.current?.getState();
213
198
  }
214
199
  set pageData(data) {
215
- var _a;
216
- (_a = this.stateRef.current) === null || _a === void 0 ? void 0 : _a.setPageData(data);
200
+ this.stateRef.current?.setPageData(data);
217
201
  }
218
202
  get pageData() {
219
- var _a;
220
- return (_a = this.stateRef.current) === null || _a === void 0 ? void 0 : _a.getPageData();
203
+ return this.stateRef.current?.getPageData();
221
204
  }
222
- getPageInitData(_a) {
223
- return __awaiter(this, arguments, void 0, function* ({ locale }) {
224
- var _b;
225
- return (_b = this.stateRef.current) === null || _b === void 0 ? void 0 : _b.getPageInitData({ locale });
226
- });
205
+ async getPageInitData({ locale }) {
206
+ return this.stateRef.current?.getPageInitData({ locale });
227
207
  }
228
- waitForState() {
229
- return __awaiter(this, void 0, void 0, function* () {
230
- var _a;
231
- return (_a = this.stateRef.current) === null || _a === void 0 ? void 0 : _a.waitForState();
232
- });
208
+ async waitForState() {
209
+ return this.stateRef.current?.waitForState();
233
210
  }
234
211
  render() {
235
- return _jsx(RuntimeComponent, Object.assign({}, this.props, { stateRef: this.stateRef }));
212
+ return _jsx(RuntimeComponent, { ...this.props, stateRef: this.stateRef });
236
213
  }
237
214
  }
@@ -19,11 +19,11 @@ export function CreateResource({ open, onClose, blockletDid, mode, }) {
19
19
  },
20
20
  }
21
21
  : {};
22
- return (_jsx(Box, Object.assign({}, boxProps, { children: _jsx(Suspense, { children: _jsx(BlockletStudio, { mode: mode, tenantScope: tenantScope, title: "Pages Kit Blocks", description: "", note: "", introduction: "", logo: "", componentDid: blockletDid,
22
+ return (_jsx(Box, { ...boxProps, children: _jsx(Suspense, { children: _jsx(BlockletStudio, { mode: mode, tenantScope: tenantScope, title: "Pages Kit Blocks", description: "", note: "", introduction: "", logo: "", componentDid: blockletDid,
23
23
  // 透传到 get blocklet resource 的参数
24
24
  resourcesParams: {}, dependentComponentsMode: "readonly", open: true, setOpen: () => onClose(), onConnected: () => { }, onUploaded: () => { }, onReleased: () => { },
25
25
  // onOpened={() => onOpened?.()}
26
26
  // 默认选中的资源
27
- resources: {} }) }) })));
27
+ resources: {} }) }) }));
28
28
  }
29
29
  export default CreateResource;