@@ -21,6 +21,7 @@ import {
2121 WOS ,
2222} from "@/app/store/global" ;
2323import { TabBarModel } from "@/app/tab/tabbar-model" ;
24+ import type { TermViewModel } from "@/app/view/term/term-model" ;
2425import { WorkspaceLayoutModel } from "@/app/workspace/workspace-layout-model" ;
2526import { deleteLayoutModelForTab , getLayoutModelForStaticTab , NavigateDirection } from "@/layout/index" ;
2627import * as keyutil from "@/util/keyutil" ;
@@ -628,14 +629,71 @@ function registerGlobalKeys() {
628629 return true ;
629630 } ) ;
630631 }
632+ function getSelectedText ( ) : string {
633+ // Check for terminal selection first
634+ const bcm = getBlockComponentModel ( getFocusedBlockInStaticTab ( ) ) ;
635+ if ( bcm ?. viewModel ?. viewType === "term" ) {
636+ const termViewModel = bcm . viewModel as TermViewModel ;
637+ if ( termViewModel . termRef ?. current ?. terminal ) {
638+ const terminalSelection = termViewModel . termRef . current . terminal . getSelection ( ) ;
639+ if ( terminalSelection && terminalSelection . length > 0 ) {
640+ return terminalSelection . trim ( ) ;
641+ }
642+ }
643+ }
644+ // Check for regular text selection
645+ const selection = window . getSelection ( ) ;
646+ if ( selection && selection . rangeCount > 0 && ! selection . isCollapsed ) {
647+ const selectedText = selection . toString ( ) . trim ( ) ;
648+ if ( selectedText . length > 0 ) {
649+ return selectedText ;
650+ }
651+ }
652+ return "" ;
653+ }
654+
655+ function focusSearchInput ( ) {
656+ setTimeout ( ( ) => {
657+ const blockId = getFocusedBlockInStaticTab ( ) ;
658+ if ( ! blockId ) {
659+ return ;
660+ }
661+
662+ // Directly find the search container by data-blockid attribute
663+ const searchContainer = document . querySelector (
664+ `.search-container[data-blockid="${ blockId } "]`
665+ ) as HTMLElement ;
666+ if ( searchContainer ) {
667+ const searchInput = searchContainer . querySelector ( "input" ) as HTMLInputElement ;
668+ if ( searchInput ) {
669+ searchInput . focus ( ) ;
670+ searchInput . select ( ) ;
671+ }
672+ }
673+ } , 0 ) ;
674+ }
675+
631676 function activateSearch ( event : WaveKeyboardEvent ) : boolean {
632677 const bcm = getBlockComponentModel ( getFocusedBlockInStaticTab ( ) ) ;
633- // Ctrl+f is reserved in most shells
678+ // Ctrl+f is reserved in most shells.
634679 if ( event . control && bcm . viewModel . viewType == "term" ) {
635680 return false ;
636681 }
637682 if ( bcm . viewModel . searchAtoms ) {
638- globalStore . set ( bcm . viewModel . searchAtoms . isOpen , true ) ;
683+ const searchAtoms = bcm . viewModel . searchAtoms ;
684+ const isOpen = globalStore . get ( searchAtoms . isOpen ) ;
685+ const selectedText = getSelectedText ( ) ;
686+
687+ // Open search dialog if not already open
688+ if ( ! isOpen ) {
689+ globalStore . set ( searchAtoms . isOpen , true ) ;
690+ }
691+ // Set search value (use selected text if available, otherwise empty string)
692+ globalStore . set ( searchAtoms . searchValue , selectedText || "" ) ;
693+ // Reset search results
694+ globalStore . set ( searchAtoms . resultsIndex , 0 ) ;
695+ globalStore . set ( searchAtoms . resultsCount , 0 ) ;
696+ focusSearchInput ( ) ;
639697 return true ;
640698 }
641699 return false ;
0 commit comments