From a879be49cba9c18f195846e782f6f61949b55afa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8A=9B=E7=9A=93?= Date: Mon, 4 Jan 2021 11:15:02 +0800 Subject: [PATCH] =?UTF-8?q?chore:=20=E4=BC=98=E5=8C=96=20plugin=20types=20?= =?UTF-8?q?=E5=AE=9A=E4=B9=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../designer/src/plugin/plugin-context.ts | 34 ++++-------------- .../designer/src/plugin/plugin-manager.ts | 20 +++++------ packages/designer/src/plugin/plugin.ts | 34 +++++++++++------- packages/editor-preset-vision/src/index.ts | 2 +- packages/types/src/plugin.ts | 36 ++++++++++++------- 5 files changed, 62 insertions(+), 64 deletions(-) diff --git a/packages/designer/src/plugin/plugin-context.ts b/packages/designer/src/plugin/plugin-context.ts index 980ba2a68..72901ab42 100644 --- a/packages/designer/src/plugin/plugin-context.ts +++ b/packages/designer/src/plugin/plugin-context.ts @@ -1,53 +1,33 @@ import { Editor, Hotkey, hotkey } from '@ali/lowcode-editor-core'; import { Skeleton } from '@ali/lowcode-editor-skeleton'; -import { ComponentAction, ILowCodePluginConfig } from '@ali/lowcode-types'; +import { ILowCodePluginConfig, ILowCodePluginManager, ILowCodePluginContext, IDesignerHelper } from '@ali/lowcode-types'; import { getLogger, Logger } from '../utils'; import { registerMetadataTransducer, addBuiltinComponentAction, removeBuiltinComponentAction, - MetadataTransducer, } from '../component-meta'; import { Designer } from '../designer'; -export interface IDesignerHelper { - registerMetadataTransducer: (transducer: MetadataTransducer, level = 100, id?: string) => void; - addBuiltinComponentAction: (action: ComponentAction) => void; - removeBuiltinComponentAction: (actionName: string) => void; -} - -export interface ILowCodePluginContext { - skeleton: Skeleton; - designer: Designer; - editor: Editor; - hotkey: Hotkey; - logger: Logger; - plugins: LowCodePluginManager; - designerHelper: IDesignerHelper; - /** - 其他暂不增加,按需增加 - */ -} - export default class PluginContext implements ILowCodePluginContext { editor: Editor; skeleton: Skeleton; designer: Designer; hotkey: Hotkey; logger: Logger; - plugins: LowCodePluginManager; + plugins: ILowCodePluginManager; designerHelper: IDesignerHelper; - constructor(editor: Editor, plugins: LowCodePluginManager) { + constructor(editor: Editor, plugins: ILowCodePluginManager) { this.editor = editor; - this.designer = editor.get('designer'); - this.skeleton = editor.get('skeleton'); + this.designer = editor.get('designer')!; + this.skeleton = editor.get('skeleton')!; this.hotkey = hotkey; this.plugins = plugins; this.designerHelper = this.createDesignerHelper(); } - private createDesignerHelper(): () => IDesignerHelper { + private createDesignerHelper(): IDesignerHelper { return { registerMetadataTransducer, addBuiltinComponentAction, @@ -55,7 +35,7 @@ export default class PluginContext implements ILowCodePluginContext { }; } - setLogger(config: ILowCodePluginConfig): (config: ILowCodePluginConfig) => void { + setLogger(config: ILowCodePluginConfig): void { this.logger = getLogger({ level: 'log', bizName: `designer:plugin:${config.name}` }); } } diff --git a/packages/designer/src/plugin/plugin-manager.ts b/packages/designer/src/plugin/plugin-manager.ts index c031e5db6..40b01127d 100644 --- a/packages/designer/src/plugin/plugin-manager.ts +++ b/packages/designer/src/plugin/plugin-manager.ts @@ -1,5 +1,5 @@ import { Editor } from '@ali/lowcode-editor-core'; -import { CompositeObject, ILowCodePlugin, ILowCodePluginConfig, ILowCodePluginManager } from '@ali/lowcode-types'; +import { CompositeObject, ILowCodePlugin, ILowCodePluginConfig, ILowCodePluginManager, ILowCodePluginContext } from '@ali/lowcode-types'; import { LowCodePlugin } from './plugin'; import LowCodePluginContext from './plugin-context'; import { getLogger, invariant } from '../utils'; @@ -37,7 +37,7 @@ export class LowCodePluginManager implements ILowCodePluginManager { logger.log('plugin registered with config:', config, ', options:', options); } - get(pluginName: string): ILowCodePlugin { + get(pluginName: string): ILowCodePlugin | undefined { return this.pluginsMap.get(pluginName); } @@ -49,9 +49,9 @@ export class LowCodePluginManager implements ILowCodePluginManager { return this.pluginsMap.has(pluginName); } - async delete(pluginName: string): boolean { + async delete(pluginName: string): Promise { const idx = this.plugins.findIndex(plugin => plugin.name === pluginName); - if (idx < -1) return; + if (idx < -1) return false; const plugin = this.plugins[idx]; await plugin.destroy(); @@ -60,8 +60,8 @@ export class LowCodePluginManager implements ILowCodePluginManager { } async init() { - const pluginNames = []; - const pluginObj = {}; + const pluginNames: string[] = []; + const pluginObj: { [name: string]: ILowCodePlugin } = {}; this.plugins.forEach(plugin => { pluginNames.push(plugin.name); pluginObj[plugin.name] = plugin; @@ -71,7 +71,7 @@ export class LowCodePluginManager implements ILowCodePluginManager { logger.log('load plugin sequence:', sequence); for (const pluginName of sequence) { - await this.pluginsMap.get(pluginName).init(); + await this.pluginsMap.get(pluginName)!.init(); } } @@ -88,12 +88,12 @@ export class LowCodePluginManager implements ILowCodePluginManager { toProxy() { return new Proxy(this, { get(target, prop, receiver) { - if (target.pluginsMap.has(prop)) { + if (target.pluginsMap.has(prop as string)) { // 禁用态的插件,直接返回 undefined - if (target.pluginsMap.get(prop).disabled) { + if (target.pluginsMap.get(prop as string)!.disabled) { return undefined; } - return target.pluginsMap.get(prop)?.toProxy(); + return target.pluginsMap.get(prop as string)?.toProxy(); } return Reflect.get(target, prop, receiver); }, diff --git a/packages/designer/src/plugin/plugin.ts b/packages/designer/src/plugin/plugin.ts index 07615ade2..7ad0c1982 100644 --- a/packages/designer/src/plugin/plugin.ts +++ b/packages/designer/src/plugin/plugin.ts @@ -16,7 +16,7 @@ export class LowCodePlugin implements ILowCodePlugin { private options?: CompositeObject; - private emiter: EventEmitter; + private emitter: EventEmitter; private _inited: boolean; @@ -27,14 +27,14 @@ export class LowCodePlugin implements ILowCodePlugin { constructor( manager: ILowCodePluginManager, - config: ILowCodePluginConfig = {}, + config: ILowCodePluginConfig, options: CompositeObject = {}, ) { this.manager = manager; this.config = config; this.options = options; - this.emiter = new EventEmitter(); - this.logger = getLogger({ level: 'log', bizName: `designer:plugin:${config.name}` }); + this.emitter = new EventEmitter(); + this.logger = getLogger({ level: 'warn', bizName: `designer:plugin:${config.name}` }); } get name() { @@ -49,26 +49,34 @@ export class LowCodePlugin implements ILowCodePlugin { return this._disabled; } - on(...args) { - return this.emiter.on(...args); + on(event: string | symbol, listener: (...args: any[]) => void): any { + return this.emitter.on(event, listener); } - emit(...args) { - return this.emiter.emit(...args); + emit(event: string | symbol, ...args: any[]) { + return this.emitter.emit(event, ...args); + } + + off(event: string | symbol, listener: (...args: any[]) => void): any { + return this.emitter.off(event, listener); + } + + removeAllListeners(event: string | symbol): any { + return this.emitter.removeAllListeners(event); } async init() { this.logger.log('method init called'); - await this.config.init?.call(); + await this.config.init?.call(undefined); this._inited = true; } async destroy() { this.logger.log('method destroy called'); - await this.config.destroy?.call(); + await this.config?.destroy?.call(undefined); } - private setDisabled(flag = true) { + setDisabled(flag = true) { this._disabled = flag; } @@ -77,8 +85,8 @@ export class LowCodePlugin implements ILowCodePlugin { const exports = this.config.exports?.(); return new Proxy(this, { get(target, prop, receiver) { - if (hasOwnProperty.call(exports, prop)) { - return exports[prop]; + if ({}.hasOwnProperty.call(exports, prop)) { + return exports?.[prop as string]; } return Reflect.get(target, prop, receiver); }, diff --git a/packages/editor-preset-vision/src/index.ts b/packages/editor-preset-vision/src/index.ts index 105fd2875..0faaf2c52 100644 --- a/packages/editor-preset-vision/src/index.ts +++ b/packages/editor-preset-vision/src/index.ts @@ -9,9 +9,9 @@ import { registerMetadataTransducer, addBuiltinComponentAction, removeBuiltinComponentAction, - ILowCodePluginContext, // modifyBuiltinComponentAction, } from '@ali/lowcode-designer'; +import { ILowCodePluginContext, ILowCodePluginConfig } from '@ali/lowcode-types'; import { createElement } from 'react'; import { VE_EVENTS as EVENTS, VE_HOOKS as HOOKS, VERSION as Version } from './base/const'; import Bus from './bus'; diff --git a/packages/types/src/plugin.ts b/packages/types/src/plugin.ts index ed4a20f89..3a2e179f3 100644 --- a/packages/types/src/plugin.ts +++ b/packages/types/src/plugin.ts @@ -1,15 +1,18 @@ -import { CompositeObject } from '@ali/lowcode-types'; +import { CompositeObject, ComponentAction } from '@ali/lowcode-types'; import Logger from 'zen-logger'; import { Skeleton } from '@ali/lowcode-editor-skeleton'; import { Editor, Hotkey } from '@ali/lowcode-editor-core'; +import { + MetadataTransducer, + Designer, +} from '@ali/lowcode-designer'; export interface ILowCodePluginConfig { - manager: ILowCodePluginManager; name: string; - dep: string[]; // 依赖插件名 + dep?: string[]; // 依赖插件名 init(): void; - destroy(): void; - exports(): CompositeObject; + destroy?(): void; + exports?(): CompositeObject; } export interface ILowCodePlugin { @@ -18,20 +21,30 @@ export interface ILowCodePlugin { disabled: boolean; config: ILowCodePluginConfig; logger: Logger; - emit(): void; - on(): void; + on(event: string | symbol, listener: (...args: any[]) => void): any; + off(event: string | symbol, listener: (...args: any[]) => void): any; + emit(event: string | symbol, ...args: any[]): boolean; + removeAllListeners(event?: string | symbol): this; init(): void; destroy(): void; toProxy(): any; setDisabled(flag: boolean): void; } +export interface IDesignerHelper { + registerMetadataTransducer: (transducer: MetadataTransducer, level: number, id?: string) => void; + addBuiltinComponentAction: (action: ComponentAction) => void; + removeBuiltinComponentAction: (actionName: string) => void; +} + export interface ILowCodePluginContext { skeleton: Skeleton; + designer: Designer; editor: Editor; - plugins: ILowCodePluginManager; hotkey: Hotkey; logger: Logger; + plugins: ILowCodePluginManager; + designerHelper: IDesignerHelper; /** 其他暂不增加,按需增加 */ @@ -42,13 +55,10 @@ export interface ILowCodePluginManager { pluginConfig: (ctx: ILowCodePluginContext, options: CompositeObject) => ILowCodePluginConfig, options: CompositeObject, ): void; - get(pluginName: string): ILowCodePlugin; + get(pluginName: string): ILowCodePlugin | undefined; getAll(): ILowCodePlugin[]; has(pluginName: string): boolean; - delete(pluginName: string): boolean; + delete(pluginName: string): any; setDisabled(pluginName: string, flag: boolean): void; dispose(): void; - /** - 后续可以补充插件操作,比如 disable / enable 之类的 - */ }