@hzab/form-render 1.6.18 → 1.6.20

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.
Files changed (60) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/README.md +247 -239
  3. package/package.json +57 -57
  4. package/src/common/formily-utils.ts +125 -125
  5. package/src/common/schema-handler.ts +253 -253
  6. package/src/common/schema-merge.ts +68 -68
  7. package/src/components/ArrayBase/index.tsx +349 -349
  8. package/src/components/ArrayBase/style.less +90 -90
  9. package/src/components/ArrayBase/style.ts +2 -2
  10. package/src/components/ArrayCards/index.tsx +149 -149
  11. package/src/components/ArrayCards/style.less +14 -14
  12. package/src/components/ArrayCards/style.ts +4 -4
  13. package/src/components/ArrayTable/index.tsx +411 -411
  14. package/src/components/ArrayTable/style.less +52 -52
  15. package/src/components/ArrayTable/style.ts +7 -7
  16. package/src/components/DatePicker/index.tsx +108 -108
  17. package/src/components/LocationListPicker/assets/icon.js +1 -1
  18. package/src/components/LocationListPicker/components/AddrList/index.less +55 -55
  19. package/src/components/LocationListPicker/components/AddrList/index.tsx +75 -75
  20. package/src/components/LocationListPicker/components/Popup/address.schema.json +21 -21
  21. package/src/components/LocationListPicker/components/Popup/index.less +22 -22
  22. package/src/components/LocationListPicker/components/Popup/index.tsx +92 -92
  23. package/src/components/LocationListPicker/index.less +34 -34
  24. package/src/components/LocationListPicker/index.tsx +521 -520
  25. package/src/components/LocationPicker/Map/AMap/common/loader.ts +58 -58
  26. package/src/components/LocationPicker/Map/AMap/common/utils.ts +431 -431
  27. package/src/components/LocationPicker/Map/AMap/index.jsx +51 -51
  28. package/src/components/LocationPicker/README.md +44 -44
  29. package/src/components/LocationPicker/common/utils.ts +30 -30
  30. package/src/components/LocationPicker/components/ModalContent/index.tsx +387 -387
  31. package/src/components/LocationPicker/components/PickerInfo/index.tsx +109 -109
  32. package/src/components/LocationPicker/components/ResInfo/index.less +38 -38
  33. package/src/components/LocationPicker/components/ResInfo/index.tsx +65 -65
  34. package/src/components/LocationPicker/index.tsx +197 -197
  35. package/src/components/PersonnelSelect/index.less +21 -21
  36. package/src/components/PersonnelSelect/index.module.less +33 -33
  37. package/src/components/PersonnelSelect/type.ts +92 -92
  38. package/src/components/RichEditor/index.less +38 -38
  39. package/src/components/RichEditor/index.tsx +238 -238
  40. package/src/components/TreeCheckbox/components/CheckboxTable/index.tsx +61 -55
  41. package/src/components/TreeCheckbox/components/Render/index.tsx +28 -28
  42. package/src/components/TreeCheckbox/components/TabsRender/index.tsx +61 -58
  43. package/src/components/TreeCheckbox/index.less +18 -12
  44. package/src/components/TreeCheckbox/index.tsx +63 -63
  45. package/src/components/Upload/README.md +64 -64
  46. package/src/components/Upload/common/OfflineUpload.ts +339 -339
  47. package/src/components/Upload/common/customRequest.ts +81 -81
  48. package/src/components/Upload/common/fileName.ts +142 -142
  49. package/src/components/Upload/common/handleIOFileList.ts +393 -393
  50. package/src/components/Upload/common/nanoid.ts +7 -7
  51. package/src/components/Upload/common/ossUpload.js +159 -159
  52. package/src/components/Upload/common/utils.js +194 -194
  53. package/src/components/Upload/components/ItemList/index.tsx +52 -52
  54. package/src/components/Upload/components/PreviewModal/previewRender.tsx +80 -80
  55. package/src/components/Upload/index.tsx +17 -17
  56. package/src/components/Upload/uploader-input.jsx +187 -187
  57. package/src/components/Upload/uploader.jsx +316 -316
  58. package/src/components/UserSelect/index.tsx +123 -123
  59. package/src/components/index.tsx +17 -17
  60. package/src/index.tsx +198 -198
