@cicctencent/low-code-simulate-base 0.0.0-stage → 1.1.9-beta.5
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 +1 -3
- package/__tests__/plugin.spec.ts +40 -0
- package/__tests__/utils.transform.test.ts +37 -0
- package/api-extractor.json +36 -0
- package/build/md.js +25 -0
- package/dist/VCComponent.d.ts +5 -0
- package/dist/VCComponent.js +91 -0
- package/dist/domain/BaseProjectDomain.d.ts +349 -0
- package/dist/domain/BaseProjectDomain.js +1305 -0
- package/dist/domain/ComponentManager.d.ts +78 -0
- package/dist/domain/ComponentManager.js +293 -0
- package/dist/domain/DataHistoryDomain.d.ts +53 -0
- package/dist/domain/DataHistoryDomain.js +110 -0
- package/dist/domain/PreviewDomain.d.ts +208 -0
- package/dist/domain/PreviewDomain.js +492 -0
- package/dist/domain/ProjectDomain.d.ts +209 -0
- package/dist/domain/ProjectDomain.js +138 -0
- package/dist/event/generateEventHandler.d.ts +21 -0
- package/dist/event/generateEventHandler.js +218 -0
- package/dist/event/generateEventSchema.d.ts +123 -0
- package/dist/event/generateEventSchema.js +342 -0
- package/dist/event/register.d.ts +56 -0
- package/dist/event/register.js +207 -0
- package/dist/index.d.ts +1330 -0
- package/dist/index.js +28 -0
- package/dist/plugin/onH5PageValidate.d.ts +7 -0
- package/dist/plugin/onH5PageValidate.js +178 -0
- package/dist/plugin/onPlaceholder.d.ts +6 -0
- package/dist/plugin/onPlaceholder.js +68 -0
- package/dist/plugin/subscribeInsertUploadList.d.ts +6 -0
- package/dist/plugin/subscribeInsertUploadList.js +49 -0
- package/dist/plugin/subscribeSetTitle.d.ts +6 -0
- package/dist/plugin/subscribeSetTitle.js +16 -0
- package/dist/plugin/subscribeTransformComponentMeta.d.ts +14 -0
- package/dist/plugin/subscribeTransformComponentMeta.js +31 -0
- package/dist/plugin/subscribeValidateField.d.ts +7 -0
- package/dist/plugin/subscribeValidateField.js +57 -0
- package/dist/plugin/subscribeValidateUpload.d.ts +6 -0
- package/dist/plugin/subscribeValidateUpload.js +92 -0
- package/dist/utils/base64.d.ts +14 -0
- package/dist/utils/base64.js +14 -0
- package/dist/utils/buildInVariable.d.ts +61 -0
- package/dist/utils/buildInVariable.js +216 -0
- package/dist/utils/dom.d.ts +14 -0
- package/dist/utils/dom.js +19 -0
- package/dist/utils/index.d.ts +55 -0
- package/dist/utils/index.js +201 -0
- package/dist/utils/transform.d.ts +39 -0
- package/dist/utils/transform.js +191 -0
- package/dist/utils/validate.d.ts +7 -0
- package/dist/utils/validate.js +18 -0
- package/docs/.api/low-code-simulate-base.api.json +8450 -0
- package/jest.config.ts +20 -0
- package/package.json +44 -4
- package/src/VCComponent.ts +118 -0
- package/src/domain/BaseProjectDomain.ts +1341 -0
- package/src/domain/ComponentManager.ts +241 -0
- package/src/domain/DataHistoryDomain.ts +102 -0
- package/src/domain/PreviewDomain.ts +424 -0
- package/src/domain/ProjectDomain.ts +244 -0
- package/src/event/generateEventHandler.ts +162 -0
- package/src/event/generateEventSchema.ts +371 -0
- package/src/event/register.ts +181 -0
- package/src/index.ts +61 -0
- package/src/plugin/onH5PageValidate.ts +179 -0
- package/src/plugin/onPlaceholder.ts +71 -0
- package/src/plugin/subscribeInsertUploadList.ts +51 -0
- package/src/plugin/subscribeSetTitle.ts +18 -0
- package/src/plugin/subscribeTransformComponentMeta.ts +38 -0
- package/src/plugin/subscribeValidateField.ts +65 -0
- package/src/plugin/subscribeValidateUpload.ts +116 -0
- package/src/utils/base64.ts +17 -0
- package/src/utils/buildInVariable.ts +157 -0
- package/src/utils/dom.ts +30 -0
- package/src/utils/index.ts +194 -0
- package/src/utils/transform.ts +171 -0
- package/src/utils/validate.ts +19 -0
- package/temp/low-code-simulate-base.api.md +597 -0
- package/test/plugin.test.ts +4 -0
- package/tsconfig.build.json +9 -0
- package/tsconfig.json +9 -0
- package/tsconfig.test.json +14 -0
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import { reactive } from 'vue-demi';
|
|
2
|
+
import { decodeContent, runScript } from 'jm-jsruntime';
|
|
3
|
+
import { get, getLog, set } from '@jt/ark-util';
|
|
4
|
+
import { getParent } from './dom';
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
const initBuildInVariable = {
|
|
8
|
+
$request: null,
|
|
9
|
+
$monitor: null, // 监控上报
|
|
10
|
+
$user: null, // 后期设置
|
|
11
|
+
$state: null, // 数据源
|
|
12
|
+
$form: reactive({}),
|
|
13
|
+
$refs: reactive({}), // 组件的$refs
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
// 确保全局唯一
|
|
17
|
+
const buildInVariable = (window['buildInVariable'] =
|
|
18
|
+
window['buildInVariable'] || initBuildInVariable);
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* @public
|
|
22
|
+
* @returns
|
|
23
|
+
*/
|
|
24
|
+
export function genRandomName() {
|
|
25
|
+
return 'u' + Date.now().toString(36) + (Math.random().toString(36).substring(2, 3));
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* @public
|
|
30
|
+
* @param inputEle - inputEle
|
|
31
|
+
* @returns
|
|
32
|
+
*/
|
|
33
|
+
export function getFormName(inputEle: HTMLElement) {
|
|
34
|
+
const formEle = getParent(inputEle, (parentElement: HTMLElement) => !!parentElement.dataset?.['formName']);
|
|
35
|
+
return formEle?.dataset?.['formName'] || '$default';
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// const buildInVariable = initBuildInVariable;
|
|
39
|
+
/**
|
|
40
|
+
* @public
|
|
41
|
+
* @returns
|
|
42
|
+
*/
|
|
43
|
+
export function getBuildInVariable() {
|
|
44
|
+
return buildInVariable;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* @public
|
|
48
|
+
* @param path - 路径
|
|
49
|
+
* @returns
|
|
50
|
+
*/
|
|
51
|
+
export function getBuildInVariableByPath(path) {
|
|
52
|
+
return get(buildInVariable, path);
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
*
|
|
56
|
+
* @public
|
|
57
|
+
* @param path - 路径
|
|
58
|
+
* @param value - 值
|
|
59
|
+
*/
|
|
60
|
+
export function setBuildInVariable(path: string, value) {
|
|
61
|
+
set(buildInVariable, path, value);
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* @public
|
|
65
|
+
* @param path - 路径
|
|
66
|
+
* @param value - 值
|
|
67
|
+
*/
|
|
68
|
+
export function setBuildInForm(path: string, value) {
|
|
69
|
+
setBuildInVariable(`$form.${path}`, value);
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* @public
|
|
73
|
+
* @param path - 路径
|
|
74
|
+
* @returns
|
|
75
|
+
*/
|
|
76
|
+
export function getBuildInForm(path: string) {
|
|
77
|
+
return getBuildInVariableByPath(`$form.${path}`);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* @public
|
|
82
|
+
* @param key - key
|
|
83
|
+
* @param value - value
|
|
84
|
+
*/
|
|
85
|
+
export function setBuildInState(key, value) {
|
|
86
|
+
if (buildInVariable.$state === null) {
|
|
87
|
+
buildInVariable.$state = {};
|
|
88
|
+
}
|
|
89
|
+
buildInVariable.$state[key] = value;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* @public
|
|
94
|
+
* @param code - code
|
|
95
|
+
* @param context - context
|
|
96
|
+
* @returns
|
|
97
|
+
*/
|
|
98
|
+
export async function queryExe(code, context?) {
|
|
99
|
+
try {
|
|
100
|
+
let queryParam = undefined;
|
|
101
|
+
let dsl = code || '';
|
|
102
|
+
// 判断是否含有模板字符串
|
|
103
|
+
if (/\$\{([\s\S]*)\}/g.test(dsl.trim())) {
|
|
104
|
+
queryParam = await decodeContent(dsl, {
|
|
105
|
+
params: context || buildInVariable,
|
|
106
|
+
promise: true,
|
|
107
|
+
});
|
|
108
|
+
// 如果是对象形式 则转化为对象
|
|
109
|
+
if (/^\{([\s\S]*)\}$/g.test(queryParam.trim())) {
|
|
110
|
+
queryParam = JSON.parse(queryParam);
|
|
111
|
+
}
|
|
112
|
+
} else {
|
|
113
|
+
// 如果是对象形式 则添加return
|
|
114
|
+
if (/^\{([\s\S]*)\}$/g.test(dsl.trim())) {
|
|
115
|
+
dsl = `return ${dsl.trim()}`;
|
|
116
|
+
}
|
|
117
|
+
// 如果有return 则当作脚本执行
|
|
118
|
+
if (/(\s|^)return\s+/.test(dsl)) {
|
|
119
|
+
queryParam = await runScript(dsl, {
|
|
120
|
+
params: context || buildInVariable,
|
|
121
|
+
promise: true,
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
return queryParam;
|
|
126
|
+
} catch (e) {
|
|
127
|
+
throw {
|
|
128
|
+
msg: e.message,
|
|
129
|
+
ret: -10086,
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* @public
|
|
135
|
+
* @param code - code
|
|
136
|
+
* @param context - context
|
|
137
|
+
* @returns
|
|
138
|
+
*/
|
|
139
|
+
export async function dslExe(code, context?) {
|
|
140
|
+
const logger = getLog('[dslExe]');
|
|
141
|
+
// 打日志
|
|
142
|
+
logger.info('[code]', code);
|
|
143
|
+
try {
|
|
144
|
+
const res = await queryExe(code, {
|
|
145
|
+
console: logger, // 日志
|
|
146
|
+
...(context || buildInVariable),
|
|
147
|
+
});
|
|
148
|
+
logger.info('[result]', res);
|
|
149
|
+
return res;
|
|
150
|
+
} catch (e) {
|
|
151
|
+
// 打日志
|
|
152
|
+
logger.error('[error]', e.msg);
|
|
153
|
+
throw {
|
|
154
|
+
...e,
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
}
|
package/src/utils/dom.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @public
|
|
5
|
+
* 定义一个回调函数类型,用于获取父元素。
|
|
6
|
+
* @param parentElement - 父元素的 HTMLElement。
|
|
7
|
+
* @returns 返回一个布尔值,表示是否满足条件。
|
|
8
|
+
*/
|
|
9
|
+
export type GetParentCallback = (parentElement: HTMLElement)=> boolean;
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* @public
|
|
14
|
+
* @param el - HTMLElement元素
|
|
15
|
+
* @param fun - 回调
|
|
16
|
+
* @returns
|
|
17
|
+
*/
|
|
18
|
+
export function getParent(el: HTMLElement, fun: GetParentCallback){
|
|
19
|
+
if(el && el !== document.body){
|
|
20
|
+
const parentElement = el.parentElement;
|
|
21
|
+
if(!parentElement){
|
|
22
|
+
return null;
|
|
23
|
+
}
|
|
24
|
+
if(fun(parentElement)){
|
|
25
|
+
return parentElement
|
|
26
|
+
}
|
|
27
|
+
return getParent(parentElement, fun);
|
|
28
|
+
}
|
|
29
|
+
return null;
|
|
30
|
+
}
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
ComponentItem,
|
|
3
|
+
ImageUrlOptions,
|
|
4
|
+
ImageUrlResponse,
|
|
5
|
+
} from '@jt/low-code-core';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @public
|
|
9
|
+
* 根据key获取组件value
|
|
10
|
+
* @param key - key
|
|
11
|
+
* @param componentInfo - componentInfo
|
|
12
|
+
* @returns
|
|
13
|
+
*/
|
|
14
|
+
export function getComponentKeyValue(key, componentInfo: ComponentItem) {
|
|
15
|
+
switch (key) {
|
|
16
|
+
case 'formData':
|
|
17
|
+
return componentInfo.formData;
|
|
18
|
+
case 'componentId':
|
|
19
|
+
return componentInfo.componentId;
|
|
20
|
+
case 'component':
|
|
21
|
+
return componentInfo;
|
|
22
|
+
case 'componentName':
|
|
23
|
+
return componentInfo.name;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* @public
|
|
28
|
+
* 获取图片Urls
|
|
29
|
+
* @param param0 - param0
|
|
30
|
+
* @returns
|
|
31
|
+
*/
|
|
32
|
+
export function getImageUrls({
|
|
33
|
+
data,
|
|
34
|
+
path,
|
|
35
|
+
result,
|
|
36
|
+
}: ImageUrlOptions): ImageUrlResponse[] {
|
|
37
|
+
Object.keys(data ?? {}).forEach(key => {
|
|
38
|
+
if (typeof data[key] === 'string') {
|
|
39
|
+
path.push(key);
|
|
40
|
+
const suffixName = getImageType(data[key]) as string;
|
|
41
|
+
if (['png', 'jpeg', 'jpg', 'webp'].includes(suffixName)) {
|
|
42
|
+
result.push({ path: path.join('.'), url: data[key] });
|
|
43
|
+
}
|
|
44
|
+
path.pop();
|
|
45
|
+
} else if (data[key] && Array.isArray(data[key])) {
|
|
46
|
+
path.push(key);
|
|
47
|
+
for (let i = 0; i < data[key].length; i++) {
|
|
48
|
+
path.push(`${i}`);
|
|
49
|
+
if (typeof data[key][i] === 'string') {
|
|
50
|
+
const suffixName = getImageType(data[key][i]) as string;
|
|
51
|
+
if (['png', 'jpeg', 'jpg', 'webp'].includes(suffixName)) {
|
|
52
|
+
result.push({ path: path.join('.'), url: data[key][i] });
|
|
53
|
+
}
|
|
54
|
+
} else if (
|
|
55
|
+
data[key][i] &&
|
|
56
|
+
typeof data[key][i] === 'object' &&
|
|
57
|
+
!Array.isArray(data[key][i])
|
|
58
|
+
) {
|
|
59
|
+
getImageUrls({ data: data[key][i], path, result });
|
|
60
|
+
}
|
|
61
|
+
path.pop();
|
|
62
|
+
}
|
|
63
|
+
path.pop();
|
|
64
|
+
} else if (data[key] && typeof data[key] === 'object') {
|
|
65
|
+
path.push(key);
|
|
66
|
+
getImageUrls({ data: data[key], path, result });
|
|
67
|
+
path.pop();
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
return result;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* @public
|
|
74
|
+
* 通过value获取Schema配置
|
|
75
|
+
*/
|
|
76
|
+
export function getSchemaConfigByValue(
|
|
77
|
+
config: Record<string, any>,
|
|
78
|
+
formData: any,
|
|
79
|
+
value: any
|
|
80
|
+
) {
|
|
81
|
+
// 获取formData对应value的路径
|
|
82
|
+
const paths: string[] = getFormDataPath(formData, value);
|
|
83
|
+
// 找不到路径
|
|
84
|
+
if (!paths.length) {
|
|
85
|
+
return null;
|
|
86
|
+
}
|
|
87
|
+
return getSchemaObjectAttribute(config, paths[paths.length - 1]);
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* @public
|
|
91
|
+
* @param formData - formData
|
|
92
|
+
* @param value - value
|
|
93
|
+
* @returns
|
|
94
|
+
*/
|
|
95
|
+
export function getFormDataPath(formData, value) {
|
|
96
|
+
for (let key in formData) {
|
|
97
|
+
//数组直接判断
|
|
98
|
+
if (typeof formData[key] === 'object') {
|
|
99
|
+
if (Array.isArray(formData[key]) && Array.isArray(value)) {
|
|
100
|
+
if (formData[key].length === value.length) {
|
|
101
|
+
return [key];
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
const paths = getFormDataPath(formData[key], value);
|
|
105
|
+
if (paths.length) {
|
|
106
|
+
return [key, ...paths];
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
if (formData[key] === value) {
|
|
111
|
+
return [key];
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
return [];
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* @public
|
|
119
|
+
* 获取Schema某个属性
|
|
120
|
+
* @param config - 配置
|
|
121
|
+
* @param keyword - key
|
|
122
|
+
* @returns
|
|
123
|
+
*/
|
|
124
|
+
|
|
125
|
+
export function getSchemaObjectAttribute(
|
|
126
|
+
config: Record<string, any>,
|
|
127
|
+
keyword: string
|
|
128
|
+
) {
|
|
129
|
+
// 当前对象存在properties 就去它的子集找
|
|
130
|
+
if (config && 'properties' in config) {
|
|
131
|
+
return getSchemaObjectAttribute(config?.properties, keyword);
|
|
132
|
+
}
|
|
133
|
+
const keys = config ? Object.keys(config) : [];
|
|
134
|
+
|
|
135
|
+
for (let i = 0; i < keys.length; i++) {
|
|
136
|
+
if (keys[i] === keyword) {
|
|
137
|
+
// 找到了这个属性
|
|
138
|
+
return config[keys[i]];
|
|
139
|
+
} else if (config[keys[i]]?.properties) {
|
|
140
|
+
// 当前属性不是我们要找的 判断是否存在properties
|
|
141
|
+
const res = getSchemaObjectAttribute(
|
|
142
|
+
config[keys[i]]?.properties,
|
|
143
|
+
keyword
|
|
144
|
+
);
|
|
145
|
+
if (res) {
|
|
146
|
+
return res;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
return null;
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* @public
|
|
154
|
+
* 获取文件大小字符串
|
|
155
|
+
* @param size - 大小
|
|
156
|
+
*/
|
|
157
|
+
export function getFileSizeStr(size: number) {
|
|
158
|
+
const _size = size ?? 0;
|
|
159
|
+
if (_size < 1024) {
|
|
160
|
+
return `${_size} B`;
|
|
161
|
+
} else if (_size < Math.pow(1024, 2)) {
|
|
162
|
+
return `${(_size / Math.pow(1024, 1)).toFixed(2)} KB`;
|
|
163
|
+
} else if (_size < Math.pow(1024, 3)) {
|
|
164
|
+
return `${(_size / Math.pow(1024, 2)).toFixed(2)} MB`;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* @public
|
|
170
|
+
* @param url - url
|
|
171
|
+
* @returns
|
|
172
|
+
*/
|
|
173
|
+
export function getImageType(url: string = '') {
|
|
174
|
+
return url.match(/\.([^.]+)$/)?.[1] || url.match(/^data:image\/([^;]+)/)?.[1];
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* @public
|
|
179
|
+
* @param objects - objects
|
|
180
|
+
* @param key - key
|
|
181
|
+
* @returns
|
|
182
|
+
*/
|
|
183
|
+
export function groupBy(objects, key) {
|
|
184
|
+
const result = {};
|
|
185
|
+
objects.forEach(obj => {
|
|
186
|
+
if (obj.hasOwnProperty(key)) {
|
|
187
|
+
if (!result.hasOwnProperty(obj[key])) {
|
|
188
|
+
result[obj[key]] = [];
|
|
189
|
+
}
|
|
190
|
+
result[obj[key]].push(obj);
|
|
191
|
+
}
|
|
192
|
+
});
|
|
193
|
+
return result;
|
|
194
|
+
}
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 占位词映射表 key为占位词 value为占位词对应的真实数据
|
|
3
|
+
*/
|
|
4
|
+
let flagWordMap = {};
|
|
5
|
+
/**
|
|
6
|
+
* @public
|
|
7
|
+
* 转化每个表单项的配置
|
|
8
|
+
* @param item - item
|
|
9
|
+
* @returns
|
|
10
|
+
*/
|
|
11
|
+
export function transformItem(item) {
|
|
12
|
+
const result: any = {};
|
|
13
|
+
|
|
14
|
+
// 修改或忽略原有字段
|
|
15
|
+
for (const key in item) {
|
|
16
|
+
if (key === 'properties') {
|
|
17
|
+
result[key] = transformProperties(item[key], flagWordMap);
|
|
18
|
+
} else if (key === 'items') {
|
|
19
|
+
result[key] = item[key];
|
|
20
|
+
// 处理对象数组
|
|
21
|
+
if ('properties' in item[key]) {
|
|
22
|
+
result[key].properties = transformProperties(
|
|
23
|
+
item[key].properties,
|
|
24
|
+
flagWordMap
|
|
25
|
+
);
|
|
26
|
+
}
|
|
27
|
+
} else if (key === 'x-component-props') {
|
|
28
|
+
result[key] = {};
|
|
29
|
+
// 处理x-component-props里的占位词
|
|
30
|
+
Object.keys(item[key]).forEach(prop => {
|
|
31
|
+
// 判断是否为占位词
|
|
32
|
+
if (
|
|
33
|
+
typeof item[key][prop] === 'string' &&
|
|
34
|
+
item[key][prop].startsWith('$')
|
|
35
|
+
) {
|
|
36
|
+
// 若映射表里有占位词对应的数据 则替换 否则隐藏这一项
|
|
37
|
+
if (Object.keys(flagWordMap).includes(item[key][prop])) {
|
|
38
|
+
result[key][prop] = flagWordMap[`${item[key][prop]}`];
|
|
39
|
+
} else {
|
|
40
|
+
result['x-hidden'] = true;
|
|
41
|
+
}
|
|
42
|
+
} else {
|
|
43
|
+
result[key][prop] = item[key][prop];
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
} else {
|
|
47
|
+
// 目前除了配置项中properties属性 所有字段都保持不动
|
|
48
|
+
result[key] = item[key];
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// 修改转化后的字段或添加新字段
|
|
53
|
+
|
|
54
|
+
if (result['x-component'] === 'Select') {
|
|
55
|
+
// 占位词替换
|
|
56
|
+
if (typeof result['enum'] === 'string' && result['enum'].startsWith('$')) {
|
|
57
|
+
if (Object.keys(flagWordMap).includes(result['enum'])) {
|
|
58
|
+
result['enum'] = flagWordMap[`${item['enum']}`];
|
|
59
|
+
} else {
|
|
60
|
+
result['x-hidden'] = true;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return result;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* @public
|
|
69
|
+
* 转化全部配置(formily的业务相关转化逻辑搬过来后使用)
|
|
70
|
+
* @param config - schema配置
|
|
71
|
+
* @param map - 占位词映射表
|
|
72
|
+
* @returns
|
|
73
|
+
*/
|
|
74
|
+
export function transform(config, map) {
|
|
75
|
+
if (!config) {
|
|
76
|
+
return {};
|
|
77
|
+
}
|
|
78
|
+
flagWordMap = map;
|
|
79
|
+
|
|
80
|
+
const result: any = {};
|
|
81
|
+
Object.keys(config).forEach(key => {
|
|
82
|
+
result[key] = transformItem(config[key]);
|
|
83
|
+
});
|
|
84
|
+
return result;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
*
|
|
88
|
+
* @public
|
|
89
|
+
* @param schema - schema
|
|
90
|
+
* @param fillData - fillData
|
|
91
|
+
* @returns
|
|
92
|
+
*/
|
|
93
|
+
export function transformSchemaToForm(schema: Record<string, any>, fillData) {
|
|
94
|
+
try {
|
|
95
|
+
let data = {};
|
|
96
|
+
Object.keys(schema).forEach(key => {
|
|
97
|
+
if ('properties' in schema[key] && schema[key]?.type === 'object') {
|
|
98
|
+
const result = transformSchemaToForm(schema[key].properties, fillData);
|
|
99
|
+
if (Object.keys(result).length) {
|
|
100
|
+
data[key] = result;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// void类型
|
|
105
|
+
if ('properties' in schema[key] && schema[key]?.type === 'void') {
|
|
106
|
+
Object.keys(schema[key].properties || {}).forEach(it => {
|
|
107
|
+
const result = transformSchemaToForm(
|
|
108
|
+
schema[key].properties,
|
|
109
|
+
fillData
|
|
110
|
+
);
|
|
111
|
+
Object.assign(data, result);
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
// 填充值
|
|
115
|
+
const xData = schema[key]?.['x-data'] || {};
|
|
116
|
+
if ('autoFillFieldData' in xData) {
|
|
117
|
+
const templateStr: string = xData.autoFillFieldData || '';
|
|
118
|
+
const value = templateStr.replace(/\${{(.*?)}}/g, ($1, $2) => {
|
|
119
|
+
console.log('autoFillFieldData $1', $1);
|
|
120
|
+
return fillData[$2];
|
|
121
|
+
});
|
|
122
|
+
data[key] = value;
|
|
123
|
+
}
|
|
124
|
+
// 默认值
|
|
125
|
+
if (schema[key].default) {
|
|
126
|
+
data[key] = schema[key].default;
|
|
127
|
+
}
|
|
128
|
+
});
|
|
129
|
+
return data;
|
|
130
|
+
} catch (error) {
|
|
131
|
+
console.log('transformSchemaToForm error', String(error));
|
|
132
|
+
return {};
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* @public
|
|
138
|
+
* 转化properties对象
|
|
139
|
+
* @param properties - properties
|
|
140
|
+
* @returns
|
|
141
|
+
*/
|
|
142
|
+
export function transformProperties(properties, map?) {
|
|
143
|
+
flagWordMap = map;
|
|
144
|
+
const result: any = {};
|
|
145
|
+
for (const key in properties) {
|
|
146
|
+
result[key] = transformItem(properties[key]);
|
|
147
|
+
}
|
|
148
|
+
return result;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* @public
|
|
153
|
+
* 获取遍历对象的path数组
|
|
154
|
+
* @param obj - 对象
|
|
155
|
+
* @param prefix - 前缀,父路径
|
|
156
|
+
* @param visited - 用于跟踪已经访问过的对象,避免死循环
|
|
157
|
+
* @returns
|
|
158
|
+
*/
|
|
159
|
+
export function getObjectPath(obj, prefix = '', visited = new Set()) {
|
|
160
|
+
if (!obj || typeof obj !== 'object' || visited.has(obj)) {
|
|
161
|
+
return [];
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
visited.add(obj);
|
|
165
|
+
|
|
166
|
+
return Object.keys(obj).reduce((acc, key) => {
|
|
167
|
+
const currentPath = `${prefix}${key}`;
|
|
168
|
+
const childPaths = getObjectPath(obj[key], `${currentPath}.`, visited);
|
|
169
|
+
return acc.concat(currentPath, ...childPaths);
|
|
170
|
+
}, []);
|
|
171
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { cloneDeep } from '@jt/ark-util';
|
|
2
|
+
import { FormilyValidate } from '@jt/low-code-validate';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @public
|
|
6
|
+
* @param component - 组件配置
|
|
7
|
+
* @param validateData - 校验数据
|
|
8
|
+
* @returns
|
|
9
|
+
*/
|
|
10
|
+
export function formilyValidate(component: any, validateData: any[]) {
|
|
11
|
+
const data = cloneDeep(component?.formData || {});
|
|
12
|
+
const config = cloneDeep(component?.config || {});
|
|
13
|
+
const formilyValidate = new FormilyValidate({
|
|
14
|
+
data,
|
|
15
|
+
schema: config,
|
|
16
|
+
validateData,
|
|
17
|
+
});
|
|
18
|
+
return formilyValidate?.validate();
|
|
19
|
+
}
|