|
| 1 | +import React, { useMemo } from "react" |
| 2 | +import { HotKeys } from "react-hotkeys" |
| 3 | + |
| 4 | +export const defaultHotkeys = [ |
| 5 | + { |
| 6 | + id: "select_tool", |
| 7 | + description: "Switch to the Select Tool", |
| 8 | + binding: "escape", |
| 9 | + }, |
| 10 | + { |
| 11 | + id: "zoom_tool", |
| 12 | + description: "Select the Zoom Tool", |
| 13 | + binding: "z", |
| 14 | + }, |
| 15 | + { |
| 16 | + id: "create_point", |
| 17 | + description: "Create a point", |
| 18 | + }, |
| 19 | + { |
| 20 | + id: "pan_tool", |
| 21 | + description: "Select the Pan Tool", |
| 22 | + }, |
| 23 | + { |
| 24 | + id: "create_polygon", |
| 25 | + description: "Create a Polygon", |
| 26 | + }, |
| 27 | + { |
| 28 | + id: "create_pixel", |
| 29 | + description: "Create a Pixel Mask", |
| 30 | + }, |
| 31 | + { |
| 32 | + id: "save_and_previous_sample", |
| 33 | + description: "Save and go to previous sample", |
| 34 | + }, |
| 35 | + { |
| 36 | + id: "save_and_next_sample", |
| 37 | + description: "Save and go to next sample", |
| 38 | + }, |
| 39 | + { |
| 40 | + id: "save_and_exit_sample", |
| 41 | + description: "Save and exit current sample", |
| 42 | + }, |
| 43 | + { |
| 44 | + id: "exit_sample", |
| 45 | + description: "Exit sample without saving", |
| 46 | + }, |
| 47 | +] |
| 48 | +export const defaultKeyMap = {} |
| 49 | +for (const { id, binding } of defaultHotkeys) defaultKeyMap[id] = binding |
| 50 | + |
| 51 | +export default ({ children, dispatch }) => { |
| 52 | + const handlers = useMemo( |
| 53 | + () => ({ |
| 54 | + select_tool: () => { |
| 55 | + dispatch({ |
| 56 | + type: "SELECT_TOOL", |
| 57 | + selectedTool: "select-tool", |
| 58 | + }) |
| 59 | + }, |
| 60 | + zoom_tool: () => { |
| 61 | + dispatch({ |
| 62 | + type: "SELECT_TOOL", |
| 63 | + selectedTool: "zoom", |
| 64 | + }) |
| 65 | + }, |
| 66 | + create_point: () => { |
| 67 | + dispatch({ |
| 68 | + type: "SELECT_TOOL", |
| 69 | + selectedTool: "create-point", |
| 70 | + }) |
| 71 | + }, |
| 72 | + pan_tool: () => { |
| 73 | + dispatch({ |
| 74 | + type: "SELECT_TOOL", |
| 75 | + selectedTool: "pan", |
| 76 | + }) |
| 77 | + }, |
| 78 | + create_polygon: () => { |
| 79 | + dispatch({ |
| 80 | + type: "SELECT_TOOL", |
| 81 | + selectedTool: "create-polygon", |
| 82 | + }) |
| 83 | + }, |
| 84 | + create_pixel: () => { |
| 85 | + dispatch({ |
| 86 | + type: "SELECT_TOOL", |
| 87 | + selectedTool: "create-pixel", |
| 88 | + }) |
| 89 | + }, |
| 90 | + save_and_previous_sample: () => { |
| 91 | + dispatch({ |
| 92 | + type: "HEADER_BUTTON_CLICKED", |
| 93 | + buttonName: "Prev", |
| 94 | + }) |
| 95 | + }, |
| 96 | + save_and_next_sample: () => { |
| 97 | + dispatch({ |
| 98 | + type: "HEADER_BUTTON_CLICKED", |
| 99 | + buttonName: "Next", |
| 100 | + }) |
| 101 | + }, |
| 102 | + save_and_exit_sample: () => { |
| 103 | + dispatch({ |
| 104 | + type: "HEADER_BUTTON_CLICKED", |
| 105 | + buttonName: "Save", |
| 106 | + }) |
| 107 | + }, |
| 108 | + // TODO |
| 109 | + // exit_sample: () => { |
| 110 | + // dispatch({ |
| 111 | + // type: "", |
| 112 | + // }) |
| 113 | + // } |
| 114 | + }), |
| 115 | + [dispatch] |
| 116 | + ) |
| 117 | + return <HotKeys handlers={handlers}>{children}</HotKeys> |
| 118 | +} |
0 commit comments