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:
@@ -13,6 +13,7 @@ import LoginModal from 'src/components/LoginModal'
|
|||||||
import { FORK_PART_MUTATION } from 'src/components/IdePartCell'
|
import { FORK_PART_MUTATION } from 'src/components/IdePartCell'
|
||||||
import { QUERY as UsersPartsQuery } from 'src/components/PartsOfUserCell'
|
import { QUERY as UsersPartsQuery } from 'src/components/PartsOfUserCell'
|
||||||
import useUser from 'src/helpers/hooks/useUser'
|
import useUser from 'src/helpers/hooks/useUser'
|
||||||
|
import useKeyPress from 'src/helpers/hooks/useKeyPress'
|
||||||
|
|
||||||
const IdeToolbar = ({
|
const IdeToolbar = ({
|
||||||
canEdit,
|
canEdit,
|
||||||
@@ -30,6 +31,14 @@ const IdeToolbar = ({
|
|||||||
const showForkButton = !(canEdit || isDraft)
|
const showForkButton = !(canEdit || isDraft)
|
||||||
const [title, setTitle] = useState('untitled-part')
|
const [title, setTitle] = useState('untitled-part')
|
||||||
const { user } = useUser()
|
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 { addMessage } = useFlash()
|
||||||
const [forkPart] = useMutation(FORK_PART_MUTATION, {
|
const [forkPart] = useMutation(FORK_PART_MUTATION, {
|
||||||
|
|||||||
19
web/src/helpers/hooks/useKeyPress.js
Normal file
19
web/src/helpers/hooks/useKeyPress.js
Normal 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
|
||||||
Reference in New Issue
Block a user