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