@@ -8,6 +8,8 @@ export class SearchController {
88 this . currentMatchIndex = - 1 ;
99 this . searchAbortController = null ;
1010 this . highlightOverlay = null ; // Container for highlights
11+ this . activeTextLayer = null ;
12+ this . activePage = 0 ;
1113 }
1214
1315 async find ( query ) {
@@ -115,12 +117,20 @@ export class SearchController {
115117 }
116118
117119 showMatch ( match ) {
118- // Navigate to the page
120+ if ( match . pageNum === this . activePage && this . activeTextLayer ) {
121+ // Already on the same page, just redraw highlights to try to avoid full re-render
122+ // Update: calling getPageCallback might be ignored by index.js if page is same,
123+ // so we MUST redraw manually here to update the 'selected' highlight.
124+ this . drawPageMatches ( match . pageNum , this . activeTextLayer ) ;
125+ }
126+ // Always request page navigation to ensure we are on the right page
119127 this . getPageCallback ( match . pageNum ) ;
120128 }
121129
122130 drawPageMatches ( pageNum , textLayerDiv , skipScroll = false ) {
123131 this . removeHighlights ( ) ;
132+ this . activeTextLayer = textLayerDiv ;
133+ this . activePage = pageNum ;
124134
125135 if ( this . matches . length === 0 ) {
126136 return ;
@@ -142,10 +152,6 @@ export class SearchController {
142152 const currentMatch = ( this . currentMatchIndex !== - 1 ) ? this . matches [ this . currentMatchIndex ] : null ;
143153 let selectedDiv = null ;
144154
145- // Get the scale factor from CSS custom property
146- const containerEl = document . getElementById ( "container" ) ;
147- const scaleFactor = parseFloat ( containerEl . style . getPropertyValue ( "--scale-factor" ) ) || 1 ;
148-
149155 let currentLen = 0 ;
150156
151157 spans . forEach ( ( span ) => {
@@ -181,12 +187,11 @@ export class SearchController {
181187 }
182188 }
183189
184- // Calculate positions relative to text layer using current viewport positions
185- // and then normalize by the scale factor to get unscaled coordinates
186- const left = ( rect . left - textLayerRect . left ) / scaleFactor ;
187- const top = ( rect . top - textLayerRect . top ) / scaleFactor ;
188- const width = rect . width / scaleFactor ;
189- const height = rect . height / scaleFactor ;
190+ // Calculate positions relative to text layer
191+ const left = rect . left - textLayerRect . left ;
192+ const top = rect . top - textLayerRect . top ;
193+ const width = rect . width ;
194+ const height = rect . height ;
190195
191196 div . style . left = left + "px" ;
192197 div . style . top = top + "px" ;
0 commit comments