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
372 lines (347 loc) · 9.98 KB
/
config.ts
File metadata and controls
372 lines (347 loc) · 9.98 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
import { get as lodashGet } from 'lodash';
import { isPlainObject } from '@alilc/lowcode-utils';
import {
IPublicTypeEngineOptions,
IPublicModelEngineConfig,
IPublicModelPreference,
} from '@alilc/lowcode-types';
import { getLogger } from './utils/logger';
import Preference from './utils/preference';
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', '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[], e: MouseEvent) => 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 上',
},
hideSettingsTabsWhenOnlyOneItem: {
type: 'boolean',
description: '是否在只有一个 item 的时候隐藏设置 tabs',
},
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 的地址',
},
// 与 react-renderer 的 appHelper 一致,https://lowcode-engine.cn/site/docs/guide/expand/runtime/renderer#apphelper
appHelper: {
type: 'object',
description: '定义 utils 和 constants 等对象',
},
requestHandlersMap: {
type: 'object',
description: '数据源引擎的请求处理器映射',
},
thisRequiredInJSE: {
type: 'boolean',
description: 'JSExpression 是否只支持使用 this 来访问上下文变量',
},
enableStrictNotFoundMode: {
type: 'boolean',
description: '当开启组件未找到严格模式时,渲染模块不会默认给一个容器组件',
},
focusNodeSelector: {
type: 'function',
description: '配置指定节点为根组件',
},
enableAutoOpenFirstWindow: {
type: 'boolean',
description: '应用级设计模式下,自动打开第一个窗口',
default: true,
},
enableWorkspaceMode: {
type: 'boolean',
description: '是否开启应用级设计模式',
default: false,
},
workspaceEmptyComponent: {
type: 'function',
description: '应用级设计模式下,窗口为空时展示的占位组件',
},
enableContextMenu: {
type: 'boolean',
description: '是否开启右键菜单',
default: false,
},
hideComponentAction: {
type: 'boolean',
description: '是否隐藏设计器辅助层',
default: false,
},
};
const getStrictModeValue = (engineOptions: IPublicTypeEngineOptions, defaultValue: boolean): boolean => {
if (!engineOptions || !isPlainObject(engineOptions)) {
return defaultValue;
}
if (engineOptions.enableStrictPluginMode === undefined
|| engineOptions.enableStrictPluginMode === null) {
return defaultValue;
}
return engineOptions.enableStrictPluginMode;
};
export interface IEngineConfig extends IPublicModelEngineConfig {
/**
* if engineOptions.strictPluginMode === true, only accept propertied predefined in EngineOptions.
*
* @param {IPublicTypeEngineOptions} engineOptions
*/
setEngineOptions(engineOptions: IPublicTypeEngineOptions): void;
notifyGot(key: string): void;
setWait(key: string, resolve: (data: any) => void, once?: boolean): void;
delWait(key: string, fn: any): void;
}
export class EngineConfig implements IEngineConfig {
private config: { [key: string]: any } = {};
private waits = new Map<
string,
Array<{
once?: boolean;
resolve: (data: any) => void;
}>
>();
/**
* used to store preferences
*
*/
readonly preference: IPublicModelPreference;
constructor(config?: { [key: string]: any }) {
this.config = config || {};
this.preference = new Preference();
}
/**
* 判断指定 key 是否有值
* @param key
*/
has(key: string): boolean {
return this.config[key] !== undefined;
}
/**
* 获取指定 key 的值
* @param key
* @param defaultValue
*/
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 {IPublicTypeEngineOptions} engineOptions
*/
setEngineOptions(engineOptions: IPublicTypeEngineOptions) {
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 as any)[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);
}
this.setWait(key, fn);
return () => {
this.delWait(key, fn);
};
}
notifyGot(key: string): void {
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);
}
}
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 }]);
}
}
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);
}
}
getPreference(): IPublicModelPreference {
return this.preference;
}
}
export const engineConfig = new EngineConfig();