@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,162 @@
|
|
|
1
|
+
import compose from 'koa-compose';
|
|
2
|
+
import type {
|
|
3
|
+
IEventDescriptor,
|
|
4
|
+
IConfiguredEvent,
|
|
5
|
+
Middleware,
|
|
6
|
+
Next,
|
|
7
|
+
IConfiguredMiddleware,
|
|
8
|
+
} from '@jt/low-code-core';
|
|
9
|
+
import {
|
|
10
|
+
eventRegistry,
|
|
11
|
+
eventMiddlewareRegistry,
|
|
12
|
+
nativeEventTypeRegistry,
|
|
13
|
+
} from './register';
|
|
14
|
+
|
|
15
|
+
type PlainObject<T = any> = { [key: string]: T };
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* 关键:返回事件处理,供组件调用
|
|
19
|
+
* @public
|
|
20
|
+
* @param VCComponentInstance - 组件实例
|
|
21
|
+
* @param events - 事件列表list
|
|
22
|
+
* @returns
|
|
23
|
+
*/
|
|
24
|
+
export function generateEventHandlers(
|
|
25
|
+
VCComponentInstance: any,
|
|
26
|
+
events: IConfiguredEvent[]
|
|
27
|
+
) {
|
|
28
|
+
if (!Array.isArray(events)) return { on: {}, nativeOn: {} };
|
|
29
|
+
const { customEvents, nativeEvents } = transformConfiguredEvents(events);
|
|
30
|
+
return {
|
|
31
|
+
on: initEventHandlers(VCComponentInstance, customEvents),
|
|
32
|
+
nativeOn: initEventHandlers(VCComponentInstance, nativeEvents),
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* @public
|
|
38
|
+
* 初始化事件的处理函数:同一个事件可能存在多个处理函数;
|
|
39
|
+
*/
|
|
40
|
+
function initEventHandlers(
|
|
41
|
+
VCComponentInstance: any,
|
|
42
|
+
eventsMap: PlainObject<IConfiguredEvent[]>
|
|
43
|
+
) {
|
|
44
|
+
const handlers: any = {};
|
|
45
|
+
for (const name of Object.keys(eventsMap)) {
|
|
46
|
+
handlers[name] = ($event: Event) => {
|
|
47
|
+
const events = eventsMap[name];
|
|
48
|
+
for (const configuredEvent of events) {
|
|
49
|
+
executeHandler(VCComponentInstance, configuredEvent, $event);
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
return handlers;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* @public
|
|
58
|
+
* 执行处理程序:
|
|
59
|
+
* 1. 根据事件配置数据(handler),获取对应的处理程序:先取事件注册表中的handler,其次取组件methods中的handler
|
|
60
|
+
* 2. 若存在中间件配置,以中间件方式执行程序
|
|
61
|
+
* */
|
|
62
|
+
function executeHandler(
|
|
63
|
+
VCComponentInstance: any,
|
|
64
|
+
configuredEvent: IConfiguredEvent,
|
|
65
|
+
$event?: Event
|
|
66
|
+
) {
|
|
67
|
+
const renderedComponentInstance = VCComponentInstance;
|
|
68
|
+
const {
|
|
69
|
+
handler: { name: cbName, options },
|
|
70
|
+
middlewares = [],
|
|
71
|
+
} = configuredEvent;
|
|
72
|
+
let _handler: ((...args: any) => any) | null = null;
|
|
73
|
+
if (eventRegistry[cbName]) {
|
|
74
|
+
const { handler } = eventRegistry[cbName] as IEventDescriptor;
|
|
75
|
+
if (typeof handler === 'function') {
|
|
76
|
+
_handler = async () => {
|
|
77
|
+
handler(renderedComponentInstance, options, $event);
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
} else if (typeof renderedComponentInstance[cbName] === 'function') {
|
|
81
|
+
_handler = renderedComponentInstance[cbName];
|
|
82
|
+
}
|
|
83
|
+
if (_handler !== null) {
|
|
84
|
+
if (middlewares && middlewares.length > 0) {
|
|
85
|
+
executeMiddleware(
|
|
86
|
+
middlewares,
|
|
87
|
+
renderedComponentInstance,
|
|
88
|
+
_handler,
|
|
89
|
+
options
|
|
90
|
+
);
|
|
91
|
+
} else {
|
|
92
|
+
_handler(renderedComponentInstance, options, $event);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* @public
|
|
99
|
+
* 使用koa-compose执行中间件
|
|
100
|
+
* @param middlewares - 中间件列表
|
|
101
|
+
* @param context - 上下文:组件实例
|
|
102
|
+
* @param next - 调用下一个中间件函数/最终执行函数
|
|
103
|
+
* @param options - 事件配置
|
|
104
|
+
*/
|
|
105
|
+
export function executeMiddleware(
|
|
106
|
+
middlewares: IConfiguredMiddleware[],
|
|
107
|
+
context: any,
|
|
108
|
+
next: Next = () => { },
|
|
109
|
+
options = {}
|
|
110
|
+
) {
|
|
111
|
+
const middlewareFunctions: Middleware[] = [];
|
|
112
|
+
middlewares.forEach(mw => {
|
|
113
|
+
if (typeof mw === 'object' && !mw?.name) {
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
const mwName = typeof mw === 'object' ? mw.name : mw;
|
|
117
|
+
const mwOptions = typeof mw === 'object' ? mw.options : null;
|
|
118
|
+
const mwFunc = eventMiddlewareRegistry[mwName]?.middleware;
|
|
119
|
+
if (typeof mwFunc === 'function') {
|
|
120
|
+
const func = (context, next) => {
|
|
121
|
+
const contextProxy = new Proxy(context, {
|
|
122
|
+
get(target, key) {
|
|
123
|
+
if (key === 'middlewareOptions' && mwOptions) return mwOptions;
|
|
124
|
+
else if (key === 'eventOptions') return options;
|
|
125
|
+
else if (key === 'instance') return target;
|
|
126
|
+
else return Reflect.get(target, key);
|
|
127
|
+
},
|
|
128
|
+
});
|
|
129
|
+
mwFunc(contextProxy, next);
|
|
130
|
+
};
|
|
131
|
+
middlewareFunctions.push(func);
|
|
132
|
+
}
|
|
133
|
+
});
|
|
134
|
+
compose(middlewareFunctions)(context, next);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* @public
|
|
139
|
+
* 转换事件配置数据的数据结构
|
|
140
|
+
*/
|
|
141
|
+
function transformConfiguredEvents(events: IConfiguredEvent[]) {
|
|
142
|
+
const customEvents: PlainObject<IConfiguredEvent[]> = {};
|
|
143
|
+
const nativeEvents: PlainObject<IConfiguredEvent[]> = {};
|
|
144
|
+
for (const eventConfigData of events) {
|
|
145
|
+
const { type } = eventConfigData;
|
|
146
|
+
if (type === '') continue;
|
|
147
|
+
const nativeEentTypes = Object.values(nativeEventTypeRegistry).map(
|
|
148
|
+
it => it.name
|
|
149
|
+
);
|
|
150
|
+
if (nativeEentTypes.includes(type)) {
|
|
151
|
+
nativeEvents[type] = nativeEvents[type] || [];
|
|
152
|
+
nativeEvents[type].push(eventConfigData);
|
|
153
|
+
} else {
|
|
154
|
+
customEvents[type] = customEvents[type] || [];
|
|
155
|
+
customEvents[type].push(eventConfigData);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
return {
|
|
159
|
+
customEvents,
|
|
160
|
+
nativeEvents,
|
|
161
|
+
};
|
|
162
|
+
}
|
|
@@ -0,0 +1,371 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
IEventDescriptor,
|
|
3
|
+
ICustomEventDescriptor,
|
|
4
|
+
InativeEventTypeDescriptor,
|
|
5
|
+
} from '@jt/low-code-core';
|
|
6
|
+
import { UniqueType } from '@jt/low-code-core';
|
|
7
|
+
|
|
8
|
+
import {
|
|
9
|
+
eventRegistry,
|
|
10
|
+
eventMiddlewareRegistry,
|
|
11
|
+
nativeEventTypeRegistry,
|
|
12
|
+
} from './register';
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* @public
|
|
16
|
+
* 关键:初始化注册表中所有的事件的表单配置 json-schema
|
|
17
|
+
* @param extraEvent - 添加的事件类型
|
|
18
|
+
* @param ignoredEvent - 过滤的事件类型
|
|
19
|
+
* @returns
|
|
20
|
+
*/
|
|
21
|
+
export function initAllEventsFormSchema(
|
|
22
|
+
extraEvent?: InativeEventTypeDescriptor[],
|
|
23
|
+
ignoredEvent?: string[]
|
|
24
|
+
) {
|
|
25
|
+
const eventDescriptors = Object.values(eventRegistry || {});
|
|
26
|
+
return {
|
|
27
|
+
type: 'object',
|
|
28
|
+
properties: {
|
|
29
|
+
list: generateEventsFormSchema(
|
|
30
|
+
eventDescriptors,
|
|
31
|
+
extraEvent,
|
|
32
|
+
ignoredEvent
|
|
33
|
+
),
|
|
34
|
+
},
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* @public
|
|
40
|
+
* @param eventDescriptors - eventDescriptors
|
|
41
|
+
* @param extraEvent - extraEvent 扩展事件
|
|
42
|
+
* @param ignoredEvent - ignoredEvent 过滤事件
|
|
43
|
+
* @returns
|
|
44
|
+
*/
|
|
45
|
+
export function initEventsFormSchema(
|
|
46
|
+
eventDescriptors: Array<IEventDescriptor | ICustomEventDescriptor>,
|
|
47
|
+
extraEvent?: InativeEventTypeDescriptor[],
|
|
48
|
+
ignoredEvent?: string[]
|
|
49
|
+
) {
|
|
50
|
+
return {
|
|
51
|
+
type: 'object',
|
|
52
|
+
properties: {
|
|
53
|
+
list: generateEventsFormSchema(
|
|
54
|
+
eventDescriptors,
|
|
55
|
+
extraEvent,
|
|
56
|
+
ignoredEvent
|
|
57
|
+
),
|
|
58
|
+
},
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* @public
|
|
64
|
+
* 初始化事件中间件
|
|
65
|
+
* @returns
|
|
66
|
+
*/
|
|
67
|
+
export function initMiddlewareFormSchema() {
|
|
68
|
+
return {
|
|
69
|
+
type: 'object',
|
|
70
|
+
properties: {
|
|
71
|
+
middlewares: generateEventMiddlewareFormSchema(),
|
|
72
|
+
},
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* @public
|
|
78
|
+
* 生成事件json schema
|
|
79
|
+
* @param eventDescriptors - 事件信息
|
|
80
|
+
* @param ignoredEvent - 过滤事件
|
|
81
|
+
* @returns
|
|
82
|
+
*/
|
|
83
|
+
export function generateEventsFormSchema(
|
|
84
|
+
eventDescriptors: Array<IEventDescriptor | ICustomEventDescriptor>,
|
|
85
|
+
extraEvent?: InativeEventTypeDescriptor[],
|
|
86
|
+
ignoredEvent?: string[]
|
|
87
|
+
) {
|
|
88
|
+
const properties = generateEventProperties(
|
|
89
|
+
eventDescriptors,
|
|
90
|
+
extraEvent,
|
|
91
|
+
ignoredEvent
|
|
92
|
+
);
|
|
93
|
+
return {
|
|
94
|
+
type: 'array',
|
|
95
|
+
// title: '事件配置',
|
|
96
|
+
'ui:hidden': properties === null,
|
|
97
|
+
items: {
|
|
98
|
+
type: 'object',
|
|
99
|
+
required: ['type'],
|
|
100
|
+
properties,
|
|
101
|
+
},
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* @public
|
|
107
|
+
* 生成事件属性json schema
|
|
108
|
+
* @param eventDescriptors - 事件描述
|
|
109
|
+
* @param ignoredEvent - 过滤事件
|
|
110
|
+
* @returns
|
|
111
|
+
*/
|
|
112
|
+
export function generateEventProperties(
|
|
113
|
+
eventDescriptors: Array<IEventDescriptor | ICustomEventDescriptor>,
|
|
114
|
+
extraEvent?: InativeEventTypeDescriptor[],
|
|
115
|
+
ignoredEvent?: string[]
|
|
116
|
+
) {
|
|
117
|
+
if (Array.isArray(eventDescriptors) && eventDescriptors.length > 0) {
|
|
118
|
+
const targetProperties: any = {};
|
|
119
|
+
const customEventDescriptors: any[] = eventDescriptors.filter(
|
|
120
|
+
(it: any) => typeof it.handler !== 'function'
|
|
121
|
+
) as Array<ICustomEventDescriptor>;
|
|
122
|
+
const nativeEventDeclarations: any[] = Object.values(
|
|
123
|
+
nativeEventTypeRegistry
|
|
124
|
+
);
|
|
125
|
+
let types = [...nativeEventDeclarations, ...customEventDescriptors];
|
|
126
|
+
if (ignoredEvent && ignoredEvent.length) {
|
|
127
|
+
types = types.filter(type => !ignoredEvent.includes(type.name));
|
|
128
|
+
}
|
|
129
|
+
if (extraEvent && extraEvent.length) {
|
|
130
|
+
types.push(...extraEvent);
|
|
131
|
+
}
|
|
132
|
+
targetProperties['type'] = {
|
|
133
|
+
title: '事件类型',
|
|
134
|
+
enum: types.map(it => it.name),
|
|
135
|
+
enumNames: types.map(it => it.label),
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
const middlewareConf = generateEventMiddlewareFormSchema();
|
|
139
|
+
if (middlewareConf !== null) {
|
|
140
|
+
targetProperties['middlewares'] = middlewareConf;
|
|
141
|
+
}
|
|
142
|
+
// 获取name配置
|
|
143
|
+
const nameConfig = getEventHandleName(eventDescriptors);
|
|
144
|
+
// 以下是新代码
|
|
145
|
+
targetProperties['handler'] = {
|
|
146
|
+
title: '事件名称',
|
|
147
|
+
type: 'object',
|
|
148
|
+
'x-reactions': {
|
|
149
|
+
dependencies: ['type'],
|
|
150
|
+
fulfill: {
|
|
151
|
+
state: {
|
|
152
|
+
// 事件类型变化,将事件名称清空
|
|
153
|
+
value: '{{$self.value = {}}}',
|
|
154
|
+
},
|
|
155
|
+
},
|
|
156
|
+
},
|
|
157
|
+
// oneOf: options,
|
|
158
|
+
properties: {
|
|
159
|
+
name: nameConfig,
|
|
160
|
+
options: getEventOptions(eventDescriptors, nameConfig),
|
|
161
|
+
},
|
|
162
|
+
};
|
|
163
|
+
return targetProperties;
|
|
164
|
+
}
|
|
165
|
+
return null;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* @public
|
|
170
|
+
* 获取事件name
|
|
171
|
+
* @param data - 事件原始数据
|
|
172
|
+
* @returns
|
|
173
|
+
*/
|
|
174
|
+
function getEventHandleName(data) {
|
|
175
|
+
const enums = [];
|
|
176
|
+
data.forEach(it => {
|
|
177
|
+
const name = getEventNameByDescriptor(it);
|
|
178
|
+
enums.push({
|
|
179
|
+
value: name,
|
|
180
|
+
label: it.title,
|
|
181
|
+
isNoLogin: it.isNoLogin ?? false,
|
|
182
|
+
eventType: it.eventType,
|
|
183
|
+
});
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
return {
|
|
187
|
+
type: 'string',
|
|
188
|
+
title: '事件名称',
|
|
189
|
+
enum: enums,
|
|
190
|
+
required: true,
|
|
191
|
+
'x-reactions': {
|
|
192
|
+
dependencies: ['..type'],
|
|
193
|
+
fulfill: {
|
|
194
|
+
schema: {
|
|
195
|
+
'x-component-props.options': '{{filterOptions($deps[0])}}',
|
|
196
|
+
},
|
|
197
|
+
},
|
|
198
|
+
},
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* @public
|
|
204
|
+
* 获取事件options组
|
|
205
|
+
* @param data - 事件原始数据
|
|
206
|
+
* @param nameConfig - 根据name配置设置联动
|
|
207
|
+
* @returns
|
|
208
|
+
*/
|
|
209
|
+
function getEventOptions(data, nameConfig) {
|
|
210
|
+
const propertiesGroups = {};
|
|
211
|
+
data.forEach((it, index) => {
|
|
212
|
+
const desc = getEventOptionsPropertiesByDescriptor(it);
|
|
213
|
+
propertiesGroups[`${nameConfig.enum[index].value}`] = {
|
|
214
|
+
type: 'object',
|
|
215
|
+
properties: Object.assign(
|
|
216
|
+
{
|
|
217
|
+
buryReportName: {
|
|
218
|
+
default: '',
|
|
219
|
+
title: '埋点名',
|
|
220
|
+
description:
|
|
221
|
+
'输入埋点名,系统将补全埋点信息:例 运营.可视化创造.输入埋点名.点击',
|
|
222
|
+
type: 'string',
|
|
223
|
+
required: true,
|
|
224
|
+
unique: UniqueType.page,
|
|
225
|
+
'x-validator': {
|
|
226
|
+
asyncValidator: true,
|
|
227
|
+
type: 'buryReportName',
|
|
228
|
+
},
|
|
229
|
+
'x-component-props': {
|
|
230
|
+
description:
|
|
231
|
+
'埋点名称应为:大/小写字母、数字、中文、横杠、下划线及其组合且不可重复',
|
|
232
|
+
},
|
|
233
|
+
},
|
|
234
|
+
},
|
|
235
|
+
desc
|
|
236
|
+
),
|
|
237
|
+
};
|
|
238
|
+
});
|
|
239
|
+
return propertiesGroups;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* @public
|
|
244
|
+
* 生成中间件json schema
|
|
245
|
+
* @returns
|
|
246
|
+
*/
|
|
247
|
+
function generateEventMiddlewareFormSchema() {
|
|
248
|
+
const eventMiddlewareDescriptors = Object.values(eventMiddlewareRegistry);
|
|
249
|
+
if (eventMiddlewareDescriptors.length === 0) return null;
|
|
250
|
+
const middlewareDes = [];
|
|
251
|
+
return {
|
|
252
|
+
title: '前置执行程序',
|
|
253
|
+
type: 'array',
|
|
254
|
+
'x-component': 'ArrayItems',
|
|
255
|
+
'x-decorator-props': {
|
|
256
|
+
layout: 'vertical',
|
|
257
|
+
},
|
|
258
|
+
items: {
|
|
259
|
+
type: 'object',
|
|
260
|
+
properties: {
|
|
261
|
+
_btn: {
|
|
262
|
+
type: 'void',
|
|
263
|
+
'x-component': 'Row',
|
|
264
|
+
properties: {
|
|
265
|
+
remove: {
|
|
266
|
+
type: 'void',
|
|
267
|
+
'x-component': 'ArrayItems.Remove',
|
|
268
|
+
},
|
|
269
|
+
sort: {
|
|
270
|
+
type: 'void',
|
|
271
|
+
'x-component': 'ArrayItems.SortHandle',
|
|
272
|
+
},
|
|
273
|
+
},
|
|
274
|
+
},
|
|
275
|
+
name: {
|
|
276
|
+
title: '前置执行程序',
|
|
277
|
+
type: 'string',
|
|
278
|
+
'x-component-props': {
|
|
279
|
+
placeholder: '请添加(执行顺序为列表顺序,可拖动修改)',
|
|
280
|
+
},
|
|
281
|
+
enum: getMiddlewareNameConfig(eventMiddlewareDescriptors),
|
|
282
|
+
'x-reactions': '{{middlewareFilter}}',
|
|
283
|
+
},
|
|
284
|
+
options: {
|
|
285
|
+
type: 'object',
|
|
286
|
+
properties: getMiddlewareOptionsConfig(eventMiddlewareDescriptors),
|
|
287
|
+
},
|
|
288
|
+
},
|
|
289
|
+
'x-data': {
|
|
290
|
+
addButtonTitle: '添加前置执行程序配置组',
|
|
291
|
+
addButtonType: 'primary',
|
|
292
|
+
addButtonSize: 'small',
|
|
293
|
+
addButtonPlain: true,
|
|
294
|
+
},
|
|
295
|
+
},
|
|
296
|
+
};
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/**
|
|
300
|
+
* @public
|
|
301
|
+
* 获取中间件name配置
|
|
302
|
+
*/
|
|
303
|
+
function getMiddlewareNameConfig(eventMiddlewareDescriptors) {
|
|
304
|
+
return eventMiddlewareDescriptors.map(it => {
|
|
305
|
+
return {
|
|
306
|
+
label: it.title,
|
|
307
|
+
value: it.name || it.middleware.name,
|
|
308
|
+
isNoLogin: it?.isNoLogin ?? false,
|
|
309
|
+
};
|
|
310
|
+
});
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
/**
|
|
314
|
+
* @public
|
|
315
|
+
* 获取中间件options 参数
|
|
316
|
+
* @param eventMiddlewareDescriptors - 中间件内容
|
|
317
|
+
* @returns
|
|
318
|
+
*/
|
|
319
|
+
function getMiddlewareOptionsConfig(eventMiddlewareDescriptors: any[]) {
|
|
320
|
+
const res = {};
|
|
321
|
+
eventMiddlewareDescriptors.forEach((it, index) => {
|
|
322
|
+
res['_' + it.name] = {
|
|
323
|
+
type: 'void',
|
|
324
|
+
'x-reactions': {
|
|
325
|
+
dependencies: ['..name'],
|
|
326
|
+
fulfill: {
|
|
327
|
+
schema: {
|
|
328
|
+
'x-visible': `{{$deps[0] == '${it.name}' }}`,
|
|
329
|
+
},
|
|
330
|
+
},
|
|
331
|
+
},
|
|
332
|
+
properties: it.parametersDescriptor,
|
|
333
|
+
};
|
|
334
|
+
});
|
|
335
|
+
return { ...res };
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
/**
|
|
339
|
+
* @public
|
|
340
|
+
* @param descriptor - 描述
|
|
341
|
+
* @returns
|
|
342
|
+
*/
|
|
343
|
+
function getEventNameByDescriptor(
|
|
344
|
+
descriptor: IEventDescriptor | ICustomEventDescriptor | any
|
|
345
|
+
) {
|
|
346
|
+
return (
|
|
347
|
+
descriptor['name'] ||
|
|
348
|
+
(typeof descriptor['handler'] === 'string'
|
|
349
|
+
? descriptor['handler']
|
|
350
|
+
: typeof descriptor['handler'] === 'function'
|
|
351
|
+
? descriptor['handler'].name
|
|
352
|
+
: descriptor['type'])
|
|
353
|
+
);
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
/**
|
|
357
|
+
* @public
|
|
358
|
+
* @param descriptor - 描述
|
|
359
|
+
* @returns
|
|
360
|
+
*/
|
|
361
|
+
function getEventOptionsPropertiesByDescriptor(
|
|
362
|
+
descriptor: IEventDescriptor | ICustomEventDescriptor
|
|
363
|
+
) {
|
|
364
|
+
const name = getEventNameByDescriptor(descriptor);
|
|
365
|
+
return (
|
|
366
|
+
(eventRegistry &&
|
|
367
|
+
eventRegistry[name] &&
|
|
368
|
+
eventRegistry[name].parametersDescriptor) ||
|
|
369
|
+
{}
|
|
370
|
+
);
|
|
371
|
+
}
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
IEventDescriptor,
|
|
3
|
+
IEventMiddlewareDescriptor,
|
|
4
|
+
InativeEventTypeDescriptor,
|
|
5
|
+
} from '@jt/low-code-core';
|
|
6
|
+
type IValidateRules = { [key: string]: { required?: boolean; type: string } };
|
|
7
|
+
/**
|
|
8
|
+
* @public
|
|
9
|
+
* 注册表管理
|
|
10
|
+
* 1. 初始化注册表;
|
|
11
|
+
* 2. 提供注册校验功能;
|
|
12
|
+
* 3. 提供单个注册和批量注册功能。
|
|
13
|
+
*/
|
|
14
|
+
class RegistryManager<Descriptor> {
|
|
15
|
+
/**
|
|
16
|
+
* @public
|
|
17
|
+
* 注册表
|
|
18
|
+
*/
|
|
19
|
+
registry: { [key: string]: Descriptor } = {};
|
|
20
|
+
/**
|
|
21
|
+
* @public
|
|
22
|
+
* 注册的内容名称
|
|
23
|
+
*/
|
|
24
|
+
registeredContent: string = '';
|
|
25
|
+
/**
|
|
26
|
+
* @public
|
|
27
|
+
*/
|
|
28
|
+
invaildNames: string[] = [];
|
|
29
|
+
/**
|
|
30
|
+
* @public
|
|
31
|
+
*/
|
|
32
|
+
vaildateRules: IValidateRules = {};
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* @public
|
|
36
|
+
* @param content - content
|
|
37
|
+
* @param vaildateRules - vaildateRules
|
|
38
|
+
*/
|
|
39
|
+
constructor(content: string, vaildateRules: IValidateRules) {
|
|
40
|
+
this.registeredContent = content;
|
|
41
|
+
this.registry = window[`${this.registeredContent}Registry`] =
|
|
42
|
+
window[`${this.registeredContent}Registry`] || {};
|
|
43
|
+
this.vaildateRules = vaildateRules;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* @public
|
|
48
|
+
*/
|
|
49
|
+
get errMsgPrefix() {
|
|
50
|
+
return `[${this.registeredContent} ${name})registration failure]:`;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* @public
|
|
55
|
+
* 批量注册
|
|
56
|
+
*/
|
|
57
|
+
batchRegister(descriptors: Descriptor[]) {
|
|
58
|
+
for (const item of descriptors) {
|
|
59
|
+
this.register(item);
|
|
60
|
+
}
|
|
61
|
+
return this.registry;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* 注册函数:往注册表添加内容,并校验内容描述
|
|
66
|
+
* @public
|
|
67
|
+
*/
|
|
68
|
+
register(descriptor: Descriptor) {
|
|
69
|
+
if (!this.verifyDescriptor(descriptor)) return {};
|
|
70
|
+
//@ts-ignore
|
|
71
|
+
const name = descriptor['name'];
|
|
72
|
+
if (this.registry[name]) {
|
|
73
|
+
console.error(`${this.errMsgPrefix} name conflict。`);
|
|
74
|
+
return {};
|
|
75
|
+
}
|
|
76
|
+
this.registry[name] = descriptor;
|
|
77
|
+
return this.registry;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* @public
|
|
82
|
+
* 校验注册内容配置描述:
|
|
83
|
+
* 1. 检查“内容描述”是否符合规范;
|
|
84
|
+
* 2. 内容项命名不可相同
|
|
85
|
+
*/
|
|
86
|
+
verifyDescriptor(descriptor: Descriptor): boolean {
|
|
87
|
+
const verifiedMsgs: string[] = [];
|
|
88
|
+
const getTypeCheckMsg = (key: string, value: any, expectedType: string) =>
|
|
89
|
+
`Parameter '${key}' is invalid, Expected ${expectedType}, but got ${typeof value}`;
|
|
90
|
+
|
|
91
|
+
for (const [key, rule] of Object.entries(this.vaildateRules)) {
|
|
92
|
+
const { required = false, type } = rule;
|
|
93
|
+
// @ts-ignore
|
|
94
|
+
const value = descriptor[key];
|
|
95
|
+
if (typeof value !== type) {
|
|
96
|
+
verifiedMsgs.push(getTypeCheckMsg(key, value, type));
|
|
97
|
+
}
|
|
98
|
+
if (required && !value) {
|
|
99
|
+
verifiedMsgs.push(`Parameter '${key}'' is required`);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
if (verifiedMsgs.length > 0) {
|
|
104
|
+
console.error(this.errMsgPrefix, verifiedMsgs.join('. '), descriptor);
|
|
105
|
+
return false;
|
|
106
|
+
}
|
|
107
|
+
return true;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const eventRegister = new RegistryManager<IEventDescriptor>('event', {
|
|
112
|
+
handler: { required: true, type: 'function' },
|
|
113
|
+
name: { required: true, type: 'string' },
|
|
114
|
+
});
|
|
115
|
+
/**
|
|
116
|
+
* @public
|
|
117
|
+
* 事件注册表
|
|
118
|
+
*/
|
|
119
|
+
export const eventRegistry = eventRegister.registry;
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* @public
|
|
123
|
+
* 批量注册事件
|
|
124
|
+
*/
|
|
125
|
+
export const batchRegisterEvents = (descriptors: IEventDescriptor[]) =>
|
|
126
|
+
eventRegister.batchRegister(descriptors);
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* @public
|
|
130
|
+
* 注册事件:
|
|
131
|
+
*/
|
|
132
|
+
export const registerEvent = (descriptor: IEventDescriptor) =>
|
|
133
|
+
eventRegister.register(descriptor);
|
|
134
|
+
|
|
135
|
+
const middlewareRegister = new RegistryManager<IEventMiddlewareDescriptor>(
|
|
136
|
+
'middleware',
|
|
137
|
+
{
|
|
138
|
+
middleware: { required: true, type: 'function' },
|
|
139
|
+
name: { required: true, type: 'string' },
|
|
140
|
+
}
|
|
141
|
+
);
|
|
142
|
+
/**
|
|
143
|
+
* 事件中间件注册表
|
|
144
|
+
* @public
|
|
145
|
+
*/
|
|
146
|
+
export const eventMiddlewareRegistry = middlewareRegister.registry;
|
|
147
|
+
/**
|
|
148
|
+
* @public
|
|
149
|
+
* 批量注册事件中间件
|
|
150
|
+
*/
|
|
151
|
+
export const batchRegisterEventMiddlewares = (
|
|
152
|
+
descriptors: IEventMiddlewareDescriptor[]
|
|
153
|
+
) => middlewareRegister.batchRegister(descriptors);
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* 注册事件中间件:
|
|
157
|
+
* @public
|
|
158
|
+
*/
|
|
159
|
+
export const registerEventMiddleware = (
|
|
160
|
+
descriptor: IEventMiddlewareDescriptor
|
|
161
|
+
) => middlewareRegister.register(descriptor);
|
|
162
|
+
|
|
163
|
+
const nativeEventTypeRegister = new RegistryManager<InativeEventTypeDescriptor>(
|
|
164
|
+
'nativeEventType',
|
|
165
|
+
{
|
|
166
|
+
name: { required: true, type: 'string' },
|
|
167
|
+
label: { required: true, type: 'string' },
|
|
168
|
+
}
|
|
169
|
+
);
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* @public
|
|
173
|
+
*/
|
|
174
|
+
export const nativeEventTypeRegistry = nativeEventTypeRegister.registry;
|
|
175
|
+
/**
|
|
176
|
+
* @public
|
|
177
|
+
* 批量注册支持的原生事件类型
|
|
178
|
+
*/
|
|
179
|
+
export const batchRegisterNativeEventTypes = (
|
|
180
|
+
descriptors: InativeEventTypeDescriptor[]
|
|
181
|
+
) => nativeEventTypeRegister.batchRegister(descriptors);
|