forked from alibaba/lowcode-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin-context.ts
More file actions
86 lines (80 loc) · 2.7 KB
/
plugin-context.ts
File metadata and controls
86 lines (80 loc) · 2.7 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
/* eslint-disable no-multi-assign */
import { Editor, EngineConfig, engineConfig } from '@alilc/lowcode-editor-core';
import { Designer, ILowCodePluginManager } from '@alilc/lowcode-designer';
import { Skeleton as InnerSkeleton } from '@alilc/lowcode-editor-skeleton';
import {
Hotkey,
Project,
Skeleton,
Setters,
Material,
Event,
editorSymbol,
designerSymbol,
skeletonSymbol,
} from '@alilc/lowcode-shell';
import { getLogger, Logger } from '@alilc/lowcode-utils';
import {
ILowCodePluginContext,
IPluginContextOptions,
ILowCodePluginPreferenceDeclaration,
PreferenceValueType,
IPluginPreferenceMananger,
} from './plugin-types';
import { isValidPreferenceKey } from './plugin-utils';
export default class PluginContext implements ILowCodePluginContext {
private readonly [editorSymbol]: Editor;
private readonly [designerSymbol]: Designer;
private readonly [skeletonSymbol]: InnerSkeleton;
hotkey: Hotkey;
project: Project;
skeleton: Skeleton;
logger: Logger;
setters: Setters;
material: Material;
config: EngineConfig;
event: Event;
plugins: ILowCodePluginManager;
preference: IPluginPreferenceMananger;
constructor(plugins: ILowCodePluginManager, options: IPluginContextOptions) {
const editor = this[editorSymbol] = plugins.editor;
const designer = this[designerSymbol] = editor.get('designer')!;
const skeleton = this[skeletonSymbol] = editor.get('skeleton')!;
const { pluginName = 'anonymous' } = options;
const project = designer?.project;
this.hotkey = new Hotkey();
this.project = new Project(project);
this.skeleton = new Skeleton(skeleton);
this.setters = new Setters();
this.material = new Material(editor);
this.config = engineConfig;
this.plugins = plugins;
this.event = new Event(editor, { prefix: 'common' });
this.logger = getLogger({ level: 'warn', bizName: `designer:plugin:${pluginName}` });
const enhancePluginContextHook = engineConfig.get('enhancePluginContextHook');
if (enhancePluginContextHook) {
enhancePluginContextHook(this);
}
}
setPreference(
pluginName: string,
preferenceDeclaration: ILowCodePluginPreferenceDeclaration,
): void {
const getPreferenceValue = (
key: string,
defaultValue?: PreferenceValueType,
): PreferenceValueType | undefined => {
if (!isValidPreferenceKey(key, preferenceDeclaration)) {
return undefined;
}
const pluginPreference = this.plugins.getPluginPreference(pluginName) || {};
if (pluginPreference[key] === undefined || pluginPreference[key] === null) {
return defaultValue;
}
return pluginPreference[key];
};
this.preference = {
getPreferenceValue,
};
}
}