From 43969af14bbaefc86637a7029b0de4e03c049b52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=A1=97=E8=A7=92=E5=B0=8F=E6=9E=97?= <1013335014@qq.com> Date: Thu, 8 Aug 2024 11:52:02 +0800 Subject: [PATCH] =?UTF-8?q?Fix=EF=BC=9A=E4=BF=AE=E5=A4=8D=E5=8F=AA?= =?UTF-8?q?=E8=AF=BB=E6=A8=A1=E5=BC=8F=E4=B8=8B=E6=90=9C=E7=B4=A2=E8=8A=82?= =?UTF-8?q?=E7=82=B9=E7=9A=84=E9=AB=98=E4=BA=AE=E4=B8=8D=E4=BC=9A=E6=B6=88?= =?UTF-8?q?=E5=A4=B1=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- simple-mind-map/index.js | 4 +++- simple-mind-map/src/plugins/Search.js | 28 ++++++++++++++++++++++++++- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/simple-mind-map/index.js b/simple-mind-map/index.js index 6d2bfceb..66b643eb 100644 --- a/simple-mind-map/index.js +++ b/simple-mind-map/index.js @@ -410,7 +410,9 @@ class MindMap { if (![CONSTANTS.MODE.READONLY, CONSTANTS.MODE.EDIT].includes(mode)) { return } - this.opt.readonly = mode === CONSTANTS.MODE.READONLY + const isReadonly = mode === CONSTANTS.MODE.READONLY + if (isReadonly === this.opt.readonly) return + this.opt.readonly = isReadonly if (this.opt.readonly) { // 取消当前激活的元素 this.execCommand('CLEAR_ACTIVE_NODE') diff --git a/simple-mind-map/src/plugins/Search.js b/simple-mind-map/src/plugins/Search.js index 8e7727ac..722c4cc8 100644 --- a/simple-mind-map/src/plugins/Search.js +++ b/simple-mind-map/src/plugins/Search.js @@ -5,6 +5,7 @@ import { replaceHtmlText } from '../utils/index' import Node from '../core/render/node/Node' +import { CONSTANTS } from '../constants/constant' // 搜索插件 class Search { @@ -29,11 +30,14 @@ class Search { bindEvent() { this.onDataChange = this.onDataChange.bind(this) + this.onModeChange = this.onModeChange.bind(this) this.mindMap.on('data_change', this.onDataChange) + this.mindMap.on('mode_change', this.onModeChange) } unBindEvent() { this.mindMap.off('data_change', this.onDataChange) + this.mindMap.off('mode_change', this.onModeChange) } // 节点数据改变了,需要重新搜索 @@ -50,6 +54,19 @@ class Search { this.searchText = '' } + // 监听只读模式切换 + onModeChange(mode) { + const isReadonly = mode === CONSTANTS.MODE.READONLY + // 如果是由只读模式切换为非只读模式,需要清除只读模式下的节点高亮 + if ( + !isReadonly && + this.isSearching && + this.matchNodeList[this.currentIndex] + ) { + this.matchNodeList[this.currentIndex].closeHighlight() + } + } + // 搜索 search(text, callback = () => {}) { if (isUndef(text)) return this.endSearch() @@ -117,6 +134,15 @@ class Search { } else { this.currentIndex = 0 } + const { readonly } = this.mindMap.opt + // 只读模式下需要激活之前节点的高亮 + if (readonly) { + this.matchNodeList.forEach(node => { + if (this.isNodeInstance(node)) { + node.closeHighlight() + } + }) + } const currentNode = this.matchNodeList[this.currentIndex] this.notResetSearchText = true const uid = this.isNodeInstance(currentNode) @@ -129,7 +155,7 @@ class Search { } callback() // 只读模式下节点无法激活,所以通过高亮的方式 - if (this.mindMap.opt.readonly) { + if (readonly) { node.highlight() } // 如果当前节点实例已经存在,则不会触发data_change事件,那么需要手动把标志复位