选中背景色优化

This commit is contained in:
cwchen 2025-11-11 09:43:28 +08:00
parent 4e93be11a2
commit 2417dd974d
1 changed files with 66 additions and 29 deletions

View File

@ -3,7 +3,8 @@
<div class="search-toolbar"> <div class="search-toolbar">
<el-input v-model="keyword" class="search-input" placeholder="输入关键字搜索 PDF" clearable <el-input v-model="keyword" class="search-input" placeholder="输入关键字搜索 PDF" clearable
@keyup.enter.native="handleSearch" @clear="resetSearch"> @keyup.enter.native="handleSearch" @clear="resetSearch">
<el-button slot="append" icon="el-icon-search" @click="handleSearch" :loading="searching" :disabled="searching"> <el-button slot="append" icon="el-icon-search" @click="handleSearch" :loading="searching"
:disabled="searching">
搜索 搜索
</el-button> </el-button>
</el-input> </el-input>
@ -217,7 +218,7 @@ export default {
const placeholder = document.createElement('div') const placeholder = document.createElement('div')
placeholder.className = 'pdf-page placeholder' placeholder.className = 'pdf-page placeholder'
placeholder.style.margin = '0px auto 10px' placeholder.style.margin = '0px auto 10px'
placeholder.style.position= 'relative'; placeholder.style.position = 'relative';
placeholder.dataset.page = pageNumber placeholder.dataset.page = pageNumber
placeholder.dataset.status = 'placeholder' placeholder.dataset.status = 'placeholder'
fragment.appendChild(placeholder) fragment.appendChild(placeholder)
@ -541,8 +542,8 @@ export default {
console.error('全文预处理失败', error) console.error('全文预处理失败', error)
} }
} }
this.highlightMatches() this.highlightMatches()
} finally { } finally {
this.searching = false this.searching = false
@ -728,7 +729,7 @@ export default {
if (!skipNavigate) { if (!skipNavigate) {
this.navigateToResult(this.currentResultIndex, true) this.navigateToResult(this.currentResultIndex, true)
} else { } else {
this.updateCurrentResultActiveState() this.scheduleActiveHighlightRefresh()
} }
}, },
@ -749,7 +750,7 @@ export default {
} }
this.currentResultIndex = index this.currentResultIndex = index
this.updateCurrentResultActiveState() this.scheduleActiveHighlightRefresh()
const target = this.searchResults[this.currentResultIndex]?.element const target = this.searchResults[this.currentResultIndex]?.element
if (!target) return if (!target) return
@ -842,22 +843,48 @@ export default {
this.searchResults[matchIndex].element = elements[0] || null this.searchResults[matchIndex].element = elements[0] || null
}) })
this.updateCurrentResultActiveState() this.scheduleActiveHighlightRefresh()
}, },
updateCurrentResultActiveState() { updateCurrentResultActiveState() {
const activePageContainers = new Set()
this.searchResults.forEach((item, idx) => { this.searchResults.forEach((item, idx) => {
if (!item) return if (!item) return
const elements = item.elements && item.elements.length ? item.elements : item.element ? [item.element] : [] const elements = item.elements && item.elements.length ? item.elements : item.element ? [item.element] : []
elements.forEach((el) => { elements.forEach((el) => {
if (!el) return if (!el) return
if (idx === this.currentResultIndex) { if (idx === this.currentResultIndex) {
el.classList.add('is-active') el.classList.add('is-active', 'is-current')
const container = el.closest('.pdf-page')
if (container) {
activePageContainers.add(container)
}
} else { } else {
el.classList.remove('is-active') el.classList.remove('is-active', 'is-current')
} }
}) })
}) })
this.pageContainers.forEach((container) => {
if (!container) return
container.classList.remove('has-active-match')
})
activePageContainers.forEach((container) => {
container.classList.add('has-active-match')
})
},
scheduleActiveHighlightRefresh() {
this.$nextTick(() => {
if (typeof window !== 'undefined' && window.requestAnimationFrame) {
window.requestAnimationFrame(() => {
this.updateCurrentResultActiveState()
})
} else {
this.updateCurrentResultActiveState()
}
})
}, },
goToPrevious() { goToPrevious() {
@ -955,11 +982,11 @@ export default {
this.observer = new IntersectionObserver((entries) => { this.observer = new IntersectionObserver((entries) => {
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)
if (page) { if (page) {
this.renderTextLayer(page, { visible: true, force: true }) this.renderTextLayer(page, { visible: true, force: true })
this.scheduleRender(page, { priority: true }) this.scheduleRender(page, { priority: true })
} }
} else { } else {
const page = Number(entry.target.dataset.page) const page = Number(entry.target.dataset.page)
if (page) { if (page) {
@ -978,15 +1005,15 @@ export default {
initial.forEach((container) => { initial.forEach((container) => {
const page = Number(container.dataset.page) const page = Number(container.dataset.page)
if (page) { if (page) {
if (page === 1) { if (page === 1) {
// //
return return
} }
if (page <= this.initialPreloadedCount) { if (page <= this.initialPreloadedCount) {
this.scheduleRender(page, { priority: true }) this.scheduleRender(page, { priority: true })
} else { } else {
this.scheduleRender(page) this.scheduleRender(page)
} }
} }
}) })
}, },
@ -1008,8 +1035,8 @@ export default {
.document-search { .document-search {
display: flex; display: flex;
flex-direction: column; flex-direction: column;
height: calc(100vh - 84px); height: calc(100vh - 84px);
overflow: hidden; overflow: hidden;
background: #f4f7ff; background: #f4f7ff;
padding: 16px; padding: 16px;
box-sizing: border-box; box-sizing: border-box;
@ -1171,19 +1198,20 @@ export default {
0% { 0% {
transform: rotate(0deg); transform: rotate(0deg);
} }
100% { 100% {
transform: rotate(360deg); transform: rotate(360deg);
} }
} }
.pdf-page > canvas.pdf-canvas { .pdf-page>canvas.pdf-canvas {
position: relative; position: relative;
top: 0; top: 0;
left: 50%; left: 50%;
transform: translateX(-50%); transform: translateX(-50%);
} }
.pdf-page > .textLayer { .pdf-page>.textLayer {
position: absolute; position: absolute;
top: 0; top: 0;
left: 50%; left: 50%;
@ -1196,9 +1224,18 @@ export default {
border-radius: 2px; border-radius: 2px;
} }
.search-highlight.is-active { ::v-deep .search-highlight.is-active,
background: rgba(32, 109, 255, 0.85); ::v-deep .search-highlight.is-current {
background: #206dff !important;
color: #ffffff; color: #ffffff;
box-shadow: 0 0 0 2px rgba(32, 109, 255, 0.35);
border-radius: 4px;
transition: background 0.2s ease, box-shadow 0.2s ease;
}
.pdf-page.has-active-match {
box-shadow: 0 0 0 3px rgba(32, 109, 255, 0.45), 0 12px 36px rgba(32, 109, 255, 0.18);
transition: box-shadow 0.3s ease;
} }
.state-panel { .state-panel {