forked from alibaba/lowcode-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.ts
More file actions
415 lines (395 loc) · 11.3 KB
/
config.ts
File metadata and controls
415 lines (395 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
import { ComponentType } from 'react';
import { get as lodashGet } from 'lodash';
import { isPlainObject } from '@alilc/lowcode-utils';
import { RequestHandlersMap } from '@alilc/lowcode-datasource-types';
import { getLogger } from './utils/logger';
const logger = getLogger({ level: 'log', bizName: 'config' });
// this default behavior will be different later
const STRICT_PLUGIN_MODE_DEFAULT = true;
// used in strict mode, when only options in this VALID_ENGINE_OPTIONS can be accepted
// type and description are only used for developer`s assistance, won`t affect runtime
const VALID_ENGINE_OPTIONS = {
enableCondition: {
type: 'boolean',
description: '是否开启 condition 的能力,默认在设计器中不管 condition 是啥都正常展示',
},
designMode: {
type: 'string',
enum: ['design', 'live'],
default: 'design',
description: '设计模式,live 模式将会实时展示变量值',
},
device: {
type: 'string',
enum: ['default', 'mobile', 'any string value'],
default: 'default',
description: '设备类型',
},
deviceClassName: {
type: 'string',
default: undefined,
description: '指定初始化的 deviceClassName,挂载到画布的顶层节点上',
},
locale: {
type: 'string',
default: 'zh_CN',
description: '语言',
},
renderEnv: {
type: 'string',
enum: ['react', 'rax', 'any string value'],
default: 'react',
description: '渲染器类型',
},
deviceMapper: {
type: 'object',
description: '设备类型映射器,处理设计器与渲染器中 device 的映射',
},
enableStrictPluginMode: {
type: 'boolean',
default: STRICT_PLUGIN_MODE_DEFAULT,
description: '开启严格插件模式,默认值: STRICT_PLUGIN_MODE_DEFAULT , 严格模式下,插件将无法通过engineOptions传递自定义配置项',
},
enableReactiveContainer: {
type: 'boolean',
default: false,
description: '开启拖拽组件时,即将被放入的容器是否有视觉反馈',
},
disableAutoRender: {
type: 'boolean',
default: false,
description: '关闭画布自动渲染,在资产包多重异步加载的场景有效',
},
disableDetecting: {
type: 'boolean',
default: false,
description: '关闭拖拽组件时的虚线响应,性能考虑',
},
customizeIgnoreSelectors: {
type: 'function',
default: undefined,
description: '定制画布中点击被忽略的 selectors, eg. (defaultIgnoreSelectors: string[]) => string[]',
},
disableDefaultSettingPanel: {
type: 'boolean',
default: false,
description: '禁止默认的设置面板',
},
disableDefaultSetters: {
type: 'boolean',
default: false,
description: '禁止默认的设置器',
},
enableCanvasLock: {
type: 'boolean',
default: false,
description: '打开画布的锁定操作',
},
enableLockedNodeSetting: {
type: 'boolean',
default: false,
description: '容器锁定后,容器本身是否可以设置属性,仅当画布锁定特性开启时生效',
},
stayOnTheSameSettingTab: {
type: 'boolean',
default: false,
description: '当选中节点切换时,是否停留在相同的设置 tab 上',
},
loadingComponent: {
type: 'ComponentType',
default: undefined,
description: '自定义 loading 组件',
},
supportVariableGlobally: {
type: 'boolean',
default: false,
description: '设置所有属性支持变量配置',
},
visionSettings: {
type: 'object',
description: 'Vision-polyfill settings',
},
simulatorUrl: {
type: 'array',
description: '自定义 simulatorUrl 的地址',
},
requestHandlersMap: {
type: 'object',
description: '数据源引擎的请求处理器映射',
},
};
export interface EngineOptions {
/**
* 是否开启 condition 的能力,默认在设计器中不管 condition 是啥都正常展示
*/
enableCondition?: boolean;
/**
* @todo designMode 无法映射到文档渲染模块
*
* @see https://yuque.antfin.com/ali-lowcode/docs/hk2ogo#designMode
*
* 设计模式,live 模式将会实时展示变量值,默认值:'design'
*/
designMode?: 'design' | 'live';
/**
* 设备类型,默认值:'default'
*/
device?: 'default' | 'mobile' | string;
/**
* 指定初始化的 deviceClassName,挂载到画布的顶层节点上
*/
deviceClassName?: string;
/**
* 语言,默认值:'zh_CN'
*/
locale?: string;
/**
* 渲染器类型,默认值:'react'
*/
renderEnv?: 'react' | 'rax' | string;
/**
* 设备类型映射器,处理设计器与渲染器中 device 的映射
*/
deviceMapper?: {
transform: (originalDevice: string) => string;
};
/**
* 开启严格插件模式,默认值: STRICT_PLUGIN_MODE_DEFAULT , 严格模式下,插件将无法通过engineOptions传递自定义配置项
* enable strict plugin mode, default value: false
* under strict mode, customed engineOption is not accepted.
*/
enableStrictPluginMode?: boolean;
/**
* 开启拖拽组件时,即将被放入的容器是否有视觉反馈,默认值:false
*/
enableReactiveContainer?: boolean;
/**
* 关闭画布自动渲染,在资产包多重异步加载的场景有效,默认值:false
*/
disableAutoRender?: boolean;
/**
* 关闭拖拽组件时的虚线响应,性能考虑,默认值:false
*/
disableDetecting?: boolean;
/**
* 定制画布中点击被忽略的 selectors,默认值:undefined
*/
customizeIgnoreSelectors?: (defaultIgnoreSelectors: string[]) => string[];
/**
* 禁止默认的设置面板,默认值:false
*/
disableDefaultSettingPanel?: boolean;
/**
* 禁止默认的设置器,默认值:false
*/
disableDefaultSetters?: boolean;
/**
* 打开画布的锁定操作,默认值:false
*/
enableCanvasLock?: boolean;
/**
* 容器锁定后,容器本身是否可以设置属性,仅当画布锁定特性开启时生效, 默认值为:false
*/
enableLockedNodeSetting?: boolean;
/**
* 当选中节点切换时,是否停留在相同的设置 tab 上,默认值:false
*/
stayOnTheSameSettingTab?: boolean;
/**
* 自定义 loading 组件
*/
loadingComponent?: ComponentType;
/**
* 设置所有属性支持变量配置,默认值:false
*/
supportVariableGlobally?: boolean;
/**
* 设置 simulator 相关的 url,默认值:undefined
*/
simulatorUrl?: string[];
/**
* Vision-polyfill settings
*/
visionSettings?: {
// 是否禁用降级 reducer,默认值:false
disableCompatibleReducer?: boolean;
// 是否开启在 render 阶段开启 filter reducer,默认值:false
enableFilterReducerInRenderStage?: boolean;
};
/**
* 与 react-renderer 的 appHelper 一致, https://lowcode-engine.cn/docV2/nhilce#appHelper
*/
appHelper?: {
/** 全局公共函数 */
utils?: Record<string, any>;
/** 全局常量 */
constants?: Record<string, any>;
};
/**
* 数据源引擎的请求处理器映射
*/
requestHandlersMap: RequestHandlersMap;
}
const getStrictModeValue = (engineOptions: EngineOptions, defaultValue: boolean): boolean => {
if (!engineOptions || !isPlainObject(engineOptions)) {
return defaultValue;
}
if (engineOptions.enableStrictPluginMode === undefined
|| engineOptions.enableStrictPluginMode === null) {
return defaultValue;
}
return engineOptions.enableStrictPluginMode;
};
export class EngineConfig {
private config: { [key: string]: any } = {};
private waits = new Map<
string,
Array<{
once?: boolean;
resolve: (data: any) => void;
}>
>();
constructor(config?: { [key: string]: any }) {
this.config = config || {};
}
/**
* 判断指定 key 是否有值
* @param key
* @returns
*/
has(key: string): boolean {
return this.config[key] !== undefined;
}
/**
* 获取指定 key 的值
* @param key
* @param defaultValue
* @returns
*/
get(key: string, defaultValue?: any): any {
return lodashGet(this.config, key, defaultValue);
}
/**
* 设置指定 key 的值
* @param key
* @param value
*/
set(key: string, value: any) {
this.config[key] = value;
this.notifyGot(key);
}
/**
* 批量设值,set 的对象版本
* @param config
*/
setConfig(config: { [key: string]: any }) {
if (config) {
Object.keys(config).forEach((key) => {
this.set(key, config[key]);
});
}
}
/**
* if engineOptions.strictPluginMode === true, only accept propertied predefined in EngineOptions.
*
* @param {EngineOptions} engineOptions
* @memberof EngineConfig
*/
setEngineOptions(engineOptions: EngineOptions) {
if (!engineOptions || !isPlainObject(engineOptions)) {
return;
}
const strictMode = getStrictModeValue(engineOptions, STRICT_PLUGIN_MODE_DEFAULT) === true;
if (strictMode) {
const isValidKey = (key: string) => {
const result = (VALID_ENGINE_OPTIONS as any)[key];
return !(result === undefined || result === null);
};
Object.keys(engineOptions).forEach((key) => {
if (isValidKey(key)) {
this.set(key, engineOptions[key]);
} else {
logger.warn(`failed to config ${key} to engineConfig, only predefined options can be set under strict mode, predefined options: `, VALID_ENGINE_OPTIONS);
}
});
} else {
this.setConfig(engineOptions as any);
}
}
/**
* 获取指定 key 的值,若此时还未赋值,则等待,若已有值,则直接返回值
* 注:此函数返回 Promise 实例,只会执行(fullfill)一次
* @param key
* @returns
*/
onceGot(key: string): Promise<any> {
const val = this.config[key];
if (val !== undefined) {
return Promise.resolve(val);
}
return new Promise((resolve) => {
this.setWait(key, resolve, true);
});
}
/**
* 获取指定 key 的值,函数回调模式,若多次被赋值,回调会被多次调用
* @param key
* @param fn
* @returns
*/
onGot(key: string, fn: (data: any) => void): () => void {
const val = this.config?.[key];
if (val !== undefined) {
fn(val);
return () => {};
} else {
this.setWait(key, fn);
return () => {
this.delWait(key, fn);
};
}
}
private notifyGot(key: string) {
let waits = this.waits.get(key);
if (!waits) {
return;
}
waits = waits.slice().reverse();
let i = waits.length;
while (i--) {
waits[i].resolve(this.get(key));
if (waits[i].once) {
waits.splice(i, 1);
}
}
if (waits.length > 0) {
this.waits.set(key, waits);
} else {
this.waits.delete(key);
}
}
private setWait(key: string, resolve: (data: any) => void, once?: boolean) {
const waits = this.waits.get(key);
if (waits) {
waits.push({ resolve, once });
} else {
this.waits.set(key, [{ resolve, once }]);
}
}
private delWait(key: string, fn: any) {
const waits = this.waits.get(key);
if (!waits) {
return;
}
let i = waits.length;
while (i--) {
if (waits[i].resolve === fn) {
waits.splice(i, 1);
}
}
if (waits.length < 1) {
this.waits.delete(key);
}
}
}
export const engineConfig = new EngineConfig();