pdf搜索

This commit is contained in:
cwchen 2025-11-28 10:56:44 +08:00
parent f341e1822b
commit 5fa6511ee9
1 changed files with 43 additions and 8 deletions

View File

@ -515,7 +515,16 @@ export default {
try { try {
while (this.renderQueue.length) { while (this.renderQueue.length) {
const pageNumber = this.renderQueue.shift() const pageNumber = this.renderQueue.shift()
await new Promise((resolve) => requestAnimationFrame(resolve)) // 使 requestIdleCallback
if (typeof window.requestIdleCallback === 'function') {
await new Promise((resolve) => {
window.requestIdleCallback(() => {
requestAnimationFrame(resolve)
}, { timeout: 100 })
})
} else {
await new Promise((resolve) => requestAnimationFrame(resolve))
}
await this.renderSinglePage(pageNumber) await this.renderSinglePage(pageNumber)
} }
} finally { } finally {
@ -551,9 +560,11 @@ export default {
const schedule = () => { const schedule = () => {
this.prefetchScheduled = true this.prefetchScheduled = true
if (typeof window.requestIdleCallback === 'function') { if (typeof window.requestIdleCallback === 'function') {
this.prefetchHandle = window.requestIdleCallback(fetchNext, { timeout: 1000 }) //
this.prefetchHandle = window.requestIdleCallback(fetchNext, { timeout: 2000 })
} else { } else {
this.prefetchHandle = window.setTimeout(fetchNext, 80) //
this.prefetchHandle = window.setTimeout(fetchNext, 150)
} }
} }
schedule() schedule()
@ -565,10 +576,10 @@ export default {
this.isPrefetching = true this.isPrefetching = true
if (typeof window.requestIdleCallback === 'function') { if (typeof window.requestIdleCallback === 'function') {
this.prefetchScheduled = true this.prefetchScheduled = true
this.prefetchHandle = window.requestIdleCallback(fetchNext, { timeout: 800 }) this.prefetchHandle = window.requestIdleCallback(fetchNext, { timeout: 1500 })
} else { } else {
this.prefetchScheduled = true this.prefetchScheduled = true
this.prefetchHandle = window.setTimeout(fetchNext, 120) this.prefetchHandle = window.setTimeout(fetchNext, 200)
} }
}, },
@ -1211,11 +1222,19 @@ export default {
const options = { const options = {
root: this.$refs.pdfWrapper, root: this.$refs.pdfWrapper,
rootMargin: '120px 0px', rootMargin: '200px 0px', //
threshold: 0.01, threshold: [0, 0.1, 0.5, 1], // 使
} }
this.observer = new IntersectionObserver((entries) => { // 使 requestAnimationFrame IntersectionObserver
let rafScheduled = false
const pendingEntries = []
const processEntries = () => {
rafScheduled = false
const entries = [...pendingEntries]
pendingEntries.length = 0
entries.forEach((entry) => { entries.forEach((entry) => {
if (entry.isIntersecting) { if (entry.isIntersecting) {
const page = Number(entry.target.dataset.page) const page = Number(entry.target.dataset.page)
@ -1235,6 +1254,14 @@ export default {
} }
} }
}) })
}
this.observer = new IntersectionObserver((entries) => {
pendingEntries.push(...entries)
if (!rafScheduled) {
rafScheduled = true
requestAnimationFrame(processEntries)
}
}, options) }, options)
this.pageContainers.forEach((container) => this.observer.observe(container)) this.pageContainers.forEach((container) => this.observer.observe(container))
@ -1597,6 +1624,8 @@ export default {
scrollbar-gutter: stable both-edges; scrollbar-gutter: stable both-edges;
-webkit-overflow-scrolling: touch; -webkit-overflow-scrolling: touch;
overflow-x: auto; overflow-x: auto;
/* 性能优化 */
will-change: scroll-position;
} }
.pdf-page { .pdf-page {
@ -1697,6 +1726,10 @@ export default {
.pdf-canvas { .pdf-canvas {
display: block; display: block;
margin: 0 auto; margin: 0 auto;
/* 性能优化 */
image-rendering: -webkit-optimize-contrast;
image-rendering: crisp-edges;
will-change: contents;
} }
.textLayer { .textLayer {
@ -1708,6 +1741,8 @@ export default {
pointer-events: auto; pointer-events: auto;
user-select: text; user-select: text;
-webkit-user-select: text; -webkit-user-select: text;
/* 性能优化 */
will-change: contents;
} }
.textLayer>span { .textLayer>span {