tldraw/hooks/useKeyboardEvents.ts

376 lines
8.8 KiB
TypeScript
Raw Normal View History

2021-06-21 21:35:28 +00:00
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
import { useEffect } from 'react'
2021-05-28 20:30:27 +00:00
import state from 'state'
2021-06-30 20:56:42 +00:00
import inputs from 'state/inputs'
import { ColorStyle, MoveType, SizeStyle } 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() {
2021-05-12 11:27:33 +00:00
useEffect(() => {
function handleKeyDown(e: KeyboardEvent) {
const info = inputs.keydown(e)
const meta = metaKey(e)
if (
meta &&
![
'a',
'i',
'r',
'j',
'ArrowLeft',
'ArrowRight',
'ArrowUp',
'ArrowDown',
'z',
].includes(e.key)
) {
2021-05-17 21:27:18 +00:00
e.preventDefault()
}
2021-05-23 13:46:04 +00:00
switch (e.key) {
case '1': {
if (meta) {
state.send('CHANGED_STYLE', { color: ColorStyle.Black })
break
}
if (e.altKey) {
state.send('CHANGED_STYLE', { size: SizeStyle.Small })
break
}
state.send('SELECTED_SELECT_TOOL', info)
break
}
case '2': {
if (meta) {
state.send('CHANGED_STYLE', { color: ColorStyle.White })
break
}
if (e.altKey) {
state.send('CHANGED_STYLE', { size: SizeStyle.Medium })
break
}
state.send('SELECTED_DRAW_TOOL', info)
break
}
case '3': {
if (meta) {
state.send('CHANGED_STYLE', { color: ColorStyle.Green })
break
}
if (e.altKey) {
state.send('CHANGED_STYLE', { size: SizeStyle.Large })
break
}
state.send('SELECTED_RECTANGLE_TOOL', info)
break
}
case '4': {
if (meta) {
state.send('CHANGED_STYLE', { color: ColorStyle.Blue })
}
state.send('SELECTED_ELLIPSE_TOOL', info)
break
}
case '5': {
if (meta) {
state.send('CHANGED_STYLE', { color: ColorStyle.Indigo })
break
}
state.send('SELECTED_ARROW_TOOL', info)
break
}
case '6': {
if (meta) {
state.send('CHANGED_STYLE', { color: ColorStyle.Violet })
break
}
state.send('SELECTED_TEXT_TOOL', info)
break
}
case '7': {
if (meta) {
state.send('CHANGED_STYLE', { color: ColorStyle.Red })
break
}
state.send('TOGGLED_TOOL_LOCK', info)
break
}
case '8': {
if (meta) {
state.send('CHANGED_STYLE', { color: ColorStyle.Orange })
break
}
break
}
case '9': {
if (meta) {
2021-07-13 20:53:16 +00:00
state.send('CHANGED_STYLE', { color: ColorStyle.Yellow })
break
}
break
}
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 '!': {
// 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': {
if (meta) {
2021-05-23 13:46:04 +00:00
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 '': {
if (meta) {
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 '“': {
if (meta) {
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 ']': {
if (meta) {
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 '[': {
if (meta) {
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': {
if (meta) {
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 (meta) {
2021-06-04 16:08:43 +00:00
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': {
if (meta) {
if (e.shiftKey) {
2021-06-26 11:52:36 +00:00
state.send('SAVED_AS_TO_FILESYSTEM', info)
} else {
2021-06-26 11:52:36 +00:00
state.send('SAVED', info)
}
2021-05-23 13:46:04 +00:00
}
break
}
case 'o': {
if (meta) {
2021-06-21 21:35:28 +00:00
break
} else {
2021-06-26 11:52:36 +00:00
state.send('SELECTED_DOT_TOOL', info)
}
break
}
2021-05-31 19:13:43 +00:00
case 'v': {
if (meta) {
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': {
if (meta) {
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 (meta) {
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': {
if (meta) {
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': {
if (meta) {
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': {
if (meta) {
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-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': {
if (meta) {
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': {
if (meta) {
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': {
if (meta) {
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
}
case '|': {
state.send('COPIED_STATE_TO_CLIPBOARD')
break
}
2021-05-23 13:46:04 +00:00
default: {
null
2021-05-23 13:46:04 +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
}
window.addEventListener('keydown', handleKeyDown)
window.addEventListener('keyup', handleKeyUp)
2021-05-12 11:27:33 +00:00
return () => {
window.removeEventListener('keydown', handleKeyDown)
window.removeEventListener('keyup', handleKeyUp)
2021-05-12 11:27:33 +00:00
}
}, [])
}