Merge pull request #194 from Irev-Dev/kurt/193

issue-193 Stop backspace key from going back a page in IDE
This commit was merged in pull request #194.
This commit is contained in:
Kurt Hutten
2020-12-31 18:10:50 +11:00
committed by GitHub
2 changed files with 28 additions and 0 deletions

View File

@@ -13,6 +13,7 @@ import LoginModal from 'src/components/LoginModal'
import { FORK_PART_MUTATION } from 'src/components/IdePartCell'
import { QUERY as UsersPartsQuery } from 'src/components/PartsOfUserCell'
import useUser from 'src/helpers/hooks/useUser'
import useKeyPress from 'src/helpers/hooks/useKeyPress'
const IdeToolbar = ({
canEdit,
@@ -30,6 +31,14 @@ const IdeToolbar = ({
const showForkButton = !(canEdit || isDraft)
const [title, setTitle] = useState('untitled-part')
const { user } = useUser()
useKeyPress((e) => {
const rx = /INPUT|SELECT|TEXTAREA/i
const didPressBackspaceOutsideOfInput =
(e.key == 'Backspace' || e.keyCode == 8) && !rx.test(e.target.tagName)
if (didPressBackspaceOutsideOfInput) {
e.preventDefault()
}
})
const { addMessage } = useFlash()
const [forkPart] = useMutation(FORK_PART_MUTATION, {

View File

@@ -0,0 +1,19 @@
import { useRef, useEffect } from 'react'
const useKeyPress = (fn) => {
const cb = useRef(fn)
useEffect(() => {
cb.current = fn
}, [fn])
useEffect(() => {
const onUnload = cb.current
window.addEventListener('keydown', onUnload)
return () => window.removeEventListener('keydown', onUnload)
}, [])
}
export default useKeyPress