@blocklet/pages-kit-runtime 0.1.19 → 0.1.20

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
@@ -57,6 +57,7 @@ const Box_1 = __importDefault(require("@mui/material/Box"));
57
57
  const ahooks_1 = require("ahooks");
58
58
  const react_1 = __importStar(require("react"));
59
59
  const ufo_1 = require("ufo");
60
+ const PAGES_KIT_BLOCKLET_DID = 'z8iZiDFg3vkkrPwsiba1TLXy3H9XHzFERsP8o';
60
61
  const CenteredContainer = ({ children }) => ((0, jsx_runtime_1.jsx)(Box_1.default, { display: "flex", justifyContent: "center", alignItems: "center", minHeight: "100vh", children: children }));
61
62
  const api = (0, js_sdk_1.createAxios)({
62
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,12 +65,17 @@ const api = (0, js_sdk_1.createAxios)({
64
65
  const RuntimeComponent = (props) => {
65
66
  const proxyState = (0, ahooks_1.useReactive)({
66
67
  rawState: props.state,
68
+ pageData: props.pageData,
67
69
  });
68
70
  const listeners = (0, react_1.useRef)(new Set());
69
71
  // 暴露 proxyState 和 waitForState 给父组件
70
72
  (0, react_1.useImperativeHandle)(props.stateRef, () => {
71
73
  return {
72
74
  getState: () => proxyState.rawState,
75
+ getPageData: () => proxyState.pageData,
76
+ setPageData: (data) => {
77
+ proxyState.pageData = data;
78
+ },
73
79
  waitForState: () => {
74
80
  if (proxyState.rawState) {
75
81
  return Promise.resolve(proxyState.rawState);
@@ -94,25 +100,54 @@ const RuntimeComponent = (props) => {
94
100
  }
95
101
  }, [proxyState.rawState]);
96
102
  const { loading } = (0, ahooks_1.useRequest)(() => __awaiter(void 0, void 0, void 0, function* () {
97
- if (!props.did || props.state) {
103
+ var _a;
104
+ if (props.state) {
98
105
  return null;
99
106
  }
100
- const { data } = yield api.get('/api/pages', {
101
- params: {
102
- did: props.did,
103
- },
104
- });
105
- proxyState.rawState = data.state;
106
- return data;
107
+ if (!props.did && !props.projectId) {
108
+ return null;
109
+ }
110
+ let stateData;
111
+ if (props.did) {
112
+ // get state from resource blocklet by did
113
+ const { data } = yield api.get('/api/pages', {
114
+ params: {
115
+ did: props.did,
116
+ },
117
+ });
118
+ proxyState.rawState = data.state;
119
+ stateData = data;
120
+ }
121
+ else if (props.projectId) {
122
+ let pagesKitBaseUrl = props.siteFrom;
123
+ if (!pagesKitBaseUrl) {
124
+ pagesKitBaseUrl = window.location.origin;
125
+ // find pages kit prefix from window.blocklet
126
+ const pagesKitMountPoint = (_a = window.blocklet) === null || _a === void 0 ? void 0 : _a.componentMountPoints.find((i) => i.did === PAGES_KIT_BLOCKLET_DID);
127
+ if (pagesKitMountPoint) {
128
+ pagesKitBaseUrl = (0, ufo_1.joinURL)(pagesKitBaseUrl, pagesKitMountPoint.mountPoint);
129
+ }
130
+ }
131
+ const apiUrl = (0, ufo_1.joinURL)(pagesKitBaseUrl, 'api/projects', props.projectId, 'pages');
132
+ // get state from project by projectId
133
+ const { data } = yield api.get(apiUrl);
134
+ proxyState.rawState = data;
135
+ stateData = data;
136
+ }
137
+ return stateData;
107
138
  }), {
108
139
  refreshDeps: [props.did, props.state],
109
140
  });
141
+ (0, react_1.useEffect)(() => { }, [proxyState.pageData]);
110
142
  if (loading) {
111
143
  return ((0, jsx_runtime_1.jsx)(CenteredContainer, { children: (0, jsx_runtime_1.jsx)(Spinner_1.default, {}) }));
112
144
  }
113
145
  if (!proxyState.rawState) {
114
146
  return ((0, jsx_runtime_1.jsx)(CenteredContainer, { children: (0, jsx_runtime_1.jsx)(Result_1.default, { status: 404 }) }));
115
147
  }
148
+ if (props.path) {
149
+ return ((0, jsx_runtime_1.jsx)(home_1.PageViewByPath, { mode: "production", state: proxyState.rawState, path: props.path, pageData: proxyState.pageData }));
150
+ }
116
151
  return (0, jsx_runtime_1.jsx)(home_1.PageRoutes, { mode: "production", state: proxyState.rawState });
117
152
  };
118
153
  exports.RuntimeComponent = RuntimeComponent;
@@ -125,6 +160,14 @@ class Runtime {
125
160
  var _a;
126
161
  return (_a = this.stateRef.current) === null || _a === void 0 ? void 0 : _a.getState();
127
162
  }
163
+ set pageData(data) {
164
+ var _a;
165
+ (_a = this.stateRef.current) === null || _a === void 0 ? void 0 : _a.setPageData(data);
166
+ }
167
+ get pageData() {
168
+ var _a;
169
+ return (_a = this.stateRef.current) === null || _a === void 0 ? void 0 : _a.getPageData();
170
+ }
128
171
  waitForState() {
129
172
  return __awaiter(this, void 0, void 0, function* () {
130
173
  var _a;
@@ -1,7 +1,18 @@
1
1
  "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
2
16
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.PropertiesConfig = exports.BasicInfo = void 0;
4
17
  // @ts-ignore
5
- var setting_1 = require("@blocklet/pages-kit-inner-components/setting");
6
- Object.defineProperty(exports, "BasicInfo", { enumerable: true, get: function () { return setting_1.BasicInfo; } });
7
- Object.defineProperty(exports, "PropertiesConfig", { enumerable: true, get: function () { return setting_1.PropertiesConfig; } });
18
+ __exportStar(require("@blocklet/pages-kit-inner-components/setting"), exports);