@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 +13 -0
- package/lib/cjs/client.js +301 -0
- package/lib/cjs/components/create-resource.js +32 -0
- package/lib/cjs/components/custom-component/index.js +18 -0
- package/lib/cjs/components/custom-component/settings.js +18 -0
- package/lib/cjs/components/index.js +19 -0
- package/lib/cjs/index.js +2 -0
- package/lib/cjs/locales/index.js +6 -0
- package/lib/cjs/tsconfig.tsbuildinfo +1 -0
- package/lib/cjs/types/index.js +5 -0
- package/lib/cjs/utils/index.js +1 -0
- package/lib/esm/client.js +260 -0
- package/lib/esm/components/create-resource.js +29 -0
- package/lib/esm/components/custom-component/index.js +2 -0
- package/lib/esm/components/custom-component/settings.js +2 -0
- package/lib/esm/components/index.js +3 -0
- package/lib/esm/index.js +1 -0
- package/lib/esm/locales/index.js +2 -0
- package/lib/esm/tsconfig.tsbuildinfo +1 -0
- package/lib/esm/types/index.js +2 -0
- package/lib/esm/utils/index.js +1 -0
- package/lib/types/client.d.ts +36 -0
- package/lib/types/components/create-resource.d.ts +7 -0
- package/lib/types/components/custom-component/index.d.ts +1 -0
- package/lib/types/components/custom-component/settings.d.ts +1 -0
- package/lib/types/components/index.d.ts +2 -0
- package/lib/types/index.d.ts +1 -0
- package/lib/types/locales/index.d.ts +1 -0
- package/lib/types/tsconfig.tsbuildinfo +1 -0
- package/lib/types/types/index.d.ts +39 -0
- package/lib/types/utils/index.d.ts +0 -0
- package/package.json +161 -0
- package/tsconfig.json +13 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";
|
|
@@ -0,0 +1,260 @@
|
|
|
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
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
12
|
+
import Result from '@arcblock/ux/lib/Result';
|
|
13
|
+
import Spinner from '@arcblock/ux/lib/Spinner';
|
|
14
|
+
import { createAxios } from '@blocklet/js-sdk';
|
|
15
|
+
// @ts-ignore
|
|
16
|
+
import { PageRoutes, PageViewByPath } from '@blocklet/pages-kit-inner-components/home';
|
|
17
|
+
import Box from '@mui/material/Box';
|
|
18
|
+
import { useRequest, useReactive } from 'ahooks';
|
|
19
|
+
import React, { useImperativeHandle, useEffect, useRef } from 'react';
|
|
20
|
+
import { joinURL } from 'ufo';
|
|
21
|
+
const PAGES_KIT_BLOCKLET_DID = 'z8iZiDFg3vkkrPwsiba1TLXy3H9XHzFERsP8o';
|
|
22
|
+
const CenteredContainer = ({ children }) => (_jsx(Box, { display: "flex", justifyContent: "center", alignItems: "center", minHeight: "100vh", children: children }));
|
|
23
|
+
const api = createAxios({
|
|
24
|
+
baseURL: joinURL(((_a = window === null || window === void 0 ? void 0 : window.blocklet) === null || _a === void 0 ? void 0 : _a.prefix) || ''),
|
|
25
|
+
});
|
|
26
|
+
// convert page data to page init data, use for AIGNE
|
|
27
|
+
function convertPageData(state, page, locale) {
|
|
28
|
+
var _a, _b, _c;
|
|
29
|
+
// 如果页面不存在,返回 null
|
|
30
|
+
if (!page) {
|
|
31
|
+
return null;
|
|
32
|
+
}
|
|
33
|
+
// 获取默认语言
|
|
34
|
+
const defaultLocale = (_a = state.config) === null || _a === void 0 ? void 0 : _a.defaultLocale;
|
|
35
|
+
// 获取页面在指定语言下的数据,如果不存在则使用默认语言的数据
|
|
36
|
+
const pageLocaleData = ((_b = page.locales) === null || _b === void 0 ? void 0 : _b[locale]) || ((_c = page.locales) === null || _c === void 0 ? void 0 : _c[defaultLocale]) || {};
|
|
37
|
+
// 初始化基础页面数据
|
|
38
|
+
const result = {
|
|
39
|
+
title: pageLocaleData.title,
|
|
40
|
+
image: pageLocaleData.image,
|
|
41
|
+
description: pageLocaleData.description,
|
|
42
|
+
sectionsData: {},
|
|
43
|
+
};
|
|
44
|
+
// 处理每个 section 的数据
|
|
45
|
+
page.sectionIds.forEach((sectionId) => {
|
|
46
|
+
var _a, _b, _c, _d, _e, _f, _g;
|
|
47
|
+
const section = page.sections[sectionId];
|
|
48
|
+
if (!section)
|
|
49
|
+
return;
|
|
50
|
+
// 获取 section 在指定语言下的配置,如果不存在则使用默认语言的配置
|
|
51
|
+
const sectionConfig = section.config || {};
|
|
52
|
+
const sectionData = ((_a = section.locales) === null || _a === void 0 ? void 0 : _a[locale]) || ((_b = section.locales) === null || _b === void 0 ? void 0 : _b[defaultLocale]);
|
|
53
|
+
const sectionName = section.name || section.id;
|
|
54
|
+
if (section.component === 'custom-component') {
|
|
55
|
+
// 处理自定义组件
|
|
56
|
+
const componentId = (sectionConfig === null || sectionConfig === void 0 ? void 0 : sectionConfig.componentId) || ((_c = section.config) === null || _c === void 0 ? void 0 : _c.componentId);
|
|
57
|
+
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);
|
|
58
|
+
if (component) {
|
|
59
|
+
result.sectionsData[sectionName] = {};
|
|
60
|
+
// 遍历组件的属性定义
|
|
61
|
+
Object.entries(component.properties || {}).forEach(([, prop]) => {
|
|
62
|
+
var _a, _b, _c, _d, _e;
|
|
63
|
+
const key = ((_a = prop.data) === null || _a === void 0 ? void 0 : _a.key) || ((_b = prop.data) === null || _b === void 0 ? void 0 : _b.id);
|
|
64
|
+
if (key) {
|
|
65
|
+
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;
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
else if (section.component === 'iframe') {
|
|
71
|
+
// 处理 iframe 组件
|
|
72
|
+
result.sectionsData[sectionName] = {
|
|
73
|
+
src: sectionData === null || sectionData === void 0 ? void 0 : sectionData.src,
|
|
74
|
+
title: sectionData === null || sectionData === void 0 ? void 0 : sectionData.title,
|
|
75
|
+
description: sectionData === null || sectionData === void 0 ? void 0 : sectionData.description,
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
else if (section.component === 'section') {
|
|
79
|
+
// 处理普通 section 组件
|
|
80
|
+
result.sectionsData[sectionName] = {
|
|
81
|
+
title: sectionData === null || sectionData === void 0 ? void 0 : sectionData.title,
|
|
82
|
+
description: sectionData === null || sectionData === void 0 ? void 0 : sectionData.description,
|
|
83
|
+
image: sectionData === null || sectionData === void 0 ? void 0 : sectionData.image,
|
|
84
|
+
imageMeta: sectionData === null || sectionData === void 0 ? void 0 : sectionData.imageMeta,
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
else if (section.component === 'section-card-list') {
|
|
88
|
+
// 处理卡片列表组件
|
|
89
|
+
result.sectionsData[sectionName] = {
|
|
90
|
+
title: sectionData === null || sectionData === void 0 ? void 0 : sectionData.title,
|
|
91
|
+
description: sectionData === null || sectionData === void 0 ? void 0 : sectionData.description,
|
|
92
|
+
list: (sectionData === null || sectionData === void 0 ? void 0 : sectionData.list) || [],
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
else if (section.component === 'toc') {
|
|
96
|
+
// 处理目录组件
|
|
97
|
+
result.sectionsData[sectionName] = {
|
|
98
|
+
title: sectionData === null || sectionData === void 0 ? void 0 : sectionData.title,
|
|
99
|
+
description: sectionData === null || sectionData === void 0 ? void 0 : sectionData.description,
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
else {
|
|
103
|
+
// 处理其他类型的组件
|
|
104
|
+
result.sectionsData[sectionName] = sectionData;
|
|
105
|
+
}
|
|
106
|
+
});
|
|
107
|
+
return result;
|
|
108
|
+
}
|
|
109
|
+
export const RuntimeComponent = (props) => {
|
|
110
|
+
const proxyState = useReactive({
|
|
111
|
+
rawState: props.state,
|
|
112
|
+
pageData: props.pageData,
|
|
113
|
+
});
|
|
114
|
+
const listeners = useRef(new Set());
|
|
115
|
+
// 暴露 proxyState 和 waitForState 给父组件
|
|
116
|
+
useImperativeHandle(props.stateRef, () => {
|
|
117
|
+
return {
|
|
118
|
+
getState: () => proxyState.rawState,
|
|
119
|
+
getPageData: () => proxyState.pageData,
|
|
120
|
+
setPageData: (data) => {
|
|
121
|
+
proxyState.pageData = data;
|
|
122
|
+
},
|
|
123
|
+
waitForState: () => {
|
|
124
|
+
if (proxyState.rawState) {
|
|
125
|
+
return Promise.resolve(proxyState.rawState);
|
|
126
|
+
}
|
|
127
|
+
return new Promise((resolve) => {
|
|
128
|
+
const listener = (state) => {
|
|
129
|
+
if (state) {
|
|
130
|
+
listeners.current.delete(listener);
|
|
131
|
+
resolve(state);
|
|
132
|
+
}
|
|
133
|
+
};
|
|
134
|
+
listeners.current.add(listener);
|
|
135
|
+
});
|
|
136
|
+
},
|
|
137
|
+
getPageInitData: ({ locale }) => {
|
|
138
|
+
return new Promise((resolve) => {
|
|
139
|
+
if (!props.path) {
|
|
140
|
+
resolve(null);
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
const getPageInitData = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
144
|
+
var _a;
|
|
145
|
+
let pageInitData = {};
|
|
146
|
+
if (proxyState.rawState.pages) {
|
|
147
|
+
const page = Object.values(proxyState.rawState.pages).find((page) => (page === null || page === void 0 ? void 0 : page.slug) === props.path);
|
|
148
|
+
if (page) {
|
|
149
|
+
pageInitData = convertPageData(proxyState.rawState, page, locale || ((_a = proxyState.rawState.config) === null || _a === void 0 ? void 0 : _a.defaultLocale));
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
resolve(pageInitData);
|
|
153
|
+
});
|
|
154
|
+
// get init data, check if state is ready
|
|
155
|
+
if (proxyState.rawState) {
|
|
156
|
+
getPageInitData();
|
|
157
|
+
}
|
|
158
|
+
else {
|
|
159
|
+
const listener = (state) => {
|
|
160
|
+
if (state) {
|
|
161
|
+
listeners.current.delete(listener);
|
|
162
|
+
getPageInitData();
|
|
163
|
+
}
|
|
164
|
+
};
|
|
165
|
+
listeners.current.add(listener);
|
|
166
|
+
}
|
|
167
|
+
});
|
|
168
|
+
},
|
|
169
|
+
};
|
|
170
|
+
});
|
|
171
|
+
// 当状态更新时通知所有监听器
|
|
172
|
+
useEffect(() => {
|
|
173
|
+
var _a;
|
|
174
|
+
if ((_a = props.stateRef) === null || _a === void 0 ? void 0 : _a.current) {
|
|
175
|
+
listeners.current.forEach((listener) => listener(proxyState.rawState));
|
|
176
|
+
}
|
|
177
|
+
}, [proxyState.rawState]);
|
|
178
|
+
const { loading } = useRequest(() => __awaiter(void 0, void 0, void 0, function* () {
|
|
179
|
+
var _a;
|
|
180
|
+
if (props.state) {
|
|
181
|
+
return null;
|
|
182
|
+
}
|
|
183
|
+
if (!props.did && !props.projectId) {
|
|
184
|
+
return null;
|
|
185
|
+
}
|
|
186
|
+
let stateData;
|
|
187
|
+
if (props.did) {
|
|
188
|
+
// get state from resource blocklet by did
|
|
189
|
+
const { data } = yield api.get('/api/pages', {
|
|
190
|
+
params: {
|
|
191
|
+
did: props.did,
|
|
192
|
+
},
|
|
193
|
+
});
|
|
194
|
+
proxyState.rawState = data.state;
|
|
195
|
+
stateData = data;
|
|
196
|
+
}
|
|
197
|
+
else if (props.projectId) {
|
|
198
|
+
let pagesKitBaseUrl = props.siteFrom;
|
|
199
|
+
if (!pagesKitBaseUrl) {
|
|
200
|
+
pagesKitBaseUrl = window.location.origin;
|
|
201
|
+
// find pages kit prefix from window.blocklet
|
|
202
|
+
const pagesKitMountPoint = (_a = window.blocklet) === null || _a === void 0 ? void 0 : _a.componentMountPoints.find((i) => i.did === PAGES_KIT_BLOCKLET_DID);
|
|
203
|
+
if (pagesKitMountPoint) {
|
|
204
|
+
pagesKitBaseUrl = joinURL(pagesKitBaseUrl, pagesKitMountPoint.mountPoint);
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
const apiUrl = joinURL(pagesKitBaseUrl, 'api/projects', props.projectId, 'pages');
|
|
208
|
+
// get state from project by projectId
|
|
209
|
+
const { data } = yield api.get(apiUrl);
|
|
210
|
+
proxyState.rawState = data;
|
|
211
|
+
stateData = data;
|
|
212
|
+
}
|
|
213
|
+
return stateData;
|
|
214
|
+
}), {
|
|
215
|
+
refreshDeps: [props.did, props.state, props.projectId],
|
|
216
|
+
});
|
|
217
|
+
if (loading) {
|
|
218
|
+
return (_jsx(CenteredContainer, { children: _jsx(Spinner, {}) }));
|
|
219
|
+
}
|
|
220
|
+
if (!proxyState.rawState) {
|
|
221
|
+
return (_jsx(CenteredContainer, { children: _jsx(Result, { status: 404 }) }));
|
|
222
|
+
}
|
|
223
|
+
if (props.path) {
|
|
224
|
+
return (_jsx(PageViewByPath, { mode: "production", state: proxyState.rawState, path: props.path, pageData: proxyState.pageData }));
|
|
225
|
+
}
|
|
226
|
+
return _jsx(PageRoutes, { mode: "production", state: proxyState.rawState });
|
|
227
|
+
};
|
|
228
|
+
export default class Runtime {
|
|
229
|
+
constructor(props) {
|
|
230
|
+
this.stateRef = React.createRef();
|
|
231
|
+
this.props = props;
|
|
232
|
+
}
|
|
233
|
+
get rawState() {
|
|
234
|
+
var _a;
|
|
235
|
+
return (_a = this.stateRef.current) === null || _a === void 0 ? void 0 : _a.getState();
|
|
236
|
+
}
|
|
237
|
+
set pageData(data) {
|
|
238
|
+
var _a;
|
|
239
|
+
(_a = this.stateRef.current) === null || _a === void 0 ? void 0 : _a.setPageData(data);
|
|
240
|
+
}
|
|
241
|
+
get pageData() {
|
|
242
|
+
var _a;
|
|
243
|
+
return (_a = this.stateRef.current) === null || _a === void 0 ? void 0 : _a.getPageData();
|
|
244
|
+
}
|
|
245
|
+
getPageInitData(_a) {
|
|
246
|
+
return __awaiter(this, arguments, void 0, function* ({ locale }) {
|
|
247
|
+
var _b;
|
|
248
|
+
return (_b = this.stateRef.current) === null || _b === void 0 ? void 0 : _b.getPageInitData({ locale });
|
|
249
|
+
});
|
|
250
|
+
}
|
|
251
|
+
waitForState() {
|
|
252
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
253
|
+
var _a;
|
|
254
|
+
return (_a = this.stateRef.current) === null || _a === void 0 ? void 0 : _a.waitForState();
|
|
255
|
+
});
|
|
256
|
+
}
|
|
257
|
+
render() {
|
|
258
|
+
return _jsx(RuntimeComponent, Object.assign({}, this.props, { stateRef: this.stateRef }));
|
|
259
|
+
}
|
|
260
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { BlockletStudio } from '@blocklet/ui-react';
|
|
3
|
+
import { Box } from '@mui/material';
|
|
4
|
+
import { Suspense } from 'react';
|
|
5
|
+
export function CreateResource({ open, onClose, blockletDid, mode, }) {
|
|
6
|
+
if (!open) {
|
|
7
|
+
return null;
|
|
8
|
+
}
|
|
9
|
+
const tenantScope = 'pages-kit-block-studio';
|
|
10
|
+
const isPage = mode === 'page';
|
|
11
|
+
const boxProps = isPage
|
|
12
|
+
? {
|
|
13
|
+
sx: {
|
|
14
|
+
iframe: {
|
|
15
|
+
width: 'calc(100% - 16px) !important',
|
|
16
|
+
height: 'calc(100% - 16px) !important',
|
|
17
|
+
padding: '16px !important',
|
|
18
|
+
},
|
|
19
|
+
},
|
|
20
|
+
}
|
|
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,
|
|
23
|
+
// 透传到 get blocklet resource 的参数
|
|
24
|
+
resourcesParams: {}, dependentComponentsMode: "readonly", open: true, setOpen: () => onClose(), onConnected: () => { }, onUploaded: () => { }, onReleased: () => { },
|
|
25
|
+
// onOpened={() => onOpened?.()}
|
|
26
|
+
// 默认选中的资源
|
|
27
|
+
resources: {} }) }) })));
|
|
28
|
+
}
|
|
29
|
+
export default CreateResource;
|
package/lib/esm/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|