@blocklet/pages-kit-runtime 0.1.0

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/LICENSE ADDED
@@ -0,0 +1,13 @@
1
+ Copyright 2018-2020 ArcBlock
2
+
3
+ Licensed under the Apache License, Version 2.0 (the "License");
4
+ you may not use this file except in compliance with the License.
5
+ You may obtain a copy of the License at
6
+
7
+ http://www.apache.org/licenses/LICENSE-2.0
8
+
9
+ Unless required by applicable law or agreed to in writing, software
10
+ distributed under the License is distributed on an "AS IS" BASIS,
11
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ See the License for the specific language governing permissions and
13
+ limitations under the License.
@@ -0,0 +1,301 @@
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 __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
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
+ var __importDefault = (this && this.__importDefault) || function (mod) {
45
+ return (mod && mod.__esModule) ? mod : { "default": mod };
46
+ };
47
+ var _a;
48
+ Object.defineProperty(exports, "__esModule", { value: true });
49
+ exports.RuntimeComponent = void 0;
50
+ const jsx_runtime_1 = require("react/jsx-runtime");
51
+ const Result_1 = __importDefault(require("@arcblock/ux/lib/Result"));
52
+ const Spinner_1 = __importDefault(require("@arcblock/ux/lib/Spinner"));
53
+ const js_sdk_1 = require("@blocklet/js-sdk");
54
+ // @ts-ignore
55
+ const home_1 = require("@blocklet/pages-kit-inner-components/home");
56
+ const Box_1 = __importDefault(require("@mui/material/Box"));
57
+ const ahooks_1 = require("ahooks");
58
+ const react_1 = __importStar(require("react"));
59
+ const ufo_1 = require("ufo");
60
+ const PAGES_KIT_BLOCKLET_DID = 'z8iZiDFg3vkkrPwsiba1TLXy3H9XHzFERsP8o';
61
+ const CenteredContainer = ({ children }) => ((0, jsx_runtime_1.jsx)(Box_1.default, { display: "flex", justifyContent: "center", alignItems: "center", minHeight: "100vh", children: children }));
62
+ const api = (0, js_sdk_1.createAxios)({
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
+ });
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
+ }
148
+ const RuntimeComponent = (props) => {
149
+ const proxyState = (0, ahooks_1.useReactive)({
150
+ rawState: props.state,
151
+ pageData: props.pageData,
152
+ });
153
+ const listeners = (0, react_1.useRef)(new Set());
154
+ // 暴露 proxyState 和 waitForState 给父组件
155
+ (0, react_1.useImperativeHandle)(props.stateRef, () => {
156
+ return {
157
+ getState: () => proxyState.rawState,
158
+ getPageData: () => proxyState.pageData,
159
+ setPageData: (data) => {
160
+ proxyState.pageData = data;
161
+ },
162
+ waitForState: () => {
163
+ if (proxyState.rawState) {
164
+ return Promise.resolve(proxyState.rawState);
165
+ }
166
+ return new Promise((resolve) => {
167
+ const listener = (state) => {
168
+ if (state) {
169
+ listeners.current.delete(listener);
170
+ resolve(state);
171
+ }
172
+ };
173
+ listeners.current.add(listener);
174
+ });
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
+ },
208
+ };
209
+ });
210
+ // 当状态更新时通知所有监听器
211
+ (0, react_1.useEffect)(() => {
212
+ var _a;
213
+ if ((_a = props.stateRef) === null || _a === void 0 ? void 0 : _a.current) {
214
+ listeners.current.forEach((listener) => listener(proxyState.rawState));
215
+ }
216
+ }, [proxyState.rawState]);
217
+ const { loading } = (0, ahooks_1.useRequest)(() => __awaiter(void 0, void 0, void 0, function* () {
218
+ var _a;
219
+ if (props.state) {
220
+ return null;
221
+ }
222
+ if (!props.did && !props.projectId) {
223
+ return null;
224
+ }
225
+ let stateData;
226
+ if (props.did) {
227
+ // get state from resource blocklet by did
228
+ const { data } = yield api.get('/api/pages', {
229
+ params: {
230
+ did: props.did,
231
+ },
232
+ });
233
+ proxyState.rawState = data.state;
234
+ stateData = data;
235
+ }
236
+ else if (props.projectId) {
237
+ let pagesKitBaseUrl = props.siteFrom;
238
+ if (!pagesKitBaseUrl) {
239
+ pagesKitBaseUrl = window.location.origin;
240
+ // find pages kit prefix from window.blocklet
241
+ const pagesKitMountPoint = (_a = window.blocklet) === null || _a === void 0 ? void 0 : _a.componentMountPoints.find((i) => i.did === PAGES_KIT_BLOCKLET_DID);
242
+ if (pagesKitMountPoint) {
243
+ pagesKitBaseUrl = (0, ufo_1.joinURL)(pagesKitBaseUrl, pagesKitMountPoint.mountPoint);
244
+ }
245
+ }
246
+ const apiUrl = (0, ufo_1.joinURL)(pagesKitBaseUrl, 'api/projects', props.projectId, 'pages');
247
+ // get state from project by projectId
248
+ const { data } = yield api.get(apiUrl);
249
+ proxyState.rawState = data;
250
+ stateData = data;
251
+ }
252
+ return stateData;
253
+ }), {
254
+ refreshDeps: [props.did, props.state, props.projectId],
255
+ });
256
+ if (loading) {
257
+ return ((0, jsx_runtime_1.jsx)(CenteredContainer, { children: (0, jsx_runtime_1.jsx)(Spinner_1.default, {}) }));
258
+ }
259
+ if (!proxyState.rawState) {
260
+ return ((0, jsx_runtime_1.jsx)(CenteredContainer, { children: (0, jsx_runtime_1.jsx)(Result_1.default, { status: 404 }) }));
261
+ }
262
+ if (props.path) {
263
+ return ((0, jsx_runtime_1.jsx)(home_1.PageViewByPath, { mode: "production", state: proxyState.rawState, path: props.path, pageData: proxyState.pageData }));
264
+ }
265
+ return (0, jsx_runtime_1.jsx)(home_1.PageRoutes, { mode: "production", state: proxyState.rawState });
266
+ };
267
+ exports.RuntimeComponent = RuntimeComponent;
268
+ class Runtime {
269
+ constructor(props) {
270
+ this.stateRef = react_1.default.createRef();
271
+ this.props = props;
272
+ }
273
+ get rawState() {
274
+ var _a;
275
+ return (_a = this.stateRef.current) === null || _a === void 0 ? void 0 : _a.getState();
276
+ }
277
+ set pageData(data) {
278
+ var _a;
279
+ (_a = this.stateRef.current) === null || _a === void 0 ? void 0 : _a.setPageData(data);
280
+ }
281
+ get pageData() {
282
+ var _a;
283
+ return (_a = this.stateRef.current) === null || _a === void 0 ? void 0 : _a.getPageData();
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
+ }
291
+ waitForState() {
292
+ return __awaiter(this, void 0, void 0, function* () {
293
+ var _a;
294
+ return (_a = this.stateRef.current) === null || _a === void 0 ? void 0 : _a.waitForState();
295
+ });
296
+ }
297
+ render() {
298
+ return (0, jsx_runtime_1.jsx)(exports.RuntimeComponent, Object.assign({}, this.props, { stateRef: this.stateRef }));
299
+ }
300
+ }
301
+ exports.default = Runtime;
@@ -0,0 +1,32 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CreateResource = CreateResource;
4
+ const jsx_runtime_1 = require("react/jsx-runtime");
5
+ const ui_react_1 = require("@blocklet/ui-react");
6
+ const material_1 = require("@mui/material");
7
+ const react_1 = require("react");
8
+ function CreateResource({ open, onClose, blockletDid, mode, }) {
9
+ if (!open) {
10
+ return null;
11
+ }
12
+ const tenantScope = 'pages-kit-block-studio';
13
+ const isPage = mode === 'page';
14
+ const boxProps = isPage
15
+ ? {
16
+ sx: {
17
+ iframe: {
18
+ width: 'calc(100% - 16px) !important',
19
+ height: 'calc(100% - 16px) !important',
20
+ padding: '16px !important',
21
+ },
22
+ },
23
+ }
24
+ : {};
25
+ 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)(ui_react_1.BlockletStudio, { mode: mode, tenantScope: tenantScope, title: "Pages Kit Blocks", description: "", note: "", introduction: "", logo: "", componentDid: blockletDid,
26
+ // 透传到 get blocklet resource 的参数
27
+ resourcesParams: {}, dependentComponentsMode: "readonly", open: true, setOpen: () => onClose(), onConnected: () => { }, onUploaded: () => { }, onReleased: () => { },
28
+ // onOpened={() => onOpened?.()}
29
+ // 默认选中的资源
30
+ resources: {} }) }) })));
31
+ }
32
+ exports.default = CreateResource;
@@ -0,0 +1,18 @@
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
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ // eslint-disable-next-line import/export
18
+ __exportStar(require("./settings"), exports);
@@ -0,0 +1,18 @@
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
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ // @ts-ignore
18
+ __exportStar(require("@blocklet/pages-kit-inner-components/setting"), exports);
@@ -0,0 +1,19 @@
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
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./create-resource"), exports);
18
+ // eslint-disable-next-line import/export
19
+ __exportStar(require("./custom-component"), exports);
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.translations = void 0;
4
+ // @ts-ignore
5
+ var locales_1 = require("@blocklet/pages-kit-inner-components/locales");
6
+ Object.defineProperty(exports, "translations", { enumerable: true, get: function () { return locales_1.translations; } });