Merge branch 'feat/plugin' into 'release/1.0.28'

chore: 优化 plugin types 定义



See merge request !1105593
This commit is contained in:
高凯 2021-01-04 14:16:09 +08:00
commit ea9d12ca85
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 { Editor, Hotkey, hotkey } from '@ali/lowcode-editor-core';
import { Skeleton } from '@ali/lowcode-editor-skeleton'; 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 { getLogger, Logger } from '../utils';
import { import {
registerMetadataTransducer, registerMetadataTransducer,
addBuiltinComponentAction, addBuiltinComponentAction,
removeBuiltinComponentAction, removeBuiltinComponentAction,
MetadataTransducer,
} from '../component-meta'; } from '../component-meta';
import { Designer } from '../designer'; 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 { export default class PluginContext implements ILowCodePluginContext {
editor: Editor; editor: Editor;
skeleton: Skeleton; skeleton: Skeleton;
designer: Designer; designer: Designer;
hotkey: Hotkey; hotkey: Hotkey;
logger: Logger; logger: Logger;
plugins: LowCodePluginManager; plugins: ILowCodePluginManager;
designerHelper: IDesignerHelper; designerHelper: IDesignerHelper;
constructor(editor: Editor, plugins: LowCodePluginManager) { constructor(editor: Editor, plugins: ILowCodePluginManager) {
this.editor = editor; this.editor = editor;
this.designer = editor.get('designer'); this.designer = editor.get('designer')!;
this.skeleton = editor.get('skeleton'); this.skeleton = editor.get('skeleton')!;
this.hotkey = hotkey; this.hotkey = hotkey;
this.plugins = plugins; this.plugins = plugins;
this.designerHelper = this.createDesignerHelper(); this.designerHelper = this.createDesignerHelper();
} }
private createDesignerHelper(): () => IDesignerHelper { private createDesignerHelper(): IDesignerHelper {
return { return {
registerMetadataTransducer, registerMetadataTransducer,
addBuiltinComponentAction, 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}` }); this.logger = getLogger({ level: 'log', bizName: `designer:plugin:${config.name}` });
} }
} }

View File

@ -1,5 +1,5 @@
import { Editor } from '@ali/lowcode-editor-core'; 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 { LowCodePlugin } from './plugin';
import LowCodePluginContext from './plugin-context'; import LowCodePluginContext from './plugin-context';
import { getLogger, invariant } from '../utils'; import { getLogger, invariant } from '../utils';
@ -37,7 +37,7 @@ export class LowCodePluginManager implements ILowCodePluginManager {
logger.log('plugin registered with config:', config, ', options:', options); logger.log('plugin registered with config:', config, ', options:', options);
} }
get(pluginName: string): ILowCodePlugin { get(pluginName: string): ILowCodePlugin | undefined {
return this.pluginsMap.get(pluginName); return this.pluginsMap.get(pluginName);
} }
@ -49,9 +49,9 @@ export class LowCodePluginManager implements ILowCodePluginManager {
return this.pluginsMap.has(pluginName); 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); const idx = this.plugins.findIndex(plugin => plugin.name === pluginName);
if (idx < -1) return; if (idx < -1) return false;
const plugin = this.plugins[idx]; const plugin = this.plugins[idx];
await plugin.destroy(); await plugin.destroy();
@ -60,8 +60,8 @@ export class LowCodePluginManager implements ILowCodePluginManager {
} }
async init() { async init() {
const pluginNames = []; const pluginNames: string[] = [];
const pluginObj = {}; const pluginObj: { [name: string]: ILowCodePlugin } = {};
this.plugins.forEach(plugin => { this.plugins.forEach(plugin => {
pluginNames.push(plugin.name); pluginNames.push(plugin.name);
pluginObj[plugin.name] = plugin; pluginObj[plugin.name] = plugin;
@ -71,7 +71,7 @@ export class LowCodePluginManager implements ILowCodePluginManager {
logger.log('load plugin sequence:', sequence); logger.log('load plugin sequence:', sequence);
for (const pluginName of 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() { toProxy() {
return new Proxy(this, { return new Proxy(this, {
get(target, prop, receiver) { get(target, prop, receiver) {
if (target.pluginsMap.has(prop)) { if (target.pluginsMap.has(prop as string)) {
// 禁用态的插件,直接返回 undefined // 禁用态的插件,直接返回 undefined
if (target.pluginsMap.get(prop).disabled) { if (target.pluginsMap.get(prop as string)!.disabled) {
return undefined; return undefined;
} }
return target.pluginsMap.get(prop)?.toProxy(); return target.pluginsMap.get(prop as string)?.toProxy();
} }
return Reflect.get(target, prop, receiver); return Reflect.get(target, prop, receiver);
}, },

View File

@ -16,7 +16,7 @@ export class LowCodePlugin implements ILowCodePlugin {
private options?: CompositeObject; private options?: CompositeObject;
private emiter: EventEmitter; private emitter: EventEmitter;
private _inited: boolean; private _inited: boolean;
@ -27,14 +27,14 @@ export class LowCodePlugin implements ILowCodePlugin {
constructor( constructor(
manager: ILowCodePluginManager, manager: ILowCodePluginManager,
config: ILowCodePluginConfig = {}, config: ILowCodePluginConfig,
options: CompositeObject = {}, options: CompositeObject = {},
) { ) {
this.manager = manager; this.manager = manager;
this.config = config; this.config = config;
this.options = options; this.options = options;
this.emiter = new EventEmitter(); this.emitter = new EventEmitter();
this.logger = getLogger({ level: 'log', bizName: `designer:plugin:${config.name}` }); this.logger = getLogger({ level: 'warn', bizName: `designer:plugin:${config.name}` });
} }
get name() { get name() {
@ -49,26 +49,34 @@ export class LowCodePlugin implements ILowCodePlugin {
return this._disabled; return this._disabled;
} }
on(...args) { on(event: string | symbol, listener: (...args: any[]) => void): any {
return this.emiter.on(...args); return this.emitter.on(event, listener);
} }
emit(...args) { emit(event: string | symbol, ...args: any[]) {
return this.emiter.emit(...args); 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() { async init() {
this.logger.log('method init called'); this.logger.log('method init called');
await this.config.init?.call(); await this.config.init?.call(undefined);
this._inited = true; this._inited = true;
} }
async destroy() { async destroy() {
this.logger.log('method destroy called'); 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; this._disabled = flag;
} }
@ -77,8 +85,8 @@ export class LowCodePlugin implements ILowCodePlugin {
const exports = this.config.exports?.(); const exports = this.config.exports?.();
return new Proxy(this, { return new Proxy(this, {
get(target, prop, receiver) { get(target, prop, receiver) {
if (hasOwnProperty.call(exports, prop)) { if ({}.hasOwnProperty.call(exports, prop)) {
return exports[prop]; return exports?.[prop as string];
} }
return Reflect.get(target, prop, receiver); return Reflect.get(target, prop, receiver);
}, },

View File

@ -9,9 +9,9 @@ import {
registerMetadataTransducer, registerMetadataTransducer,
addBuiltinComponentAction, addBuiltinComponentAction,
removeBuiltinComponentAction, removeBuiltinComponentAction,
ILowCodePluginContext,
// modifyBuiltinComponentAction, // modifyBuiltinComponentAction,
} from '@ali/lowcode-designer'; } from '@ali/lowcode-designer';
import { ILowCodePluginContext, ILowCodePluginConfig } from '@ali/lowcode-types';
import { createElement } from 'react'; import { createElement } from 'react';
import { VE_EVENTS as EVENTS, VE_HOOKS as HOOKS, VERSION as Version } from './base/const'; import { VE_EVENTS as EVENTS, VE_HOOKS as HOOKS, VERSION as Version } from './base/const';
import Bus from './bus'; 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 Logger from 'zen-logger';
import { Skeleton } from '@ali/lowcode-editor-skeleton'; import { Skeleton } from '@ali/lowcode-editor-skeleton';
import { Editor, Hotkey } from '@ali/lowcode-editor-core'; import { Editor, Hotkey } from '@ali/lowcode-editor-core';
import {
MetadataTransducer,
Designer,
} from '@ali/lowcode-designer';
export interface ILowCodePluginConfig { export interface ILowCodePluginConfig {
manager: ILowCodePluginManager;
name: string; name: string;
dep: string[]; // 依赖插件名 dep?: string[]; // 依赖插件名
init(): void; init(): void;
destroy(): void; destroy?(): void;
exports(): CompositeObject; exports?(): CompositeObject;
} }
export interface ILowCodePlugin { export interface ILowCodePlugin {
@ -18,20 +21,30 @@ export interface ILowCodePlugin {
disabled: boolean; disabled: boolean;
config: ILowCodePluginConfig; config: ILowCodePluginConfig;
logger: Logger; logger: Logger;
emit(): void; on(event: string | symbol, listener: (...args: any[]) => void): any;
on(): void; off(event: string | symbol, listener: (...args: any[]) => void): any;
emit(event: string | symbol, ...args: any[]): boolean;
removeAllListeners(event?: string | symbol): this;
init(): void; init(): void;
destroy(): void; destroy(): void;
toProxy(): any; toProxy(): any;
setDisabled(flag: boolean): void; 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 { export interface ILowCodePluginContext {
skeleton: Skeleton; skeleton: Skeleton;
designer: Designer;
editor: Editor; editor: Editor;
plugins: ILowCodePluginManager;
hotkey: Hotkey; hotkey: Hotkey;
logger: Logger; logger: Logger;
plugins: ILowCodePluginManager;
designerHelper: IDesignerHelper;
/** /**
*/ */
@ -42,13 +55,10 @@ export interface ILowCodePluginManager {
pluginConfig: (ctx: ILowCodePluginContext, options: CompositeObject) => ILowCodePluginConfig, pluginConfig: (ctx: ILowCodePluginContext, options: CompositeObject) => ILowCodePluginConfig,
options: CompositeObject, options: CompositeObject,
): void; ): void;
get(pluginName: string): ILowCodePlugin; get(pluginName: string): ILowCodePlugin | undefined;
getAll(): ILowCodePlugin[]; getAll(): ILowCodePlugin[];
has(pluginName: string): boolean; has(pluginName: string): boolean;
delete(pluginName: string): boolean; delete(pluginName: string): any;
setDisabled(pluginName: string, flag: boolean): void; setDisabled(pluginName: string, flag: boolean): void;
dispose(): void; dispose(): void;
/**
disable / enable
*/
} }