@blocklet/pages-kit-runtime 0.4.32 → 0.4.33

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
@@ -32,19 +32,9 @@ var __importStar = (this && this.__importStar) || (function () {
32
32
  return result;
33
33
  };
34
34
  })();
35
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
36
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
37
- return new (P || (P = Promise))(function (resolve, reject) {
38
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
39
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
40
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
41
- step((generator = generator.apply(thisArg, _arguments || [])).next());
42
- });
43
- };
44
35
  var __importDefault = (this && this.__importDefault) || function (mod) {
45
36
  return (mod && mod.__esModule) ? mod : { "default": mod };
46
37
  };
47
- var _a;
48
38
  Object.defineProperty(exports, "__esModule", { value: true });
49
39
  exports.RuntimeComponent = void 0;
50
40
  const jsx_runtime_1 = require("react/jsx-runtime");
@@ -59,19 +49,18 @@ const react_1 = __importStar(require("react"));
59
49
  const ufo_1 = require("ufo");
60
50
  const CenteredContainer = ({ children }) => ((0, jsx_runtime_1.jsx)(Box_1.default, { display: "flex", justifyContent: "center", alignItems: "center", minHeight: "100vh", children: children }));
61
51
  const api = (0, js_sdk_1.createAxios)({
62
- baseURL: (0, ufo_1.joinURL)(((_a = window === null || window === void 0 ? void 0 : window.blocklet) === null || _a === void 0 ? void 0 : _a.prefix) || ''),
52
+ baseURL: (0, ufo_1.joinURL)(window?.blocklet?.prefix || ''),
63
53
  });
64
54
  // convert page data to page init data, use for AIGNE
