From f3ac23bc894d839dcbd3202434cd5ec4a09a9203 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B9=BD=E5=9F=8E?= Date: Fri, 21 Aug 2020 16:50:55 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E7=BC=96=E8=BE=91=E5=99=A8=20hooks=20?= =?UTF-8?q?=E8=83=BD=E5=8A=9B=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/editor-core/src/editor.ts | 50 +++++++++++++++++++++++++++--- packages/types/src/editor.ts | 2 +- 2 files changed, 47 insertions(+), 5 deletions(-) diff --git a/packages/editor-core/src/editor.ts b/packages/editor-core/src/editor.ts index db9e0d784..f55c03df3 100644 --- a/packages/editor-core/src/editor.ts +++ b/packages/editor-core/src/editor.ts @@ -1,13 +1,22 @@ import { EventEmitter } from 'events'; -import { IEditor, EditorConfig, PluginClassSet, KeyType, GetOptions, GetReturnType } from '@ali/lowcode-types'; +import { + IEditor, + EditorConfig, + PluginClassSet, + KeyType, + GetOptions, + GetReturnType, + HookConfig, +} from '@ali/lowcode-types'; import { IocContext, RegisterOptions } from './di'; import { globalLocale } from './intl'; +import * as utils from './utils'; +import { tipHandler } from './widgets/tip/tip-handler'; + EventEmitter.defaultMaxListeners = 100; const NOT_FOUND = Symbol.for('not_found'); -import * as utils from './utils'; - export class Editor extends EventEmitter implements IEditor { /** * Ioc Container @@ -22,6 +31,8 @@ export class Editor extends EventEmitter implements IEditor { readonly utils = utils; + private hooks: HookConfig[] = []; + get(keyOrType: KeyOrType, opt?: GetOptions): GetReturnType | undefined { const x = this.context.get(keyOrType, opt); if (x === NOT_FOUND) { @@ -79,15 +90,19 @@ export class Editor extends EventEmitter implements IEditor { async init(config?: EditorConfig, components?: PluginClassSet): Promise { this.config = config || {}; this.components = components || {}; - const { shortCuts = [], lifeCycles } = this.config; + const { shortCuts = [], hooks = [], lifeCycles } = this.config; this.emit('editor.beforeInit'); const init = (lifeCycles && lifeCycles.init) || ((): void => {}); + try { await init(this); // 注册快捷键 // registShortCuts(shortCuts, this); + // 注册 hooks + this.registerHooks(hooks); this.emit('editor.afterInit'); + return true; } catch (err) { console.error(err); @@ -101,6 +116,9 @@ export class Editor extends EventEmitter implements IEditor { try { const { shortCuts = [], lifeCycles = {} } = this.config; // unRegistShortCuts(shortCuts); + + this.unRegisterHooks(); + if (lifeCycles.destroy) { lifeCycles.destroy(this); } @@ -109,6 +127,30 @@ export class Editor extends EventEmitter implements IEditor { } } + initHooks = (hooks: HookConfig[]) => { + this.hooks = hooks.map((hook) => ({ + ...hook, + // 指定第一个参数为 editor + handler: hook.handler.bind(this, this), + })); + + return this.hooks; + }; + + registerHooks = (hooks: HookConfig[]) => { + this.initHooks(hooks).forEach(({ message, type, handler }) => { + if (['on', 'once'].indexOf(type) !== -1) { + this[type](message, handler); + } + }); + }; + + unRegisterHooks = () => { + this.hooks.forEach(({ message, handler }) => { + this.removeListener(message, handler); + }); + }; + private waits = new Map< KeyType, Array<{ diff --git a/packages/types/src/editor.ts b/packages/types/src/editor.ts index bfb2682b8..2b5efd989 100644 --- a/packages/types/src/editor.ts +++ b/packages/types/src/editor.ts @@ -92,7 +92,7 @@ export type HooksConfig = HookConfig[]; export interface HookConfig { message: string; type: 'on' | 'once'; - handler: (editor: IEditor, ...args: any[]) => void; + handler: (this: IEditor, editor: IEditor, ...args: any[]) => void; } export type ShortCutsConfig = ShortCutConfig[];