forked from alibaba/lowcode-plugins
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathService.ts
More file actions
89 lines (74 loc) · 2.32 KB
/
Service.ts
File metadata and controls
89 lines (74 loc) · 2.32 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
import { ReactElement } from 'react';
import { EditorController, HookHandleFn } from './Controller';
import { EditorHook } from './EditorHook';
import type {IPublicApiSkeleton} from '@alilc/lowcode-types';
import { Monaco } from './types';
export enum PluginHooks {
onActive = 'onActive',
onDeActive = 'onDeActive',
onSelectFileChange = 'onSelectFileChange',
onEditorMount = 'onEditorMount',
onMonacoLoaded = 'onMonacoLoaded',
}
export interface EditorPluginInterface {
apply(service: Service): void;
}
export interface ServiceInitOptions {
plugins?: EditorPluginInterface[];
}
export interface PluginAction {
key: string;
title: string;
icon: ReactElement;
action: () => any;
priority: number;
}
export class Service extends EditorHook {
// private options: ServiceInitOptions;
public onActive = this.hookFactory(PluginHooks.onActive);
public onDeActive = this.hookFactory(PluginHooks.onDeActive);
public onSelectFileChange = this.hookFactory(PluginHooks.onSelectFileChange);
public onEditorMount = this.hookFactory(PluginHooks.onEditorMount);
public onMonacoLoaded: HookHandleFn<(monaco: Monaco) => void> =
this.hookFactory(PluginHooks.onMonacoLoaded);
actionMap: Array<PluginAction>;
constructor(public controller: EditorController, private skeleton: IPublicApiSkeleton) {
super();
this.actionMap = [];
}
init(options: ServiceInitOptions) {
const { plugins } = options;
if (plugins) {
for (const plugin of plugins) {
plugin.apply(this);
}
}
this.setupHooks();
return this;
}
private setupHooks() {
this.skeleton.onHidePanel((pluginName) => {
if (pluginName === 'codeEditor') {
this.triggerHook(PluginHooks.onDeActive);
}
});
this.skeleton.onShowPanel((pluginName) => {
if (pluginName === 'codeEditor') {
this.triggerHook(PluginHooks.onActive);
}
});
}
public triggerHook(key: PluginHooks, ...args: any[]): void {
super.triggerHook(key, ...args);
}
public registerAction(action: PluginAction) {
const index = this.actionMap.findIndex((item) => item.key === action.key);
if (index > -1) {
console.error(
`Action ${action.key}, 已被注册,此 Action 将覆盖原 Action`
);
this.actionMap.splice(index, 1);
}
this.actionMap.push(action);
}
}