chore: 优化 plugin types 定义

This commit is contained in:
力皓 2021-01-04 11:15:02 +08:00
parent eebd4a2d25
commit a879be49cb
5 changed files with 62 additions and 64 deletions

View File

@ -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}` });
}
}

View File

@ -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<boolean> {
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);
},

View File

@ -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);
},

View File

@ -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';

View File

@ -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
*/
}