From 8ba255e11aa31c5cfb25a31291fa2190c4610629 Mon Sep 17 00:00:00 2001 From: cwchen <1048842385@qq.com> Date: Tue, 11 Nov 2025 10:17:01 +0800 Subject: [PATCH] =?UTF-8?q?=E6=90=9C=E7=B4=A2=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/views/common/DocumentSearch.vue | 36 +++++++++++++++++------------ 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/src/views/common/DocumentSearch.vue b/src/views/common/DocumentSearch.vue index f7f7f1c..ca03020 100644 --- a/src/views/common/DocumentSearch.vue +++ b/src/views/common/DocumentSearch.vue @@ -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() }