feat(editor): 编辑器销毁后销毁services

This commit is contained in:
roymondchen 2022-08-29 20:17:54 +08:00 committed by jia000
parent 6c0b92e808
commit 4a59b4b0f4
7 changed files with 53 additions and 13 deletions

View File

@ -267,7 +267,16 @@ export default defineComponent({
}, },
); );
onUnmounted(() => editorService.destroy()); uiService.initColumnWidth();
onUnmounted(() => {
editorService.destroy();
historyService.destroy();
propsService.destroy();
uiService.destroy();
componentListService.destroy();
storageService.destroy();
});
const services: Services = { const services: Services = {
componentListService, componentListService,

View File

@ -41,6 +41,11 @@ class ComponentList extends BaseService {
public getList(): ComponentGroup[] { public getList(): ComponentGroup[] {
return this.state.list; return this.state.list;
} }
public destroy() {
this.state.list = [];
this.removeAllListeners();
}
} }
export type ComponentListService = ComponentList; export type ComponentListService = ComponentList;

View File

@ -741,6 +741,10 @@ class Editor extends BaseService {
this.set('nodes', []); this.set('nodes', []);
this.set('page', null); this.set('page', null);
this.set('parent', null); this.set('parent', null);
this.set('stage', null);
this.set('highlightNode', null);
this.set('modifiedNodeIds', new Map());
this.set('pageLength', new Map());
} }
public resetModifiedNodeId() { public resetModifiedNodeId() {

View File

@ -26,8 +26,8 @@ import type { ComponentGroup } from '../type';
import BaseService from './BaseService'; import BaseService from './BaseService';
const eventMap: Record<string, EventOption[]> = reactive({}); let eventMap: Record<string, EventOption[]> = reactive({});
const methodMap: Record<string, EventOption[]> = reactive({}); let methodMap: Record<string, EventOption[]> = reactive({});
class Events extends BaseService { class Events extends BaseService {
constructor() { constructor() {
@ -75,6 +75,12 @@ class Events extends BaseService {
public getMethod(type: string) { public getMethod(type: string) {
return cloneDeep(methodMap[type] || DEFAULT_METHODS); return cloneDeep(methodMap[type] || DEFAULT_METHODS);
} }
public destroy() {
eventMap = reactive({});
methodMap = reactive({});
this.removeAllListeners();
}
} }
export type EventsService = Events; export type EventsService = Events;

View File

@ -171,6 +171,12 @@ class Props extends BaseService {
}; };
} }
public destroy() {
this.state.propsConfigMap = {};
this.state.propsValueMap = {};
this.removeAllListeners();
}
/** /**
* GUID- * GUID-
* @param digit 8 * @param digit 8

View File

@ -107,6 +107,10 @@ export class WebStorage extends BaseService {
storage.setItem(`${options.namespace || namespace}:${key}`, item); storage.setItem(`${options.namespace || namespace}:${key}`, item);
} }
public destroy() {
this.removeAllListeners();
}
private getValueAndProtocol(value: string | null) { private getValueAndProtocol(value: string | null) {
let protocol = ''; let protocol = '';

View File

@ -64,16 +64,6 @@ class Ui extends BaseService {
center: 'auto', center: 'auto',
}); });
}); });
const columnWidthCacheData = globalThis.localStorage.getItem(COLUMN_WIDTH_STORAGE_KEY);
if (columnWidthCacheData) {
try {
const columnWidthCache = JSON.parse(columnWidthCacheData);
this.setColumnWidth(columnWidthCache);
} catch (e) {
console.error(e);
}
}
} }
public set<T = any>(name: keyof UiState, value: T) { public set<T = any>(name: keyof UiState, value: T) {
@ -104,6 +94,18 @@ class Ui extends BaseService {
return (state as any)[name]; return (state as any)[name];
} }
public initColumnWidth() {
const columnWidthCacheData = globalThis.localStorage.getItem(COLUMN_WIDTH_STORAGE_KEY);
if (columnWidthCacheData) {
try {
const columnWidthCache = JSON.parse(columnWidthCacheData);
this.setColumnWidth(columnWidthCache);
} catch (e) {
console.error(e);
}
}
}
public zoom(zoom: number) { public zoom(zoom: number) {
this.set('zoom', (this.get<number>('zoom') * 100 + zoom * 100) / 100); this.set('zoom', (this.get<number>('zoom') * 100 + zoom * 100) / 100);
if (this.get<number>('zoom') < 0.1) this.set('zoom', 0.1); if (this.get<number>('zoom') < 0.1) this.set('zoom', 0.1);
@ -125,6 +127,10 @@ class Ui extends BaseService {
return Math.min((width - 60) / stageWidth || 1, (height - 80) / stageHeight || 1); return Math.min((width - 60) / stageWidth || 1, (height - 80) / stageHeight || 1);
} }
public destroy() {
this.removeAllListeners();
}
private setColumnWidth({ left, center, right }: SetColumnWidth) { private setColumnWidth({ left, center, right }: SetColumnWidth) {
const columnWidth = { const columnWidth = {
...toRaw(this.get<GetColumnWidth>('columnWidth')), ...toRaw(this.get<GetColumnWidth>('columnWidth')),