-
Notifications
You must be signed in to change notification settings - Fork 404
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: terminal search optimization (#4384)
- Loading branch information
Showing
10 changed files
with
313 additions
and
95 deletions.
There are no files selected for viewing
61 changes: 61 additions & 0 deletions
61
packages/terminal-next/src/browser/component/search.module.less
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
@option-size: 20px; | ||
|
||
.terminalSearch { | ||
display: flex; | ||
justify-content: center; | ||
align-items: center; | ||
position: absolute; | ||
z-index: 999; | ||
right: 16px; | ||
border-radius: 2px; | ||
padding: 6px 8px; | ||
box-shadow: rgba(0, 0, 0, 0.133) 0px 3.2px 7.2px 0px, rgba(0, 0, 0, 0.11) 0px 0.6px 1.8px 0px; | ||
background: var(--kt-panelTitle-background); | ||
|
||
.searchField { | ||
&:focus { | ||
border-color: var(--focusBorder); | ||
} | ||
|
||
.optionBtn { | ||
width: @option-size; | ||
height: @option-size; | ||
margin: 0 1px; | ||
box-sizing: border-box; | ||
display: flex; | ||
justify-content: center; | ||
user-select: none; | ||
background-repeat: no-repeat; | ||
background-position: center; | ||
border: 1px solid transparent; | ||
cursor: pointer; | ||
opacity: 0.7; | ||
|
||
&:hover { | ||
opacity: 1; | ||
} | ||
|
||
&::before { | ||
display: inline-block; | ||
height: @option-size - 2; | ||
line-height: @option-size - 2; | ||
} | ||
|
||
&.select { | ||
background-color: var(--inputOption-activeBackground); | ||
border-color: var(--inputOption-activeBorder); | ||
opacity: 1; | ||
} | ||
} | ||
} | ||
|
||
.panelBtn { | ||
padding: 0px 6px; | ||
cursor: pointer; | ||
} | ||
|
||
.searchResult { | ||
margin: 0 4px; | ||
font-size: 14px; | ||
} | ||
} |
142 changes: 142 additions & 0 deletions
142
packages/terminal-next/src/browser/component/search.view.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
import cls from 'classnames'; | ||
import React from 'react'; | ||
|
||
import { ValidateInput } from '@opensumi/ide-components'; | ||
import { getIcon, localize, useInjectable } from '@opensumi/ide-core-browser'; | ||
|
||
import { ISearchResult, ITerminalSearchService } from '../../common'; | ||
|
||
import styles from './search.module.less'; | ||
|
||
export const TerminalSearch: React.FC<{}> = React.memo((props) => { | ||
const searchService = useInjectable<ITerminalSearchService>(ITerminalSearchService); | ||
const [UIState, setUIState] = React.useState(searchService.UIState); | ||
const [searchResult, setSearchResult] = React.useState<ISearchResult | null>(null); | ||
const [inputText, setInputText] = React.useState(searchService.text || ''); | ||
const inputRef = React.useRef<HTMLInputElement>(null); | ||
|
||
React.useEffect(() => { | ||
const dispose = searchService.onVisibleChange((show) => { | ||
if (show && inputRef.current) { | ||
inputRef.current.focus(); | ||
|
||
if (inputRef.current.value.length > 0) { | ||
inputRef.current.setSelectionRange(0, inputRef.current.value.length); | ||
} | ||
} | ||
}); | ||
return () => dispose.dispose(); | ||
}, [searchService]); | ||
|
||
React.useEffect(() => { | ||
if (!searchService.onResultChange) { | ||
return; | ||
} | ||
|
||
const dispose = searchService.onResultChange((event) => { | ||
setSearchResult(event); | ||
}); | ||
|
||
return () => dispose.dispose(); | ||
}, [searchService]); | ||
|
||
const searchInput = React.useCallback( | ||
(event: React.ChangeEvent<HTMLInputElement>) => { | ||
searchService.text = event.target.value; | ||
searchService.search(); | ||
setInputText(event.target.value); | ||
}, | ||
[searchService], | ||
); | ||
|
||
const searchKeyDown = React.useCallback( | ||
(event: React.KeyboardEvent<HTMLInputElement>) => { | ||
if (event.key === 'Enter') { | ||
searchService.search(); | ||
} | ||
|
||
if (event.key === 'Escape') { | ||
searchService.close(); | ||
searchService.clear(); | ||
} | ||
}, | ||
[searchService], | ||
); | ||
|
||
const toggleMatchCase = React.useCallback(() => { | ||
searchService.updateUIState({ isMatchCase: !UIState.isMatchCase }); | ||
setUIState(searchService.UIState); | ||
}, [searchService, UIState]); | ||
|
||
const toggleRegex = React.useCallback(() => { | ||
searchService.updateUIState({ isUseRegexp: !UIState.isUseRegexp }); | ||
setUIState(searchService.UIState); | ||
}, [searchService, UIState]); | ||
|
||
const toggleWholeWord = React.useCallback(() => { | ||
searchService.updateUIState({ isWholeWord: !UIState.isWholeWord }); | ||
setUIState(searchService.UIState); | ||
}, [searchService, UIState]); | ||
|
||
const searchNext = React.useCallback(() => { | ||
searchService.searchNext(); | ||
}, [searchService]); | ||
|
||
const searchPrev = React.useCallback(() => { | ||
searchService.searchPrevious(); | ||
}, [searchService]); | ||
|
||
const close = React.useCallback(() => { | ||
searchService.close(); | ||
}, [searchService]); | ||
|
||
return ( | ||
<div className={styles.terminalSearch}> | ||
<ValidateInput | ||
className={styles.searchField} | ||
autoFocus | ||
id='search-input-field' | ||
title={localize('search.input.placeholder')} | ||
type='text' | ||
value={inputText} | ||
placeholder={localize('common.find')} | ||
onKeyDown={searchKeyDown} | ||
onChange={searchInput} | ||
ref={inputRef} | ||
validateMessage={undefined} | ||
addonAfter={[ | ||
<span | ||
key={localize('search.caseDescription')} | ||
className={cls(getIcon('ab'), styles['match-case'], styles.optionBtn, { | ||
[styles.select]: UIState.isMatchCase, | ||
})} | ||
title={localize('search.caseDescription')} | ||
onClick={toggleMatchCase} | ||
></span>, | ||
<span | ||
key={localize('search.wordsDescription')} | ||
className={cls(getIcon('abl'), styles['whole-word'], styles.optionBtn, { | ||
[styles.select]: UIState.isWholeWord, | ||
})} | ||
title={localize('search.wordsDescription')} | ||
onClick={toggleWholeWord} | ||
></span>, | ||
<span | ||
key={localize('search.regexDescription')} | ||
className={cls(getIcon('regex'), styles['use-regexp'], styles.optionBtn, { | ||
[styles.select]: UIState.isUseRegexp, | ||
})} | ||
title={localize('search.regexDescription')} | ||
onClick={toggleRegex} | ||
></span>, | ||
]} | ||
/> | ||
<div className={styles.searchResult}> | ||
{searchResult ? `${searchResult.resultIndex + 1}/${searchResult.resultCount}` : '0/0'} | ||
</div> | ||
<div className={cls(styles.panelBtn, getIcon('up'))} onClick={searchPrev}></div> | ||
<div className={cls(styles.panelBtn, getIcon('down'))} onClick={searchNext}></div> | ||
<div className={cls(styles.panelBtn, getIcon('close'))} onClick={close}></div> | ||
</div> | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.