@gant-lowcode/plugin-code-generator 0.0.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/README.md +23 -0
- package/dist/components/code-gen-action/index.css +18 -0
- package/dist/components/code-gen-action/index.d.ts +6 -0
- package/dist/components/code-gen-action/index.js +143 -0
- package/dist/components/code-gen-preview/fixPreviewCode.d.ts +2 -0
- package/dist/components/code-gen-preview/fixPreviewCode.js +119 -0
- package/dist/components/code-gen-preview/index.css +18 -0
- package/dist/components/code-gen-preview/index.d.ts +8 -0
- package/dist/components/code-gen-preview/index.js +9 -0
- package/dist/components/code-gen-result/index.css +18 -0
- package/dist/components/code-gen-result/index.d.ts +8 -0
- package/dist/components/code-gen-result/index.js +162 -0
- package/dist/components/codesandbox-preview/index.css +17 -0
- package/dist/components/codesandbox-preview/index.d.ts +3 -0
- package/dist/components/codesandbox-preview/index.js +147 -0
- package/dist/components/file-tree/index.css +55 -0
- package/dist/components/file-tree/index.d.ts +20 -0
- package/dist/components/file-tree/index.js +139 -0
- package/dist/components/file-type-icon/index.css +61 -0
- package/dist/components/file-type-icon/index.d.ts +6 -0
- package/dist/components/file-type-icon/index.js +10 -0
- package/dist/components/sources-view/index.css +11 -0
- package/dist/components/sources-view/index.d.ts +7 -0
- package/dist/components/sources-view/index.js +128 -0
- package/dist/index.d.ts +28 -0
- package/dist/index.js +50 -0
- package/dist/types/index.d.ts +17 -0
- package/dist/types/index.js +1 -0
- package/package.json +57 -0
package/README.md
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# Rslib Project
|
|
2
|
+
|
|
3
|
+
## Setup
|
|
4
|
+
|
|
5
|
+
Install the dependencies:
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pnpm install
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Get Started
|
|
12
|
+
|
|
13
|
+
Build the library:
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
pnpm build
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
Build the library in watch mode:
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
pnpm dev
|
|
23
|
+
```
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
.code-gen-plugin-loading {
|
|
2
|
+
width: 100%;
|
|
3
|
+
margin: 2em auto;
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
.code-gen-drawer .ant-drawer-body {
|
|
7
|
+
margin: 0;
|
|
8
|
+
padding: 0;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
.code-gen-plugin-result {
|
|
12
|
+
width: 100%;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
.code-gen-plugin-result .next-message-error .next-message-content {
|
|
16
|
+
white-space: pre-wrap;
|
|
17
|
+
}
|
|
18
|
+
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { IPublicTypeProjectSchema, IPublicModelPluginContext } from "@gant-lowcode/lowcode-types";
|
|
2
|
+
import "./index.less";
|
|
3
|
+
export declare function CodeGenActionBtn({ ctx }: {
|
|
4
|
+
ctx: IPublicModelPluginContext;
|
|
5
|
+
}): import("react/jsx-runtime").JSX.Element;
|
|
6
|
+
export declare function fixSchema(schema: IPublicTypeProjectSchema): Promise<IPublicTypeProjectSchema>;
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import * as __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__ from "react/jsx-runtime";
|
|
2
|
+
import * as __WEBPACK_EXTERNAL_MODULE_react__ from "react";
|
|
3
|
+
import * as __WEBPACK_EXTERNAL_MODULE__gant_lowcode_code_generator_standalone_loader__ from "@gant-lowcode/code-generator/standalone-loader";
|
|
4
|
+
import * as __WEBPACK_EXTERNAL_MODULE__gant_lowcode_lowcode_types__ from "@gant-lowcode/lowcode-types";
|
|
5
|
+
import * as __WEBPACK_EXTERNAL_MODULE_antd__ from "antd";
|
|
6
|
+
import * as __WEBPACK_EXTERNAL_MODULE_semver_functions_coerce__ from "semver/functions/coerce";
|
|
7
|
+
import * as __WEBPACK_EXTERNAL_MODULE__code_gen_result_index_js__ from "../code-gen-result/index.js";
|
|
8
|
+
import "./index.css";
|
|
9
|
+
import * as __WEBPACK_EXTERNAL_MODULE__ant_design_icons__ from "@ant-design/icons";
|
|
10
|
+
function CodeGenActionBtn({ ctx }) {
|
|
11
|
+
const [state, setState] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)({
|
|
12
|
+
open: false,
|
|
13
|
+
hasError: false,
|
|
14
|
+
error: null,
|
|
15
|
+
loading: false,
|
|
16
|
+
result: null,
|
|
17
|
+
schema: null,
|
|
18
|
+
originalSchema: null
|
|
19
|
+
});
|
|
20
|
+
const handleClick = async ()=>{
|
|
21
|
+
try {
|
|
22
|
+
// 打开基于 Gravity 的编辑器/出码预览器
|
|
23
|
+
setState((prev)=>({
|
|
24
|
+
...prev,
|
|
25
|
+
loading: true,
|
|
26
|
+
open: true,
|
|
27
|
+
hasError: false
|
|
28
|
+
}));
|
|
29
|
+
console.log('--ctx', ctx);
|
|
30
|
+
const getProjectSchema = ctx.config.get('getProjectSchema');
|
|
31
|
+
let originalSchema;
|
|
32
|
+
originalSchema = getProjectSchema ? await getProjectSchema() : await ctx.project.exportSchema(__WEBPACK_EXTERNAL_MODULE__gant_lowcode_lowcode_types__.IPublicEnumTransformStage.Save);
|
|
33
|
+
// 获取 schema,并修正
|
|
34
|
+
const schema = await fixSchema(originalSchema);
|
|
35
|
+
console.log("got schema: ", schema);
|
|
36
|
+
setState((prev)=>({
|
|
37
|
+
...prev,
|
|
38
|
+
schema,
|
|
39
|
+
originalSchema
|
|
40
|
+
}));
|
|
41
|
+
// 出码...
|
|
42
|
+
const result = await (0, __WEBPACK_EXTERNAL_MODULE__gant_lowcode_code_generator_standalone_loader__.generateCode)({
|
|
43
|
+
solution: "gantLegacy",
|
|
44
|
+
schema,
|
|
45
|
+
flattenResult: true,
|
|
46
|
+
builderOptions: {
|
|
47
|
+
// mode: 'fully',
|
|
48
|
+
rootPaths: [
|
|
49
|
+
'packages',
|
|
50
|
+
'icost'
|
|
51
|
+
],
|
|
52
|
+
pagePaths: [],
|
|
53
|
+
configPaths: []
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
console.log("generated: ", result);
|
|
57
|
+
setState((prev)=>({
|
|
58
|
+
...prev,
|
|
59
|
+
loading: false,
|
|
60
|
+
result
|
|
61
|
+
}));
|
|
62
|
+
} catch (e) {
|
|
63
|
+
console.error("failed to run code generator: ", e);
|
|
64
|
+
setState((prev)=>({
|
|
65
|
+
...prev,
|
|
66
|
+
hasError: true,
|
|
67
|
+
error: e instanceof Error ? e : new Error(`${e?.message || e}`)
|
|
68
|
+
}));
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
let drawerChildren = null;
|
|
72
|
+
if (state.hasError) drawerChildren = /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Alert, {
|
|
73
|
+
type: "error",
|
|
74
|
+
message: "出错了",
|
|
75
|
+
description: state.error?.message
|
|
76
|
+
});
|
|
77
|
+
if (state.loading) drawerChildren = /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Spin, {
|
|
78
|
+
className: "code-gen-plugin-loading",
|
|
79
|
+
spinning: true,
|
|
80
|
+
tip: "正在出码..."
|
|
81
|
+
});
|
|
82
|
+
drawerChildren = /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__code_gen_result_index_js__.CodeGenResult, {
|
|
83
|
+
result: state.result,
|
|
84
|
+
schema: state.schema,
|
|
85
|
+
originalSchema: state.originalSchema
|
|
86
|
+
});
|
|
87
|
+
return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)(__WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.Fragment, {
|
|
88
|
+
children: [
|
|
89
|
+
/*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Button, {
|
|
90
|
+
type: "text",
|
|
91
|
+
onClick: handleClick,
|
|
92
|
+
children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__ant_design_icons__.CodeOutlined, {})
|
|
93
|
+
}),
|
|
94
|
+
/*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Drawer, {
|
|
95
|
+
open: state.open,
|
|
96
|
+
title: "出码结果",
|
|
97
|
+
width: "95vw",
|
|
98
|
+
rootClassName: "code-gen-drawer",
|
|
99
|
+
onClose: ()=>{
|
|
100
|
+
setState((prev)=>({
|
|
101
|
+
...prev,
|
|
102
|
+
open: false
|
|
103
|
+
}));
|
|
104
|
+
},
|
|
105
|
+
children: drawerChildren
|
|
106
|
+
})
|
|
107
|
+
]
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
async function fixSchema(schema) {
|
|
111
|
+
return deepClone({
|
|
112
|
+
...schema,
|
|
113
|
+
componentsMap: schema.componentsMap.filter((c)=>c.package) // 去掉没有 package 的组件
|
|
114
|
+
.map((c)=>{
|
|
115
|
+
// 修正版本号(对于没有有效版本号的组件,默认使用 latest)
|
|
116
|
+
if (!isValidVersion(c.version)) {
|
|
117
|
+
console.warn('[WARN] got invalid version "%o" for component "%s", use "latest" as fallback', c.version, c);
|
|
118
|
+
return {
|
|
119
|
+
...c,
|
|
120
|
+
version: "latest"
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
return c;
|
|
124
|
+
})
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
function deepClone(x) {
|
|
128
|
+
try {
|
|
129
|
+
return JSON.parse(JSON.stringify(x));
|
|
130
|
+
} catch (e) {
|
|
131
|
+
throw new Error(`failed to clone schema -- ${e}`);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
function isValidVersion(version) {
|
|
135
|
+
if (!version) return false;
|
|
136
|
+
// 对于一些明显非法的版本号过滤下
|
|
137
|
+
if ("{{version}}" === version || "null" === version || "undefined" === version) return false;
|
|
138
|
+
// 对于 latest/beta/rc 这样的 tag 版本号要支持下
|
|
139
|
+
if (/^[a-z][a-z0-9]+([a-z0-9-]+)?$/i.test(version)) return true;
|
|
140
|
+
// 最后支持下所有 semver 能识别的版本
|
|
141
|
+
return null !== (0, __WEBPACK_EXTERNAL_MODULE_semver_functions_coerce__["default"])(version);
|
|
142
|
+
}
|
|
143
|
+
export { CodeGenActionBtn, fixSchema };
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
// 默认生成的代码不符合在线预览的规则,需要处理下
|
|
2
|
+
function fixPreviewCode(code, unused) {
|
|
3
|
+
if (!code) return null;
|
|
4
|
+
const fixedModules = Object.entries(code.modules || {}) // 去掉隐藏文件和 html 以及 md 等无用文件:
|
|
5
|
+
.filter(([fpath])=>!fpath.startsWith("/.") && !fpath.endsWith(".html") && !fpath.endsWith(".md")) // 所有的 jsx 文件改成 js 文件(因为 gravity 不支持 jsx)
|
|
6
|
+
.map(([fpath, module])=>[
|
|
7
|
+
fpath.replace(/\.jsx$/, ".js"),
|
|
8
|
+
module
|
|
9
|
+
]).map(([fpath, module])=>[
|
|
10
|
+
fpath.replace(/\.scss$/, ".css"),
|
|
11
|
+
module
|
|
12
|
+
]).reduce((acc, [fpath, module])=>({
|
|
13
|
+
...acc,
|
|
14
|
+
[fpath]: {
|
|
15
|
+
...module,
|
|
16
|
+
fpath,
|
|
17
|
+
code: module.code // 所有的 import xxx from '@/yyy' 转换为 import xxx from '/src/yyy'
|
|
18
|
+
.replace(/import\s+([^\s]+)\s+from\s+['"]@\/([^'"]+)['"]/g, (_, name, path)=>`import ${name} from '/src/${path}'`) // 所有的 import styles from 'xxx.scss' 转换为 import styles from '/src/xxx.css'
|
|
19
|
+
.replace(/import\s+([^\s]+)\s+from\s+['"]([^'"]+)\.scss['"]/g, (_, name, path)=>`import ${name} from '${path}.css'`)
|
|
20
|
+
}
|
|
21
|
+
}), {});
|
|
22
|
+
const fixedCode = {
|
|
23
|
+
...code,
|
|
24
|
+
modules: fixedModules
|
|
25
|
+
};
|
|
26
|
+
fixedCode.modules["/src/app.js"] = {
|
|
27
|
+
...fixedCode.modules["/src/app.js"],
|
|
28
|
+
code: `
|
|
29
|
+
// 注意:为了方便在浏览器中进行预览,这里简化了一些内容
|
|
30
|
+
import './shims';
|
|
31
|
+
import './global.css';
|
|
32
|
+
|
|
33
|
+
import React from 'react';
|
|
34
|
+
import ReactDOM from 'react-dom';
|
|
35
|
+
|
|
36
|
+
import Page from '${Object.keys(fixedModules).find((fpath)=>fpath.startsWith("/src/pages/"))}';
|
|
37
|
+
|
|
38
|
+
ReactDOM.render(<Page/>, document.getElementById('root'));
|
|
39
|
+
`
|
|
40
|
+
};
|
|
41
|
+
// 一些垫片
|
|
42
|
+
fixedCode.modules["/src/shims.js"] = {
|
|
43
|
+
fpath: "/src/shims.js",
|
|
44
|
+
code: ""
|
|
45
|
+
};
|
|
46
|
+
fixedCode.modules["/package.json"] = {
|
|
47
|
+
...fixedCode.modules["/package.json"],
|
|
48
|
+
code: JSON.stringify({
|
|
49
|
+
name: "demo",
|
|
50
|
+
version: "1.0.0",
|
|
51
|
+
dependencies: {
|
|
52
|
+
react: "^16.8.3",
|
|
53
|
+
"react-dom": "^16.8.3",
|
|
54
|
+
...JSON.parse(fixedCode.modules["/package.json"]?.code || "{}")?.dependencies
|
|
55
|
+
}
|
|
56
|
+
})
|
|
57
|
+
};
|
|
58
|
+
fixedCode.modules = pickKeys(fixedCode.modules, [
|
|
59
|
+
// '/tsconfig.json',
|
|
60
|
+
// '/jsconfig.json',
|
|
61
|
+
// '/build.json',
|
|
62
|
+
// '/abc.json',
|
|
63
|
+
"/package.json",
|
|
64
|
+
"/src/routes.js",
|
|
65
|
+
"/src/app.js",
|
|
66
|
+
"/src/constants.js",
|
|
67
|
+
"/src/utils.js",
|
|
68
|
+
"/src/i18n.js",
|
|
69
|
+
"/src/global.css",
|
|
70
|
+
"/src/index.js",
|
|
71
|
+
"/src/shims.js",
|
|
72
|
+
// layouts 暂时先不加载吧
|
|
73
|
+
// '/src/layouts/BasicLayout/index.js',
|
|
74
|
+
// '/src/layouts/BasicLayout/menuConfig.js',
|
|
75
|
+
// '/src/layouts/BasicLayout/components/Footer/index.js',
|
|
76
|
+
// '/src/layouts/BasicLayout/components/Footer/index.module.css',
|
|
77
|
+
// '/src/layouts/BasicLayout/components/Logo/index.js',
|
|
78
|
+
// '/src/layouts/BasicLayout/components/Logo/index.module.css',
|
|
79
|
+
// '/src/layouts/BasicLayout/components/PageNav/index.js',
|
|
80
|
+
...Object.keys(fixedModules).filter((fpath)=>fpath.startsWith("/src/pages/"))
|
|
81
|
+
]);
|
|
82
|
+
fixedCode.modules["/src/routes.js"] = {
|
|
83
|
+
...fixedCode.modules["/src/routes.js"],
|
|
84
|
+
code: `
|
|
85
|
+
`
|
|
86
|
+
};
|
|
87
|
+
fixedCode.modules["/src/global.css"] = {
|
|
88
|
+
fpath: "/src/global.css",
|
|
89
|
+
code: `
|
|
90
|
+
body {
|
|
91
|
+
-webkit-font-smoothing: antialiased;
|
|
92
|
+
}
|
|
93
|
+
`
|
|
94
|
+
};
|
|
95
|
+
Object.assign(fixedCode.modules, {
|
|
96
|
+
"/src/index.html": {
|
|
97
|
+
code: '<div id="root"></div>',
|
|
98
|
+
fpath: "/src/index.html"
|
|
99
|
+
},
|
|
100
|
+
"/src/index.less": {
|
|
101
|
+
fpath: "/src/index.less",
|
|
102
|
+
code: `
|
|
103
|
+
|
|
104
|
+
`
|
|
105
|
+
}
|
|
106
|
+
});
|
|
107
|
+
fixedCode.type = "riddle";
|
|
108
|
+
return fixedCode;
|
|
109
|
+
}
|
|
110
|
+
function pickKeys(data, keys) {
|
|
111
|
+
return keys.reduce((acc, key)=>{
|
|
112
|
+
if (data[key]) return {
|
|
113
|
+
...acc,
|
|
114
|
+
[key]: data[key]
|
|
115
|
+
};
|
|
116
|
+
return acc;
|
|
117
|
+
}, {});
|
|
118
|
+
}
|
|
119
|
+
export { fixPreviewCode };
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
.code-gen-preview-tabs {
|
|
2
|
+
flex-direction: column;
|
|
3
|
+
height: 100%;
|
|
4
|
+
display: flex;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
.code-gen-preview-tabs .next-tabs-bar {
|
|
8
|
+
flex-shrink: 0;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
.code-gen-preview-tabs .next-tabs-content {
|
|
12
|
+
flex: 1;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
.code-gen-preview-tabs .next-tabs-content .next-tabs-tabpane {
|
|
16
|
+
height: 100%;
|
|
17
|
+
}
|
|
18
|
+
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { GravityCode } from "../../types";
|
|
2
|
+
import "./index.less";
|
|
3
|
+
export type CodeGenPreviewProps = {
|
|
4
|
+
code: GravityCode | null;
|
|
5
|
+
height: string | number;
|
|
6
|
+
refresh: string | number;
|
|
7
|
+
};
|
|
8
|
+
export declare function CodeGenPreview(props: CodeGenPreviewProps): import("react/jsx-runtime").JSX.Element;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import * as __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__ from "react/jsx-runtime";
|
|
2
|
+
import * as __WEBPACK_EXTERNAL_MODULE__codesandbox_preview_index_js__ from "../codesandbox-preview/index.js";
|
|
3
|
+
import "./index.css";
|
|
4
|
+
function CodeGenPreview(props) {
|
|
5
|
+
return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__codesandbox_preview_index_js__.CodeSandboxPreview, {
|
|
6
|
+
...props
|
|
7
|
+
});
|
|
8
|
+
}
|
|
9
|
+
export { CodeGenPreview };
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
.code-gen-tabs {
|
|
2
|
+
height: 100%;
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
.code-gen-tabs .ant-tabs-nav {
|
|
6
|
+
margin: 0;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
.code-gen-tabs .ant-tabs-content, .code-gen-tabs .ant-tabs-tabpane, .code-gen-source-view {
|
|
10
|
+
height: 100%;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
.source-view-header {
|
|
14
|
+
position: absolute;
|
|
15
|
+
top: 18px;
|
|
16
|
+
right: 20px;
|
|
17
|
+
}
|
|
18
|
+
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { IPublicTypeProjectSchema } from "@gant-lowcode/lowcode-types";
|
|
2
|
+
import { Result } from "@gant-lowcode/code-generator/standalone-loader";
|
|
3
|
+
import "./index.less";
|
|
4
|
+
export declare function CodeGenResult({ result, schema, }: {
|
|
5
|
+
result: Result | null | undefined;
|
|
6
|
+
schema: IPublicTypeProjectSchema | null;
|
|
7
|
+
originalSchema: IPublicTypeProjectSchema | null;
|
|
8
|
+
}): import("react/jsx-runtime").JSX.Element | null;
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import * as __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__ from "react/jsx-runtime";
|
|
2
|
+
import * as __WEBPACK_EXTERNAL_MODULE_react__ from "react";
|
|
3
|
+
import * as __WEBPACK_EXTERNAL_MODULE_antd__ from "antd";
|
|
4
|
+
import * as __WEBPACK_EXTERNAL_MODULE_file_saver__ from "file-saver";
|
|
5
|
+
import * as __WEBPACK_EXTERNAL_MODULE_jszip__ from "jszip";
|
|
6
|
+
import * as __WEBPACK_EXTERNAL_MODULE__sources_view_index_js__ from "../sources-view/index.js";
|
|
7
|
+
import "./index.css";
|
|
8
|
+
function CodeGenResult({ result, schema }) {
|
|
9
|
+
const [paneState, setPaneState] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)({
|
|
10
|
+
expandedKeys: [
|
|
11
|
+
"sources",
|
|
12
|
+
"preview"
|
|
13
|
+
]
|
|
14
|
+
});
|
|
15
|
+
const [gravityCode, setGravityCode] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)(null);
|
|
16
|
+
const [refresh, setRefresh] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)(0);
|
|
17
|
+
(0, __WEBPACK_EXTERNAL_MODULE_react__.useEffect)(()=>{
|
|
18
|
+
console.log("--result, schema", result, schema);
|
|
19
|
+
setGravityCode(convertCodeGenResult(result, schema));
|
|
20
|
+
}, [
|
|
21
|
+
result
|
|
22
|
+
]);
|
|
23
|
+
if (!result) return null;
|
|
24
|
+
const handleDownloadSources = async (e)=>{
|
|
25
|
+
try {
|
|
26
|
+
e.preventDefault();
|
|
27
|
+
e.stopPropagation();
|
|
28
|
+
const zip = new __WEBPACK_EXTERNAL_MODULE_jszip__["default"]();
|
|
29
|
+
Object.values(gravityCode?.modules || {}).forEach((file)=>{
|
|
30
|
+
zip.file(file.fpath.replace(/^\/+/, ""), file.code);
|
|
31
|
+
});
|
|
32
|
+
await zip.generateAsync({
|
|
33
|
+
type: "blob"
|
|
34
|
+
}).then((content)=>{
|
|
35
|
+
__WEBPACK_EXTERNAL_MODULE_file_saver__["default"].saveAs(content, "gant-lowcode-generated-sources.zip");
|
|
36
|
+
});
|
|
37
|
+
} catch (e) {
|
|
38
|
+
console.log("failed to download sources: ", e);
|
|
39
|
+
__WEBPACK_EXTERNAL_MODULE_antd__.message.error("下载失败!");
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
console.log("--gravityCode", gravityCode);
|
|
43
|
+
// const items: TabsProps["items"] = [
|
|
44
|
+
// {
|
|
45
|
+
// key: "sources",
|
|
46
|
+
// label: (
|
|
47
|
+
// <span>
|
|
48
|
+
// 出码生成的源代码{" "}
|
|
49
|
+
// <a href="javascript:void(0)" onClick={handleDownloadSources}>
|
|
50
|
+
// 导出/下载 zip 包
|
|
51
|
+
// </a>
|
|
52
|
+
// </span>
|
|
53
|
+
// ),
|
|
54
|
+
// children: gravityCode != null && (
|
|
55
|
+
// <SourcesView code={gravityCode} onCodeChange={setGravityCode} />
|
|
56
|
+
// ),
|
|
57
|
+
// },
|
|
58
|
+
// {
|
|
59
|
+
// key: "preview",
|
|
60
|
+
// label: (
|
|
61
|
+
// <span>
|
|
62
|
+
// 在线预览{" "}
|
|
63
|
+
// <a
|
|
64
|
+
// href="#refresh"
|
|
65
|
+
// onClick={(e) => {
|
|
66
|
+
// e.preventDefault();
|
|
67
|
+
// e.stopPropagation();
|
|
68
|
+
// setRefresh(Date.now());
|
|
69
|
+
// }}
|
|
70
|
+
// >
|
|
71
|
+
// 刷新
|
|
72
|
+
// </a>
|
|
73
|
+
// </span>
|
|
74
|
+
// ),
|
|
75
|
+
// children: (
|
|
76
|
+
// <div
|
|
77
|
+
// className="code-gen-result-gravity-demo"
|
|
78
|
+
// style={{ height: "100%" }}
|
|
79
|
+
// >
|
|
80
|
+
// <CodeGenPreview
|
|
81
|
+
// code={gravityCode}
|
|
82
|
+
// height={"100%"}
|
|
83
|
+
// refresh={refresh}
|
|
84
|
+
// />
|
|
85
|
+
// </div>
|
|
86
|
+
// ),
|
|
87
|
+
// },
|
|
88
|
+
// ];
|
|
89
|
+
// return <Tabs className="code-gen-tabs" items={items} />;
|
|
90
|
+
if (!gravityCode) return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Empty, {});
|
|
91
|
+
return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)("div", {
|
|
92
|
+
className: "code-gen-source-view",
|
|
93
|
+
children: [
|
|
94
|
+
/*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("div", {
|
|
95
|
+
className: "source-view-header",
|
|
96
|
+
children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("a", {
|
|
97
|
+
href: "javascript:void(0)",
|
|
98
|
+
onClick: handleDownloadSources,
|
|
99
|
+
children: "源代码导出/下载 zip 包"
|
|
100
|
+
})
|
|
101
|
+
}),
|
|
102
|
+
/*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__sources_view_index_js__.SourcesView, {
|
|
103
|
+
code: gravityCode,
|
|
104
|
+
onCodeChange: setGravityCode
|
|
105
|
+
})
|
|
106
|
+
]
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
function convertCodeGenResult(result, schema) {
|
|
110
|
+
const schemaFiles = {
|
|
111
|
+
"/.project-schema.json": {
|
|
112
|
+
fpath: "/.project-schema.json",
|
|
113
|
+
code: `${JSON.stringify(schema, null, 2)}\n`
|
|
114
|
+
}
|
|
115
|
+
};
|
|
116
|
+
if (!result || !Array.isArray(result) || !result.length) return {
|
|
117
|
+
type: "demo",
|
|
118
|
+
modules: schemaFiles
|
|
119
|
+
};
|
|
120
|
+
const code = {
|
|
121
|
+
type: "demo",
|
|
122
|
+
modules: result.reduce((acc, file)=>({
|
|
123
|
+
...acc,
|
|
124
|
+
[`/${file.pathName}`]: {
|
|
125
|
+
fpath: `/${file.pathName}`,
|
|
126
|
+
code: file.content,
|
|
127
|
+
entry: void 0,
|
|
128
|
+
packagejson: [
|
|
129
|
+
"package.json"
|
|
130
|
+
].includes(file.pathName) ? 1 : void 0
|
|
131
|
+
}
|
|
132
|
+
}), {})
|
|
133
|
+
};
|
|
134
|
+
let foundEntry = false;
|
|
135
|
+
// 设置入口文件
|
|
136
|
+
[
|
|
137
|
+
"index.js",
|
|
138
|
+
"index.ts",
|
|
139
|
+
"index.tsx",
|
|
140
|
+
"app.js",
|
|
141
|
+
"app.ts",
|
|
142
|
+
"app.tsx"
|
|
143
|
+
].forEach((fileName)=>{
|
|
144
|
+
if (!foundEntry) {
|
|
145
|
+
const filePath = `/src/${fileName}`;
|
|
146
|
+
if (code.modules[filePath]) {
|
|
147
|
+
foundEntry = true;
|
|
148
|
+
if ("index.js" === fileName) code.modules[filePath].entry = 1;
|
|
149
|
+
else code.modules["/src/index.js"] = {
|
|
150
|
+
fpath: "/src/index.js",
|
|
151
|
+
entry: 1,
|
|
152
|
+
code: `import "./${fileName.replace(/\.\w+$/, "")}"`
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
});
|
|
157
|
+
if (!foundEntry) console.warn("Failed to find entry file for demo.");
|
|
158
|
+
// 补充 schema 文件
|
|
159
|
+
// Object.assign(code.modules, schemaFiles);
|
|
160
|
+
return code;
|
|
161
|
+
}
|
|
162
|
+
export { CodeGenResult };
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
.code-gen-plugin-code-sandbox-preview {
|
|
2
|
+
flex-direction: row;
|
|
3
|
+
place-content: center;
|
|
4
|
+
place-items: center;
|
|
5
|
+
height: 100%;
|
|
6
|
+
display: flex;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
.code-gen-plugin-code-sandbox-preview iframe {
|
|
10
|
+
border: 0;
|
|
11
|
+
border-radius: 0;
|
|
12
|
+
width: 100%;
|
|
13
|
+
height: 100%;
|
|
14
|
+
min-height: 300px;
|
|
15
|
+
overflow: hidden;
|
|
16
|
+
}
|
|
17
|
+
|