Merge branch 'feat/editor-hooks' into 'master'

feat: 编辑器 hooks 能力实现

编辑器 hooks 能力实现

See merge request !944724
This commit is contained in:
荣彬 2020-08-24 14:24:38 +08:00
commit 41cae06f1c
2 changed files with 47 additions and 5 deletions

View File

@ -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<T = undefined, KeyOrType = any>(keyOrType: KeyOrType, opt?: GetOptions): GetReturnType<T, KeyOrType> | undefined {
const x = this.context.get<T, KeyOrType>(keyOrType, opt);
if (x === NOT_FOUND) {
@ -79,15 +90,19 @@ export class Editor extends EventEmitter implements IEditor {
async init(config?: EditorConfig, components?: PluginClassSet): Promise<any> {
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<{

View File

@ -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[];