@gant-lowcode/plugin-code-generator 1.0.0 → 1.0.2

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.
@@ -7,6 +7,7 @@ import * as __WEBPACK_EXTERNAL_MODULE_semver_functions_coerce_js__ from "semver/
7
7
  import * as __WEBPACK_EXTERNAL_MODULE__code_gen_result_index_js__ from "../code-gen-result/index.js";
8
8
  import "./index.css";
9
9
  import * as __WEBPACK_EXTERNAL_MODULE__ant_design_icons__ from "@ant-design/icons";
10
+ import * as __WEBPACK_EXTERNAL_MODULE_file_saver__ from "file-saver";
10
11
  function CodeGenActionBtn({ ctx }) {
11
12
  const [state, setState] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)({
12
13
  open: false,
@@ -17,6 +18,7 @@ function CodeGenActionBtn({ ctx }) {
17
18
  schema: null,
18
19
  originalSchema: null
19
20
  });
21
+ const codeGenResultRef = (0, __WEBPACK_EXTERNAL_MODULE_react__.useRef)(null);
20
22
  const handleClick = async ()=>{
21
23
  try {
22
24
  // 打开基于 Gravity 的编辑器/出码预览器
@@ -26,8 +28,8 @@ function CodeGenActionBtn({ ctx }) {
26
28
  open: true,
27
29
  hasError: false
28
30
  }));
29
- console.log('--ctx', ctx);
30
- const getProjectSchema = ctx.config.get('getProjectSchema');
31
+ console.log("--ctx", ctx);
32
+ const getProjectSchema = ctx.config.get("getProjectSchema");
31
33
  let originalSchema;
32
34
  originalSchema = getProjectSchema ? await getProjectSchema() : await ctx.project.exportSchema(__WEBPACK_EXTERNAL_MODULE__gant_lowcode_lowcode_types__.IPublicEnumTransformStage.Save);
33
35
  // 获取 schema,并修正
@@ -46,8 +48,8 @@ function CodeGenActionBtn({ ctx }) {
46
48
  builderOptions: {
47
49
  // mode: 'fully',
48
50
  rootPaths: [
49
- 'packages',
50
- 'icost'
51
+ "packages",
52
+ "icost"
51
53
  ],
52
54
  pagePaths: [],
53
55
  configPaths: []
@@ -68,6 +70,33 @@ function CodeGenActionBtn({ ctx }) {
68
70
  }));
69
71
  }
70
72
  };
73
+ const handleDownload = async (e)=>{
74
+ try {
75
+ e.preventDefault();
76
+ e.stopPropagation();
77
+ const codeGen = codeGenResultRef.current;
78
+ if (!codeGen) return;
79
+ const content = await codeGen.codeZip();
80
+ __WEBPACK_EXTERNAL_MODULE_file_saver__["default"].saveAs(content, "gant-lowcode-generated-sources.zip");
81
+ } catch (e) {
82
+ console.log("failed to download sources: ", e);
83
+ __WEBPACK_EXTERNAL_MODULE_antd__.message.error("下载失败!");
84
+ }
85
+ };
86
+ const handleUpload = async (e)=>{
87
+ const codeGen = codeGenResultRef.current;
88
+ if (!codeGen) return;
89
+ try {
90
+ const blob = await codeGen.codeZip();
91
+ const setCodeUpload = ctx.config.get("setCodeUpload");
92
+ if (setCodeUpload) await setCodeUpload({
93
+ zipFile: blob
94
+ });
95
+ } catch (error) {
96
+ console.log("failed to git push: ", e);
97
+ __WEBPACK_EXTERNAL_MODULE_antd__.message.error("下载失败!");
98
+ }
99
+ };
71
100
  let drawerChildren = null;
72
101
  if (state.hasError) drawerChildren = /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Alert, {
73
102
  type: "error",
@@ -80,6 +109,7 @@ function CodeGenActionBtn({ ctx }) {
80
109
  tip: "正在出码..."
81
110
  });
