mirror of
https://github.com/alibaba/lowcode-engine.git
synced 2026-01-13 01:21:58 +00:00
feat: 编辑器 hooks 能力实现
This commit is contained in:
parent
ffaa15fc6b
commit
f3ac23bc89
@ -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<{
|
||||
|
||||
@ -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[];
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user