pdf 搜索优化

This commit is contained in:
cwchen 2025-11-28 10:49:37 +08:00
parent add19a94aa
commit f341e1822b
1 changed files with 70 additions and 8 deletions

View File

@ -73,7 +73,7 @@ if (resolvedWorkerSrc) {
pdfjsLib.GlobalWorkerOptions.workerSrc = null
}
const DEFAULT_SCALE = 1
const DEFAULT_SCALE = 1.1
const DEFAULT_PDF_URL = 'http://192.168.0.14:9090/smart-bid/technicalSolutionDatabase/2025/10/30/fe5b46ea37554516a71e7c0c486d3715.pdf'
export default {
@ -867,6 +867,9 @@ export default {
this.currentResultIndex = index
this.scheduleActiveHighlightRefresh()
//
await this.$nextTick()
const performScroll = () => {
const target = this.searchResults[this.currentResultIndex]?.element
@ -876,24 +879,83 @@ export default {
const container = target.closest('.pdf-page') || target
if (!container) return
// pdf-page
//
if (!useSmoothScroll) {
//
const targetRect = target.getBoundingClientRect()
const wrapperRect = wrapper.getBoundingClientRect()
const containerRect = container.getBoundingClientRect()
// wrapper
const targetTop = container.offsetTop + (targetRect.top - containerRect.top)
const targetLeft = container.offsetLeft + (targetRect.left - containerRect.left)
const targetHeight = targetRect.height || 20
const targetWidth = targetRect.width || 50
//
const viewportHeight = wrapper.clientHeight
const viewportWidth = wrapper.clientWidth
const verticalPadding = 80 //
const horizontalPadding = 40 //
//
let scrollTop = wrapper.scrollTop
const targetTopInViewport = targetTop - wrapper.scrollTop
if (targetTopInViewport < verticalPadding) {
//
scrollTop = targetTop - verticalPadding
} else if (targetTopInViewport + targetHeight > viewportHeight - verticalPadding) {
//
scrollTop = targetTop + targetHeight - viewportHeight + verticalPadding
}
// canvas
let scrollLeft = wrapper.scrollLeft
const targetLeftInViewport = targetLeft - wrapper.scrollLeft
if (targetLeftInViewport < horizontalPadding) {
//
scrollLeft = targetLeft - horizontalPadding
} else if (targetLeftInViewport + targetWidth > viewportWidth - horizontalPadding) {
//
scrollLeft = targetLeft + targetWidth - viewportWidth + horizontalPadding
}
//
this.cancelScrollAnimation()
wrapper.scrollTop = container.offsetTop
wrapper.scrollTop = Math.max(0, Math.min(scrollTop, wrapper.scrollHeight - viewportHeight))
wrapper.scrollLeft = Math.max(0, Math.min(scrollLeft, wrapper.scrollWidth - viewportWidth))
return
}
//
const wrapperOffsetTop = container.offsetTop
const containerHeight = container.offsetHeight || target.offsetHeight || 0
const desired = wrapperOffsetTop - Math.max((wrapper.clientHeight - containerHeight) / 2, 0)
this.smoothScrollTo(wrapper, desired)
}
if (useSmoothScroll) {
this.$nextTick(() => performScroll())
} else {
performScroll()
}
// DOM
await this.$nextTick()
// 使 requestAnimationFrame
requestAnimationFrame(() => {
requestAnimationFrame(() => {
//
const currentTarget = this.searchResults[this.currentResultIndex]?.element
if (currentTarget && currentTarget.isConnected) {
performScroll()
} else {
// elements
const result = this.searchResults[this.currentResultIndex]
if (result && result.elements && result.elements.length > 0) {
// element
result.element = result.elements[0]
performScroll()
}
}
})
})
},
applyHighlightsToPage(pageIndex) {