generated from arvinxx/umi-chrome-extensions-template
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
208 additions
and
46 deletions.
There are no files selected for viewing
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
110 changes: 110 additions & 0 deletions
110
src/contentScripts/searchBar/app/SearchInput/useKeyboardService.ts
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,110 @@ | ||
import { useContext, useEffect, useRef, useState } from 'react'; | ||
import type { Input } from 'antd'; | ||
|
||
import { getServiceToken } from '@/utils'; | ||
import { SearchService } from '../useSearchService'; | ||
import { SearchBarService } from '../useSearchBarService'; | ||
|
||
type FocusType = 'input' | 'options' | 'result'; | ||
|
||
/** | ||
* Keyboard 需要的状态 | ||
*/ | ||
export const useKeyboardService = () => { | ||
const { setType, optionKeys, optionActiveIndex } = useContext(SearchService); | ||
const { hide } = useContext(SearchBarService); | ||
|
||
const [focusKey, setFocusKey] = useState<FocusType>('input'); | ||
const inputRef = useRef<Input>(null); | ||
|
||
/** | ||
* 按 Tabs 键切换选中 type | ||
* @param back | ||
*/ | ||
const switchOptionIndex = (back?: boolean) => { | ||
let newIndex: number; | ||
|
||
if (back) { | ||
newIndex = | ||
optionActiveIndex === 0 ? optionKeys.length - 1 : optionActiveIndex - 1; | ||
} else { | ||
newIndex = | ||
optionActiveIndex === optionKeys.length - 1 ? 0 : optionActiveIndex + 1; | ||
} | ||
|
||
setType(optionKeys[newIndex]); | ||
}; | ||
|
||
const focusOnInput = () => { | ||
inputRef.current?.focus(); | ||
setFocusKey('input'); | ||
}; | ||
|
||
const focusOnOptions = () => { | ||
inputRef.current?.blur(); | ||
setFocusKey('options'); | ||
}; | ||
|
||
// 将焦点切换到 Options | ||
const onKeyDown = (event: KeyboardEvent) => { | ||
// 焦点在 options 的情况 | ||
if (focusKey === 'options') { | ||
console.log(event.key); | ||
switch (event.key) { | ||
case 'Tab': | ||
event.preventDefault(); | ||
switchOptionIndex(event.shiftKey); | ||
break; | ||
|
||
case 'ArrowRight': | ||
switchOptionIndex(); | ||
break; | ||
case 'ArrowLeft': | ||
switchOptionIndex(true); | ||
break; | ||
case 'ArrowUp': | ||
case 'Escape': | ||
focusOnInput(); | ||
break; | ||
default: | ||
} | ||
} | ||
|
||
// 焦点在 input 的情况 | ||
if (focusKey === 'input') { | ||
switch (event.key) { | ||
case 'Tab': | ||
event.preventDefault(); | ||
focusOnOptions(); | ||
switchOptionIndex(event.shiftKey); | ||
break; | ||
case 'Escape': | ||
hide(); | ||
break; | ||
case 'ArrowDown': | ||
event.preventDefault(); | ||
focusOnOptions(); | ||
break; | ||
default: | ||
} | ||
} | ||
}; | ||
|
||
useEffect(() => { | ||
window.onkeydown = onKeyDown; | ||
|
||
return () => { | ||
window.onkeydown = null; | ||
}; | ||
}, [focusKey, optionActiveIndex]); | ||
|
||
return { | ||
onKeyDown, | ||
focusKey, | ||
inputRef, | ||
focusOnInput, | ||
focusOnOptions, | ||
}; | ||
}; | ||
|
||
export const KeyboardService = getServiceToken(useKeyboardService); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
@import '~theme/index'; | ||
.skeleton { | ||
margin-top: 16px; | ||
padding: 8px 12px 24px; | ||
} | ||
.repo { | ||
|
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.