Skip to content

Commit

Permalink
FileView: added quick select by typing any key
Browse files Browse the repository at this point in the history
  • Loading branch information
warpdesign committed Apr 16, 2024
1 parent 8cfcb3d commit f9d7e49
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
39 changes: 38 additions & 1 deletion src/components/FileView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,47 @@ const FileView = observer(({ hide }: Props) => {
}
console.log('render!', { cursorIndex, cursor })

const searchStringRef = React.useRef<string>('')
const timeStampRef = React.useRef<number>(0)

// quick select
useKeyDown(
React.useCallback(
(event: KeyboardEvent) => {
let searchString = searchStringRef.current
// we only want to catch printable keys
if (
!viewState.isActive ||
!appState.isExplorer ||
event.key.length !== 1 ||
event.ctrlKey ||
event.altKey ||
event.metaKey
) {
return
}

// previous keyevent > 1sec ?
if (event.timeStamp - timeStampRef.current > 1000) searchString = ''

// search_string += key
searchString += event.key

// call select file marching search_string
if (searchString.length) cache.selectMatchingFile(searchString)

searchStringRef.current = searchString
timeStampRef.current = event.timeStamp
},
[cursor, cache, rowCount],
),
['*'],
)

useKeyDown(
React.useCallback(
(event: KeyboardEvent) => {
if (!viewState.isActive) {
if (!viewState.isActive || !appState.isExplorer) {
return
}

Expand Down
10 changes: 10 additions & 0 deletions src/state/fileState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ export class FileState {
doLogin: action,
clearSelection: action,
invertSelection: action,
selectMatchingFile: action,
reset: action,
setSort: action,
refreshSelection: action,
Expand Down Expand Up @@ -472,6 +473,15 @@ export class FileState {
}
}

selectMatchingFile(str: string) {
// find first matching file (could be the same, we don't care)
const file = this.files.find((file) => file.name.toLowerCase().startsWith(str.toLowerCase()))
if (file) {
this.selected.replace([file])
this.setCursor(this.selected[0])
}
}

invertSelection() {
if (this.selected.length === this.files.length) {
this.selected.clear()
Expand Down

0 comments on commit f9d7e49

Please sign in to comment.