Skip to content

Commit

Permalink
修复计数为零时的BUG、移除无效代码
Browse files Browse the repository at this point in the history
  • Loading branch information
TCOTC committed Mar 26, 2024
1 parent 98e8cba commit 8c5d5a6
Showing 1 changed file with 7 additions and 33 deletions.
40 changes: 7 additions & 33 deletions src/Search.vue
Original file line number Diff line number Diff line change
Expand Up @@ -99,32 +99,7 @@ function highlightHitResult(value: string) {
const str = value.trim().toLowerCase()
if (!str) return
/*
// 查找所有文本节点是否包含搜索词
const ranges = allTextNodes
.map((el) => {
return { el, text: el.textContent.toLowerCase() }
})
.map(({ el, text }) => {
const indices = []
let startPos = 0
while (startPos < text.length) {
const index = text.indexOf(str, startPos)
if (index === -1) break
indices.push(index)
startPos = index + str.length
}
// 根据搜索词的位置创建选区
return indices.map((index) => {
const range = new Range()
range.setStart(el, index)
range.setEnd(el, index + str.length)
return range
})
})
*/
// 替换为:
// 查找所有文本节点是否包含搜索词,并创建对应的 Range 对象
let ranges = [];
allTextNodes.forEach((node) => {
Expand All @@ -143,10 +118,6 @@ function highlightHitResult(value: string) {
}
});
// 创建高亮对象
const searchResultsHighlight = new Highlight(...ranges.flat())
resultCount.value = ranges.flat().length
Expand Down Expand Up @@ -174,17 +145,20 @@ function clickLast() {
else if (resultIndex.value <= 1) {
resultIndex.value = resultCount.value
}
else if (resultCount.value == 0) {
return;
}
scroollIntoRanges(resultIndex.value -1)
}
function clickNext() {
if (resultIndex.value < resultCount.value) {
resultIndex.value = resultIndex.value + 1
}
else if (resultIndex.value >= resultCount.value || resultCount.value != 0) {
else if (resultIndex.value >= resultCount.value && resultCount.value != 0) {
resultIndex.value = 1
}
else if (resultCount.value = 0) {
resultIndex.value = 0
else if (resultCount.value == 0) {
return;
}
scroollIntoRanges(resultIndex.value -1)
}
Expand Down

0 comments on commit 8c5d5a6

Please sign in to comment.