- Notifications
You must be signed in to change notification settings - Fork 185
[WIP] Feature: Keyboard Shortcuts (rebased on master) #34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
2a764cb
269f81e
7a9a88c
9fd6df7
3c8ca7d
97374ad
3085fb9
3f15755
a507a71
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
| @@ -127,5 +127,5 @@ export type Action = | |
| {| type: "CLOSE_REGION_EDITOR", region: Region |} | ||
| {| type: "DELETE_REGION", region: Region |} | ||
| {| type: "HEADER_BUTTON_CLICKED", buttonName: string |} | ||
| {| type: "SELECT_TOOL", selectedTool: string |} | ||
| {| type: "SELECT_TOOL", selectedTool: ToolEnum |} | ||
| {| type: "CANCEL" |} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. need to add new actions here...
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import React from "react" | ||
import TextField from '@material-ui/core/TextField'; | ||
import { makeStyles } from '@material-ui/core/styles'; | ||
| ||
const useStyles = makeStyles({ | ||
shortcutKeyFieldWrapper: { | ||
paddingTop: 8, | ||
display: "inline-flex", | ||
width: '100%' | ||
}, | ||
shortcutKeyText: { | ||
lineHeight: 0 | ||
}, | ||
shortcutTextfield: { | ||
width: "100%", | ||
boxSizing: "border-box", | ||
textAlign: 'center' | ||
} | ||
}) | ||
| ||
const ShortcutField = ({actionId, actionName, keyName, onChangeShortcut}) =>{ | ||
const classes = useStyles() | ||
| ||
return( | ||
<div | ||
className={classes.shortcutKeyFieldWrapper} | ||
> | ||
<TextField | ||
variant="outlined" | ||
label={actionName} | ||
className={classes.shortcutTextfield} | ||
value={keyName} | ||
onKeyPress={e => { | ||
onChangeShortcut(actionId, e.key) | ||
e.stopPropagation() | ||
}} | ||
/> | ||
</div> | ||
) | ||
} | ||
| ||
export default ShortcutField |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import React, { useState, useReducer, useEffect, useCallback } from "react" | ||
import SidebarBoxContainer from "../SidebarBoxContainer" | ||
import { setIn } from "seamless-immutable" | ||
import ShortcutField from "./ShortcutField" | ||
| ||
const defaultShortcuts = { | ||
select: { | ||
name: "Select Region", | ||
key: "Escape" | ||
}, | ||
zoom: { | ||
name: "Zoom In/Out", | ||
key: "z" | ||
}, | ||
"create-point": { | ||
name: "Create Point" | ||
}, | ||
"create-box": { | ||
name: "Add Bounding Box", | ||
key: "b" | ||
}, | ||
pan: { | ||
name: "Pan" | ||
}, | ||
"create-polygon": { | ||
name: "Create Polygon" | ||
}, | ||
"create-pixel": { | ||
name: "Create Pixel" | ||
}, | ||
"prev-image": { | ||
//TODO: { type: "GO_TO_PREV_IMAGE" } | ||
name: "Previous Image" | ||
}, | ||
"next-image": { | ||
//TODO: { type: "GO_TO_NEXT_IMAGE" } | ||
name: "Next Image" | ||
| ||
} | ||
} | ||
| ||
export default ({ onShortcutActionDispatched }) => { | ||
const [shortcuts, setShortcuts] = useState({}) // useLocalStorage | ||
| ||
useEffect(() => { | ||
const newShortcuts = { ...shortcuts } | ||
for (const actionId of Object.keys(defaultShortcuts)) { | ||
if (!newShortcuts[actionId]) { | ||
newShortcuts[actionId] = defaultShortcuts[actionId] | ||
} | ||
} | ||
setShortcuts(newShortcuts) | ||
}, []) | ||
| ||
const onChangeShortcut = (actionId, keyName) => { | ||
setShortcuts(setIn(shortcuts, [actionId, "key"], keyName)) | ||
} | ||
| ||
useEffect(() => { | ||
const handleKeyPress = e => { | ||
for (const actionId in shortcuts) { | ||
const shortcut = shortcuts[actionId] | ||
if (!shortcut || !shortcut.key) { | ||
continue | ||
} | ||
if (e.key === shortcut.key) { | ||
onShortcutActionDispatched({ | ||
type: "SELECT_TOOL", | ||
selectedTool: actionId | ||
}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. change this to... onShortcutActionDispatched(shortcut.action) | ||
} | ||
} | ||
} | ||
| ||
window.addEventListener("keypress", handleKeyPress) | ||
| ||
return () => { | ||
window.removeEventListener("keypress", handleKeyPress) | ||
} | ||
}, [shortcuts]) | ||
| ||
return ( | ||
<SidebarBoxContainer title="Shortcuts"> | ||
{Object.keys(shortcuts) | ||
.map((actionId, index) => { | ||
if (!shortcuts[actionId]) return null | ||
return ( | ||
<ShortcutField | ||
key={actionId} | ||
actionId={actionId} | ||
actionName={shortcuts[actionId].name} | ||
keyName={shortcuts[actionId].key || ""} | ||
onChangeShortcut={onChangeShortcut} | ||
/> | ||
) | ||
}) | ||
.filter(Boolean)} | ||
</SidebarBoxContainer> | ||
) | ||
} |
Uh oh!
There was an error while loading. Please reload this page.