roymondchen 959c0322b6 fix(editor): 修复 eventsService resetState 响应式失效并在编辑器重置时清空
resetState 改为原地清空 reactive 对象以保留代理身份,避免 computed 不更新。
编辑器 reset 时同步调用 eventsService.resetState,并补充文档与单测。
2026-09-03 20:02:03 +08:00

97 lines
3.1 KiB
TypeScript

/*
* Tencent is pleased to support the open source community by making TMagicEditor available.
*
* Copyright (C) 2025 Tencent. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { reactive } from 'vue';
import { cloneDeep } from 'lodash-es';
import type { Writable } from 'type-fest';
import type { EventOption, Id, MNode } 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']>;
const eventMap: Record<string, EventOption[]> = reactive({});
const methodMap: Record<string, EventOption[]> = reactive({});
class Events extends BaseService {
constructor() {
super([
...canUsePluginMethods.async.map((methodName) => ({ name: methodName, isAsync: true })),
...canUsePluginMethods.sync.map((methodName) => ({ name: methodName, isAsync: false })),
]);
}
public setEvents(events: Record<string, EventOption[]>) {
Object.keys(events).forEach((type: string) => {
this.setEvent(toLine(type), events[type] || []);
});
}
public setEvent(type: string, events: EventOption[]) {
eventMap[toLine(type)] = [...events];
}
public getEvent(type: string, _data: { node?: MNode | null } = {}): EventOption[] {
return cloneDeep(eventMap[toLine(type)]) || [];
}
public setMethods(methods: Record<string, EventOption[]>) {
Object.keys(methods).forEach((type: string) => {
this.setMethod(toLine(type), methods[type] || []);
});
}
public setMethod(type: string, method: EventOption[]) {
methodMap[toLine(type)] = [...method];
}
public getMethod(type: string, _data: { node?: MNode | null; targetId?: Id } = {}) {
return cloneDeep(methodMap[toLine(type)]) || [];
}
public resetState() {
// 原地清空而不是重新赋值,保留 reactive 代理身份,已建立的 computed / 渲染副作用才能收到更新
Object.keys(eventMap).forEach((type) => delete eventMap[type]);
Object.keys(methodMap).forEach((type) => delete methodMap[type]);
}
public destroy() {
this.resetState();
this.removeAllListeners();
this.removeAllPlugins();
}
public usePlugin(options: AsyncHookPlugin<AsyncMethodName, Events> & SyncHookPlugin<SyncMethodName, Events>): void {
super.usePlugin(options);
}
}
export type EventsService = Events;
export default new Events();