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