Skip to content
This repository has been archived by the owner on Jan 20, 2024. It is now read-only.

Commit

Permalink
Add hotkeys to the editor view
Browse files Browse the repository at this point in the history
  • Loading branch information
tdemin committed Oct 9, 2019
1 parent 27cc96c commit 559bf63
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/views/components/taskSelect.tsx
Expand Up @@ -18,6 +18,7 @@ interface Props extends HTMLAttributes<HTMLSelectElement> {
}
const TaskSelect: React.FC<Props> = (props) => (
<select
id={props.id}
onChange={props.onChange}
value={props.initialValue}
className={props.className}
Expand Down
32 changes: 32 additions & 0 deletions src/views/editorView.tsx
Expand Up @@ -73,6 +73,35 @@ class EditorView extends React.Component<Props, State> {
title: strings.editor_newTaskTitle,
});
}
document.addEventListener("keydown", this.handleHotkeys);
};
componentWillUnmount = () => {
document.removeEventListener("keydown", this.handleHotkeys);
};
handleHotkeys = (event: KeyboardEvent) => {
const textInput = document.getElementById("taskTextInput");
const parentSelect = document.getElementById("taskParentSelect");
const statusBtn = document.getElementById("taskStatusButton");
const editFocused =
document.activeElement === textInput ||
document.activeElement === parentSelect ||
document.activeElement === statusBtn;
if (event.key === "b" && !editFocused) {
event.preventDefault();
this.props.history.push("/");
} else if (event.key === "s" && !editFocused) {
event.preventDefault();
this.saveChanges();
} else if (event.key === "d" && !editFocused && !this.state.newTask) {
event.preventDefault();
this.delete();
} else if (
event.keyCode === 27 &&
document.activeElement === textInput
) {
event.preventDefault();
(textInput as HTMLElement).blur();
}
};
delete = () => {
this.props.dispatch(deleteTask(this.state.task));
Expand Down Expand Up @@ -152,6 +181,7 @@ class EditorView extends React.Component<Props, State> {
</label>
<div className="control">
<input
id="taskStatusButton"
className="button"
type="button"
value={
Expand All @@ -169,6 +199,7 @@ class EditorView extends React.Component<Props, State> {
</label>
<div className="control">
<input
id="taskTextInput"
className="input"
type="text"
autoFocus
Expand All @@ -184,6 +215,7 @@ class EditorView extends React.Component<Props, State> {
<div className="control">
<div className="select">
<TaskSelect
id="taskParentSelect"
current={task}
initialValue={task.PID}
onChange={this.updateParent}
Expand Down
3 changes: 3 additions & 0 deletions src/views/mainView.tsx
Expand Up @@ -54,6 +54,9 @@ class MainView extends React.Component<Props, State> {
this.refetch();
document.addEventListener("keydown", this.handleHotkeys);
};
componentWillUnmount = () => {
document.removeEventListener("keydown", this.handleHotkeys);
};
componentDidUpdate = (prevProps: Props) => {
if (prevProps.tasks !== this.props.tasks) {
this.setState({
Expand Down

0 comments on commit 559bf63

Please sign in to comment.