@@ -628,14 +628,71 @@ 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+ }
671+ }
672+ } , 0 ) ;
673+ }
674+
631675 function activateSearch ( event : WaveKeyboardEvent ) : boolean {
632676 const bcm = getBlockComponentModel ( getFocusedBlockInStaticTab ( ) ) ;
633- // Ctrl+f is reserved in most shells
677+ // Ctrl+f is reserved in most shells.
634678 if ( event . control && bcm . viewModel . viewType == "term" ) {
635679 return false ;
636680 }
637681 if ( bcm . viewModel . searchAtoms ) {
638- globalStore . set ( bcm . viewModel . searchAtoms . isOpen , true ) ;
682+ const searchAtoms = bcm . viewModel . searchAtoms ;
683+ const isOpen = globalStore . get ( searchAtoms . isOpen ) ;
684+ const selectedText = getSelectedText ( ) ;
685+
686+ // Open search dialog if not already open
687+ if ( ! isOpen ) {
688+ globalStore . set ( searchAtoms . isOpen , true ) ;
689+ }
690+ // Set search value (use selected text if available, otherwise empty string)
691+ globalStore . set ( searchAtoms . searchValue , selectedText || "" ) ;
692+ // Reset search results
693+ globalStore . set ( searchAtoms . resultsIndex , 0 ) ;
694+ globalStore . set ( searchAtoms . resultsCount , 0 ) ;
695+ focusSearchInput ( ) ;
639696 return true ;
640697 }
641698 return false ;
0 commit comments