|
| 1 | +import cls from 'classnames'; |
| 2 | +import React from 'react'; |
| 3 | + |
| 4 | +import { ValidateInput } from '@opensumi/ide-components'; |
| 5 | +import { getIcon, localize, useInjectable } from '@opensumi/ide-core-browser'; |
| 6 | + |
| 7 | +import { ISearchResult, ITerminalSearchService } from '../../common'; |
| 8 | + |
| 9 | +import styles from './search.module.less'; |
| 10 | + |
| 11 | +export const TerminalSearch: React.FC<{}> = React.memo((props) => { |
| 12 | + const searchService = useInjectable<ITerminalSearchService>(ITerminalSearchService); |
| 13 | + const [UIState, setUIState] = React.useState(searchService.UIState); |
| 14 | + const [searchResult, setSearchResult] = React.useState<ISearchResult | null>(null); |
| 15 | + const [inputText, setInputText] = React.useState(searchService.text || ''); |
| 16 | + const inputRef = React.useRef<HTMLInputElement>(null); |
| 17 | + |
| 18 | + React.useEffect(() => { |
| 19 | + const dispose = searchService.onVisibleChange((show) => { |
| 20 | + if (show && inputRef.current) { |
| 21 | + inputRef.current.focus(); |
| 22 | + |
| 23 | + if (inputRef.current.value.length > 0) { |
| 24 | + inputRef.current.setSelectionRange(0, inputRef.current.value.length); |
| 25 | + } |
| 26 | + } |
| 27 | + }); |
| 28 | + return () => dispose.dispose(); |
| 29 | + }, [searchService]); |
| 30 | + |
| 31 | + React.useEffect(() => { |
| 32 | + if (!searchService.onResultChange) { |
| 33 | + return; |
| 34 | + } |
| 35 | + |
| 36 | + const dispose = searchService.onResultChange((event) => { |
| 37 | + setSearchResult(event); |
| 38 | + }); |
| 39 | + |
| 40 | + return () => dispose.dispose(); |
| 41 | + }, [searchService]); |
| 42 | + |
| 43 | + const searchInput = React.useCallback( |
| 44 | + (event: React.ChangeEvent<HTMLInputElement>) => { |
| 45 | + searchService.text = event.target.value; |
| 46 | + searchService.search(); |
| 47 | + setInputText(event.target.value); |
| 48 | + }, |
| 49 | + [searchService], |
| 50 | + ); |
| 51 | + |
| 52 | + const searchKeyDown = React.useCallback( |
| 53 | + (event: React.KeyboardEvent<HTMLInputElement>) => { |
| 54 | + if (event.key === 'Enter') { |
| 55 | + searchService.search(); |
| 56 | + } |
| 57 | + |
| 58 | + if (event.key === 'Escape') { |
| 59 | + searchService.close(); |
| 60 | + searchService.clear(); |
| 61 | + } |
| 62 | + }, |
| 63 | + [searchService], |
| 64 | + ); |
| 65 | + |
| 66 | + const toggleMatchCase = React.useCallback(() => { |
| 67 | + searchService.updateUIState({ isMatchCase: !UIState.isMatchCase }); |
| 68 | + setUIState(searchService.UIState); |
| 69 | + }, [searchService, UIState]); |
| 70 | + |
| 71 | + const toggleRegex = React.useCallback(() => { |
| 72 | + searchService.updateUIState({ isUseRegexp: !UIState.isUseRegexp }); |
| 73 | + setUIState(searchService.UIState); |
| 74 | + }, [searchService, UIState]); |
| 75 | + |
| 76 | + const toggleWholeWord = React.useCallback(() => { |
| 77 | + searchService.updateUIState({ isWholeWord: !UIState.isWholeWord }); |
| 78 | + setUIState(searchService.UIState); |
| 79 | + }, [searchService, UIState]); |
| 80 | + |
| 81 | + const searchNext = React.useCallback(() => { |
| 82 | + searchService.searchNext(); |
| 83 | + }, [searchService]); |
| 84 | + |
| 85 | + const searchPrev = React.useCallback(() => { |
| 86 | + searchService.searchPrevious(); |
| 87 | + }, [searchService]); |
| 88 | + |
| 89 | + const close = React.useCallback(() => { |
| 90 | + searchService.close(); |
| 91 | + }, [searchService]); |
| 92 | + |
| 93 | + return ( |
| 94 | + <div className={styles.terminalSearch}> |
| 95 | + <ValidateInput |
| 96 | + className={styles.searchField} |
| 97 | + autoFocus |
| 98 | + id='search-input-field' |
| 99 | + title={localize('search.input.placeholder')} |
| 100 | + type='text' |
| 101 | + value={inputText} |
| 102 | + placeholder={localize('common.find')} |
| 103 | + onKeyDown={searchKeyDown} |
| 104 | + onChange={searchInput} |
| 105 | + ref={inputRef} |
| 106 | + validateMessage={undefined} |
| 107 | + addonAfter={[ |
| 108 | + <span |
| 109 | + key={localize('search.caseDescription')} |
| 110 | + className={cls(getIcon('ab'), styles['match-case'], styles.optionBtn, { |
| 111 | + [styles.select]: UIState.isMatchCase, |
| 112 | + })} |
| 113 | + title={localize('search.caseDescription')} |
| 114 | + onClick={toggleMatchCase} |
| 115 | + ></span>, |
| 116 | + <span |
| 117 | + key={localize('search.wordsDescription')} |
| 118 | + className={cls(getIcon('abl'), styles['whole-word'], styles.optionBtn, { |
| 119 | + [styles.select]: UIState.isWholeWord, |
| 120 | + })} |
| 121 | + title={localize('search.wordsDescription')} |
| 122 | + onClick={toggleWholeWord} |
| 123 | + ></span>, |
| 124 | + <span |
| 125 | + key={localize('search.regexDescription')} |
| 126 | + className={cls(getIcon('regex'), styles['use-regexp'], styles.optionBtn, { |
| 127 | + [styles.select]: UIState.isUseRegexp, |
| 128 | + })} |
| 129 | + title={localize('search.regexDescription')} |
| 130 | + onClick={toggleRegex} |
| 131 | + ></span>, |
| 132 | + ]} |
| 133 | + /> |
| 134 | + <div className={styles.searchResult}> |
| 135 | + {searchResult ? `${searchResult.resultIndex + 1}/${searchResult.resultCount}` : '0/0'} |
| 136 | + </div> |
| 137 | + <div className={cls(styles.panelBtn, getIcon('up'))} onClick={searchPrev}></div> |
| 138 | + <div className={cls(styles.panelBtn, getIcon('down'))} onClick={searchNext}></div> |
| 139 | + <div className={cls(styles.panelBtn, getIcon('close'))} onClick={close}></div> |
| 140 | + </div> |
| 141 | + ); |
| 142 | +}); |
0 commit comments