82
111
  drawerChildren = /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__code_gen_result_index_js__.CodeGenResult, {
112
+ ref: codeGenResultRef,
83
113
  result: state.result,
84
114
  schema: state.schema,
85
115
  originalSchema: state.originalSchema
@@ -96,6 +126,20 @@ function CodeGenActionBtn({ ctx }) {
96
126
  title: "出码结果",
97
127
  width: "95vw",
98
128
  rootClassName: "code-gen-drawer",
129
+ extra: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)(__WEBPACK_EXTERNAL_MODULE_antd__.Space, {
130
+ children: [
131
+ /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Button, {
132
+ type: "link",
133
+ onClick: handleUpload,
134
+ children: "上传git"
135
+ }),
136
+ /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Button, {
137
+ type: "link",
138
+ onClick: handleDownload,
139
+ children: "源代码导出/下载 zip 包"
140
+ })
141
+ ]
142
+ }),
99
143
  onClose: ()=>{
100
144
  setState((prev)=>({
101
145
  ...prev,
@@ -1,8 +1,14 @@
1
+ import React from "react";
1
2
  import { IPublicTypeProjectSchema } from "@gant-lowcode/lowcode-types";
2
3
  import { Result } from "@gant-lowcode/code-generator/standalone-loader";
3
4
  import "./index.less";
4
- export declare function CodeGenResult({ result, schema, }: {
5
+ interface CodeGenResultProps {
5
6
  result: Result | null | undefined;
6
7
  schema: IPublicTypeProjectSchema | null;
7
8
  originalSchema: IPublicTypeProjectSchema | null;
8
- }): import("react/jsx-runtime").JSX.Element | null;
9
+ }
10
+ export interface CodeGenResultRef {
11
+ codeZip: () => Promise<Blob>;
12
+ }
13
+ export declare const CodeGenResult: React.ForwardRefExoticComponent<CodeGenResultProps & React.RefAttributes<CodeGenResultRef>>;
14
+ export {};
@@ -1,11 +1,12 @@
1
1
  import * as __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__ from "react/jsx-runtime";
2
2
  import * as __WEBPACK_EXTERNAL_MODULE_react__ from "react";
3
3
  import * as __WEBPACK_EXTERNAL_MODULE_antd__ from "antd";
4
- import * as __WEBPACK_EXTERNAL_MODULE_file_saver__ from "file-saver";
4
+ import "file-saver";
5
5
  import * as __WEBPACK_EXTERNAL_MODULE_jszip__ from "jszip";
6
6
  import * as __WEBPACK_EXTERNAL_MODULE__sources_view_index_js__ from "../sources-view/index.js";
7
7
  import "./index.css";
8
- function CodeGenResult({ result, schema }) {
8
+ // import { CodeGenPreview } from "../code-gen-preview/index";
9
+ const code_gen_result_rslib_entry_CodeGenResult = /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react__.forwardRef)(function({ result, schema }, ref) {
9
10
  const [paneState, setPaneState] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)({
10
11
  expandedKeys: [
11
12
  "sources",
@@ -20,25 +21,19 @@ function CodeGenResult({ result, schema }) {
20
21
  }, [
21
22
  result
22
23
  ]);
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
- }
24
+ const codeZip = async ()=>{
25
+ const zip = new __WEBPACK_EXTERNAL_MODULE_jszip__["default"]();
26
+ Object.values(gravityCode?.modules || {}).forEach((file)=>{
27
+ zip.file(file.fpath.replace(/^\/+/, ""), file.code);
28
+ });
29
+ return await zip.generateAsync({
30
+ type: "blob"
31
+ });
41
32
  };
33
+ (0, __WEBPACK_EXTERNAL_MODULE_react__.useImperativeHandle)(ref, ()=>({
34
+ codeZip
35
+ }));
36
+ if (!result) return null;
42
37
  console.log("--gravityCode", gravityCode);
43
38
  // const items: TabsProps["items"] = [
44
39
  // {
@@ -88,24 +83,14 @@ function CodeGenResult({ result, schema }) {
88
83
  // ];
89
84
  // return <Tabs className="code-gen-tabs" items={items} />;
90
85
  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", {
86
+ return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("div", {
92
87
  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
- ]
88
+ children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__sources_view_index_js__.SourcesView, {
89
+ code: gravityCode,
90
+ onCodeChange: setGravityCode
91
+ })
107
92
  });
108
- }
93
+ });
109
94
  function convertCodeGenResult(result, schema) {
110
95
  const schemaFiles = {
111
96
  "/.project-schema.json": {
@@ -159,4 +144,4 @@ function convertCodeGenResult(result, schema) {
159
144
  // Object.assign(code.modules, schemaFiles);
160
145
  return code;
161
146
  }
162
- export { CodeGenResult };
147
+ export { code_gen_result_rslib_entry_CodeGenResult as CodeGenResult };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gant-lowcode/plugin-code-generator",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": {
@@ -44,7 +44,7 @@
44
44
  "react-window": "^1.8.10",
45
45
  "semver": "^7.6.3",
46
46
  "string-natural-compare": "^3.0.1",
47
- "@gant-lowcode/code-generator": "0.0.1"
47
+ "@gant-lowcode/code-generator": "0.0.2"
48
48
  },
49
49
  "publishConfig": {
50
50
  "registry": "https://registry.npmjs.org/",