fix(editor): services在组件unmounted时只重置状态不移除事件

This commit is contained in:
roymondchen 2022-12-06 20:32:06 +08:00
parent 67c7faf832
commit 45a20d9405
9 changed files with 55 additions and 16 deletions

View File

@ -316,13 +316,12 @@ export default defineComponent({
);
onUnmounted(() => {
editorService.destroy();
historyService.destroy();
propsService.destroy();
uiService.destroy();
componentListService.destroy();
storageService.destroy();
codeBlockService.destroy();
editorService.resetState();
historyService.resetState();
propsService.resetState();
uiService.resetState();
componentListService.resetState();
codeBlockService.resetState();
});
const services: Services = {

View File

@ -344,7 +344,7 @@ class CodeBlock extends BaseService {
this.setCodeDsl(codeDsl);
}
public destroy(): void {
public resetState() {
this.state.isShowCodeEditor = false;
this.state.codeDsl = null;
this.state.id = '';
@ -354,6 +354,12 @@ class CodeBlock extends BaseService {
this.state.undeletableList = [];
}
public destroy(): void {
this.resetState();
this.removeAllListeners();
this.removeAllPlugins();
}
/**
*
* @param {MNode} node

View File

@ -42,9 +42,14 @@ class ComponentList extends BaseService {
return this.state.list;
}
public destroy() {
public resetState() {
this.state.list = [];
}
public destroy() {
this.resetState();
this.removeAllListeners();
this.removeAllPlugins();
}
}

View File

@ -207,7 +207,7 @@ class Editor extends BaseService {
if (page) {
historyService.changePage(toRaw(page));
} else {
historyService.empty();
historyService.resetState();
}
if (node?.id) {
@ -762,8 +762,7 @@ class Editor extends BaseService {
});
}
public destroy() {
this.removeAllListeners();
public resetState() {
this.set('root', null);
this.set('node', null);
this.set('nodes', []);
@ -775,6 +774,12 @@ class Editor extends BaseService {
this.set('pageLength', new Map());
}
public destroy() {
this.removeAllListeners();
this.resetState();
this.removeAllPlugins();
}
public resetModifiedNodeId() {
this.get<Map<Id, Id>>('modifiedNodeIds').clear();
}

View File

@ -76,10 +76,15 @@ class Events extends BaseService {
return cloneDeep(methodMap[type] || DEFAULT_METHODS);
}
public destroy() {
public resetState() {
eventMap = reactive({});
methodMap = reactive({});
}
public destroy() {
this.resetState();
this.removeAllListeners();
this.removeAllPlugins();
}
}

View File

@ -68,7 +68,7 @@ class History extends BaseService {
this.emit('page-change', this.state.pageSteps[this.state.pageId]);
}
public empty(): void {
public resetState(): void {
this.state.pageId = undefined;
this.state.pageSteps = {};
this.state.canRedo = false;
@ -100,8 +100,9 @@ class History extends BaseService {
}
public destroy(): void {
this.empty();
this.resetState();
this.removeAllListeners();
this.removeAllPlugins();
}
private getUndoRedo() {

View File

@ -171,10 +171,15 @@ class Props extends BaseService {
};
}
public destroy() {
public resetState() {
this.state.propsConfigMap = {};
this.state.propsValueMap = {};
}
public destroy() {
this.resetState();
this.removeAllListeners();
this.removeAllPlugins();
}
/**

View File

@ -109,6 +109,7 @@ export class WebStorage extends BaseService {
public destroy() {
this.removeAllListeners();
this.removeAllPlugins();
}
private getValueAndProtocol(value: string | null) {

View File

@ -97,8 +97,20 @@ class Ui extends BaseService {
return Math.min((width - 60) / stageWidth || 1, (height - 80) / stageHeight || 1);
}
public resetState() {
this.set('showSrc', false);
this.set('uiSelectMode', false);
this.set('zoom', 1);
this.set('stageContainerRect', {
width: 0,
height: 0,
});
}
public destroy() {
this.resetState();
this.removeAllListeners();
this.removeAllPlugins();
}
private async setStageRect(value: StageRect) {