Fix:修复部分事件在思维导图卸载后未取消监听的问题

This commit is contained in:
wanglin2 2023-11-22 16:23:04 +08:00
parent cadd159a46
commit 7f9a1e9309
5 changed files with 68 additions and 32 deletions

View File

@ -486,6 +486,7 @@ class MindMap {
// 销毁 // 销毁
destroy() { destroy() {
this.emit('beforeDestroy')
// 移除插件 // 移除插件
;[...MindMap.pluginList].forEach(plugin => { ;[...MindMap.pluginList].forEach(plugin => {
if (this[plugin.instanceName].beforePluginDestroy) { if (this[plugin.instanceName].beforePluginDestroy) {

View File

@ -38,6 +38,7 @@ export default class KeyCommand {
// 绑定事件 // 绑定事件
bindEvent() { bindEvent() {
this.onKeydown = this.onKeydown.bind(this)
// 只有当鼠标在画布内才响应快捷键 // 只有当鼠标在画布内才响应快捷键
this.mindMap.on('svg_mouseenter', () => { this.mindMap.on('svg_mouseenter', () => {
this.isInSvg = true this.isInSvg = true
@ -55,7 +56,19 @@ export default class KeyCommand {
} }
this.isInSvg = false this.isInSvg = false
}) })
window.addEventListener('keydown', e => { window.addEventListener('keydown', this.onKeydown)
this.mindMap.on('beforeDestroy', () => {
this.unBindEvent()
})
}
// 解绑事件
unBindEvent() {
window.removeEventListener('keydown', this.onKeydown)
}
// 按键事件
onKeydown(e) {
if ( if (
this.isPause || this.isPause ||
(this.mindMap.opt.enableShortcutOnlyWhenMouseInSvg && !this.isInSvg) (this.mindMap.opt.enableShortcutOnlyWhenMouseInSvg && !this.isInSvg)
@ -74,7 +87,6 @@ export default class KeyCommand {
}) })
} }
}) })
})
} }
// 检查键值是否符合 // 检查键值是否符合

View File

@ -28,6 +28,7 @@ export default class TextEdit {
bindEvent() { bindEvent() {
this.show = this.show.bind(this) this.show = this.show.bind(this)
this.onScale = this.onScale.bind(this) this.onScale = this.onScale.bind(this)
this.onKeydown = this.onKeydown.bind(this)
// 节点双击事件 // 节点双击事件
this.mindMap.on('node_dblclick', this.show) this.mindMap.on('node_dblclick', this.show)
// 点击事件 // 点击事件
@ -63,7 +64,20 @@ export default class TextEdit {
this.mindMap.on('scale', this.onScale) this.mindMap.on('scale', this.onScale)
// // 监听按键事件,判断是否自动进入文本编辑模式 // // 监听按键事件,判断是否自动进入文本编辑模式
if (this.mindMap.opt.enableAutoEnterTextEditWhenKeydown) { if (this.mindMap.opt.enableAutoEnterTextEditWhenKeydown) {
window.addEventListener('keydown', e => { window.addEventListener('keydown', this.onKeydown)
}
this.mindMap.on('beforeDestroy', () => {
this.unBindEvent()
})
}
// 解绑事件
unBindEvent() {
window.removeEventListener('keydown', this.onKeydown)
}
// 按键事件
onKeydown(e) {
const activeNodeList = this.mindMap.renderer.activeNodeList const activeNodeList = this.mindMap.renderer.activeNodeList
if (activeNodeList.length <= 0 || activeNodeList.length > 1) return if (activeNodeList.length <= 0 || activeNodeList.length > 1) return
const node = activeNodeList[0] const node = activeNodeList[0]
@ -71,8 +85,6 @@ export default class TextEdit {
if (node && this.checkIsAutoEnterTextEditKey(e)) { if (node && this.checkIsAutoEnterTextEditKey(e)) {
this.show(node, e, false, true) this.show(node, e, false, true)
} }
})
}
} }
// 判断是否是自动进入文本编模式的按钮 // 判断是否是自动进入文本编模式的按钮

View File

@ -651,11 +651,13 @@ class RichText {
beforePluginRemove() { beforePluginRemove() {
this.transformAllNodesToNormalNode() this.transformAllNodesToNormalNode()
document.head.removeChild(this.styleEl) document.head.removeChild(this.styleEl)
this.unbindEvent()
} }
// 插件被卸载前做的事情 // 插件被卸载前做的事情
beforePluginDestroy() { beforePluginDestroy() {
document.head.removeChild(this.styleEl) document.head.removeChild(this.styleEl)
this.unbindEvent()
} }
} }

View File

@ -308,11 +308,15 @@ export default {
enableAutoEnterTextEditWhenKeydown: true, enableAutoEnterTextEditWhenKeydown: true,
customHandleClipboardText: handleClipboardText, customHandleClipboardText: handleClipboardText,
handleIsSplitByWrapOnPasteCreateNewNode: () => { handleIsSplitByWrapOnPasteCreateNewNode: () => {
return this.$confirm(this.$t('edit.splitByWrap'), this.$t('edit.tip'), { return this.$confirm(
this.$t('edit.splitByWrap'),
this.$t('edit.tip'),
{
confirmButtonText: this.$t('edit.yes'), confirmButtonText: this.$t('edit.yes'),
cancelButtonText: this.$t('edit.no'), cancelButtonText: this.$t('edit.no'),
type: 'warning' type: 'warning'
}) }
)
} }
// isUseCustomNodeContent: true, // isUseCustomNodeContent: true,
// 1routerstorei18nvue西 // 1routerstorei18nvue西
@ -399,6 +403,11 @@ export default {
} }
// //
this.cooperateTest() this.cooperateTest()
//
// setTimeout(() => {
// console.log('')
// this.mindMap.destroy()
// }, 10000)
}, },
// url // url