@cicctencent/low-code-simulate-base 0.0.0-stage → 1.1.10
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 +389 -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 +490 -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 +1369 -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 +45 -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,241 @@
|
|
|
1
|
+
import { Component, ComponentItem, ComponentType } from '@cicctencent/low-code-core';
|
|
2
|
+
|
|
3
|
+
import { getSchemaObjectAttribute } from '../utils';
|
|
4
|
+
import { cloneDeep, merge, get, set } from '@cicctencent/ark-util';
|
|
5
|
+
import { transformSchemaToForm } from '../utils/transform';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @public
|
|
9
|
+
*/
|
|
10
|
+
export default class ComponentManager {
|
|
11
|
+
private static instance: ComponentManager;
|
|
12
|
+
/**全局最大Id,从0开始 */
|
|
13
|
+
private maxId: number = 0;
|
|
14
|
+
/**组件最大ID映射 ,从0开始*/
|
|
15
|
+
private componentMaxIdMap: { [key: string]: number } = {};
|
|
16
|
+
/**组件个数统计,从1开始 */
|
|
17
|
+
private componentCountMap: { [key: string]: number } = {};
|
|
18
|
+
|
|
19
|
+
// 这里的schema需要读取配置文件
|
|
20
|
+
private static componentSchemaMap = {} as Record<string, Component>;
|
|
21
|
+
|
|
22
|
+
// 私有构造函数确保外部代码不能使用new操作符创建新的实例
|
|
23
|
+
private constructor() {}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* @public
|
|
27
|
+
* 提供一个公共的静态方法来获取单例实例
|
|
28
|
+
* @param componentConfigs - 组件配置
|
|
29
|
+
* @returns
|
|
30
|
+
*/
|
|
31
|
+
public static getInstance(componentConfigs?: Component[]): ComponentManager {
|
|
32
|
+
if (!ComponentManager.instance) {
|
|
33
|
+
ComponentManager.instance = new ComponentManager();
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
if (componentConfigs) {
|
|
37
|
+
for (let item of componentConfigs) {
|
|
38
|
+
ComponentManager.componentSchemaMap[item.name] = item;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return ComponentManager.instance;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* @public
|
|
46
|
+
* @param name - 组件名
|
|
47
|
+
* @returns
|
|
48
|
+
*/
|
|
49
|
+
public get(name: string) {
|
|
50
|
+
return ComponentManager.componentSchemaMap[name];
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* @public
|
|
54
|
+
* 初始化
|
|
55
|
+
* @param components - 组件列表
|
|
56
|
+
*/
|
|
57
|
+
public initComponens(components: ComponentItem[]): void {
|
|
58
|
+
this.updateComponentMaps(components);
|
|
59
|
+
this.maxId = Math.max(0, ...this.updateComponentMaps(components));
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* @public
|
|
64
|
+
* @returns
|
|
65
|
+
*/
|
|
66
|
+
public getImxId() {
|
|
67
|
+
return this.maxId;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// 创建id
|
|
71
|
+
/**
|
|
72
|
+
* @public
|
|
73
|
+
* @param name - 组件名
|
|
74
|
+
* @returns
|
|
75
|
+
*/
|
|
76
|
+
private createId(name: string) {
|
|
77
|
+
++this.maxId;
|
|
78
|
+
this.componentMaxIdMap[name] = (this.componentMaxIdMap[name] || 0) + 1;
|
|
79
|
+
this.componentCountMap[name] = (this.componentCountMap[name] || 0) + 1;
|
|
80
|
+
// 先加后用
|
|
81
|
+
return {
|
|
82
|
+
componentId: this.maxId,
|
|
83
|
+
nameId: this.componentMaxIdMap[name],
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* @public
|
|
89
|
+
* 插入、点击或复制时创建新的组件数据
|
|
90
|
+
* @param item - 组件信息
|
|
91
|
+
* @returns
|
|
92
|
+
*/
|
|
93
|
+
public buildItemData(item: ComponentItem | Component): ComponentItem {
|
|
94
|
+
if ('componentId' in item) {
|
|
95
|
+
// 已经初始化过的直接返回
|
|
96
|
+
return item;
|
|
97
|
+
}
|
|
98
|
+
const schema = this.get(item.name);
|
|
99
|
+
const originTitle = schema.title;
|
|
100
|
+
const { componentId, nameId } = this.createId(item.name);
|
|
101
|
+
|
|
102
|
+
const formData: any =
|
|
103
|
+
'formData' in item
|
|
104
|
+
? this.fillFieldData(
|
|
105
|
+
cloneDeep((item as ComponentItem).formData),
|
|
106
|
+
Object.assign({}, item, { componentId })
|
|
107
|
+
)
|
|
108
|
+
: this.fillFieldData({}, Object.assign({}, item, { componentId }));
|
|
109
|
+
const itemData: ComponentItem = {
|
|
110
|
+
title: originTitle + (nameId === 1 ? '' : nameId),
|
|
111
|
+
name: schema.name,
|
|
112
|
+
icon: schema.icon,
|
|
113
|
+
// picture: item.picture,
|
|
114
|
+
isNoLogin: schema.isNoLogin ?? false,
|
|
115
|
+
// group: item.group,
|
|
116
|
+
// 这里去掉了config,渲染数据不需要包含config
|
|
117
|
+
type: schema.type,
|
|
118
|
+
componentId,
|
|
119
|
+
// 这里的对象都要cloneDeep,避免影响原数据
|
|
120
|
+
formData,
|
|
121
|
+
// 是否为不可移动元素
|
|
122
|
+
immobile: schema.immobile,
|
|
123
|
+
};
|
|
124
|
+
if ('children' in item || item.type === ComponentType.Container) {
|
|
125
|
+
itemData.children =
|
|
126
|
+
'children' in item ? (item as ComponentItem).children : [];
|
|
127
|
+
}
|
|
128
|
+
return itemData;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* @public
|
|
133
|
+
* 复制组件数据
|
|
134
|
+
* @param component - 组件
|
|
135
|
+
* @returns
|
|
136
|
+
*/
|
|
137
|
+
public copy(component: ComponentItem): ComponentItem {
|
|
138
|
+
const formData = cloneDeep(component?.formData);
|
|
139
|
+
const copyClearPaths = this.get(component.name)?.copyClearPaths;
|
|
140
|
+
if (Array.isArray(copyClearPaths)) {
|
|
141
|
+
for (const path of copyClearPaths) {
|
|
142
|
+
try {
|
|
143
|
+
const propValue = get(formData, path);
|
|
144
|
+
// 找不到说明未配置该数据 不必再清除
|
|
145
|
+
if (propValue === undefined) continue;
|
|
146
|
+
// 属性名
|
|
147
|
+
const propName = path.match(/\.(\w+)$/)?.[1];
|
|
148
|
+
const config = getSchemaObjectAttribute(
|
|
149
|
+
this.get(component.name).config as any,
|
|
150
|
+
propName as any
|
|
151
|
+
);
|
|
152
|
+
// 不同类型对应清空后的值
|
|
153
|
+
const typeToDefault = {
|
|
154
|
+
object: {},
|
|
155
|
+
array: [],
|
|
156
|
+
default: null,
|
|
157
|
+
};
|
|
158
|
+
// 默认值
|
|
159
|
+
const defaultValue = config?.default;
|
|
160
|
+
const value =
|
|
161
|
+
defaultValue ?? typeToDefault[config.type] ?? typeToDefault.default;
|
|
162
|
+
set(formData, path, value);
|
|
163
|
+
} catch (error) {
|
|
164
|
+
console.error('clear path error', error);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
const newComponent: Omit<ComponentItem, 'componentId'> = {
|
|
169
|
+
...this.get(component.name),
|
|
170
|
+
formData,
|
|
171
|
+
};
|
|
172
|
+
if (component.children && component.children.length) {
|
|
173
|
+
newComponent.children = (component.children as ComponentItem[]).map(c =>
|
|
174
|
+
this.copy(c)
|
|
175
|
+
);
|
|
176
|
+
}
|
|
177
|
+
return this.buildItemData(newComponent);
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* @public
|
|
181
|
+
* 填充数据
|
|
182
|
+
*/
|
|
183
|
+
private fillFieldData(data, componentConfig) {
|
|
184
|
+
const config = this.get(componentConfig.name).config || {};
|
|
185
|
+
return merge(transformSchemaToForm(config, componentConfig), data);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* @public
|
|
190
|
+
* 删除
|
|
191
|
+
* @param name - 组件名
|
|
192
|
+
*/
|
|
193
|
+
public delete(name: string) {
|
|
194
|
+
const count = (this.componentCountMap[name] =
|
|
195
|
+
(this.componentCountMap[name] || 0) - 1);
|
|
196
|
+
this.componentCountMap[name] = count;
|
|
197
|
+
if (count === 0) {
|
|
198
|
+
this.componentMaxIdMap[name] = 0;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* @public
|
|
203
|
+
* 其他方法,例如更新maxId和映射
|
|
204
|
+
* @param components - 组件列表
|
|
205
|
+
* @param componentIds - 组件id列表
|
|
206
|
+
* @returns
|
|
207
|
+
*/
|
|
208
|
+
private updateComponentMaps(
|
|
209
|
+
components: ComponentItem[],
|
|
210
|
+
componentIds: number[] = []
|
|
211
|
+
): number[] {
|
|
212
|
+
for (const component of components) {
|
|
213
|
+
const maxNumber: string = (component.title?.match(/\d+$/) || [])[0] || '';
|
|
214
|
+
const currentMax = this.componentMaxIdMap[component.name] ?? 1;
|
|
215
|
+
this.componentMaxIdMap[component.name] = Math.max(
|
|
216
|
+
currentMax,
|
|
217
|
+
parseInt(maxNumber, 10) || 0
|
|
218
|
+
);
|
|
219
|
+
|
|
220
|
+
this.componentCountMap[component.name] =
|
|
221
|
+
(this.componentCountMap[component.name] ?? 0) + 1;
|
|
222
|
+
|
|
223
|
+
componentIds.push(component.componentId);
|
|
224
|
+
|
|
225
|
+
if (component.children?.length) {
|
|
226
|
+
this.updateComponentMaps(
|
|
227
|
+
component.children as ComponentItem[],
|
|
228
|
+
componentIds
|
|
229
|
+
);
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
return componentIds;
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
// // 使用单例
|
|
237
|
+
// const manager = ComponentManager.getInstance();
|
|
238
|
+
// manager.initComponens(components);
|
|
239
|
+
// console.log(manager.maxId);
|
|
240
|
+
// console.log(manager.componentMaxIdMap);
|
|
241
|
+
// console.log(manager.componentCountMap);
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
/**数据历史管理 */
|
|
2
|
+
import { cloneDeep, isEqual } from '@cicctencent/ark-util';
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* DataHistoryDomain
|
|
7
|
+
* @public
|
|
8
|
+
* @typeParam T - 类型参数
|
|
9
|
+
*/
|
|
10
|
+
export default class DataHistoryDomain<T> {
|
|
11
|
+
/**
|
|
12
|
+
* 项目历史数据
|
|
13
|
+
* @public
|
|
14
|
+
*/
|
|
15
|
+
historyList: Array<T> = [];
|
|
16
|
+
/**
|
|
17
|
+
* 历史数据指针
|
|
18
|
+
* @public
|
|
19
|
+
*/
|
|
20
|
+
historyIndex: number = -1;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* @public
|
|
24
|
+
*/
|
|
25
|
+
get data() {
|
|
26
|
+
return this.historyList[this.historyIndex];
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* @public
|
|
32
|
+
* 添加历史记录
|
|
33
|
+
*/
|
|
34
|
+
add(data: T) {
|
|
35
|
+
if (isEqual(this.data, data)) {
|
|
36
|
+
// 相等则直接返回
|
|
37
|
+
return false;
|
|
38
|
+
}
|
|
39
|
+
if (this.historyIndex < this.historyList.length - 1) {
|
|
40
|
+
// 撤销后再编辑,去掉后面的重做队列
|
|
41
|
+
this.historyList.splice(this.historyIndex + 1);
|
|
42
|
+
}
|
|
43
|
+
this.historyList = this.historyList.concat(cloneDeep(data));
|
|
44
|
+
// 虽然this.historyIndex++也可以,以下更稳
|
|
45
|
+
this.historyIndex = this.historyList.length - 1;
|
|
46
|
+
|
|
47
|
+
console.log('historyIndex----------', this.historyIndex, this.historyList);
|
|
48
|
+
return true;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* @public
|
|
52
|
+
* 更新
|
|
53
|
+
*/
|
|
54
|
+
update(data: T) {
|
|
55
|
+
if (this.historyIndex < 0) {
|
|
56
|
+
this.historyIndex = 0;
|
|
57
|
+
}
|
|
58
|
+
this.historyList[this.historyIndex] = cloneDeep(data);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* @public
|
|
63
|
+
*/
|
|
64
|
+
get canUndo() {
|
|
65
|
+
// 考虑到第1个数据为初始化数据。初始数据不可被撤销
|
|
66
|
+
return this.historyList.length > 1 && this.historyIndex > 0;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* @public
|
|
71
|
+
*/
|
|
72
|
+
get canRedo() {
|
|
73
|
+
return this.historyIndex < this.historyList.length - 1;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* @public
|
|
77
|
+
* @returns
|
|
78
|
+
*/
|
|
79
|
+
undo() {
|
|
80
|
+
if (this.canUndo) {
|
|
81
|
+
return this.historyList[--this.historyIndex];
|
|
82
|
+
}
|
|
83
|
+
return false;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* @public
|
|
87
|
+
* @returns
|
|
88
|
+
*/
|
|
89
|
+
redo() {
|
|
90
|
+
if (this.canRedo) {
|
|
91
|
+
return this.historyList[++this.historyIndex];
|
|
92
|
+
}
|
|
93
|
+
return false;
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* @public
|
|
97
|
+
*/
|
|
98
|
+
reset() {
|
|
99
|
+
this.historyList = [];
|
|
100
|
+
this.historyIndex = -1;
|
|
101
|
+
}
|
|
102
|
+
}
|