|
@@ -1,18 +1,20 @@
|
|
|
type TextSearchResult = {
|
|
|
- bottom: number
|
|
|
content: string
|
|
|
- left: number
|
|
|
+ quads: quad[]
|
|
|
pageNum: number
|
|
|
- right: number
|
|
|
pageSearchIndex: number
|
|
|
searchValue: string
|
|
|
+}
|
|
|
+type quad = {
|
|
|
+ bottom: number
|
|
|
+ left: number
|
|
|
+ right: number
|
|
|
top: number
|
|
|
}
|
|
|
|
|
|
export default class TextSearch {
|
|
|
searchContainer: HTMLDivElement | null = null
|
|
|
activeIndex = 0
|
|
|
- activeSearchResult: TextSearchResult | null = null
|
|
|
container: HTMLDivElement | null = null
|
|
|
viewport: any
|
|
|
scale: number = 0
|
|
@@ -52,15 +54,13 @@ export default class TextSearch {
|
|
|
}
|
|
|
|
|
|
setActiveSearchResult(search: TextSearchResult) {
|
|
|
- const { pageNum, left, top, right, bottom} = search
|
|
|
- const activeSearchResult = this.activeSearchResult
|
|
|
+ const { pageNum } = search
|
|
|
|
|
|
- if (search && ((activeSearchResult && (activeSearchResult.pageNum !== pageNum || left !== activeSearchResult.left || top !== activeSearchResult.top || right !== activeSearchResult.right || bottom !== activeSearchResult.bottom)) || !activeSearchResult)) {
|
|
|
+ if (search) {
|
|
|
if (this.searchContainer) {
|
|
|
- const searchEles = this.searchContainer.querySelectorAll('div')
|
|
|
+ const searchEles = this.searchContainer.querySelectorAll('.searchItems')
|
|
|
searchEles[search.pageSearchIndex].classList.add('selected')
|
|
|
}
|
|
|
- this.activeSearchResult = search
|
|
|
this.activeIndex = pageNum - 1
|
|
|
}
|
|
|
}
|
|
@@ -68,10 +68,9 @@ export default class TextSearch {
|
|
|
clearActiveSearchResult(result: TextSearchResult) {
|
|
|
if (this.searchContainer) {
|
|
|
const index = result.pageSearchIndex
|
|
|
- const searchEle = this.searchContainer.querySelectorAll('div')[index]
|
|
|
+ const searchEle = this.searchContainer.querySelectorAll('.searchItems')[index]
|
|
|
searchEle.classList.remove('selected')
|
|
|
}
|
|
|
- this.activeSearchResult = null
|
|
|
}
|
|
|
|
|
|
renderSearchResults() {
|
|
@@ -88,21 +87,26 @@ export default class TextSearch {
|
|
|
continue
|
|
|
}
|
|
|
const result = this.searchResults[i]
|
|
|
- const { left, top, right, bottom } = result
|
|
|
- const bounds = {
|
|
|
- left: left * scale,
|
|
|
- top: top * scale,
|
|
|
- width: right * scale - left * scale,
|
|
|
- height: bottom * scale - top * scale
|
|
|
+ const searchItems = document.createElement('div')
|
|
|
+ searchItems.className = 'searchItems'
|
|
|
+ for (let j = 0; j < result.quads.length; j++) {
|
|
|
+ const { left, top, right, bottom } = result.quads[j]
|
|
|
+ const bounds = {
|
|
|
+ left: left * scale,
|
|
|
+ top: top * scale,
|
|
|
+ width: right * scale - left * scale,
|
|
|
+ height: bottom * scale - top * scale
|
|
|
+ }
|
|
|
+ const div = document.createElement('div')
|
|
|
+ div.className = 'highlight'
|
|
|
+ div.style.position = 'absolute'
|
|
|
+ div.style.left = `${bounds.left}px`
|
|
|
+ div.style.top = `${bounds.top}px`
|
|
|
+ div.style.width = `${bounds.width}px`
|
|
|
+ div.style.height = `${bounds.height}px`
|
|
|
+ searchItems.appendChild(div)
|
|
|
}
|
|
|
- const div = document.createElement('div')
|
|
|
- div.className = 'highlight'
|
|
|
- div.style.position = 'absolute'
|
|
|
- div.style.left = `${bounds.left}px`
|
|
|
- div.style.top = `${bounds.top}px`
|
|
|
- div.style.width = `${bounds.width}px`
|
|
|
- div.style.height = `${bounds.height}px`
|
|
|
- this.searchContainer!.appendChild(div)
|
|
|
+ this.searchContainer!.appendChild(searchItems)
|
|
|
}
|
|
|
}
|
|
|
|