@@ -628,15 +628,86 @@ function registerGlobalKeys() {
628628 return true ;
629629 } ) ;
630630 }
631+ function getSelectedText ( ) : string {
632+ // Check for terminal selection first
633+ const bcm = getBlockComponentModel ( getFocusedBlockInStaticTab ( ) ) ;
634+ if ( bcm ?. viewModel ?. viewType === "term" ) {
635+ const termViewModel = bcm . viewModel as any ;
636+ if ( termViewModel . termRef ?. current ?. terminal ) {
637+ const terminalSelection = termViewModel . termRef . current . terminal . getSelection ( ) ;
638+ if ( terminalSelection && terminalSelection . length > 0 ) {
639+ return terminalSelection . trim ( ) ;
640+ }
641+ }
642+ }
643+ // Check for regular text selection
644+ const selection = window . getSelection ( ) ;
645+ if ( selection && selection . rangeCount > 0 && ! selection . isCollapsed ) {
646+ const selectedText = selection . toString ( ) . trim ( ) ;
647+ if ( selectedText . length > 0 ) {
648+ return selectedText ;
649+ }
650+ }
651+ return "" ;
652+ }
653+
654+ function focusSearchInput ( ) {
655+ setTimeout ( ( ) => {
656+ const blockId = getFocusedBlockInStaticTab ( ) ;
657+ if ( ! blockId ) {
658+ return ;
659+ }
660+
661+ // Directly find the search container by data-blockid attribute
662+ const searchContainer = document . querySelector (
663+ `.search-container[data-blockid="${ blockId } "]`
664+ ) as HTMLElement ;
665+ if ( searchContainer ) {
666+ const searchInput = searchContainer . querySelector ( "input" ) as HTMLInputElement ;
667+ if ( searchInput ) {
668+ searchInput . focus ( ) ;
669+ searchInput . select ( ) ;
670+ return ;
671+ }
672+ }
673+
674+ // Fallback: if we can't find the specific container, try to find any open search input
675+ const searchInput = document . querySelector ( ".search-container input" ) as HTMLInputElement ;
676+ if ( searchInput ) {
677+ searchInput . focus ( ) ;
678+ searchInput . select ( ) ;
679+ }
680+ } , 0 ) ;
681+ }
682+
631683 function activateSearch ( event : WaveKeyboardEvent ) : boolean {
632684 const bcm = getBlockComponentModel ( getFocusedBlockInStaticTab ( ) ) ;
633685 // Ctrl+f is reserved in most shells
634686 if ( event . control && bcm . viewModel . viewType == "term" ) {
635687 return false ;
636688 }
637689 if ( bcm . viewModel . searchAtoms ) {
638- globalStore . set ( bcm . viewModel . searchAtoms . isOpen , true ) ;
639- return true ;
690+ const searchAtoms = bcm . viewModel . searchAtoms ;
691+ const isOpen = globalStore . get ( searchAtoms . isOpen ) ;
692+ const selectedText = getSelectedText ( ) ;
693+
694+ if ( isOpen ) {
695+ // Reset search dialog when already open
696+ globalStore . set ( searchAtoms . searchValue , selectedText || "" ) ;
697+ globalStore . set ( searchAtoms . resultsIndex , 0 ) ;
698+ globalStore . set ( searchAtoms . resultsCount , 0 ) ;
699+ focusSearchInput ( ) ;
700+ return true ;
701+ } else {
702+ // Open search dialog
703+ globalStore . set ( searchAtoms . isOpen , true ) ;
704+ // Set search value to selected text if available
705+ if ( selectedText ) {
706+ globalStore . set ( searchAtoms . searchValue , selectedText ) ;
707+ }
708+ focusSearchInput ( ) ;
709+ return true ;
710+ }
640711 }
641712 return false ;
642713 }
0 commit comments