forked from alibaba/lowcode-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevent.ts
More file actions
68 lines (59 loc) · 1.69 KB
/
event.ts
File metadata and controls
68 lines (59 loc) · 1.69 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
import { Editor as InnerEditor, globalContext } from '@alilc/lowcode-editor-core';
import { getLogger } from '@alilc/lowcode-utils';
import { editorSymbol } from './symbols';
const logger = getLogger({ level: 'warn', bizName: 'shell:event' });
type EventOptions = {
prefix: string;
};
export default class Event {
private readonly [editorSymbol]: InnerEditor;
private readonly options: EventOptions;
// TODO:
/**
* 内核触发的事件名
*/
readonly names = [];
constructor(editor: InnerEditor, options: EventOptions) {
this[editorSymbol] = editor;
this.options = options;
if (!this.options.prefix) {
logger.warn('prefix is required while initializing Event');
}
}
/**
* 监听事件
* @param event 事件名称
* @param listener 事件回调
*/
on(event: string, listener: (...args: unknown[]) => void) {
if (event.startsWith('designer')) {
logger.warn('designer events are disabled');
return;
}
this[editorSymbol].on(event, listener);
}
/**
* 取消监听事件
* @param event 事件名称
* @param listener 事件回调
*/
off(event: string, listener: (...args: unknown[]) => void) {
this[editorSymbol].off(event, listener);
}
/**
* 触发事件
* @param event 事件名称
* @param args 事件参数
* @returns
*/
emit(event: string, ...args: unknown[]) {
if (!this.options.prefix) {
logger.warn('Event#emit has been forbidden while prefix is not specified');
return;
}
this[editorSymbol].emit(`${this.options.prefix}:${event}`, ...args);
}
}
export function getEvent(editor: InnerEditor, options: any = { prefix: 'common' }) {
return new Event(editor, options);
}