mirror of
https://github.com/alibaba/lowcode-engine.git
synced 2026-01-16 03:18:11 +00:00
feat: 编辑器 hooks 能力实现
This commit is contained in:
parent
ffaa15fc6b
commit
f3ac23bc89
@ -1,13 +1,22 @@
|
|||||||
import { EventEmitter } from 'events';
|
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 { IocContext, RegisterOptions } from './di';
|
||||||
import { globalLocale } from './intl';
|
import { globalLocale } from './intl';
|
||||||
|
import * as utils from './utils';
|
||||||
|
import { tipHandler } from './widgets/tip/tip-handler';
|
||||||
|
|
||||||
EventEmitter.defaultMaxListeners = 100;
|
EventEmitter.defaultMaxListeners = 100;
|
||||||
|
|
||||||
const NOT_FOUND = Symbol.for('not_found');
|
const NOT_FOUND = Symbol.for('not_found');
|
||||||
|
|
||||||
import * as utils from './utils';
|
|
||||||
|
|
||||||
export class Editor extends EventEmitter implements IEditor {
|
export class Editor extends EventEmitter implements IEditor {
|
||||||
/**
|
/**
|
||||||
* Ioc Container
|
* Ioc Container
|
||||||
@ -22,6 +31,8 @@ export class Editor extends EventEmitter implements IEditor {
|
|||||||
|
|
||||||
readonly utils = utils;
|
readonly utils = utils;
|
||||||
|
|
||||||
|
private hooks: HookConfig[] = [];
|
||||||
|
|
||||||
get<T = undefined, KeyOrType = any>(keyOrType: KeyOrType, opt?: GetOptions): GetReturnType<T, KeyOrType> | undefined {
|
get<T = undefined, KeyOrType = any>(keyOrType: KeyOrType, opt?: GetOptions): GetReturnType<T, KeyOrType> | undefined {
|
||||||
const x = this.context.get<T, KeyOrType>(keyOrType, opt);
|
const x = this.context.get<T, KeyOrType>(keyOrType, opt);
|
||||||
if (x === NOT_FOUND) {
|
if (x === NOT_FOUND) {
|
||||||
@ -79,15 +90,19 @@ export class Editor extends EventEmitter implements IEditor {
|
|||||||
async init(config?: EditorConfig, components?: PluginClassSet): Promise<any> {
|
async init(config?: EditorConfig, components?: PluginClassSet): Promise<any> {
|
||||||
this.config = config || {};
|
this.config = config || {};
|
||||||
this.components = components || {};
|
this.components = components || {};
|
||||||
const { shortCuts = [], lifeCycles } = this.config;
|
const { shortCuts = [], hooks = [], lifeCycles } = this.config;
|
||||||
|
|
||||||
this.emit('editor.beforeInit');
|
this.emit('editor.beforeInit');
|
||||||
const init = (lifeCycles && lifeCycles.init) || ((): void => {});
|
const init = (lifeCycles && lifeCycles.init) || ((): void => {});
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await init(this);
|
await init(this);
|
||||||
// 注册快捷键
|
// 注册快捷键
|
||||||
// registShortCuts(shortCuts, this);
|
// registShortCuts(shortCuts, this);
|
||||||
|
// 注册 hooks
|
||||||
|
this.registerHooks(hooks);
|
||||||
this.emit('editor.afterInit');
|
this.emit('editor.afterInit');
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(err);
|
console.error(err);
|
||||||
@ -101,6 +116,9 @@ export class Editor extends EventEmitter implements IEditor {
|
|||||||
try {
|
try {
|
||||||
const { shortCuts = [], lifeCycles = {} } = this.config;
|
const { shortCuts = [], lifeCycles = {} } = this.config;
|
||||||
// unRegistShortCuts(shortCuts);
|
// unRegistShortCuts(shortCuts);
|
||||||
|
|
||||||
|
this.unRegisterHooks();
|
||||||
|
|
||||||
if (lifeCycles.destroy) {
|
if (lifeCycles.destroy) {
|
||||||
lifeCycles.destroy(this);
|
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<
|
private waits = new Map<
|
||||||
KeyType,
|
KeyType,
|
||||||
Array<{
|
Array<{
|
||||||
|
|||||||
@ -92,7 +92,7 @@ export type HooksConfig = HookConfig[];
|
|||||||
export interface HookConfig {
|
export interface HookConfig {
|
||||||
message: string;
|
message: string;
|
||||||
type: 'on' | 'once';
|
type: 'on' | 'once';
|
||||||
handler: (editor: IEditor, ...args: any[]) => void;
|
handler: (this: IEditor, editor: IEditor, ...args: any[]) => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
export type ShortCutsConfig = ShortCutConfig[];
|
export type ShortCutsConfig = ShortCutConfig[];
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user