@@ -1,253 +1,253 @@
1
- import _ from "lodash";
2
- import { customAlphabet } from "nanoid";
3
-
4
- /**
5
- * 指定 nanoid 字符集合
6
- */
7
- export const alphabet = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
8
- export const nanoid = customAlphabet(alphabet, 20);
9
-
10
- /**
11
- * You can use the built-in context variables
12
- *
13
- * 1. `$self` is the current Field Model
14
- *
15
- * 2. `$form` is the current Form Model
16
- *
17
- * 3. `$deps` is the dependencies value 仅在 run 中存在
18
- *
19
- * 4. `$observable` function is used to create an persistent observable state object
20
- *
21
- * 5. `$memo` function is is used to create a persistent data
22
- *
23
- * 6. `$effect` function is used to handle side-effect logic
24
- *
25
- * 7. `$props` function is used to set component props to current field
26
- *
27
- * Document Links
28
- *
29
- * https://react.formilyjs.org/api/shared/schema#%E5%86%85%E7%BD%AE%E8%A1%A8%E8%BE%BE%E5%BC%8F%E4%BD%9C%E7%94%A8%E5%9F%9F
30
- **/
31
-
32
- /** formily 执行函数作用域相关参数 */
33
- export const formilyDataKeys = ["$form", "$self", "$observable", "$effect", "$memo", "$props", "$values"];
34
-
35
- /** 自增容器 */
36
- export const arrayList = ["ArrayTable", "ArrayCards"];
37
- /** 不需要绑定事件的目标 */
38
- export const skipBindList = ["FormCollapse", "FormCollapse.CollapsePanel"];
39
- /** change 回调返回 parent 数据的组件 */
40
- export const returnParentValue = ["ArrayTable", "ArrayCards"];
41
-
42
- /**
43
- * schema 每项添加 onChange,解决无全局 onChange 问题
44
- * @param schema
45
- * @param onChange
46
- * @returns
47
- */
48
- export const bindOnChange = (schema, opt) => {
49
- const { schemaScope, onChange, formRender, parent, isCloneDeep = true } = opt || {};
50
- let _schema = isCloneDeep ? _.cloneDeep(schema) : schema;
51
- _schema = _schema.schema || _schema;
52
- Object.keys(_schema.properties || {})?.forEach((key) => {
53
- let item = _schema.properties[key];
54
- if (!item) {
55
- return;
56
- }
57
- const componentName = item["x-component"];
58
- // 解决 name 丢失的问题
59
- item.name = key;
60
-
61
- const comProps = item?.["x-component-props"];
62
- if (!comProps) {
63
- item["x-component-props"] = {};
64
- } else if (isScopeKey(comProps, schemaScope)) {
65
- item["x-component-props"] = getScopeKeyVal(comProps, schemaScope) || {};
66
- }
67
-
68
- const bindParams = {
69
- componentName,
70
- target: item["x-component-props"],
71
- targetKey: key,
72
- targetId: key,
73
- formOnChange: onChange,
74
- schemaScope,
75
- formRender,
76
- parent,
77
- };
78
-
79
- // 不需要绑定事件的目标:折叠面板不需要设置 onChange 事件,否则会导致触发多次
80
- if (!skipBindList.includes(componentName)) {
81
- // 绑定新的 change 事件
82
- bindCallback({
83
- ...bindParams,
84
- actionName: "onChange",
85
- });
86
- }
87
-
88
- // 处理自增表格、卡片等情况
89
- if (arrayList.includes(componentName)) {
90
- bindOnChange(item.items, {
91
- ...opt,
92
- parent: item,
93
- isCloneDeep: false,
94
- });
95
- // 处理点击新增、删除、排序操作
96
- const actions = ["onAdd", "onCopy", "onRemove", "onMoveDown", "onMoveUp"];
97
- actions.forEach((actionName) => {
98
- bindCallback({
99
- ...bindParams,
100
- actionName,
101
- });
102
- });
103
- }
104
-
105
- // 处理嵌套的情况
106
- if (item.properties) {
107
- bindOnChange(item, {
108
- ...opt,
109
- isCloneDeep: false,
110
- });
111
- }
112
- });
113
- return _schema;
114
- };
115
-
116
- /**
117
- * 添加回调函数
118
- * @param target
119
- * @param name
120
- * @param formOnChange
121
- */
122
- export const bindCallback = (opt) => {
123
- const { target, actionName, schemaScope, formOnChange } = opt || {};
124
- const tempFn = target[actionName];
125
- if (!schemaScope._$tempData) {
126
- schemaScope._$tempData = {};
127
- }
128
- // 设置一个唯一的函数名,用于从 schema scope 传入 formily 事件执行中
129
- const fnKey = "fnKey_" + nanoid();
130
- // 设置 schemaScope 中的唯一函数,获取执行函数作用域 $form, $self, $observable, $effect, $memo, $props, $values 相关参数
131
- schemaScope._$tempData[fnKey] = function (params, sourceArgs) {
132
- const { $form, $self, $observable, $effect, $memo, $props, $values } = params;
133
- // 处理结果数据,各组件返回数据格式不一致
134
- const res = handleChangeValue({
135
- ...opt,
136
- sourceArgs,
137
- });
138
- // 触发组件 onChange 事件
139
- if (isRunStr(tempFn)) {
140
- // schemaScope 传入方式
141
- const fn = handleSchemaStrVal(tempFn, { ...params, ...schemaScope });
142
-
143
- typeof fn === "function" && fn(...sourceArgs, res);
144
- } else if (typeof tempFn === "function") {
145
- // 直接传入函数的方式
146
- tempFn(...sourceArgs, res);
147
- }
148
- formOnChange && formOnChange(res);
149
- };
150
-
151
- // 通过 formily 字符串执行的方式回去函数作用域参数
152
- target[actionName] = `{{
153
- function t() {
154
- // 执行 ${fnKey} 唯一函数,formily 当前函数作用域相关参数
155
- return _$tempData && _$tempData.${fnKey} && _$tempData.${fnKey}(
156
- { ${formilyDataKeys.join(", ")} },
157
- arguments
158
- );
159
- }
160
- }}`;
161
- };
162
-
163
- /**
164
- * 从 onChange 事件中获取数据
165
- * @param args
166
- */
167
- export const handleChangeValue = (opt) => {
168
- const { formRender, targetKey, actionName, sourceArgs } = opt || {};
169
- // 自增表格、卡片无法获知当前编辑内容所在的索引,导致无法知道当前值
170
- const res = {
171
- key: targetKey,
172
- actionName: actionName,
173
- formRender,
174
- formValues: formRender.values,
175
- sourceArgs: sourceArgs,
176
- };
177
-
178
- return res;
179
- };
180
-
181
- /**
182
- * 是否是可执行字符串,为 {{}} 包裹的字符串
183
- * @param params
184
- */
185
- export const isRunStr = (strKey) => {
186
- if (typeof strKey !== "string") {
187
- return false;
188
- }
189
- const key = strKey.trim();
190
- return key.startsWith("{{") && key.endsWith("}}");
191
- };
192
-
193
- /**
194
- * 获取可执行字符串,返回 {{}} 包裹的字符串
195
- * @param params
196
- */
197
- export const getRunStr = (strKey) => {
198
- const _isRunStr = isRunStr(strKey);
199
- if (!_isRunStr) {
200
- return "";
201
- }
202
- const key = strKey.trim();
203
- return key.replace(/^\{\{/, "").replace(/\}\}$/, "");
204
- };
205
-
206
- /**
207
- * 处理 schema 字符串值
208
- * 1.可执行字符串({{}}包裹的字符串):schema scope 取出对应的值,否则转为可执行函数
209
- * 2.其他情况直接返回对应数据
210
- * @param strKey
211
- * @param schemaScope
212
- * @returns
213
- */
214
- export const handleSchemaStrVal = (strKey, schemaScope) => {
215
- const _str = getRunStr(strKey);
216
- if (!_str) {
217
- return strKey;
218
- }
219
- let fn = getScopeKeyVal(_str, schemaScope);
220
- if (fn) {
221
- return fn;
222
- }
223
- return new Function("$root", `with($root) { return (${_str}) }`)(schemaScope);
224
- };
225
-
226
- /**
227
- * 是否是 schema scope key
228
- * @param strKey
229
- * @returns
230
- */
231
- export const isScopeKey = (strKey, schemaScope) => {
232
- const key = getRunStr(strKey);
233
- if (!schemaScope || typeof schemaScope !== "object") {
234
- throw new Error("请传入正确的 schema scope");
235
- }
236
- // 判断是否是 scope 变量名
237
- if (!schemaScope?.[key]) {
238
- return false;
239
- }
240
- return key;
241
- };
242
-
243
- /**
244
- * 获取 schema scope key
245
- * @param strKey
246
- * @returns
247
- */
248
- export const getScopeKeyVal = (strKey, schemaScope) => {
249
- if (!isScopeKey(strKey, schemaScope)) {
250
- return "";
251
- }
252
- return schemaScope[getRunStr(strKey)];
253
- };
1
+ import _ from "lodash";
2
+ import { customAlphabet } from "nanoid";
3
+
4
+ /**
5
+ * 指定 nanoid 字符集合
6
+ */
7
+ export const alphabet = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
8
+ export const nanoid = customAlphabet(alphabet, 20);
9
+
10
+ /**
11
+ * You can use the built-in context variables
12
+ *
13
+ * 1. `$self` is the current Field Model
14
+ *
15
+ * 2. `$form` is the current Form Model
16
+ *
17
+ * 3. `$deps` is the dependencies value 仅在 run 中存在
18
+ *
19
+ * 4. `$observable` function is used to create an persistent observable state object
20
+ *
21
+ * 5. `$memo` function is is used to create a persistent data
22
+ *
23
+ * 6. `$effect` function is used to handle side-effect logic
24
+ *
25
+ * 7. `$props` function is used to set component props to current field
26
+ *
27
+ * Document Links
28
+ *
29
+ * https://react.formilyjs.org/api/shared/schema#%E5%86%85%E7%BD%AE%E8%A1%A8%E8%BE%BE%E5%BC%8F%E4%BD%9C%E7%94%A8%E5%9F%9F
30
+ **/
31
+
32
+ /** formily 执行函数作用域相关参数 */
33
+ export const formilyDataKeys = ["$form", "$self", "$observable", "$effect", "$memo", "$props", "$values"];
34
+
35
+ /** 自增容器 */
36
+ export const arrayList = ["ArrayTable", "ArrayCards"];
37
+ /** 不需要绑定事件的目标 */
38
+ export const skipBindList = ["FormCollapse", "FormCollapse.CollapsePanel"];
39
+ /** change 回调返回 parent 数据的组件 */
40
+ export const returnParentValue = ["ArrayTable", "ArrayCards"];
41
+
42
+ /**
43
+ * schema 每项添加 onChange,解决无全局 onChange 问题
44
+ * @param schema
45
+ * @param onChange
46
+ * @returns
47
+ */
48
+ export const bindOnChange = (schema, opt) => {
49
+ const { schemaScope, onChange, formRender, parent, isCloneDeep = true } = opt || {};
50
+ let _schema = isCloneDeep ? _.cloneDeep(schema) : schema;
51
+ _schema = _schema.schema || _schema;
52
+ Object.keys(_schema.properties || {})?.forEach((key) => {
53
+ let item = _schema.properties[key];
54
+ if (!item) {
55
+ return;
56
+ }
57
+ const componentName = item["x-component"];
58
+ // 解决 name 丢失的问题
59
+ item.name = key;
60
+
61
+ const comProps = item?.["x-component-props"];
62
+ if (!comProps) {
63
+ item["x-component-props"] = {};
64
+ } else if (isScopeKey(comProps, schemaScope)) {
65
+ item["x-component-props"] = getScopeKeyVal(comProps, schemaScope) || {};
66
+ }
67
+
68
+ const bindParams = {
69
+ componentName,
70
+ target: item["x-component-props"],
71
+ targetKey: key,
72
+ targetId: key,
73
+ formOnChange: onChange,
74
+ schemaScope,
75
+ formRender,
76
+ parent,
77
+ };
78
+
79
+ // 不需要绑定事件的目标:折叠面板不需要设置 onChange 事件,否则会导致触发多次
80
+ if (!skipBindList.includes(componentName)) {
81
+ // 绑定新的 change 事件
82
+ bindCallback({
83
+ ...bindParams,
84
+ actionName: "onChange",
85
+ });
86
+ }
87
+
88
+ // 处理自增表格、卡片等情况
89
+ if (arrayList.includes(componentName)) {
90
+ bindOnChange(item.items, {
91
+ ...opt,
92
+ parent: item,
93
+ isCloneDeep: false,
94
+ });
95
+ // 处理点击新增、删除、排序操作
96
+ const actions = ["onAdd", "onCopy", "onRemove", "onMoveDown", "onMoveUp"];
97
+ actions.forEach((actionName) => {
98
+ bindCallback({
99
+ ...bindParams,
100
+ actionName,
101
+ });
102
+ });
103
+ }
104
+
105
+ // 处理嵌套的情况
106
+ if (item.properties) {
107
+ bindOnChange(item, {
108
+ ...opt,
109
+ isCloneDeep: false,
110
+ });
111
+ }
112
+ });
113
+ return _schema;
114
+ };
115
+
116
+ /**
117
+ * 添加回调函数
118
+ * @param target
119
+ * @param name
120
+ * @param formOnChange
121
+ */
122
+ export const bindCallback = (opt) => {
123
+ const { target, actionName, schemaScope, formOnChange } = opt || {};
124
+ const tempFn = target[actionName];
125
+ if (!schemaScope._$tempData) {
126
+ schemaScope._$tempData = {};
127
+ }
128
+ // 设置一个唯一的函数名,用于从 schema scope 传入 formily 事件执行中
129
+ const fnKey = "fnKey_" + nanoid();
130
+ // 设置 schemaScope 中的唯一函数,获取执行函数作用域 $form, $self, $observable, $effect, $memo, $props, $values 相关参数
131
+ schemaScope._$tempData[fnKey] = function (params, sourceArgs) {
132
+ const { $form, $self, $observable, $effect, $memo, $props, $values } = params;
133
+ // 处理结果数据,各组件返回数据格式不一致
134
+ const res = handleChangeValue({
135
+ ...opt,
136
+ sourceArgs,
137
+ });
138
+ // 触发组件 onChange 事件
139
+ if (isRunStr(tempFn)) {
140
+ // schemaScope 传入方式
141
+ const fn = handleSchemaStrVal(tempFn, { ...params, ...schemaScope });
142
+
143
+ typeof fn === "function" && fn(...sourceArgs, res);
144
+ } else if (typeof tempFn === "function") {
145
+ // 直接传入函数的方式
146
+ tempFn(...sourceArgs, res);
147
+ }
148
+ formOnChange && formOnChange(res);
149
+ };
150
+
151
+ // 通过 formily 字符串执行的方式回去函数作用域参数
152
+ target[actionName] = `{{
153
+ function t() {
154
+ // 执行 ${fnKey} 唯一函数,formily 当前函数作用域相关参数
155
+ return _$tempData && _$tempData.${fnKey} && _$tempData.${fnKey}(
156
+ { ${formilyDataKeys.join(", ")} },
157
+ arguments
158
+ );
159
+ }
160
+ }}`;
161
+ };
162
+
163
+ /**
164
+ * 从 onChange 事件中获取数据
165
+ * @param args
166
+ */
167
+ export const handleChangeValue = (opt) => {
168
+ const { formRender, targetKey, actionName, sourceArgs } = opt || {};
169
+ // 自增表格、卡片无法获知当前编辑内容所在的索引,导致无法知道当前值
170
+ const res = {
171
+ key: targetKey,
172
+ actionName: actionName,
173
+ formRender,
174
+ formValues: formRender.values,
175
+ sourceArgs: sourceArgs,
176
+ };
177
+
178
+ return res;
179
+ };
180
+
181
+ /**
182
+ * 是否是可执行字符串,为 {{}} 包裹的字符串
183
+ * @param params
184
+ */
185
+ export const isRunStr = (strKey) => {
186
+ if (typeof strKey !== "string") {
187
+ return false;
188
+ }
189
+ const key = strKey.trim();
190
+ return key.startsWith("{{") && key.endsWith("}}");
191
+ };
192
+
193
+ /**
194
+ * 获取可执行字符串,返回 {{}} 包裹的字符串
195
+ * @param params
196
+ */
197
+ export const getRunStr = (strKey) => {
198
+ const _isRunStr = isRunStr(strKey);
199
+ if (!_isRunStr) {
200
+ return "";
201
+ }
202
+ const key = strKey.trim();
203
+ return key.replace(/^\{\{/, "").replace(/\}\}$/, "");
204
+ };
205
+
206
+ /**
207
+ * 处理 schema 字符串值
208
+ * 1.可执行字符串({{}}包裹的字符串):schema scope 取出对应的值,否则转为可执行函数
209
+ * 2.其他情况直接返回对应数据
210
+ * @param strKey
211
+ * @param schemaScope
212
+ * @returns
213
+ */
214
+ export const handleSchemaStrVal = (strKey, schemaScope) => {
215
+ const _str = getRunStr(strKey);
216
+ if (!_str) {
217
+ return strKey;
218
+ }
219
+ let fn = getScopeKeyVal(_str, schemaScope);
220
+ if (fn) {
221
+ return fn;
222
+ }
223
+ return new Function("$root", `with($root) { return (${_str}) }`)(schemaScope);
224
+ };
225
+
226
+ /**
227
+ * 是否是 schema scope key
228
+ * @param strKey
229
+ * @returns
230
+ */
231
+ export const isScopeKey = (strKey, schemaScope) => {
232
+ const key = getRunStr(strKey);
233
+ if (!schemaScope || typeof schemaScope !== "object") {
234
+ throw new Error("请传入正确的 schema scope");
235
+ }
236
+ // 判断是否是 scope 变量名
237
+ if (!schemaScope?.[key]) {
238
+ return false;
239
+ }
240
+ return key;
241
+ };
242
+
243
+ /**
244
+ * 获取 schema scope key
245
+ * @param strKey
246
+ * @returns
247
+ */
248
+ export const getScopeKeyVal = (strKey, schemaScope) => {
249
+ if (!isScopeKey(strKey, schemaScope)) {
250
+ return "";
251
+ }
252
+ return schemaScope[getRunStr(strKey)];
253
+ };
@@ -1,68 +1,68 @@
1
- import _ from "lodash";
2
-
3
- export function mergeSchema(BaseSchema, InsideSchema, title = "") {
4
- const _schema = {
5
- form: BaseSchema.form,
6
- schema: _.cloneDeep(BaseSchema.schema),
7
- };
8
- let idx = Object.keys(_schema.schema.properties).length;
9
-
10
- if (title) {
11
- _schema.schema.properties.insideTitle = {
12
- type: "string",
13
- "x-component": "Text",
14
- "x-component-props": {
15
- content: title || "内部情况",
16
- },
17
- "x-designable-id": "insideTitle" + idx,
18
- "x-index": idx,
19
- };
20
- }
21
-
22
- Object.keys(InsideSchema.schema.properties).forEach((key) => {
23
- const obj = InsideSchema.schema.properties[key];
24
- idx += 1;
25
- obj["x-index"] = idx;
26
- _schema.schema.properties[key] = obj;
27
- });
28
- return _schema;
29
- }
30
-
31
- export function mergeSchemas(schemas) {
32
- if (!(Array.isArray(schemas) && schemas.length > 0)) {
33
- console.error("请传入 schema");
34
- return;
35
- }
36
- const _schemaList = _.cloneDeep(schemas);
37
- const _schema = {
38
- form: _schemaList[0].json.form,
39
- schema: {
40
- type: "object",
41
- properties: {},
42
- "x-designable-id": _.uniqueId("schema-merge-"),
43
- },
44
- };
45
-
46
- let idx = 0;
47
- _schemaList.forEach((schema, i) => {
48
- const titleName = `insideTitle-${i}`;
49
- _schema.schema.properties[titleName] = {
50
- type: "string",
51
- "x-component": "Text",
52
- "x-component-props": {
53
- content: schema.title,
54
- },
55
- "x-designable-id": titleName,
56
- "x-index": idx,
57
- };
58
- idx += 1;
59
- Object.keys(schema.json.schema.properties).forEach((key) => {
60
- const obj = schema.json.schema.properties[key];
61
- obj["x-index"] = idx;
62
- _schema.schema.properties[key] = obj;
63
- idx += 1;
64
- });
65
- });
66
-
67
- return _schema;
68
- }
1
+ import _ from "lodash";
2
+
3
+ export function mergeSchema(BaseSchema, InsideSchema, title = "") {
4
+ const _schema = {
5
+ form: BaseSchema.form,
6
+ schema: _.cloneDeep(BaseSchema.schema),
7
+ };
8
+ let idx = Object.keys(_schema.schema.properties).length;
9
+
10
+ if (title) {
11
+ _schema.schema.properties.insideTitle = {
12
+ type: "string",
13
+ "x-component": "Text",
14
+ "x-component-props": {
15
+ content: title || "内部情况",
16
+ },
17
+ "x-designable-id": "insideTitle" + idx,
18
+ "x-index": idx,
19
+ };
20
+ }
21
+
22
+ Object.keys(InsideSchema.schema.properties).forEach((key) => {
23
+ const obj = InsideSchema.schema.properties[key];
24
+ idx += 1;
25
+ obj["x-index"] = idx;
26
+ _schema.schema.properties[key] = obj;
27
+ });
28
+ return _schema;
29
+ }
30
+
31
+ export function mergeSchemas(schemas) {
32
+ if (!(Array.isArray(schemas) && schemas.length > 0)) {
33
+ console.error("请传入 schema");
34
+ return;
35
+ }
36
+ const _schemaList = _.cloneDeep(schemas);
37
+ const _schema = {
38
+ form: _schemaList[0].json.form,
39
+ schema: {
40
+ type: "object",
41
+ properties: {},
42
+ "x-designable-id": _.uniqueId("schema-merge-"),
43
+ },
44
+ };
45
+
46
+ let idx = 0;
47
+ _schemaList.forEach((schema, i) => {
48
+ const titleName = `insideTitle-${i}`;
49
+ _schema.schema.properties[titleName] = {
50
+ type: "string",
51
+ "x-component": "Text",
52
+ "x-component-props": {
53
+ content: schema.title,
54
+ },
55
+ "x-designable-id": titleName,
56
+ "x-index": idx,
57
+ };
58
+ idx += 1;
59
+ Object.keys(schema.json.schema.properties).forEach((key) => {
60
+ const obj = schema.json.schema.properties[key];
61
+ obj["x-index"] = idx;
62
+ _schema.schema.properties[key] = obj;
63
+ idx += 1;
64
+ });
65
+ });
66
+
67
+ return _schema;
68
+ }