搜索优化
This commit is contained in:
parent
55bff30922
commit
8ba255e11a
|
|
@ -185,7 +185,7 @@ export default {
|
|||
console.log('PDF 文档加载成功', this.pdfDoc)
|
||||
await this.renderAllPages()
|
||||
if (this.keyword) {
|
||||
this.highlightMatches()
|
||||
await this.highlightMatches()
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('加载 PDF 失败:', err)
|
||||
|
|
@ -545,7 +545,7 @@ export default {
|
|||
}
|
||||
|
||||
|
||||
this.highlightMatches()
|
||||
await this.highlightMatches()
|
||||
} finally {
|
||||
this.searching = false
|
||||
}
|
||||
|
|
@ -563,7 +563,7 @@ export default {
|
|||
}
|
||||
},
|
||||
|
||||
highlightMatches(options = {}) {
|
||||
async highlightMatches(options = {}) {
|
||||
const { preserveIndex = false, skipNavigate = false } = options
|
||||
const keyword = this.keyword.trim()
|
||||
if (!keyword) {
|
||||
|
|
@ -577,11 +577,19 @@ export default {
|
|||
const escapedKeyword = keyword.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
|
||||
const pattern = new RegExp(`(${escapedKeyword})`, 'gi')
|
||||
const results = []
|
||||
this.pageMatchRanges = Array.from({ length: this.totalPages || 0 }, () => [])
|
||||
const matchRanges = Array.from({ length: this.totalPages || 0 }, () => [])
|
||||
|
||||
this.pageTextDivs.forEach((textDivs, pageIndex) => {
|
||||
for (let pageIndex = 0; pageIndex < this.totalPages; pageIndex += 1) {
|
||||
let textDivs = this.pageTextDivs[pageIndex]
|
||||
if (!textDivs || !textDivs.length) {
|
||||
return
|
||||
const container = this.pageContainers[pageIndex]
|
||||
const isRendered = container?.dataset.status === 'rendered'
|
||||
await this.renderTextLayer(pageIndex + 1, { visible: !!isRendered, force: true })
|
||||
await this.$nextTick()
|
||||
textDivs = this.pageTextDivs[pageIndex]
|
||||
}
|
||||
if (!textDivs || !textDivs.length) {
|
||||
continue
|
||||
}
|
||||
|
||||
const segments = []
|
||||
|
|
@ -599,12 +607,12 @@ export default {
|
|||
})
|
||||
|
||||
if (!segments.length) {
|
||||
return
|
||||
continue
|
||||
}
|
||||
|
||||
const pageText = segments.map((seg) => seg.text).join('')
|
||||
if (!pageText) {
|
||||
return
|
||||
continue
|
||||
}
|
||||
|
||||
pattern.lastIndex = 0
|
||||
|
|
@ -654,7 +662,7 @@ export default {
|
|||
}
|
||||
|
||||
if (!matchRecords.length) {
|
||||
return
|
||||
continue
|
||||
}
|
||||
|
||||
perDivHighlights.forEach((ranges, div) => {
|
||||
|
|
@ -692,8 +700,7 @@ export default {
|
|||
})
|
||||
})
|
||||
|
||||
const pageMatches = this.pageMatchRanges[pageIndex] || []
|
||||
|
||||
const pageMatches = matchRanges[pageIndex]
|
||||
matchRecords.forEach((record) => {
|
||||
if (!record.elements.length) return
|
||||
const newIndex = results.length
|
||||
|
|
@ -710,10 +717,9 @@ export default {
|
|||
segments: record.segments.map((item) => ({ ...item })),
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
this.pageMatchRanges[pageIndex] = pageMatches
|
||||
})
|
||||
|
||||
this.pageMatchRanges = matchRanges
|
||||
this.searchResults = results
|
||||
if (!results.length) {
|
||||
this.currentResultIndex = -1
|
||||
|
|
@ -728,7 +734,7 @@ export default {
|
|||
}
|
||||
|
||||
if (!skipNavigate) {
|
||||
this.navigateToResult(this.currentResultIndex, true)
|
||||
await this.navigateToResult(this.currentResultIndex, true)
|
||||
} else {
|
||||
this.scheduleActiveHighlightRefresh()
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue