feat(editor): eventsServic支持插件扩展

This commit is contained in:
roymondchen 2025-05-13 16:29:34 +08:00
parent 6b481c4473
commit 054d12601b

View File

@ -18,18 +18,32 @@
import { reactive } from 'vue';
import { cloneDeep } from 'lodash-es';
import type { Writable } from 'type-fest';
import { type EventOption } from '@tmagic/core';
import { toLine } from '@tmagic/utils';
import type { AsyncHookPlugin, SyncHookPlugin } from '@editor/type';
import BaseService from './BaseService';
const canUsePluginMethods = {
async: [] as const,
sync: ['setEvent', 'getEvent', 'setMethod', 'getMethod'] as const,
};
type AsyncMethodName = Writable<(typeof canUsePluginMethods)['async']>;
type SyncMethodName = Writable<(typeof canUsePluginMethods)['sync']>;
let eventMap: Record<string, EventOption[]> = reactive({});
let methodMap: Record<string, EventOption[]> = reactive({});
class Events extends BaseService {
constructor() {
super([]);
super([
...canUsePluginMethods.async.map((methodName) => ({ name: methodName, isAsync: true })),
...canUsePluginMethods.sync.map((methodName) => ({ name: methodName, isAsync: false })),
]);
}
public setEvents(events: Record<string, EventOption[]>) {
@ -70,6 +84,10 @@ class Events extends BaseService {
this.removeAllListeners();
this.removeAllPlugins();
}
public usePlugin(options: AsyncHookPlugin<AsyncMethodName, Events> & SyncHookPlugin<SyncMethodName, Events>): void {
super.usePlugin(options);
}
}
export type EventsService = Events;