chore: update hooks file ext

This commit is contained in:
EnixCoda 2021-05-16 23:14:13 +08:00
parent 245ae4cbcc
commit f75e76c447
No known key found for this signature in database
GPG key ID: 0C1A07377913A1DD
6 changed files with 30 additions and 26 deletions

View file

@ -5,7 +5,7 @@ import { Portal } from 'components/Portal'
import { Resizable } from 'components/Resizable'
import { SettingsBar } from 'components/settings/SettingsBar'
import { ToggleShowButton } from 'components/ToggleShowButton'
import { ConfigsContextShape, useConfigs } from 'containers/ConfigsContext'
import { useConfigs } from 'containers/ConfigsContext'
import { platform } from 'platforms'
import { useGitHubAttachCopyFileButton, useGitHubAttachCopySnippetButton } from 'platforms/GitHub'
import * as React from 'react'
@ -17,13 +17,13 @@ import { useLoadedContext } from 'utils/hooks/useLoadedContext'
import { loadWithPJAX, useOnPJAXDone, usePJAX } from 'utils/hooks/usePJAX'
import { useProgressBar } from 'utils/hooks/useProgressBar'
import { useStateIO } from 'utils/hooks/useStateIO'
import * as keyHelper from 'utils/keyHelper'
import { useToggleSideBarWithKeyboard } from '../utils/hooks/useToggleSideBarWithKeyboard'
import { SideBarErrorContext } from './ErrorContext'
import { Icon } from './Icon'
import { IIFC } from './IIFC'
import { LoadingIndicator } from './LoadingIndicator'
import { RepoContext } from './RepoContext'
import { SideBarState, SideBarStateContext } from './SideBarState'
import { SideBarStateContext } from './SideBarState'
import { Theme } from './Theme'
export function SideBar() {
@ -175,26 +175,3 @@ export function SideBar() {
</Theme>
)
}
function useToggleSideBarWithKeyboard(
state: SideBarState,
configContext: ConfigsContextShape,
toggleShowSideBar: () => void,
) {
const isDisabled = state === 'disabled'
React.useEffect(
function attachKeyDown() {
if (isDisabled || !configContext.value.shortcut) return
function onKeyDown(e: KeyboardEvent) {
const keys = keyHelper.parseEvent(e)
if (keys === configContext.value.shortcut) {
toggleShowSideBar()
}
}
window.addEventListener('keydown', onKeyDown)
return () => window.removeEventListener('keydown', onKeyDown)
},
[toggleShowSideBar, isDisabled, configContext.value.shortcut],
)
}

View file

@ -0,0 +1,27 @@
import { ConfigsContextShape } from 'containers/ConfigsContext'
import * as React from 'react'
import * as keyHelper from 'utils/keyHelper'
import { SideBarState } from '../../components/SideBarState'
export function useToggleSideBarWithKeyboard(
state: SideBarState,
configContext: ConfigsContextShape,
toggleShowSideBar: () => void,
) {
const isDisabled = state === 'disabled'
React.useEffect(
function attachKeyDown() {
if (isDisabled || !configContext.value.shortcut) return
function onKeyDown(e: KeyboardEvent) {
const keys = keyHelper.parseEvent(e)
if (keys === configContext.value.shortcut) {
toggleShowSideBar()
}
}
window.addEventListener('keydown', onKeyDown)
return () => window.removeEventListener('keydown', onKeyDown)
},
[toggleShowSideBar, isDisabled, configContext.value.shortcut],
)
}