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

View File

@ -465,4 +465,9 @@ export const removeHTMLEntities = (str) => {
// 获取一个数据的类型 // 获取一个数据的类型
export const getType = (data) => { export const getType = (data) => {
return Object.prototype.toString.call(data).slice(7, -1) 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 <el-button
size="small" size="small"
slot="append" slot="append"
v-if="!!searchText.trim()" v-if="!isUndef(searchText)"
@click="showReplaceInput = true" @click="showReplaceInput = true"
>{{ $t('search.replace') }}</el-button >{{ $t('search.replace') }}</el-button
> >
@ -49,6 +49,7 @@
<script> <script>
import { mapState } from 'vuex' import { mapState } from 'vuex'
import { isUndef } from 'simple-mind-map/src/utils/index'
// //
export default { export default {
@ -74,7 +75,7 @@ export default {
}, },
watch: { watch: {
searchText() { searchText() {
if (!this.searchText.trim()) { if (isUndef(this.searchText)) {
this.currentIndex = 0 this.currentIndex = 0
this.total = 0 this.total = 0
this.showSearchInfo = false this.showSearchInfo = false
@ -94,6 +95,8 @@ export default {
}) })
}, },
methods: { methods: {
isUndef,
hideReplaceInput() { hideReplaceInput() {
this.showReplaceInput = false this.showReplaceInput = false
this.replaceText = '' this.replaceText = ''