@dckj-npm/lowcode-plugin-code-generator 1.3.26 → 1.3.28
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/es/components/CodeGenActionBtn/CodeGenActionBtn.js +216 -41
- package/es/components/CodeGenActionBtn/CodeGenActionBtn.scss +6 -0
- package/es/demo/index.js +22 -1
- package/es/index.js +1 -1
- package/es/utils/utils.js +22 -5
- package/lib/components/CodeGenActionBtn/CodeGenActionBtn.js +216 -41
- package/lib/components/CodeGenActionBtn/CodeGenActionBtn.scss +6 -0
- package/lib/demo/index.js +22 -1
- package/lib/index.js +1 -1
- package/lib/utils/utils.js +22 -5
- package/package.json +2 -2
|
@@ -6,8 +6,10 @@ import React, { useState } from 'react';
|
|
|
6
6
|
import * as CodeGenerator from '@dckj-npm/lowcode-code-generator/standalone-loader';
|
|
7
7
|
import { Button, Drawer, Loading, Message } from '@alifd/next';
|
|
8
8
|
import coerce from 'semver/functions/coerce';
|
|
9
|
+
import { project } from '@alilc/lowcode-engine';
|
|
10
|
+
import { saveAs } from 'file-saver';
|
|
9
11
|
import { CodeGenResult } from '../CodeGenResult';
|
|
10
|
-
import { getAllSchemaByResourceList, getProjectId, getResourceListFromLocalStorage } from '../../utils/utils';
|
|
12
|
+
import { getAllSchemaByResourceList, getProjectId, getResourceListFromLocalStorage, getLSName } from '../../utils/utils';
|
|
11
13
|
export function CodeGenActionBtn(_ref) {
|
|
12
14
|
var ctx = _ref.ctx;
|
|
13
15
|
var _useState = useState({
|
|
@@ -15,6 +17,7 @@ export function CodeGenActionBtn(_ref) {
|
|
|
15
17
|
hasError: false,
|
|
16
18
|
error: null,
|
|
17
19
|
loading: false,
|
|
20
|
+
saving: false,
|
|
18
21
|
result: null,
|
|
19
22
|
schema: null,
|
|
20
23
|
originalSchema: null
|
|
@@ -22,13 +25,112 @@ export function CodeGenActionBtn(_ref) {
|
|
|
22
25
|
state = _useState[0],
|
|
23
26
|
setState = _useState[1];
|
|
24
27
|
console.log('CodeGenActionBtn render, ctx: ', ctx);
|
|
25
|
-
var
|
|
28
|
+
var handleSave = /*#__PURE__*/function () {
|
|
26
29
|
var _ref2 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
|
|
27
|
-
var
|
|
30
|
+
var _resourceList$, _resourceList$2, _componentsTree, scenarioName, resourceList, exported, desiredFileName, withFileName, sanitized, matchedResource, pageSchema, error, _t;
|
|
28
31
|
return _regeneratorRuntime.wrap(function (_context) {
|
|
29
32
|
while (1) switch (_context.prev = _context.next) {
|
|
30
33
|
case 0:
|
|
31
34
|
_context.prev = 0;
|
|
35
|
+
setState(function (prev) {
|
|
36
|
+
return _extends({}, prev, {
|
|
37
|
+
saving: true
|
|
38
|
+
});
|
|
39
|
+
});
|
|
40
|
+
scenarioName = getProjectId();
|
|
41
|
+
if (scenarioName) {
|
|
42
|
+
_context.next = 1;
|
|
43
|
+
break;
|
|
44
|
+
}
|
|
45
|
+
throw new Error('无法获取 projectId,请确认已在低代码编辑器内打开该插件。');
|
|
46
|
+
case 1:
|
|
47
|
+
_context.next = 2;
|
|
48
|
+
return getResourceListFromLocalStorage(scenarioName);
|
|
49
|
+
case 2:
|
|
50
|
+
resourceList = _context.sent;
|
|
51
|
+
_context.next = 3;
|
|
52
|
+
return project.exportSchema();
|
|
53
|
+
case 3:
|
|
54
|
+
exported = _context.sent;
|
|
55
|
+
if (!(!exported || typeof exported !== 'object')) {
|
|
56
|
+
_context.next = 4;
|
|
57
|
+
break;
|
|
58
|
+
}
|
|
59
|
+
throw new Error('导出 schema 失败,请稍后重试。');
|
|
60
|
+
case 4:
|
|
61
|
+
if (isNonEmptyProjectSchema(exported)) {
|
|
62
|
+
_context.next = 5;
|
|
63
|
+
break;
|
|
64
|
+
}
|
|
65
|
+
throw new Error('当前页面 schema 为空,请确认画布中已有内容后再保存。');
|
|
66
|
+
case 5:
|
|
67
|
+
desiredFileName = getPrimaryTreeFileName(exported) || (resourceList === null || resourceList === void 0 ? void 0 : (_resourceList$ = resourceList[0]) === null || _resourceList$ === void 0 ? void 0 : _resourceList$.slug) || (resourceList === null || resourceList === void 0 ? void 0 : (_resourceList$2 = resourceList[0]) === null || _resourceList$2 === void 0 ? void 0 : _resourceList$2.id) || 'index';
|
|
68
|
+
withFileName = ensurePrimaryTreeFileName(exported, desiredFileName);
|
|
69
|
+
_context.next = 6;
|
|
70
|
+
return fixSchema(withFileName);
|
|
71
|
+
case 6:
|
|
72
|
+
sanitized = _context.sent;
|
|
73
|
+
matchedResource = resourceList.find(function (r) {
|
|
74
|
+
return (r === null || r === void 0 ? void 0 : r.slug) === desiredFileName || (r === null || r === void 0 ? void 0 : r.id) === desiredFileName;
|
|
75
|
+
}) || resourceList.find(function (r) {
|
|
76
|
+
return (r === null || r === void 0 ? void 0 : r.firstPage) === 'true';
|
|
77
|
+
}) || resourceList[0];
|
|
78
|
+
if (matchedResource !== null && matchedResource !== void 0 && matchedResource.id) {
|
|
79
|
+
_context.next = 7;
|
|
80
|
+
break;
|
|
81
|
+
}
|
|
82
|
+
throw new Error('无法定位当前页面资源信息,请先保存页面后再尝试。');
|
|
83
|
+
case 7:
|
|
84
|
+
// 1) 写入 localStorage:让“出码”能拿到最新 schema(不依赖业务侧保存)
|
|
85
|
+
window.localStorage.setItem(getLSName(scenarioName, matchedResource.id), JSON.stringify(sanitized));
|
|
86
|
+
// 2) 兜底写入 resourceList(某些环境可能没有保存动作)
|
|
87
|
+
window.localStorage.setItem(scenarioName + ":resourceList", JSON.stringify(resourceList));
|
|
88
|
+
|
|
89
|
+
// 3) 下载当前页面 schema.json(与 demo 的 public/schema.json 格式一致:单页 schema 节点)
|
|
90
|
+
pageSchema = sanitized === null || sanitized === void 0 ? void 0 : (_componentsTree = sanitized.componentsTree) === null || _componentsTree === void 0 ? void 0 : _componentsTree[0];
|
|
91
|
+
if (!(!pageSchema || typeof pageSchema !== 'object')) {
|
|
92
|
+
_context.next = 8;
|
|
93
|
+
break;
|
|
94
|
+
}
|
|
95
|
+
throw new Error('当前页面 schema 结构异常,无法下载。');
|
|
96
|
+
case 8:
|
|
97
|
+
saveAs(new Blob([JSON.stringify(pageSchema, null, 2)], {
|
|
98
|
+
type: 'application/json;charset=utf-8'
|
|
99
|
+
}), 'schema.json');
|
|
100
|
+
Message.success('已保存当前页面:已写入缓存并下载 schema.json');
|
|
101
|
+
_context.next = 10;
|
|
102
|
+
break;
|
|
103
|
+
case 9:
|
|
104
|
+
_context.prev = 9;
|
|
105
|
+
_t = _context["catch"](0);
|
|
106
|
+
console.error('failed to save schema: ', _t);
|
|
107
|
+
error = _t instanceof Error ? _t : new Error("" + ((_t === null || _t === void 0 ? void 0 : _t.message) || _t));
|
|
108
|
+
Message.error(error.message);
|
|
109
|
+
case 10:
|
|
110
|
+
_context.prev = 10;
|
|
111
|
+
setState(function (prev) {
|
|
112
|
+
return _extends({}, prev, {
|
|
113
|
+
saving: false
|
|
114
|
+
});
|
|
115
|
+
});
|
|
116
|
+
return _context.finish(10);
|
|
117
|
+
case 11:
|
|
118
|
+
case "end":
|
|
119
|
+
return _context.stop();
|
|
120
|
+
}
|
|
121
|
+
}, _callee, null, [[0, 9, 10, 11]]);
|
|
122
|
+
}));
|
|
123
|
+
return function handleSave() {
|
|
124
|
+
return _ref2.apply(this, arguments);
|
|
125
|
+
};
|
|
126
|
+
}();
|
|
127
|
+
var handleClick = /*#__PURE__*/function () {
|
|
128
|
+
var _ref3 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee2() {
|
|
129
|
+
var _sanitizedSchemas$, name, resourceList, allSchema, hasEmptySchema, exported, invalidIndex, resource, emptyIndex, _resource, sanitizedSchemas, missingFileNameIndex, _resource2, primarySchema, originalPrimarySchema, result, error, _t2, _t3;
|
|
130
|
+
return _regeneratorRuntime.wrap(function (_context2) {
|
|
131
|
+
while (1) switch (_context2.prev = _context2.next) {
|
|
132
|
+
case 0:
|
|
133
|
+
_context2.prev = 0;
|
|
32
134
|
setState(function (prev) {
|
|
33
135
|
return _extends({}, prev, {
|
|
34
136
|
loading: true,
|
|
@@ -39,64 +141,112 @@ export function CodeGenActionBtn(_ref) {
|
|
|
39
141
|
});
|
|
40
142
|
name = getProjectId();
|
|
41
143
|
if (name) {
|
|
42
|
-
|
|
144
|
+
_context2.next = 1;
|
|
43
145
|
break;
|
|
44
146
|
}
|
|
45
147
|
throw new Error('无法获取 projectId,请确认已在低代码编辑器内打开该插件。');
|
|
46
148
|
case 1:
|
|
47
|
-
|
|
149
|
+
_context2.next = 2;
|
|
48
150
|
return getResourceListFromLocalStorage(name);
|
|
49
151
|
case 2:
|
|
50
|
-
resourceList =
|
|
152
|
+
resourceList = _context2.sent;
|
|
51
153
|
if (resourceList.length) {
|
|
52
|
-
|
|
154
|
+
_context2.next = 3;
|
|
53
155
|
break;
|
|
54
156
|
}
|
|
55
157
|
throw new Error('未找到页面配置,请先保存页面后再尝试出码。');
|
|
56
158
|
case 3:
|
|
57
|
-
|
|
159
|
+
_context2.next = 4;
|
|
58
160
|
return getAllSchemaByResourceList(name, resourceList);
|
|
59
161
|
case 4:
|
|
60
|
-
allSchema =
|
|
162
|
+
allSchema = _context2.sent;
|
|
61
163
|
if (allSchema.length) {
|
|
62
|
-
|
|
164
|
+
_context2.next = 5;
|
|
63
165
|
break;
|
|
64
166
|
}
|
|
65
167
|
throw new Error('未找到页面 schema,请先保存页面后再尝试出码。');
|
|
66
168
|
case 5:
|
|
169
|
+
// 如果 localStorage 中缺少 schema(常见于 demo 或未保存页面),尝试从设计器当前工程导出 schema 做兜底。
|
|
170
|
+
// 注意:多页面场景仍建议依赖保存后的 resource schema。
|
|
171
|
+
hasEmptySchema = allSchema.some(function (s) {
|
|
172
|
+
return !isNonEmptyProjectSchema(s);
|
|
173
|
+
});
|
|
174
|
+
if (!hasEmptySchema) {
|
|
175
|
+
_context2.next = 9;
|
|
176
|
+
break;
|
|
177
|
+
}
|
|
178
|
+
_context2.prev = 6;
|
|
179
|
+
_context2.next = 7;
|
|
180
|
+
return project.exportSchema();
|
|
181
|
+
case 7:
|
|
182
|
+
exported = _context2.sent;
|
|
183
|
+
if (exported && isNonEmptyProjectSchema(exported)) {
|
|
184
|
+
// 仅对第一张页面做兜底(与 DEFAULT_RESOURCE_LIST 对齐)
|
|
185
|
+
allSchema[0] = exported;
|
|
186
|
+
}
|
|
187
|
+
_context2.next = 9;
|
|
188
|
+
break;
|
|
189
|
+
case 8:
|
|
190
|
+
_context2.prev = 8;
|
|
191
|
+
_t2 = _context2["catch"](6);
|
|
192
|
+
case 9:
|
|
67
193
|
if (!(allSchema.length !== resourceList.length)) {
|
|
68
|
-
|
|
194
|
+
_context2.next = 10;
|
|
69
195
|
break;
|
|
70
196
|
}
|
|
71
197
|
throw new Error('页面 schema 数量异常,请刷新页面后重试。');
|
|
72
|
-
case
|
|
198
|
+
case 10:
|
|
73
199
|
invalidIndex = allSchema.findIndex(function (schema) {
|
|
74
200
|
return !schema || typeof schema !== 'object';
|
|
75
201
|
});
|
|
76
202
|
if (!(invalidIndex !== -1)) {
|
|
77
|
-
|
|
203
|
+
_context2.next = 11;
|
|
78
204
|
break;
|
|
79
205
|
}
|
|
80
206
|
resource = resourceList[invalidIndex];
|
|
81
207
|
throw new Error("\u9875\u9762 \"" + ((resource === null || resource === void 0 ? void 0 : resource.title) || (resource === null || resource === void 0 ? void 0 : resource.id)) + "\" \u7F3A\u5C11\u6709\u6548 schema\uFF0C\u8BF7\u5148\u5728\u8BBE\u8BA1\u5668\u4E2D\u4FDD\u5B58\u8BE5\u9875\u9762\u3002");
|
|
82
|
-
case
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
return
|
|
208
|
+
case 11:
|
|
209
|
+
// 缺少 componentsTree 的 schema 不允许继续,否则生成器内部会因 fileName 等字段缺失而崩溃。
|
|
210
|
+
emptyIndex = allSchema.findIndex(function (schema) {
|
|
211
|
+
return !isNonEmptyProjectSchema(schema);
|
|
212
|
+
});
|
|
213
|
+
if (!(emptyIndex !== -1)) {
|
|
214
|
+
_context2.next = 12;
|
|
215
|
+
break;
|
|
216
|
+
}
|
|
217
|
+
_resource = resourceList[emptyIndex];
|
|
218
|
+
throw new Error("\u9875\u9762 \"" + ((_resource === null || _resource === void 0 ? void 0 : _resource.title) || (_resource === null || _resource === void 0 ? void 0 : _resource.id)) + "\" schema \u4E3A\u7A7A\uFF0C\u8BF7\u5148\u4FDD\u5B58\u9875\u9762\u540E\u518D\u5C1D\u8BD5\u51FA\u7801\u3002");
|
|
219
|
+
case 12:
|
|
220
|
+
_context2.next = 13;
|
|
221
|
+
return Promise.all(allSchema.map(function (item, index) {
|
|
222
|
+
var resource = resourceList[index];
|
|
223
|
+
var desiredFileName = (resource === null || resource === void 0 ? void 0 : resource.slug) || (resource === null || resource === void 0 ? void 0 : resource.id) || 'index';
|
|
224
|
+
var withFileName = ensurePrimaryTreeFileName(item, desiredFileName);
|
|
225
|
+
return fixSchema(withFileName);
|
|
86
226
|
}));
|
|
87
|
-
case
|
|
88
|
-
sanitizedSchemas =
|
|
227
|
+
case 13:
|
|
228
|
+
sanitizedSchemas = _context2.sent;
|
|
229
|
+
missingFileNameIndex = sanitizedSchemas.findIndex(function (schema) {
|
|
230
|
+
return !getPrimaryTreeFileName(schema);
|
|
231
|
+
});
|
|
232
|
+
if (!(missingFileNameIndex !== -1)) {
|
|
233
|
+
_context2.next = 14;
|
|
234
|
+
break;
|
|
235
|
+
}
|
|
236
|
+
_resource2 = resourceList[missingFileNameIndex];
|
|
237
|
+
throw new Error("\u9875\u9762 \"" + ((_resource2 === null || _resource2 === void 0 ? void 0 : _resource2.title) || (_resource2 === null || _resource2 === void 0 ? void 0 : _resource2.id)) + "\" schema \u7F3A\u5C11 fileName \u5B57\u6BB5\uFF0C\u65E0\u6CD5\u51FA\u7801\u3002");
|
|
238
|
+
case 14:
|
|
89
239
|
primarySchema = (_sanitizedSchemas$ = sanitizedSchemas[0]) !== null && _sanitizedSchemas$ !== void 0 ? _sanitizedSchemas$ : null;
|
|
90
240
|
originalPrimarySchema = allSchema[0] ? deepClone(allSchema[0]) : null; // 出码...
|
|
91
|
-
|
|
241
|
+
_context2.next = 15;
|
|
92
242
|
return CodeGenerator.generateCode({
|
|
93
243
|
solution: 'vuejs3',
|
|
94
244
|
schema: sanitizedSchemas,
|
|
95
245
|
flattenResult: true,
|
|
96
246
|
resourceList: resourceList
|
|
97
247
|
});
|
|
98
|
-
case
|
|
99
|
-
result =
|
|
248
|
+
case 15:
|
|
249
|
+
result = _context2.sent;
|
|
100
250
|
console.log('generated: ', result);
|
|
101
251
|
setState(function (prev) {
|
|
102
252
|
return _extends({}, prev, {
|
|
@@ -106,13 +256,13 @@ export function CodeGenActionBtn(_ref) {
|
|
|
106
256
|
originalSchema: originalPrimarySchema
|
|
107
257
|
});
|
|
108
258
|
});
|
|
109
|
-
|
|
259
|
+
_context2.next = 17;
|
|
110
260
|
break;
|
|
111
|
-
case
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
console.error('failed to run code generator: ',
|
|
115
|
-
error =
|
|
261
|
+
case 16:
|
|
262
|
+
_context2.prev = 16;
|
|
263
|
+
_t3 = _context2["catch"](0);
|
|
264
|
+
console.error('failed to run code generator: ', _t3);
|
|
265
|
+
error = _t3 instanceof Error ? _t3 : new Error("" + ((_t3 === null || _t3 === void 0 ? void 0 : _t3.message) || _t3));
|
|
116
266
|
setState(function (prev) {
|
|
117
267
|
return _extends({}, prev, {
|
|
118
268
|
loading: false,
|
|
@@ -121,20 +271,28 @@ export function CodeGenActionBtn(_ref) {
|
|
|
121
271
|
});
|
|
122
272
|
});
|
|
123
273
|
Message.error(error.message);
|
|
124
|
-
case
|
|
274
|
+
case 17:
|
|
125
275
|
case "end":
|
|
126
|
-
return
|
|
276
|
+
return _context2.stop();
|
|
127
277
|
}
|
|
128
|
-
},
|
|
278
|
+
}, _callee2, null, [[0, 16], [6, 8]]);
|
|
129
279
|
}));
|
|
130
280
|
return function handleClick() {
|
|
131
|
-
return
|
|
281
|
+
return _ref3.apply(this, arguments);
|
|
132
282
|
};
|
|
133
283
|
}();
|
|
134
|
-
return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(
|
|
284
|
+
return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("div", {
|
|
285
|
+
className: "code-gen-plugin-actions"
|
|
286
|
+
}, /*#__PURE__*/React.createElement(Button, {
|
|
287
|
+
onClick: handleSave,
|
|
288
|
+
loading: state.saving,
|
|
289
|
+
disabled: state.loading
|
|
290
|
+
}, "\u4FDD\u5B58"), /*#__PURE__*/React.createElement(Button, {
|
|
135
291
|
type: "primary",
|
|
136
|
-
onClick: handleClick
|
|
137
|
-
|
|
292
|
+
onClick: handleClick,
|
|
293
|
+
loading: state.loading,
|
|
294
|
+
disabled: state.saving
|
|
295
|
+
}, "\u51FA\u7801")), /*#__PURE__*/React.createElement(Drawer, {
|
|
138
296
|
visible: state.visible,
|
|
139
297
|
title: "\u51FA\u7801\u7ED3\u679C",
|
|
140
298
|
width: "95vw",
|
|
@@ -172,16 +330,16 @@ export function fixSchema(_x) {
|
|
|
172
330
|
return _fixSchema.apply(this, arguments);
|
|
173
331
|
}
|
|
174
332
|
function _fixSchema() {
|
|
175
|
-
_fixSchema = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function
|
|
333
|
+
_fixSchema = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee3(schema) {
|
|
176
334
|
var componentsMap;
|
|
177
|
-
return _regeneratorRuntime.wrap(function (
|
|
178
|
-
while (1) switch (
|
|
335
|
+
return _regeneratorRuntime.wrap(function (_context3) {
|
|
336
|
+
while (1) switch (_context3.prev = _context3.next) {
|
|
179
337
|
case 0:
|
|
180
338
|
componentsMap = Array.isArray(schema.componentsMap) ? schema.componentsMap : [];
|
|
181
339
|
if (!Array.isArray(schema.componentsMap)) {
|
|
182
340
|
console.warn('schema.componentsMap 缺失,已回退为空数组。');
|
|
183
341
|
}
|
|
184
|
-
return
|
|
342
|
+
return _context3.abrupt("return", deepClone(_extends({}, schema, {
|
|
185
343
|
componentsMap: componentsMap.filter(function (c) {
|
|
186
344
|
return c && typeof c === 'object' && c["package"];
|
|
187
345
|
}).map(function (c) {
|
|
@@ -206,12 +364,29 @@ function _fixSchema() {
|
|
|
206
364
|
})));
|
|
207
365
|
case 1:
|
|
208
366
|
case "end":
|
|
209
|
-
return
|
|
367
|
+
return _context3.stop();
|
|
210
368
|
}
|
|
211
|
-
},
|
|
369
|
+
}, _callee3);
|
|
212
370
|
}));
|
|
213
371
|
return _fixSchema.apply(this, arguments);
|
|
214
372
|
}
|
|
373
|
+
function isNonEmptyProjectSchema(schema) {
|
|
374
|
+
var tree = schema === null || schema === void 0 ? void 0 : schema.componentsTree;
|
|
375
|
+
return Array.isArray(tree) && tree.length > 0 && !!tree[0] && typeof tree[0] === 'object';
|
|
376
|
+
}
|
|
377
|
+
function getPrimaryTreeFileName(schema) {
|
|
378
|
+
var _componentsTree2, _componentsTree2$;
|
|
379
|
+
return schema === null || schema === void 0 ? void 0 : (_componentsTree2 = schema.componentsTree) === null || _componentsTree2 === void 0 ? void 0 : (_componentsTree2$ = _componentsTree2[0]) === null || _componentsTree2$ === void 0 ? void 0 : _componentsTree2$.fileName;
|
|
380
|
+
}
|
|
381
|
+
function ensurePrimaryTreeFileName(schema, fallbackFileName) {
|
|
382
|
+
var _componentsTree3;
|
|
383
|
+
var cloned = deepClone(schema);
|
|
384
|
+
var tree0 = cloned === null || cloned === void 0 ? void 0 : (_componentsTree3 = cloned.componentsTree) === null || _componentsTree3 === void 0 ? void 0 : _componentsTree3[0];
|
|
385
|
+
if (tree0 && typeof tree0 === 'object' && fallbackFileName && tree0.fileName !== fallbackFileName) {
|
|
386
|
+
tree0.fileName = fallbackFileName;
|
|
387
|
+
}
|
|
388
|
+
return cloned;
|
|
389
|
+
}
|
|
215
390
|
function deepClone(x) {
|
|
216
391
|
try {
|
|
217
392
|
return JSON.parse(JSON.stringify(x));
|
package/es/demo/index.js
CHANGED
|
@@ -30,7 +30,7 @@ preference.set('DataSourcePane', {
|
|
|
30
30
|
name: 'editor-init',
|
|
31
31
|
init: function init() {
|
|
32
32
|
return _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
|
|
33
|
-
var assets, schema, _t;
|
|
33
|
+
var assets, schema, scenarioName, resourceList, _t;
|
|
34
34
|
return _regeneratorRuntime.wrap(function (_context) {
|
|
35
35
|
while (1) switch (_context.prev = _context.next) {
|
|
36
36
|
case 0:
|
|
@@ -49,6 +49,27 @@ preference.set('DataSourcePane', {
|
|
|
49
49
|
schema = _context.sent;
|
|
50
50
|
// 加载 schema
|
|
51
51
|
ctx.project.openDocument(schema);
|
|
52
|
+
|
|
53
|
+
// 兼容本插件的出码逻辑:出码默认从 localStorage 读取 general:index:projectSchema
|
|
54
|
+
// demo 场景下没有“保存页面”的动作,因此在加载时写入一份可用的缓存。
|
|
55
|
+
try {
|
|
56
|
+
scenarioName = 'general';
|
|
57
|
+
resourceList = [{
|
|
58
|
+
title: '首页',
|
|
59
|
+
slug: 'index',
|
|
60
|
+
id: 'index',
|
|
61
|
+
path: '/',
|
|
62
|
+
firstPage: 'true'
|
|
63
|
+
}];
|
|
64
|
+
window.localStorage.setItem(scenarioName + ":resourceList", JSON.stringify(resourceList));
|
|
65
|
+
window.localStorage.setItem(scenarioName + ":index:projectSchema", JSON.stringify({
|
|
66
|
+
version: '1.0.0',
|
|
67
|
+
componentsTree: [schema],
|
|
68
|
+
componentsMap: []
|
|
69
|
+
}));
|
|
70
|
+
} catch (e) {
|
|
71
|
+
// ignore
|
|
72
|
+
}
|
|
52
73
|
case 4:
|
|
53
74
|
case "end":
|
|
54
75
|
return _context.stop();
|
package/es/index.js
CHANGED
package/es/utils/utils.js
CHANGED
|
@@ -4,11 +4,17 @@ var DEFAULT_RESOURCE_LIST = [{
|
|
|
4
4
|
title: '首页',
|
|
5
5
|
slug: 'index',
|
|
6
6
|
id: 'index',
|
|
7
|
+
path: '/',
|
|
7
8
|
firstPage: 'true'
|
|
8
9
|
}];
|
|
9
10
|
var toFirstPageFlag = function toFirstPageFlag(value) {
|
|
10
11
|
return value === 'true' ? 'true' : 'false';
|
|
11
12
|
};
|
|
13
|
+
var normalizePath = function normalizePath(value, fallback) {
|
|
14
|
+
var raw = typeof value === 'string' ? value.trim() : '';
|
|
15
|
+
var picked = raw || fallback || '/';
|
|
16
|
+
return picked.startsWith('/') ? picked : "/" + picked;
|
|
17
|
+
};
|
|
12
18
|
|
|
13
19
|
/**
|
|
14
20
|
* 从缓存获取ResourceList
|
|
@@ -46,11 +52,20 @@ export var getResourceListFromLocalStorage = /*#__PURE__*/function () {
|
|
|
46
52
|
normalized = parsed.filter(function (item) {
|
|
47
53
|
return !!item && typeof item === 'object';
|
|
48
54
|
}).map(function (item) {
|
|
55
|
+
var title = typeof item.title === 'string' ? item.title : '';
|
|
56
|
+
var slugRaw = typeof item.slug === 'string' ? item.slug : '';
|
|
57
|
+
var id = typeof item.id === 'string' ? item.id : '';
|
|
58
|
+
var firstPage = toFirstPageFlag(item.firstPage);
|
|
59
|
+
// 兼容:部分场景 resourceList 里没有 slug(或为空),而 code-generator@1.2.41 会用 item.slug 去匹配 schema.componentsTree[0].fileName
|
|
60
|
+
var slug = (slugRaw || id || '').trim();
|
|
61
|
+
var fallbackPath = firstPage === 'true' ? '/' : "/" + (slug || 'index');
|
|
62
|
+
var path = normalizePath(item.path, fallbackPath);
|
|
49
63
|
return {
|
|
50
|
-
title:
|
|
51
|
-
slug:
|
|
52
|
-
id:
|
|
53
|
-
|
|
64
|
+
title: title,
|
|
65
|
+
slug: slug,
|
|
66
|
+
id: id,
|
|
67
|
+
path: path,
|
|
68
|
+
firstPage: firstPage
|
|
54
69
|
};
|
|
55
70
|
}).filter(function (item) {
|
|
56
71
|
return item.id;
|
|
@@ -93,7 +108,9 @@ export var getAllSchemaByResourceList = /*#__PURE__*/function () {
|
|
|
93
108
|
var storageKey = getLSName(scenarioName, item.id);
|
|
94
109
|
var localSchema = window.localStorage.getItem(storageKey);
|
|
95
110
|
if (!localSchema) {
|
|
96
|
-
|
|
111
|
+
// 在 demo 或未保存页面时,这里可能读不到缓存的 schema。
|
|
112
|
+
// 上层(出码按钮)会做更友好的兜底/提示,这里降级为 warn,避免误导。
|
|
113
|
+
console.warn('no schema found for resource %s', storageKey);
|
|
97
114
|
return {
|
|
98
115
|
version: '1.0.0',
|
|
99
116
|
componentsTree: [],
|
|
@@ -12,9 +12,11 @@ var _react = _interopRequireWildcard(require("react"));
|
|
|
12
12
|
var CodeGenerator = _interopRequireWildcard(require("@dckj-npm/lowcode-code-generator/standalone-loader"));
|
|
13
13
|
var _next = require("@alifd/next");
|
|
14
14
|
var _coerce = _interopRequireDefault(require("semver/functions/coerce"));
|
|
15
|
+
var _lowcodeEngine = require("@alilc/lowcode-engine");
|
|
16
|
+
var _fileSaver = require("file-saver");
|
|
15
17
|
var _CodeGenResult = require("../CodeGenResult");
|
|
16
18
|
var _utils = require("../../utils/utils");
|
|
17
|
-
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, "default": e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var
|
|
19
|
+
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, "default": e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t4 in e) "default" !== _t4 && {}.hasOwnProperty.call(e, _t4) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t4)) && (i.get || i.set) ? o(f, _t4, i) : f[_t4] = e[_t4]); return f; })(e, t); }
|
|
18
20
|
function CodeGenActionBtn(_ref) {
|
|
19
21
|
var ctx = _ref.ctx;
|
|
20
22
|
var _useState = (0, _react.useState)({
|
|
@@ -22,6 +24,7 @@ function CodeGenActionBtn(_ref) {
|
|
|
22
24
|
hasError: false,
|
|
23
25
|
error: null,
|
|
24
26
|
loading: false,
|
|
27
|
+
saving: false,
|
|
25
28
|
result: null,
|
|
26
29
|
schema: null,
|
|
27
30
|
originalSchema: null
|
|
@@ -29,13 +32,112 @@ function CodeGenActionBtn(_ref) {
|
|
|
29
32
|
state = _useState[0],
|
|
30
33
|
setState = _useState[1];
|
|
31
34
|
console.log('CodeGenActionBtn render, ctx: ', ctx);
|
|
32
|
-
var
|
|
35
|
+
var handleSave = /*#__PURE__*/function () {
|
|
33
36
|
var _ref2 = (0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee() {
|
|
34
|
-
var
|
|
37
|
+
var _resourceList$, _resourceList$2, _componentsTree, scenarioName, resourceList, exported, desiredFileName, withFileName, sanitized, matchedResource, pageSchema, error, _t;
|
|
35
38
|
return _regenerator["default"].wrap(function (_context) {
|
|
36
39
|
while (1) switch (_context.prev = _context.next) {
|
|
37
40
|
case 0:
|
|
38
41
|
_context.prev = 0;
|
|
42
|
+
setState(function (prev) {
|
|
43
|
+
return (0, _extends2["default"])({}, prev, {
|
|
44
|
+
saving: true
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
scenarioName = (0, _utils.getProjectId)();
|
|
48
|
+
if (scenarioName) {
|
|
49
|
+
_context.next = 1;
|
|
50
|
+
break;
|
|
51
|
+
}
|
|
52
|
+
throw new Error('无法获取 projectId,请确认已在低代码编辑器内打开该插件。');
|
|
53
|
+
case 1:
|
|
54
|
+
_context.next = 2;
|
|
55
|
+
return (0, _utils.getResourceListFromLocalStorage)(scenarioName);
|
|
56
|
+
case 2:
|
|
57
|
+
resourceList = _context.sent;
|
|
58
|
+
_context.next = 3;
|
|
59
|
+
return _lowcodeEngine.project.exportSchema();
|
|
60
|
+
case 3:
|
|
61
|
+
exported = _context.sent;
|
|
62
|
+
if (!(!exported || typeof exported !== 'object')) {
|
|
63
|
+
_context.next = 4;
|
|
64
|
+
break;
|
|
65
|
+
}
|
|
66
|
+
throw new Error('导出 schema 失败,请稍后重试。');
|
|
67
|
+
case 4:
|
|
68
|
+
if (isNonEmptyProjectSchema(exported)) {
|
|
69
|
+
_context.next = 5;
|
|
70
|
+
break;
|
|
71
|
+
}
|
|
72
|
+
throw new Error('当前页面 schema 为空,请确认画布中已有内容后再保存。');
|
|
73
|
+
case 5:
|
|
74
|
+
desiredFileName = getPrimaryTreeFileName(exported) || (resourceList === null || resourceList === void 0 ? void 0 : (_resourceList$ = resourceList[0]) === null || _resourceList$ === void 0 ? void 0 : _resourceList$.slug) || (resourceList === null || resourceList === void 0 ? void 0 : (_resourceList$2 = resourceList[0]) === null || _resourceList$2 === void 0 ? void 0 : _resourceList$2.id) || 'index';
|
|
75
|
+
withFileName = ensurePrimaryTreeFileName(exported, desiredFileName);
|
|
76
|
+
_context.next = 6;
|
|
77
|
+
return fixSchema(withFileName);
|
|
78
|
+
case 6:
|
|
79
|
+
sanitized = _context.sent;
|
|
80
|
+
matchedResource = resourceList.find(function (r) {
|
|
81
|
+
return (r === null || r === void 0 ? void 0 : r.slug) === desiredFileName || (r === null || r === void 0 ? void 0 : r.id) === desiredFileName;
|
|
82
|
+
}) || resourceList.find(function (r) {
|
|
83
|
+
return (r === null || r === void 0 ? void 0 : r.firstPage) === 'true';
|
|
84
|
+
}) || resourceList[0];
|
|
85
|
+
if (matchedResource !== null && matchedResource !== void 0 && matchedResource.id) {
|
|
86
|
+
_context.next = 7;
|
|
87
|
+
break;
|
|
88
|
+
}
|
|
89
|
+
throw new Error('无法定位当前页面资源信息,请先保存页面后再尝试。');
|
|
90
|
+
case 7:
|
|
91
|
+
// 1) 写入 localStorage:让“出码”能拿到最新 schema(不依赖业务侧保存)
|
|
92
|
+
window.localStorage.setItem((0, _utils.getLSName)(scenarioName, matchedResource.id), JSON.stringify(sanitized));
|
|
93
|
+
// 2) 兜底写入 resourceList(某些环境可能没有保存动作)
|
|
94
|
+
window.localStorage.setItem(scenarioName + ":resourceList", JSON.stringify(resourceList));
|
|
95
|
+
|
|
96
|
+
// 3) 下载当前页面 schema.json(与 demo 的 public/schema.json 格式一致:单页 schema 节点)
|
|
97
|
+
pageSchema = sanitized === null || sanitized === void 0 ? void 0 : (_componentsTree = sanitized.componentsTree) === null || _componentsTree === void 0 ? void 0 : _componentsTree[0];
|
|
98
|
+
if (!(!pageSchema || typeof pageSchema !== 'object')) {
|
|
99
|
+
_context.next = 8;
|
|
100
|
+
break;
|
|
101
|
+
}
|
|
102
|
+
throw new Error('当前页面 schema 结构异常,无法下载。');
|
|
103
|
+
case 8:
|
|
104
|
+
(0, _fileSaver.saveAs)(new Blob([JSON.stringify(pageSchema, null, 2)], {
|
|
105
|
+
type: 'application/json;charset=utf-8'
|
|
106
|
+
}), 'schema.json');
|
|
107
|
+
_next.Message.success('已保存当前页面:已写入缓存并下载 schema.json');
|
|
108
|
+
_context.next = 10;
|
|
109
|
+
break;
|
|
110
|
+
case 9:
|
|
111
|
+
_context.prev = 9;
|
|
112
|
+
_t = _context["catch"](0);
|
|
113
|
+
console.error('failed to save schema: ', _t);
|
|
114
|
+
error = _t instanceof Error ? _t : new Error("" + ((_t === null || _t === void 0 ? void 0 : _t.message) || _t));
|
|
115
|
+
_next.Message.error(error.message);
|
|
116
|
+
case 10:
|
|
117
|
+
_context.prev = 10;
|
|
118
|
+
setState(function (prev) {
|
|
119
|
+
return (0, _extends2["default"])({}, prev, {
|
|
120
|
+
saving: false
|
|
121
|
+
});
|
|
122
|
+
});
|
|
123
|
+
return _context.finish(10);
|
|
124
|
+
case 11:
|
|
125
|
+
case "end":
|
|
126
|
+
return _context.stop();
|
|
127
|
+
}
|
|
128
|
+
}, _callee, null, [[0, 9, 10, 11]]);
|
|
129
|
+
}));
|
|
130
|
+
return function handleSave() {
|
|
131
|
+
return _ref2.apply(this, arguments);
|
|
132
|
+
};
|
|
133
|
+
}();
|
|
134
|
+
var handleClick = /*#__PURE__*/function () {
|
|
135
|
+
var _ref3 = (0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee2() {
|
|
136
|
+
var _sanitizedSchemas$, name, resourceList, allSchema, hasEmptySchema, exported, invalidIndex, resource, emptyIndex, _resource, sanitizedSchemas, missingFileNameIndex, _resource2, primarySchema, originalPrimarySchema, result, error, _t2, _t3;
|
|
137
|
+
return _regenerator["default"].wrap(function (_context2) {
|
|
138
|
+
while (1) switch (_context2.prev = _context2.next) {
|
|
139
|
+
case 0:
|
|
140
|
+
_context2.prev = 0;
|
|
39
141
|
setState(function (prev) {
|
|
40
142
|
return (0, _extends2["default"])({}, prev, {
|
|
41
143
|
loading: true,
|
|
@@ -46,64 +148,112 @@ function CodeGenActionBtn(_ref) {
|
|
|
46
148
|
});
|
|
47
149
|
name = (0, _utils.getProjectId)();
|
|
48
150
|
if (name) {
|
|
49
|
-
|
|
151
|
+
_context2.next = 1;
|
|
50
152
|
break;
|
|
51
153
|
}
|
|
52
154
|
throw new Error('无法获取 projectId,请确认已在低代码编辑器内打开该插件。');
|
|
53
155
|
case 1:
|
|
54
|
-
|
|
156
|
+
_context2.next = 2;
|
|
55
157
|
return (0, _utils.getResourceListFromLocalStorage)(name);
|
|
56
158
|
case 2:
|
|
57
|
-
resourceList =
|
|
159
|
+
resourceList = _context2.sent;
|
|
58
160
|
if (resourceList.length) {
|
|
59
|
-
|
|
161
|
+
_context2.next = 3;
|
|
60
162
|
break;
|
|
61
163
|
}
|
|
62
164
|
throw new Error('未找到页面配置,请先保存页面后再尝试出码。');
|
|
63
165
|
case 3:
|
|
64
|
-
|
|
166
|
+
_context2.next = 4;
|
|
65
167
|
return (0, _utils.getAllSchemaByResourceList)(name, resourceList);
|
|
66
168
|
case 4:
|
|
67
|
-
allSchema =
|
|
169
|
+
allSchema = _context2.sent;
|
|
68
170
|
if (allSchema.length) {
|
|
69
|
-
|
|
171
|
+
_context2.next = 5;
|
|
70
172
|
break;
|
|
71
173
|
}
|
|
72
174
|
throw new Error('未找到页面 schema,请先保存页面后再尝试出码。');
|
|
73
175
|
case 5:
|
|
176
|
+
// 如果 localStorage 中缺少 schema(常见于 demo 或未保存页面),尝试从设计器当前工程导出 schema 做兜底。
|
|
177
|
+
// 注意:多页面场景仍建议依赖保存后的 resource schema。
|
|
178
|
+
hasEmptySchema = allSchema.some(function (s) {
|
|
179
|
+
return !isNonEmptyProjectSchema(s);
|
|
180
|
+
});
|
|
181
|
+
if (!hasEmptySchema) {
|
|
182
|
+
_context2.next = 9;
|
|
183
|
+
break;
|
|
184
|
+
}
|
|
185
|
+
_context2.prev = 6;
|
|
186
|
+
_context2.next = 7;
|
|
187
|
+
return _lowcodeEngine.project.exportSchema();
|
|
188
|
+
case 7:
|
|
189
|
+
exported = _context2.sent;
|
|
190
|
+
if (exported && isNonEmptyProjectSchema(exported)) {
|
|
191
|
+
// 仅对第一张页面做兜底(与 DEFAULT_RESOURCE_LIST 对齐)
|
|
192
|
+
allSchema[0] = exported;
|
|
193
|
+
}
|
|
194
|
+
_context2.next = 9;
|
|
195
|
+
break;
|
|
196
|
+
case 8:
|
|
197
|
+
_context2.prev = 8;
|
|
198
|
+
_t2 = _context2["catch"](6);
|
|
199
|
+
case 9:
|
|
74
200
|
if (!(allSchema.length !== resourceList.length)) {
|
|
75
|
-
|
|
201
|
+
_context2.next = 10;
|
|
76
202
|
break;
|
|
77
203
|
}
|
|
78
204
|
throw new Error('页面 schema 数量异常,请刷新页面后重试。');
|
|
79
|
-
case
|
|
205
|
+
case 10:
|
|
80
206
|
invalidIndex = allSchema.findIndex(function (schema) {
|
|
81
207
|
return !schema || typeof schema !== 'object';
|
|
82
208
|
});
|
|
83
209
|
if (!(invalidIndex !== -1)) {
|
|
84
|
-
|
|
210
|
+
_context2.next = 11;
|
|
85
211
|
break;
|
|
86
212
|
}
|
|
87
213
|
resource = resourceList[invalidIndex];
|
|
88
214
|
throw new Error("\u9875\u9762 \"" + ((resource === null || resource === void 0 ? void 0 : resource.title) || (resource === null || resource === void 0 ? void 0 : resource.id)) + "\" \u7F3A\u5C11\u6709\u6548 schema\uFF0C\u8BF7\u5148\u5728\u8BBE\u8BA1\u5668\u4E2D\u4FDD\u5B58\u8BE5\u9875\u9762\u3002");
|
|
89
|
-
case
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
return
|
|
215
|
+
case 11:
|
|
216
|
+
// 缺少 componentsTree 的 schema 不允许继续,否则生成器内部会因 fileName 等字段缺失而崩溃。
|
|
217
|
+
emptyIndex = allSchema.findIndex(function (schema) {
|
|
218
|
+
return !isNonEmptyProjectSchema(schema);
|
|
219
|
+
});
|
|
220
|
+
if (!(emptyIndex !== -1)) {
|
|
221
|
+
_context2.next = 12;
|
|
222
|
+
break;
|
|
223
|
+
}
|
|
224
|
+
_resource = resourceList[emptyIndex];
|
|
225
|
+
throw new Error("\u9875\u9762 \"" + ((_resource === null || _resource === void 0 ? void 0 : _resource.title) || (_resource === null || _resource === void 0 ? void 0 : _resource.id)) + "\" schema \u4E3A\u7A7A\uFF0C\u8BF7\u5148\u4FDD\u5B58\u9875\u9762\u540E\u518D\u5C1D\u8BD5\u51FA\u7801\u3002");
|
|
226
|
+
case 12:
|
|
227
|
+
_context2.next = 13;
|
|
228
|
+
return Promise.all(allSchema.map(function (item, index) {
|
|
229
|
+
var resource = resourceList[index];
|
|
230
|
+
var desiredFileName = (resource === null || resource === void 0 ? void 0 : resource.slug) || (resource === null || resource === void 0 ? void 0 : resource.id) || 'index';
|
|
231
|
+
var withFileName = ensurePrimaryTreeFileName(item, desiredFileName);
|
|
232
|
+
return fixSchema(withFileName);
|
|
93
233
|
}));
|
|
94
|
-
case
|
|
95
|
-
sanitizedSchemas =
|
|
234
|
+
case 13:
|
|
235
|
+
sanitizedSchemas = _context2.sent;
|
|
236
|
+
missingFileNameIndex = sanitizedSchemas.findIndex(function (schema) {
|
|
237
|
+
return !getPrimaryTreeFileName(schema);
|
|
238
|
+
});
|
|
239
|
+
if (!(missingFileNameIndex !== -1)) {
|
|
240
|
+
_context2.next = 14;
|
|
241
|
+
break;
|
|
242
|
+
}
|
|
243
|
+
_resource2 = resourceList[missingFileNameIndex];
|
|
244
|
+
throw new Error("\u9875\u9762 \"" + ((_resource2 === null || _resource2 === void 0 ? void 0 : _resource2.title) || (_resource2 === null || _resource2 === void 0 ? void 0 : _resource2.id)) + "\" schema \u7F3A\u5C11 fileName \u5B57\u6BB5\uFF0C\u65E0\u6CD5\u51FA\u7801\u3002");
|
|
245
|
+
case 14:
|
|
96
246
|
primarySchema = (_sanitizedSchemas$ = sanitizedSchemas[0]) !== null && _sanitizedSchemas$ !== void 0 ? _sanitizedSchemas$ : null;
|
|
97
247
|
originalPrimarySchema = allSchema[0] ? deepClone(allSchema[0]) : null; // 出码...
|
|
98
|
-
|
|
248
|
+
_context2.next = 15;
|
|
99
249
|
return CodeGenerator.generateCode({
|
|
100
250
|
solution: 'vuejs3',
|
|
101
251
|
schema: sanitizedSchemas,
|
|
102
252
|
flattenResult: true,
|
|
103
253
|
resourceList: resourceList
|
|
104
254
|
});
|
|
105
|
-
case
|
|
106
|
-
result =
|
|
255
|
+
case 15:
|
|
256
|
+
result = _context2.sent;
|
|
107
257
|
console.log('generated: ', result);
|
|
108
258
|
setState(function (prev) {
|
|
109
259
|
return (0, _extends2["default"])({}, prev, {
|
|
@@ -113,13 +263,13 @@ function CodeGenActionBtn(_ref) {
|
|
|
113
263
|
originalSchema: originalPrimarySchema
|
|
114
264
|
});
|
|
115
265
|
});
|
|
116
|
-
|
|
266
|
+
_context2.next = 17;
|
|
117
267
|
break;
|
|
118
|
-
case
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
console.error('failed to run code generator: ',
|
|
122
|
-
error =
|
|
268
|
+
case 16:
|
|
269
|
+
_context2.prev = 16;
|
|
270
|
+
_t3 = _context2["catch"](0);
|
|
271
|
+
console.error('failed to run code generator: ', _t3);
|
|
272
|
+
error = _t3 instanceof Error ? _t3 : new Error("" + ((_t3 === null || _t3 === void 0 ? void 0 : _t3.message) || _t3));
|
|
123
273
|
setState(function (prev) {
|
|
124
274
|
return (0, _extends2["default"])({}, prev, {
|
|
125
275
|
loading: false,
|
|
@@ -128,20 +278,28 @@ function CodeGenActionBtn(_ref) {
|
|
|
128
278
|
});
|
|
129
279
|
});
|
|
130
280
|
_next.Message.error(error.message);
|
|
131
|
-
case
|
|
281
|
+
case 17:
|
|
132
282
|
case "end":
|
|
133
|
-
return
|
|
283
|
+
return _context2.stop();
|
|
134
284
|
}
|
|
135
|
-
},
|
|
285
|
+
}, _callee2, null, [[0, 16], [6, 8]]);
|
|
136
286
|
}));
|
|
137
287
|
return function handleClick() {
|
|
138
|
-
return
|
|
288
|
+
return _ref3.apply(this, arguments);
|
|
139
289
|
};
|
|
140
290
|
}();
|
|
141
|
-
return /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null, /*#__PURE__*/_react["default"].createElement(
|
|
291
|
+
return /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null, /*#__PURE__*/_react["default"].createElement("div", {
|
|
292
|
+
className: "code-gen-plugin-actions"
|
|
293
|
+
}, /*#__PURE__*/_react["default"].createElement(_next.Button, {
|
|
294
|
+
onClick: handleSave,
|
|
295
|
+
loading: state.saving,
|
|
296
|
+
disabled: state.loading
|
|
297
|
+
}, "\u4FDD\u5B58"), /*#__PURE__*/_react["default"].createElement(_next.Button, {
|
|
142
298
|
type: "primary",
|
|
143
|
-
onClick: handleClick
|
|
144
|
-
|
|
299
|
+
onClick: handleClick,
|
|
300
|
+
loading: state.loading,
|
|
301
|
+
disabled: state.saving
|
|
302
|
+
}, "\u51FA\u7801")), /*#__PURE__*/_react["default"].createElement(_next.Drawer, {
|
|
145
303
|
visible: state.visible,
|
|
146
304
|
title: "\u51FA\u7801\u7ED3\u679C",
|
|
147
305
|
width: "95vw",
|
|
@@ -179,16 +337,16 @@ function fixSchema(_x) {
|
|
|
179
337
|
return _fixSchema.apply(this, arguments);
|
|
180
338
|
}
|
|
181
339
|
function _fixSchema() {
|
|
182
|
-
_fixSchema = (0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function
|
|
340
|
+
_fixSchema = (0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee3(schema) {
|
|
183
341
|
var componentsMap;
|
|
184
|
-
return _regenerator["default"].wrap(function (
|
|
185
|
-
while (1) switch (
|
|
342
|
+
return _regenerator["default"].wrap(function (_context3) {
|
|
343
|
+
while (1) switch (_context3.prev = _context3.next) {
|
|
186
344
|
case 0:
|
|
187
345
|
componentsMap = Array.isArray(schema.componentsMap) ? schema.componentsMap : [];
|
|
188
346
|
if (!Array.isArray(schema.componentsMap)) {
|
|
189
347
|
console.warn('schema.componentsMap 缺失,已回退为空数组。');
|
|
190
348
|
}
|
|
191
|
-
return
|
|
349
|
+
return _context3.abrupt("return", deepClone((0, _extends2["default"])({}, schema, {
|
|
192
350
|
componentsMap: componentsMap.filter(function (c) {
|
|
193
351
|
return c && typeof c === 'object' && c["package"];
|
|
194
352
|
}).map(function (c) {
|
|
@@ -213,12 +371,29 @@ function _fixSchema() {
|
|
|
213
371
|
})));
|
|
214
372
|
case 1:
|
|
215
373
|
case "end":
|
|
216
|
-
return
|
|
374
|
+
return _context3.stop();
|
|
217
375
|
}
|
|
218
|
-
},
|
|
376
|
+
}, _callee3);
|
|
219
377
|
}));
|
|
220
378
|
return _fixSchema.apply(this, arguments);
|
|
221
379
|
}
|
|
380
|
+
function isNonEmptyProjectSchema(schema) {
|
|
381
|
+
var tree = schema === null || schema === void 0 ? void 0 : schema.componentsTree;
|
|
382
|
+
return Array.isArray(tree) && tree.length > 0 && !!tree[0] && typeof tree[0] === 'object';
|
|
383
|
+
}
|
|
384
|
+
function getPrimaryTreeFileName(schema) {
|
|
385
|
+
var _componentsTree2, _componentsTree2$;
|
|
386
|
+
return schema === null || schema === void 0 ? void 0 : (_componentsTree2 = schema.componentsTree) === null || _componentsTree2 === void 0 ? void 0 : (_componentsTree2$ = _componentsTree2[0]) === null || _componentsTree2$ === void 0 ? void 0 : _componentsTree2$.fileName;
|
|
387
|
+
}
|
|
388
|
+
function ensurePrimaryTreeFileName(schema, fallbackFileName) {
|
|
389
|
+
var _componentsTree3;
|
|
390
|
+
var cloned = deepClone(schema);
|
|
391
|
+
var tree0 = cloned === null || cloned === void 0 ? void 0 : (_componentsTree3 = cloned.componentsTree) === null || _componentsTree3 === void 0 ? void 0 : _componentsTree3[0];
|
|
392
|
+
if (tree0 && typeof tree0 === 'object' && fallbackFileName && tree0.fileName !== fallbackFileName) {
|
|
393
|
+
tree0.fileName = fallbackFileName;
|
|
394
|
+
}
|
|
395
|
+
return cloned;
|
|
396
|
+
}
|
|
222
397
|
function deepClone(x) {
|
|
223
398
|
try {
|
|
224
399
|
return JSON.parse(JSON.stringify(x));
|
package/lib/demo/index.js
CHANGED
|
@@ -34,7 +34,7 @@ preference.set('DataSourcePane', {
|
|
|
34
34
|
name: 'editor-init',
|
|
35
35
|
init: function init() {
|
|
36
36
|
return (0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee() {
|
|
37
|
-
var assets, schema, _t;
|
|
37
|
+
var assets, schema, scenarioName, resourceList, _t;
|
|
38
38
|
return _regenerator["default"].wrap(function (_context) {
|
|
39
39
|
while (1) switch (_context.prev = _context.next) {
|
|
40
40
|
case 0:
|
|
@@ -53,6 +53,27 @@ preference.set('DataSourcePane', {
|
|
|
53
53
|
schema = _context.sent;
|
|
54
54
|
// 加载 schema
|
|
55
55
|
ctx.project.openDocument(schema);
|
|
56
|
+
|
|
57
|
+
// 兼容本插件的出码逻辑:出码默认从 localStorage 读取 general:index:projectSchema
|
|
58
|
+
// demo 场景下没有“保存页面”的动作,因此在加载时写入一份可用的缓存。
|
|
59
|
+
try {
|
|
60
|
+
scenarioName = 'general';
|
|
61
|
+
resourceList = [{
|
|
62
|
+
title: '首页',
|
|
63
|
+
slug: 'index',
|
|
64
|
+
id: 'index',
|
|
65
|
+
path: '/',
|
|
66
|
+
firstPage: 'true'
|
|
67
|
+
}];
|
|
68
|
+
window.localStorage.setItem(scenarioName + ":resourceList", JSON.stringify(resourceList));
|
|
69
|
+
window.localStorage.setItem(scenarioName + ":index:projectSchema", JSON.stringify({
|
|
70
|
+
version: '1.0.0',
|
|
71
|
+
componentsTree: [schema],
|
|
72
|
+
componentsMap: []
|
|
73
|
+
}));
|
|
74
|
+
} catch (e) {
|
|
75
|
+
// ignore
|
|
76
|
+
}
|
|
56
77
|
case 4:
|
|
57
78
|
case "end":
|
|
58
79
|
return _context.stop();
|
package/lib/index.js
CHANGED
package/lib/utils/utils.js
CHANGED
|
@@ -12,11 +12,17 @@ var DEFAULT_RESOURCE_LIST = [{
|
|
|
12
12
|
title: '首页',
|
|
13
13
|
slug: 'index',
|
|
14
14
|
id: 'index',
|
|
15
|
+
path: '/',
|
|
15
16
|
firstPage: 'true'
|
|
16
17
|
}];
|
|
17
18
|
var toFirstPageFlag = function toFirstPageFlag(value) {
|
|
18
19
|
return value === 'true' ? 'true' : 'false';
|
|
19
20
|
};
|
|
21
|
+
var normalizePath = function normalizePath(value, fallback) {
|
|
22
|
+
var raw = typeof value === 'string' ? value.trim() : '';
|
|
23
|
+
var picked = raw || fallback || '/';
|
|
24
|
+
return picked.startsWith('/') ? picked : "/" + picked;
|
|
25
|
+
};
|
|
20
26
|
|
|
21
27
|
/**
|
|
22
28
|
* 从缓存获取ResourceList
|
|
@@ -54,11 +60,20 @@ var getResourceListFromLocalStorage = exports.getResourceListFromLocalStorage =
|
|
|
54
60
|
normalized = parsed.filter(function (item) {
|
|
55
61
|
return !!item && typeof item === 'object';
|
|
56
62
|
}).map(function (item) {
|
|
63
|
+
var title = typeof item.title === 'string' ? item.title : '';
|
|
64
|
+
var slugRaw = typeof item.slug === 'string' ? item.slug : '';
|
|
65
|
+
var id = typeof item.id === 'string' ? item.id : '';
|
|
66
|
+
var firstPage = toFirstPageFlag(item.firstPage);
|
|
67
|
+
// 兼容:部分场景 resourceList 里没有 slug(或为空),而 code-generator@1.2.41 会用 item.slug 去匹配 schema.componentsTree[0].fileName
|
|
68
|
+
var slug = (slugRaw || id || '').trim();
|
|
69
|
+
var fallbackPath = firstPage === 'true' ? '/' : "/" + (slug || 'index');
|
|
70
|
+
var path = normalizePath(item.path, fallbackPath);
|
|
57
71
|
return {
|
|
58
|
-
title:
|
|
59
|
-
slug:
|
|
60
|
-
id:
|
|
61
|
-
|
|
72
|
+
title: title,
|
|
73
|
+
slug: slug,
|
|
74
|
+
id: id,
|
|
75
|
+
path: path,
|
|
76
|
+
firstPage: firstPage
|
|
62
77
|
};
|
|
63
78
|
}).filter(function (item) {
|
|
64
79
|
return item.id;
|
|
@@ -101,7 +116,9 @@ var getAllSchemaByResourceList = exports.getAllSchemaByResourceList = /*#__PURE_
|
|
|
101
116
|
var storageKey = getLSName(scenarioName, item.id);
|
|
102
117
|
var localSchema = window.localStorage.getItem(storageKey);
|
|
103
118
|
if (!localSchema) {
|
|
104
|
-
|
|
119
|
+
// 在 demo 或未保存页面时,这里可能读不到缓存的 schema。
|
|
120
|
+
// 上层(出码按钮)会做更友好的兜底/提示,这里降级为 warn,避免误导。
|
|
121
|
+
console.warn('no schema found for resource %s', storageKey);
|
|
105
122
|
return {
|
|
106
123
|
version: '1.0.0',
|
|
107
124
|
componentsTree: [],
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dckj-npm/lowcode-plugin-code-generator",
|
|
3
|
-
"version": "1.3.
|
|
3
|
+
"version": "1.3.28",
|
|
4
4
|
"description": "集团低代码引擎 - 浏览器出码插件",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"module": "es/index.js",
|
|
@@ -35,7 +35,7 @@
|
|
|
35
35
|
"@alilc/lowcode-code-generator": "^1.1.7",
|
|
36
36
|
"@alilc/lowcode-plugin-base-monaco-editor": "^1.1.2",
|
|
37
37
|
"@alilc/lowcode-types": "^1.3.4",
|
|
38
|
-
"@dckj-npm/lowcode-code-generator": "^1.2.
|
|
38
|
+
"@dckj-npm/lowcode-code-generator": "^1.2.41",
|
|
39
39
|
"@types/file-saver": "^2.0.7",
|
|
40
40
|
"@types/string-natural-compare": "^3.0.4",
|
|
41
41
|
"codesandbox": "^2.2.3",
|