Feat:搜索支持搜索空白字符和替换为空白字符

This commit is contained in:
wanglin2
2023-07-28 17:34:55 +08:00
parent a2acf810cb
commit cd4f1b1bd8
3 changed files with 25 additions and 9 deletions

View File

@@ -1,4 +1,4 @@
import { bfsWalk, getTextFromHtml } from '../utils/index'
import { bfsWalk, getTextFromHtml, isUndef } from '../utils/index'
// 搜索插件
class Search {
@@ -30,8 +30,8 @@ class Search {
// 搜索
search(text, callback) {
text = String(text).trim()
if (!text) return this.endSearch()
if (isUndef(text)) return this.endSearch()
text = String(text)
this.isSearching = true
if (this.searchText === text) {
// 和上一次搜索文本一样,那么搜索下一个
@@ -89,9 +89,13 @@ class Search {
// 替换当前节点
replace(replaceText) {
replaceText = String(replaceText).trim()
if (!replaceText || !this.isSearching || this.matchNodeList.length <= 0)
if (
isUndef(replaceText) ||
!this.isSearching ||
this.matchNodeList.length <= 0
)
return
replaceText = String(replaceText)
let currentNode = this.matchNodeList[this.currentIndex]
if (!currentNode) return
let text = this.getReplacedText(currentNode, this.searchText, replaceText)
@@ -110,9 +114,13 @@ class Search {
// 替换所有
replaceAll(replaceText) {
replaceText = String(replaceText).trim()
if (!replaceText || !this.isSearching || this.matchNodeList.length <= 0)
if (
isUndef(replaceText) ||
!this.isSearching ||
this.matchNodeList.length <= 0
)
return
replaceText = String(replaceText)
this.matchNodeList.forEach(node => {
let text = this.getReplacedText(node, this.searchText, replaceText)
this.mindMap.renderer.setNodeDataRender(

View File

@@ -465,4 +465,9 @@ export const removeHTMLEntities = (str) => {
// 获取一个数据的类型
export const getType = (data) => {
return Object.prototype.toString.call(data).slice(7, -1)
}
// 判断一个数据是否是null和undefined和空字符串
export const isUndef = (data) => {
return data === null || data === undefined || data === ''
}

View File

@@ -15,7 +15,7 @@
<el-button
size="small"
slot="append"
v-if="!!searchText.trim()"
v-if="!isUndef(searchText)"
@click="showReplaceInput = true"
>{{ $t('search.replace') }}</el-button
>
@@ -49,6 +49,7 @@
<script>
import { mapState } from 'vuex'
import { isUndef } from 'simple-mind-map/src/utils/index'
// 搜索替换
export default {
@@ -74,7 +75,7 @@ export default {
},
watch: {
searchText() {
if (!this.searchText.trim()) {
if (isUndef(this.searchText)) {
this.currentIndex = 0
this.total = 0
this.showSearchInfo = false
@@ -94,6 +95,8 @@ export default {
})
},
methods: {
isUndef,
hideReplaceInput() {
this.showReplaceInput = false
this.replaceText = ''