65
55
  function convertPageData(state, page, locale) {
66
- var _a, _b, _c;
67
56
  // 如果页面不存在,返回 null
68
57
  if (!page) {
69
58
  return null;
70
59
  }
71
60
  // 获取默认语言
72
- const defaultLocale = (_a = state.config) === null || _a === void 0 ? void 0 : _a.defaultLocale;
61
+ const defaultLocale = state.config?.defaultLocale;
73
62
  // 获取页面在指定语言下的数据,如果不存在则使用默认语言的数据
74
- const pageLocaleData = ((_b = page.locales) === null || _b === void 0 ? void 0 : _b[locale]) || ((_c = page.locales) === null || _c === void 0 ? void 0 : _c[defaultLocale]) || {};
63
+ const pageLocaleData = page.locales?.[locale] || page.locales?.[defaultLocale] || {};
75
64
  // 初始化基础页面数据
76
65
  const result = {
77
66
  title: pageLocaleData.title,
@@ -81,26 +70,24 @@ function convertPageData(state, page, locale) {
81
70
  };
82
71
  // 处理每个 section 的数据
83
72
  page.sectionIds.forEach((sectionId) => {
84
- var _a, _b, _c, _d, _e, _f, _g;
85
73
  const section = page.sections[sectionId];
86
74
  if (!section)
87
75
  return;
88
76
  // 获取 section 在指定语言下的配置,如果不存在则使用默认语言的配置
89
77
  const sectionConfig = section.config || {};
90
- const sectionData = ((_a = section.locales) === null || _a === void 0 ? void 0 : _a[locale]) || ((_b = section.locales) === null || _b === void 0 ? void 0 : _b[defaultLocale]);
78
+ const sectionData = section.locales?.[locale] || section.locales?.[defaultLocale];
91
79
  const sectionName = section.name || section.id;
92
80
  if (section.component === 'custom-component') {
93
81
  // 处理自定义组件
94
- const componentId = (sectionConfig === null || sectionConfig === void 0 ? void 0 : sectionConfig.componentId) || ((_c = section.config) === null || _c === void 0 ? void 0 : _c.componentId);
95
- 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);
82
+ const componentId = sectionConfig?.componentId || section.config?.componentId;
83
+ const component = state.components[componentId]?.data || state.resources?.components?.[componentId]?.component;
96
84
  if (component) {
97
85
  result.sectionsData[sectionName] = {};
98
86
  // 遍历组件的属性定义
99
87
  Object.entries(component.properties || {}).forEach(([, prop]) => {
100
- var _a, _b, _c, _d, _e;
101
- const key = ((_a = prop.data) === null || _a === void 0 ? void 0 : _a.key) || ((_b = prop.data) === null || _b === void 0 ? void 0 : _b.id);
88
+ const key = prop.data?.key || prop.data?.id;
102
89
  if (key) {
103
- 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;
90
+ result.sectionsData[sectionName][key] = sectionData?.properties?.[prop.data?.id]?.value;
104
91
  }
105
92
  });
106
93
  }
@@ -108,33 +95,33 @@ function convertPageData(state, page, locale) {
108
95
  else if (section.component === 'iframe') {
109
96
  // 处理 iframe 组件
110
97
  result.sectionsData[sectionName] = {
111
- src: sectionData === null || sectionData === void 0 ? void 0 : sectionData.src,
112
- title: sectionData === null || sectionData === void 0 ? void 0 : sectionData.title,
113
- description: sectionData === null || sectionData === void 0 ? void 0 : sectionData.description,
98
+ src: sectionData?.src,
99
+ title: sectionData?.title,
100
+ description: sectionData?.description,
114
101
  };
115
102
  }
116
103
  else if (section.component === 'section') {
117
104
  // 处理普通 section 组件
118
105
  result.sectionsData[sectionName] = {
119
- title: sectionData === null || sectionData === void 0 ? void 0 : sectionData.title,
120
- description: sectionData === null || sectionData === void 0 ? void 0 : sectionData.description,
121
- image: sectionData === null || sectionData === void 0 ? void 0 : sectionData.image,
122
- imageMeta: sectionData === null || sectionData === void 0 ? void 0 : sectionData.imageMeta,
106
+ title: sectionData?.title,
107
+ description: sectionData?.description,
108
+ image: sectionData?.image,
109
+ imageMeta: sectionData?.imageMeta,
123
110
  };
124
111
  }
125
112
  else if (section.component === 'section-card-list') {
126
113
  // 处理卡片列表组件
127
114
  result.sectionsData[sectionName] = {
128
- title: sectionData === null || sectionData === void 0 ? void 0 : sectionData.title,
129
- description: sectionData === null || sectionData === void 0 ? void 0 : sectionData.description,
130
- list: (sectionData === null || sectionData === void 0 ? void 0 : sectionData.list) || [],
115
+ title: sectionData?.title,
116
+ description: sectionData?.description,
117
+ list: sectionData?.list || [],
131
118
  };
132
119
  }
133
120
  else if (section.component === 'toc') {
134
121
  // 处理目录组件
135
122
  result.sectionsData[sectionName] = {
136
- title: sectionData === null || sectionData === void 0 ? void 0 : sectionData.title,
137
- description: sectionData === null || sectionData === void 0 ? void 0 : sectionData.description,
123
+ title: sectionData?.title,
124
+ description: sectionData?.description,
138
125
  };
139
126
  }
140
127
  else {
@@ -178,17 +165,16 @@ const RuntimeComponent = (props) => {
178
165
  resolve(null);
179
166
  return;
180
167
  }
181
- const getPageInitData = () => __awaiter(void 0, void 0, void 0, function* () {
182
- var _a;
168
+ const getPageInitData = async () => {
183
169
  let pageInitData = {};
184
170
  if (proxyState.rawState.pages) {
185
- const page = Object.values(proxyState.rawState.pages).find((page) => (page === null || page === void 0 ? void 0 : page.slug) === props.path);
171
+ const page = Object.values(proxyState.rawState.pages).find((page) => page?.slug === props.path);
186
172
  if (page) {
187
- pageInitData = convertPageData(proxyState.rawState, page, locale || ((_a = proxyState.rawState.config) === null || _a === void 0 ? void 0 : _a.defaultLocale));
173
+ pageInitData = convertPageData(proxyState.rawState, page, locale || proxyState.rawState.config?.defaultLocale);
188
174
  }
189
175
  }
190
176
  resolve(pageInitData);
191
- });
177
+ };
192
178
  // get init data, check if state is ready
193
179
  if (proxyState.rawState) {
194
180
  getPageInitData();
@@ -208,17 +194,16 @@ const RuntimeComponent = (props) => {
208
194
  });
209
195
  // 当状态更新时通知所有监听器
210
196
  (0, react_1.useEffect)(() => {
211
- var _a;
212
- if ((_a = props.stateRef) === null || _a === void 0 ? void 0 : _a.current) {
197
+ if (props.stateRef?.current) {
213
198
  listeners.current.forEach((listener) => listener(proxyState.rawState));
214
199
  }
215
200
  }, [proxyState.rawState]);
216
- const { loading } = (0, ahooks_1.useRequest)(() => __awaiter(void 0, void 0, void 0, function* () {
201
+ const { loading } = (0, ahooks_1.useRequest)(async () => {
217
202
  if (props.state) {
218
203
  return null;
219
204
  }
220
205
  // 统一使用 /api/pages 接口
221
- const { data } = yield api.get('/api/pages', {
206
+ const { data } = await api.get('/api/pages', {
222
207
  params: {
223
208
  did: props.did,
224
209
  projectId: props.projectId,
@@ -227,7 +212,7 @@ const RuntimeComponent = (props) => {
227
212
  });
228
213
  proxyState.rawState = data.state || data;
229
214
  return data;
230
- }), {
215
+ }, {
231
216
  refreshDeps: [props.did, props.state, props.projectId],
232
217
  });
233
218
  if (loading) {
@@ -243,36 +228,28 @@ const RuntimeComponent = (props) => {
243
228
  };
244
229
  exports.RuntimeComponent = RuntimeComponent;
245
230
  class Runtime {
231
+ stateRef = react_1.default.createRef();
232
+ props;
246
233
  constructor(props) {
247
- this.stateRef = react_1.default.createRef();
248
234
  this.props = props;
249
235
  }
250
236
  get rawState() {
251
- var _a;
252
- return (_a = this.stateRef.current) === null || _a === void 0 ? void 0 : _a.getState();
237
+ return this.stateRef.current?.getState();
253
238
  }
254
239
  set pageData(data) {
255
- var _a;
256
- (_a = this.stateRef.current) === null || _a === void 0 ? void 0 : _a.setPageData(data);
240
+ this.stateRef.current?.setPageData(data);
257
241
  }
258
242
  get pageData() {
259
- var _a;
260
- return (_a = this.stateRef.current) === null || _a === void 0 ? void 0 : _a.getPageData();
243
+ return this.stateRef.current?.getPageData();
261
244
  }
262
- getPageInitData(_a) {
263
- return __awaiter(this, arguments, void 0, function* ({ locale }) {
264
- var _b;
265
- return (_b = this.stateRef.current) === null || _b === void 0 ? void 0 : _b.getPageInitData({ locale });
266
- });
245
+ async getPageInitData({ locale }) {
246
+ return this.stateRef.current?.getPageInitData({ locale });
267
247
  }
268
- waitForState() {
269
- return __awaiter(this, void 0, void 0, function* () {
270
- var _a;
271
- return (_a = this.stateRef.current) === null || _a === void 0 ? void 0 : _a.waitForState();
272
- });
248
+ async waitForState() {
249
+ return this.stateRef.current?.waitForState();
273
250
  }
274
251
  render() {
275
- return (0, jsx_runtime_1.jsx)(exports.RuntimeComponent, Object.assign({}, this.props, { stateRef: this.stateRef }));
252
+ return (0, jsx_runtime_1.jsx)(exports.RuntimeComponent, { ...this.props, stateRef: this.stateRef });
276
253
  }
277
254
  }
278
255
  exports.default = Runtime;
@@ -55,11 +55,11 @@ function CreateResource({ open, onClose, blockletDid, mode, }) {
55
55
  },
56
56
  }
57
57
  : {};
58
- return ((0, jsx_runtime_1.jsx)(material_1.Box, Object.assign({}, boxProps, { children: (0, jsx_runtime_1.jsx)(react_1.Suspense, { children: (0, jsx_runtime_1.jsx)(BlockletStudio, { mode: mode, tenantScope: tenantScope, title: "Pages Kit Blocks", description: "", note: "", introduction: "", logo: "", componentDid: blockletDid,
58
+ return ((0, jsx_runtime_1.jsx)(material_1.Box, { ...boxProps, children: (0, jsx_runtime_1.jsx)(react_1.Suspense, { children: (0, jsx_runtime_1.jsx)(BlockletStudio, { mode: mode, tenantScope: tenantScope, title: "Pages Kit Blocks", description: "", note: "", introduction: "", logo: "", componentDid: blockletDid,
59
59
  // 透传到 get blocklet resource 的参数
60
60
  resourcesParams: {}, dependentComponentsMode: "readonly", open: true, setOpen: () => onClose(), onConnected: () => { }, onUploaded: () => { }, onReleased: () => { },
61
61
  // onOpened={() => onOpened?.()}
62
62
  // 默认选中的资源
63
- resources: {} }) }) })));
63
+ resources: {} }) }) }));
64
64
  }
65
65
  exports.default = CreateResource;