| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- import { useEffect } from "react"
- import state from "state"
- import { getKeyboardEventInfo, isDarwin, metaKey } from "utils/utils"
-
- export default function useKeyboardEvents() {
- useEffect(() => {
- function handleKeyDown(e: KeyboardEvent) {
- if (e.key === "Escape") {
- state.send("CANCELLED")
- } else if (e.key === "z" && metaKey(e)) {
- if (e.shiftKey) {
- state.send("REDO")
- } else {
- state.send("UNDO")
- }
- }
-
- if (e.altKey) {
- state.send("PRESSED_ALT_KEY", getKeyboardEventInfo(e))
- }
-
- if (e.key === "Backspace" && !(metaKey(e) || e.shiftKey || e.altKey)) {
- state.send("DELETED", getKeyboardEventInfo(e))
- }
-
- if (e.key === "s" && metaKey(e)) {
- e.preventDefault()
- state.send("SAVED")
- }
- if (e.key === "a" && metaKey(e)) {
- e.preventDefault()
- state.send("SELECTED_ALL")
- }
-
- if (e.key === "v" && !(metaKey(e) || e.shiftKey || e.altKey)) {
- state.send("SELECTED_SELECT_TOOL", getKeyboardEventInfo(e))
- }
-
- if (e.key === "d" && !(metaKey(e) || e.shiftKey || e.altKey)) {
- state.send("SELECTED_DOT_TOOL", getKeyboardEventInfo(e))
- }
-
- if (e.key === "c" && !(metaKey(e) || e.shiftKey || e.altKey)) {
- state.send("SELECTED_CIRCLE_TOOL", getKeyboardEventInfo(e))
- }
-
- if (e.key === "i" && !(metaKey(e) || e.shiftKey || e.altKey)) {
- state.send("SELECTED_ELLIPSE_TOOL", getKeyboardEventInfo(e))
- }
-
- if (e.key === "l" && !(metaKey(e) || e.shiftKey || e.altKey)) {
- state.send("SELECTED_LINE_TOOL", getKeyboardEventInfo(e))
- }
-
- if (e.key === "y" && !(metaKey(e) || e.shiftKey || e.altKey)) {
- state.send("SELECTED_RAY_TOOL", getKeyboardEventInfo(e))
- }
-
- if (e.key === "p" && !(metaKey(e) || e.shiftKey || e.altKey)) {
- state.send("SELECTED_POLYLINE_TOOL", getKeyboardEventInfo(e))
- }
-
- if (e.key === "r" && !(metaKey(e) || e.shiftKey || e.altKey)) {
- state.send("SELECTED_RECTANGLE_TOOL", getKeyboardEventInfo(e))
- }
- }
-
- function handleKeyUp(e: KeyboardEvent) {
- if (e.key === "Escape") {
- state.send("CANCELLED")
- }
-
- if (e.altKey) {
- state.send("RELEASED_ALT_KEY")
- }
-
- 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)
- }
- }, [])
- }
|