@@ -54,15 +54,26 @@ export default class FindInText {
5454 const options = { matchCase : matchCase , wholeWords : wholeWord } ;
5555 findResult = textEditor . execute < CKFindResult > ( "find" , searchTerm , options ) ;
5656 totalFound = findResult . results . length ;
57- // Find the result beyond the cursor
58- const cursorPos = model . document . selection . getLastPosition ( ) ;
59- for ( let i = 0 ; i < findResult . results . length ; ++ i ) {
60- const marker = findResult . results . get ( i ) . marker ;
61- const fromPos = marker . getStart ( ) ;
62- if ( cursorPos && fromPos . compareWith ( cursorPos ) !== "before" ) {
63- currentFound = i ;
64- break ;
57+ const selection = model . document . selection ;
58+ // If text is selected, highlight the corresponding result;
59+ // otherwise, highlight the first visible result in the scrolling container.
60+ if ( ! selection . isCollapsed ) {
61+ const cursorPos = selection . getFirstPosition ( ) ;
62+ for ( let i = 0 ; i < findResult . results . length ; ++ i ) {
63+ const marker = findResult . results . get ( i ) . marker ;
64+ const fromPos = marker . getStart ( ) ;
65+ if ( cursorPos && fromPos . compareWith ( cursorPos ) !== "before" ) {
66+ currentFound = i ;
67+ break ;
68+ }
6569 }
70+ } else {
71+ const editorEl = textEditor ?. sourceElement ;
72+ const findResultElement = editorEl . querySelectorAll ( ".ck-find-result" ) ;
73+ const scrollingContainer = editorEl . closest ( '.scrolling-container' ) ;
74+ const containerTop = scrollingContainer ?. getBoundingClientRect ( ) . top ?? 0 ;
75+ const closestIndex = Array . from ( findResultElement ?? [ ] ) . findIndex ( ( el ) => el . getBoundingClientRect ( ) . top >= containerTop ) ;
76+ currentFound = closestIndex >= 0 ? closestIndex : 0 ;
6677 }
6778 }
6879
0 commit comments