Skip to content

Commit

Permalink
fix(editor): 组件销毁时移除service的时间监听
Browse files Browse the repository at this point in the history
  • Loading branch information
roymondchen committed Dec 13, 2022
1 parent 841b75e commit cfc57f1
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 11 deletions.
8 changes: 6 additions & 2 deletions packages/editor/src/Editor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ export default defineComponent({
emits: ['props-panel-mounted', 'update:modelValue'],
setup(props, { emit }) {
editorService.on('root-change', (value, preValue) => {
const rootChangeHandler = (value: MApp, preValue?: MApp | null) => {
const nodeId = editorService.get<MNode | null>('node')?.id || props.defaultSelected;
let node;
if (nodeId) {
Expand All @@ -247,7 +247,9 @@ export default defineComponent({
if (toRaw(value) !== toRaw(preValue)) {
emit('update:modelValue', value);
}
});
};
editorService.on('root-change', rootChangeHandler);
// 初始值变化,重新设置节点信息
watch(
Expand Down Expand Up @@ -326,6 +328,8 @@ export default defineComponent({
uiService.resetState();
componentListService.resetState();
codeBlockService.resetState();
editorService.off('root-change', rootChangeHandler);
});
const services: Services = {
Expand Down
6 changes: 5 additions & 1 deletion packages/editor/src/layouts/PropsPanel.vue
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
</template>

<script lang="ts" setup name="MEditorPropsPanel">
import { computed, getCurrentInstance, inject, onMounted, ref, watchEffect } from 'vue';
import { computed, getCurrentInstance, inject, onMounted, onUnmounted, ref, watchEffect } from 'vue';
import { tMagicMessage } from '@tmagic/design';
import type { FormValue } from '@tmagic/form';
Expand Down Expand Up @@ -56,6 +56,10 @@ onMounted(() => {
emit('mounted', internalInstance);
});
onUnmounted(() => {
services?.propsService.off('props-configs-change', init);
});
watchEffect(() => {
if (configForm.value && stage.value) {
configForm.value.formState.stage = stage.value;
Expand Down
16 changes: 10 additions & 6 deletions packages/editor/src/layouts/sidebar/LayerPanel.vue
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,6 @@ const filterText = ref('');
// 默认展开节点
const defaultExpandedKeys = computed(() => (selectedIds.value.length > 0 ? selectedIds.value : []));
editorService?.on('remove', () => {
setTimeout(() => {
tree.value?.getNode(editorService.get('node').id)?.updateChildren();
}, 0);
});
// 触发画布单选
const select = async (data: MNode) => {
if (!data.id) {
Expand Down Expand Up @@ -273,9 +267,17 @@ const mouseleaveHandler = () => {
isMultiSelectStatus.value = false;
};
const editorServiceRemoveHandler = () => {
setTimeout(() => {
tree.value?.getNode(editorService?.get('node').id)?.updateChildren();
}, 0);
};
let keycon: KeyController;
onMounted(() => {
editorService?.on('remove', editorServiceRemoveHandler);
keycon = new KeyController(tree.value?.$el);
const isMac = /mac os x/.test(navigator.userAgent.toLowerCase());
const ctrl = isMac ? 'meta' : 'ctrl';
Expand All @@ -293,6 +295,8 @@ onMounted(() => {
onUnmounted(() => {
keycon.destroy();
editorService?.off('remove', editorServiceRemoveHandler);
});
// 鼠标是否按下标志,用于高亮状态互斥
Expand Down
8 changes: 6 additions & 2 deletions packages/editor/src/services/BaseService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,12 @@ export default class extends EventEmitter {
}

public removeAllPlugins() {
this.pluginOptionsList = {};
this.middleware = {};
Object.keys(this.pluginOptionsList).forEach((key) => {
this.pluginOptionsList[key] = [];
});
Object.keys(this.middleware).forEach((key) => {
this.middleware[key] = [];
});
}

private async doTask() {
Expand Down

0 comments on commit cfc57f1

Please sign in to comment.