2021-06-21 21:35:28 +00:00
|
|
|
|
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
|
2021-05-28 20:30:27 +00:00
|
|
|
|
import { useEffect } from 'react'
|
|
|
|
|
import state from 'state'
|
2021-06-30 20:56:42 +00:00
|
|
|
|
import inputs from 'state/inputs'
|
2021-05-28 20:30:27 +00:00
|
|
|
|
import { MoveType } from 'types'
|
2021-06-30 20:56:42 +00:00
|
|
|
|
import { metaKey } from 'utils'
|
2021-05-12 11:27:33 +00:00
|
|
|
|
|
|
|
|
|
export default function useKeyboardEvents() {
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
function handleKeyDown(e: KeyboardEvent) {
|
2021-06-15 11:58:51 +00:00
|
|
|
|
if (
|
|
|
|
|
metaKey(e) &&
|
|
|
|
|
![
|
|
|
|
|
'a',
|
|
|
|
|
'i',
|
|
|
|
|
'r',
|
|
|
|
|
'j',
|
|
|
|
|
'ArrowLeft',
|
|
|
|
|
'ArrowRight',
|
|
|
|
|
'ArrowUp',
|
|
|
|
|
'ArrowDown',
|
|
|
|
|
'z',
|
|
|
|
|
].includes(e.key)
|
|
|
|
|
) {
|
2021-05-17 21:27:18 +00:00
|
|
|
|
e.preventDefault()
|
2021-05-15 13:54:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-30 20:56:42 +00:00
|
|
|
|
const info = inputs.keydown(e)
|
2021-06-26 11:52:36 +00:00
|
|
|
|
|
2021-05-23 13:46:04 +00:00
|
|
|
|
switch (e.key) {
|
2021-05-28 20:30:27 +00:00
|
|
|
|
case 'ArrowUp': {
|
2021-06-26 11:52:36 +00:00
|
|
|
|
state.send('NUDGED', { delta: [0, -1], ...info })
|
2021-05-28 20:30:27 +00:00
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
case 'ArrowRight': {
|
2021-06-26 11:52:36 +00:00
|
|
|
|
state.send('NUDGED', { delta: [1, 0], ...info })
|
2021-05-28 20:30:27 +00:00
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
case 'ArrowDown': {
|
2021-06-26 11:52:36 +00:00
|
|
|
|
state.send('NUDGED', { delta: [0, 1], ...info })
|
2021-05-28 20:30:27 +00:00
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
case 'ArrowLeft': {
|
2021-06-26 11:52:36 +00:00
|
|
|
|
state.send('NUDGED', { delta: [-1, 0], ...info })
|
2021-05-28 20:30:27 +00:00
|
|
|
|
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 '!': {
|
2021-05-24 17:01:27 +00:00
|
|
|
|
// Shift + 1
|
|
|
|
|
if (e.shiftKey) {
|
2021-05-28 20:30:27 +00:00
|
|
|
|
state.send('ZOOMED_TO_FIT')
|
2021-05-24 17:01:27 +00:00
|
|
|
|
}
|
|
|
|
|
break
|
|
|
|
|
}
|
2021-05-28 20:30:27 +00:00
|
|
|
|
case '@': {
|
2021-05-24 17:01:27 +00:00
|
|
|
|
// Shift + 2
|
|
|
|
|
if (e.shiftKey) {
|
2021-05-28 20:30:27 +00:00
|
|
|
|
state.send('ZOOMED_TO_SELECTION')
|
2021-05-24 17:01:27 +00:00
|
|
|
|
}
|
|
|
|
|
break
|
|
|
|
|
}
|
2021-05-28 20:30:27 +00:00
|
|
|
|
case ')': {
|
2021-05-24 17:01:27 +00:00
|
|
|
|
// Shift + 0
|
|
|
|
|
if (e.shiftKey) {
|
2021-05-28 20:30:27 +00:00
|
|
|
|
state.send('ZOOMED_TO_ACTUAL')
|
2021-05-24 17:01:27 +00:00
|
|
|
|
}
|
|
|
|
|
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-06-26 11:52:36 +00:00
|
|
|
|
state.send('REDO', info)
|
2021-05-23 13:46:04 +00:00
|
|
|
|
} else {
|
2021-06-26 11:52:36 +00:00
|
|
|
|
state.send('UNDO', info)
|
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-06-26 11:52:36 +00:00
|
|
|
|
...info,
|
2021-05-26 19:20:52 +00:00
|
|
|
|
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-06-26 11:52:36 +00:00
|
|
|
|
...info,
|
2021-05-26 19:20:52 +00:00
|
|
|
|
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-06-26 11:52:36 +00:00
|
|
|
|
...info,
|
2021-05-26 19:20:52 +00:00
|
|
|
|
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-06-26 11:52:36 +00:00
|
|
|
|
...info,
|
2021-05-26 19:20:52 +00:00
|
|
|
|
type: MoveType.Backward,
|
|
|
|
|
})
|
2021-05-23 13:46:04 +00:00
|
|
|
|
}
|
|
|
|
|
break
|
|
|
|
|
}
|
2021-05-28 20:30:27 +00:00
|
|
|
|
case 'Shift': {
|
2021-06-26 11:52:36 +00:00
|
|
|
|
state.send('PRESSED_SHIFT_KEY', info)
|
2021-05-23 13:46:04 +00:00
|
|
|
|
break
|
|
|
|
|
}
|
2021-05-28 20:30:27 +00:00
|
|
|
|
case 'Alt': {
|
2021-06-26 11:52:36 +00:00
|
|
|
|
state.send('PRESSED_ALT_KEY', info)
|
2021-05-23 13:46:04 +00:00
|
|
|
|
break
|
|
|
|
|
}
|
2021-05-28 20:30:27 +00:00
|
|
|
|
case 'Backspace': {
|
2021-06-26 11:52:36 +00:00
|
|
|
|
if (metaKey(e)) {
|
2021-06-30 13:03:36 +00:00
|
|
|
|
if (e.shiftKey) {
|
|
|
|
|
if (window.confirm('Reset document and state?')) {
|
|
|
|
|
state.send('RESET_DOCUMENT_STATE', info)
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
state.send('FORCE_CLEARED_PAGE', info)
|
|
|
|
|
}
|
2021-06-26 11:52:36 +00:00
|
|
|
|
} else {
|
|
|
|
|
state.send('DELETED', info)
|
|
|
|
|
}
|
2021-05-23 13:46:04 +00:00
|
|
|
|
break
|
|
|
|
|
}
|
2021-06-04 16:08:43 +00:00
|
|
|
|
case 'g': {
|
|
|
|
|
if (metaKey(e)) {
|
|
|
|
|
if (e.shiftKey) {
|
2021-06-26 11:52:36 +00:00
|
|
|
|
state.send('UNGROUPED', info)
|
2021-06-04 16:08:43 +00:00
|
|
|
|
} else {
|
2021-06-26 11:52:36 +00:00
|
|
|
|
state.send('GROUPED', info)
|
2021-06-04 16:08:43 +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-06-11 10:11:42 +00:00
|
|
|
|
if (e.shiftKey) {
|
2021-06-26 11:52:36 +00:00
|
|
|
|
state.send('SAVED_AS_TO_FILESYSTEM', info)
|
2021-06-11 10:11:42 +00:00
|
|
|
|
} else {
|
2021-06-26 11:52:36 +00:00
|
|
|
|
state.send('SAVED', info)
|
2021-06-11 10:11:42 +00:00
|
|
|
|
}
|
2021-05-23 13:46:04 +00:00
|
|
|
|
}
|
|
|
|
|
break
|
|
|
|
|
}
|
2021-06-20 10:22:42 +00:00
|
|
|
|
case 'o': {
|
|
|
|
|
if (metaKey(e)) {
|
2021-06-21 21:35:28 +00:00
|
|
|
|
break
|
2021-06-20 10:22:42 +00:00
|
|
|
|
} else {
|
2021-06-26 11:52:36 +00:00
|
|
|
|
state.send('SELECTED_DOT_TOOL', info)
|
2021-06-20 10:22:42 +00:00
|
|
|
|
}
|
|
|
|
|
break
|
|
|
|
|
}
|
2021-05-31 19:13:43 +00:00
|
|
|
|
case 'v': {
|
2021-05-23 13:46:04 +00:00
|
|
|
|
if (metaKey(e)) {
|
2021-06-26 11:52:36 +00:00
|
|
|
|
state.send('PASTED', info)
|
2021-05-31 19:13:43 +00:00
|
|
|
|
} else {
|
2021-06-26 11:52:36 +00:00
|
|
|
|
state.send('SELECTED_SELECT_TOOL', info)
|
2021-05-23 13:46:04 +00:00
|
|
|
|
}
|
|
|
|
|
break
|
|
|
|
|
}
|
2021-05-31 19:13:43 +00:00
|
|
|
|
case 'a': {
|
2021-05-23 13:46:04 +00:00
|
|
|
|
if (metaKey(e)) {
|
2021-06-26 11:52:36 +00:00
|
|
|
|
state.send('SELECTED_ALL', info)
|
2021-05-23 13:46:04 +00:00
|
|
|
|
} else {
|
2021-06-26 11:52:36 +00:00
|
|
|
|
state.send('SELECTED_ARROW_TOOL', info)
|
2021-05-23 13:46:04 +00:00
|
|
|
|
}
|
|
|
|
|
break
|
|
|
|
|
}
|
2021-05-28 20:30:27 +00:00
|
|
|
|
case 'd': {
|
|
|
|
|
if (metaKey(e)) {
|
2021-07-04 18:45:07 +00:00
|
|
|
|
if (e.shiftKey) {
|
|
|
|
|
state.send('TOGGLED_DEBUG_MODE')
|
|
|
|
|
} else {
|
|
|
|
|
state.send('DUPLICATED', info)
|
|
|
|
|
}
|
2021-05-28 20:30:27 +00:00
|
|
|
|
} else {
|
2021-06-26 11:52:36 +00:00
|
|
|
|
state.send('SELECTED_DRAW_TOOL', info)
|
2021-05-28 20:30:27 +00:00
|
|
|
|
}
|
2021-05-27 17:59:40 +00:00
|
|
|
|
break
|
|
|
|
|
}
|
2021-05-28 20:30:27 +00:00
|
|
|
|
case 't': {
|
2021-06-26 11:52:36 +00:00
|
|
|
|
state.send('SELECTED_TEXT_TOOL', info)
|
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-06-20 22:01:40 +00:00
|
|
|
|
if (e.shiftKey) {
|
2021-06-26 11:52:36 +00:00
|
|
|
|
state.send('COPIED_TO_SVG', info)
|
2021-06-20 22:01:40 +00:00
|
|
|
|
} else {
|
2021-06-26 11:52:36 +00:00
|
|
|
|
state.send('COPIED', info)
|
2021-06-20 22:01:40 +00:00
|
|
|
|
}
|
2021-05-23 13:46:04 +00:00
|
|
|
|
} else {
|
2021-06-26 11:52:36 +00:00
|
|
|
|
state.send('SELECTED_ELLIPSE_TOOL', info)
|
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)) {
|
2021-06-21 21:35:28 +00:00
|
|
|
|
break
|
2021-05-23 13:46:04 +00:00
|
|
|
|
} else {
|
2021-06-26 11:52:36 +00:00
|
|
|
|
state.send('SELECTED_CIRCLE_TOOL', info)
|
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)) {
|
2021-06-11 10:11:42 +00:00
|
|
|
|
if (e.shiftKey) {
|
2021-07-04 18:45:07 +00:00
|
|
|
|
state.send('TOGGLED_LOGGER')
|
|
|
|
|
} else {
|
2021-06-26 11:52:36 +00:00
|
|
|
|
state.send('LOADED_FROM_FILE_STSTEM', info)
|
2021-06-11 10:11:42 +00:00
|
|
|
|
}
|
2021-05-23 13:46:04 +00:00
|
|
|
|
} else {
|
2021-06-26 11:52:36 +00:00
|
|
|
|
state.send('SELECTED_LINE_TOOL', info)
|
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)) {
|
2021-06-21 21:35:28 +00:00
|
|
|
|
break
|
2021-05-23 13:46:04 +00:00
|
|
|
|
} else {
|
2021-06-26 11:52:36 +00:00
|
|
|
|
state.send('SELECTED_RAY_TOOL', info)
|
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)) {
|
2021-06-21 21:35:28 +00:00
|
|
|
|
break
|
2021-05-23 13:46:04 +00:00
|
|
|
|
} else {
|
2021-06-26 11:52:36 +00:00
|
|
|
|
state.send('SELECTED_POLYLINE_TOOL', info)
|
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)) {
|
2021-06-21 21:35:28 +00:00
|
|
|
|
break
|
2021-05-23 13:46:04 +00:00
|
|
|
|
} else {
|
2021-06-26 11:52:36 +00:00
|
|
|
|
state.send('SELECTED_RECTANGLE_TOOL', info)
|
2021-05-23 13:46:04 +00:00
|
|
|
|
}
|
|
|
|
|
break
|
|
|
|
|
}
|
2021-06-23 12:46:16 +00:00
|
|
|
|
case '|': {
|
|
|
|
|
state.send('COPIED_STATE_TO_CLIPBOARD')
|
|
|
|
|
break
|
|
|
|
|
}
|
2021-05-23 13:46:04 +00:00
|
|
|
|
default: {
|
2021-07-02 10:48:49 +00:00
|
|
|
|
null
|
2021-05-23 13:46:04 +00:00
|
|
|
|
}
|
2021-05-15 13:54:27 +00:00
|
|
|
|
}
|
2021-05-12 11:27:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function handleKeyUp(e: KeyboardEvent) {
|
2021-06-30 20:56:42 +00:00
|
|
|
|
const info = inputs.keyup(e)
|
2021-06-26 11:52:36 +00:00
|
|
|
|
|
2021-05-28 20:30:27 +00:00
|
|
|
|
if (e.key === 'Shift') {
|
2021-06-26 11:52:36 +00:00
|
|
|
|
state.send('RELEASED_SHIFT_KEY', info)
|
2021-05-12 11:27:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-28 20:30:27 +00:00
|
|
|
|
if (e.key === 'Alt') {
|
2021-06-26 11:52:36 +00:00
|
|
|
|
state.send('RELEASED_ALT_KEY', info)
|
2021-05-19 21:24:41 +00:00
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
}, [])
|
|
|
|
|
}
|