tldraw/hooks/useKeyboardEvents.ts

230 lines
5.9 KiB
TypeScript
Raw Normal View History

2021-05-28 20:30:27 +00:00
import { useEffect } from 'react'
import state from 'state'
import { MoveType } from 'types'
import { getKeyboardEventInfo, metaKey } from 'utils/utils'
2021-05-12 11:27:33 +00:00
export default function useKeyboardEvents() {
useEffect(() => {
function handleKeyDown(e: KeyboardEvent) {
2021-05-28 20:30:27 +00:00
if (metaKey(e) && !['i', 'r', 'j'].includes(e.key)) {
2021-05-17 21:27:18 +00:00
e.preventDefault()
}
2021-05-23 13:46:04 +00:00
switch (e.key) {
2021-05-28 20:30:27 +00:00
case 'ArrowUp': {
state.send('NUDGED', { delta: [0, -1], ...getKeyboardEventInfo(e) })
break
}
case 'ArrowRight': {
state.send('NUDGED', { delta: [1, 0], ...getKeyboardEventInfo(e) })
break
}
case 'ArrowDown': {
state.send('NUDGED', { delta: [0, 1], ...getKeyboardEventInfo(e) })
break
}
case 'ArrowLeft': {
state.send('NUDGED', { delta: [-1, 0], ...getKeyboardEventInfo(e) })
break
}
2021-05-29 13:59:11 +00:00
case '=': {
if (e.metaKey) {
state.send('ZOOMED_IN')
}
break
}
case '-': {
if (e.metaKey) {
state.send('ZOOMED_OUT')
}
break
}
2021-05-28 20:30:27 +00:00
case '!': {
// Shift + 1
if (e.shiftKey) {
2021-05-28 20:30:27 +00:00
state.send('ZOOMED_TO_FIT')
}
break
}
2021-05-28 20:30:27 +00:00
case '@': {
// Shift + 2
if (e.shiftKey) {
2021-05-28 20:30:27 +00:00
state.send('ZOOMED_TO_SELECTION')
}
break
}
2021-05-28 20:30:27 +00:00
case ')': {
// Shift + 0
if (e.shiftKey) {
2021-05-28 20:30:27 +00:00
state.send('ZOOMED_TO_ACTUAL')
}
break
}
2021-05-28 20:30:27 +00:00
case 'Escape': {
state.send('CANCELLED')
2021-05-23 13:46:04 +00:00
break
}
2021-05-28 20:30:27 +00:00
case 'z': {
2021-05-23 13:46:04 +00:00
if (metaKey(e)) {
if (e.shiftKey) {
2021-05-28 20:30:27 +00:00
state.send('REDO', getKeyboardEventInfo(e))
2021-05-23 13:46:04 +00:00
} else {
2021-05-28 20:30:27 +00:00
state.send('UNDO', getKeyboardEventInfo(e))
2021-05-23 13:46:04 +00:00
}
}
break
}
2021-05-28 20:30:27 +00:00
case '': {
2021-05-23 17:09:23 +00:00
if (metaKey(e)) {
2021-05-28 20:30:27 +00:00
state.send('MOVED', {
2021-05-26 19:20:52 +00:00
...getKeyboardEventInfo(e),
type: MoveType.ToFront,
})
2021-05-23 17:09:23 +00:00
}
break
}
2021-05-28 20:30:27 +00:00
case '“': {
2021-05-23 17:09:23 +00:00
if (metaKey(e)) {
2021-05-28 20:30:27 +00:00
state.send('MOVED', {
2021-05-26 19:20:52 +00:00
...getKeyboardEventInfo(e),
type: MoveType.ToBack,
})
2021-05-23 17:09:23 +00:00
}
break
}
2021-05-28 20:30:27 +00:00
case ']': {
2021-05-23 13:46:04 +00:00
if (metaKey(e)) {
2021-05-28 20:30:27 +00:00
state.send('MOVED', {
2021-05-26 19:20:52 +00:00
...getKeyboardEventInfo(e),
type: MoveType.Forward,
})
2021-05-23 13:46:04 +00:00
}
break
}
2021-05-28 20:30:27 +00:00
case '[': {
2021-05-23 13:46:04 +00:00
if (metaKey(e)) {
2021-05-28 20:30:27 +00:00
state.send('MOVED', {
2021-05-26 19:20:52 +00:00
...getKeyboardEventInfo(e),
type: MoveType.Backward,
})
2021-05-23 13:46:04 +00:00
}
break
}
2021-05-28 20:30:27 +00:00
case 'Shift': {
state.send('PRESSED_SHIFT_KEY', getKeyboardEventInfo(e))
2021-05-23 13:46:04 +00:00
break
}
2021-05-28 20:30:27 +00:00
case 'Alt': {
state.send('PRESSED_ALT_KEY', getKeyboardEventInfo(e))
2021-05-23 13:46:04 +00:00
break
}
2021-05-28 20:30:27 +00:00
case 'Backspace': {
state.send('DELETED', getKeyboardEventInfo(e))
2021-05-23 13:46:04 +00:00
break
}
2021-05-28 20:30:27 +00:00
case 's': {
2021-05-23 13:46:04 +00:00
if (metaKey(e)) {
2021-05-28 20:30:27 +00:00
state.send('SAVED', getKeyboardEventInfo(e))
2021-05-23 13:46:04 +00:00
}
break
}
2021-05-28 20:30:27 +00:00
case 'a': {
2021-05-23 13:46:04 +00:00
if (metaKey(e)) {
2021-05-28 20:30:27 +00:00
state.send('SELECTED_ALL', getKeyboardEventInfo(e))
2021-05-23 13:46:04 +00:00
}
break
}
2021-05-28 20:30:27 +00:00
case 'v': {
2021-05-23 13:46:04 +00:00
if (metaKey(e)) {
2021-05-28 20:30:27 +00:00
state.send('PASTED', getKeyboardEventInfo(e))
2021-05-23 13:46:04 +00:00
} else {
2021-05-28 20:30:27 +00:00
state.send('SELECTED_SELECT_TOOL', getKeyboardEventInfo(e))
2021-05-23 13:46:04 +00:00
}
break
}
2021-05-28 20:30:27 +00:00
case 'd': {
if (metaKey(e)) {
state.send('DUPLICATED', getKeyboardEventInfo(e))
} else {
state.send('SELECTED_DRAW_TOOL', getKeyboardEventInfo(e))
}
2021-05-27 17:59:40 +00:00
break
}
2021-05-28 20:30:27 +00:00
case 't': {
2021-05-23 13:46:04 +00:00
if (metaKey(e)) {
2021-05-28 20:30:27 +00:00
state.send('DUPLICATED', getKeyboardEventInfo(e))
2021-05-23 13:46:04 +00:00
} else {
2021-05-28 20:30:27 +00:00
state.send('SELECTED_DOT_TOOL', getKeyboardEventInfo(e))
2021-05-23 13:46:04 +00:00
}
break
}
2021-05-28 20:30:27 +00:00
case 'c': {
2021-05-23 13:46:04 +00:00
if (metaKey(e)) {
2021-05-28 20:30:27 +00:00
state.send('COPIED', getKeyboardEventInfo(e))
2021-05-23 13:46:04 +00:00
} else {
2021-05-28 20:30:27 +00:00
state.send('SELECTED_CIRCLE_TOOL', getKeyboardEventInfo(e))
2021-05-23 13:46:04 +00:00
}
break
}
2021-05-28 20:30:27 +00:00
case 'i': {
2021-05-23 13:46:04 +00:00
if (metaKey(e)) {
} else {
2021-05-28 20:30:27 +00:00
state.send('SELECTED_ELLIPSE_TOOL', getKeyboardEventInfo(e))
2021-05-23 13:46:04 +00:00
}
break
}
2021-05-28 20:30:27 +00:00
case 'l': {
2021-05-23 13:46:04 +00:00
if (metaKey(e)) {
} else {
2021-05-28 20:30:27 +00:00
state.send('SELECTED_LINE_TOOL', getKeyboardEventInfo(e))
2021-05-23 13:46:04 +00:00
}
break
}
2021-05-28 20:30:27 +00:00
case 'y': {
2021-05-23 13:46:04 +00:00
if (metaKey(e)) {
} else {
2021-05-28 20:30:27 +00:00
state.send('SELECTED_RAY_TOOL', getKeyboardEventInfo(e))
2021-05-23 13:46:04 +00:00
}
break
}
2021-05-28 20:30:27 +00:00
case 'p': {
2021-05-23 13:46:04 +00:00
if (metaKey(e)) {
} else {
2021-05-28 20:30:27 +00:00
state.send('SELECTED_POLYLINE_TOOL', getKeyboardEventInfo(e))
2021-05-23 13:46:04 +00:00
}
break
}
2021-05-28 20:30:27 +00:00
case 'r': {
2021-05-23 13:46:04 +00:00
if (metaKey(e)) {
} else {
2021-05-28 20:30:27 +00:00
state.send('SELECTED_RECTANGLE_TOOL', getKeyboardEventInfo(e))
2021-05-23 13:46:04 +00:00
}
break
}
default: {
2021-05-28 20:30:27 +00:00
state.send('PRESSED_KEY', getKeyboardEventInfo(e))
2021-05-23 13:46:04 +00:00
}
}
2021-05-12 11:27:33 +00:00
}
function handleKeyUp(e: KeyboardEvent) {
2021-05-28 20:30:27 +00:00
if (e.key === 'Shift') {
state.send('RELEASED_SHIFT_KEY', getKeyboardEventInfo(e))
2021-05-12 11:27:33 +00:00
}
2021-05-28 20:30:27 +00:00
if (e.key === 'Alt') {
state.send('RELEASED_ALT_KEY', getKeyboardEventInfo(e))
2021-05-19 21:24:41 +00:00
}
2021-05-28 20:30:27 +00:00
state.send('RELEASED_KEY', getKeyboardEventInfo(e))
2021-05-12 11:27:33 +00:00
}
2021-05-28 20:30:27 +00:00
document.body.addEventListener('keydown', handleKeyDown)
document.body.addEventListener('keyup', handleKeyUp)
2021-05-12 11:27:33 +00:00
return () => {
2021-05-28 20:30:27 +00:00
document.body.removeEventListener('keydown', handleKeyDown)
document.body.removeEventListener('keyup', handleKeyUp)
2021-05-12 11:27:33 +00:00
}
}, [])
}