Farmbot-Web-App/webpack/hotkeys.tsx

128 lines
3.1 KiB
TypeScript
Raw Normal View History

2017-07-06 21:26:37 -06:00
import * as React from "react";
2017-07-07 00:00:20 -06:00
import * as _ from "lodash";
2017-07-07 11:54:09 -06:00
import { t } from "i18next";
2017-07-07 00:00:20 -06:00
import {
Hotkey,
Hotkeys,
HotkeysTarget,
2017-07-07 11:54:09 -06:00
IHotkeyProps,
Overlay,
Classes
2017-07-07 00:00:20 -06:00
} from "@blueprintjs/core";
2017-08-04 15:41:15 -06:00
import { links } from "./nav/nav_links";
2017-07-07 00:00:20 -06:00
import { sync } from "./devices/actions";
2017-08-04 10:36:32 -06:00
import { history, push } from "./history";
2017-07-07 11:54:09 -06:00
import { Row, Col } from "./ui/index";
2017-07-06 21:26:37 -06:00
interface Props {
dispatch: Function;
}
2017-07-07 11:54:09 -06:00
interface State {
guideOpen: boolean;
}
let hotkeyGuideClasses = [
"hotkey-guide",
"pt-card",
Classes.ELEVATION_4
].join(" ");
2017-07-06 21:26:37 -06:00
@HotkeysTarget
2017-07-07 11:54:09 -06:00
export class HotKeys extends React.Component<Props, Partial<State>> {
state: State = { guideOpen: false };
2017-07-07 00:00:20 -06:00
render() {
2017-07-07 11:54:09 -06:00
return (
<div>
<Overlay
isOpen={this.state.guideOpen}
2017-08-23 16:26:09 -06:00
onClose={this.toggle("guideOpen")}>
2017-07-07 11:54:09 -06:00
<div className={hotkeyGuideClasses}>
<h3>{t("Hotkeys")}</h3>
<i
className="fa fa-times"
2017-08-23 16:26:09 -06:00
onClick={this.toggle("guideOpen")} />
2017-07-07 11:54:09 -06:00
{
this.hotkeys(this.props.dispatch, "")
.map(hotkey => {
return (
<Row key={hotkey.combo}>
<Col xs={5}>
<label>{hotkey.label}</label>
</Col>
<Col xs={7}>
<code>{hotkey.combo}</code>
</Col>
</Row>
);
})
}
</div>
</Overlay>
</div>
);
2017-07-07 00:00:20 -06:00
}
2017-07-07 11:54:09 -06:00
toggle = (property: keyof State) => () =>
this.setState({ [property]: !this.state[property] });
2017-07-07 00:00:20 -06:00
hotkeys(dispatch: Function, slug: string) {
let idx = _.findIndex(links, { slug });
let right = "/app/" + (links[idx + 1] || links[0]).slug;
let left = "/app/" + (links[idx - 1] || links[links.length - 1]).slug;
2017-07-07 00:01:30 -06:00
let hotkeyMap: IHotkeyProps[] = [
2017-07-07 00:00:20 -06:00
{
2017-07-07 02:16:40 -06:00
combo: "ctrl + shift + s",
label: "Sync",
2017-07-07 00:00:20 -06:00
onKeyDown: () => dispatch(sync())
},
{
2017-07-07 02:16:40 -06:00
combo: "ctrl + shift + right",
label: "Navigate Right",
2017-07-07 00:00:20 -06:00
onKeyDown: () => push(right)
},
{
2017-07-07 02:16:40 -06:00
combo: "ctrl + shift + left",
label: "Navigate Left",
2017-07-07 00:00:20 -06:00
onKeyDown: () => push(left)
},
2017-07-07 00:49:21 -06:00
{
2017-07-07 02:16:40 -06:00
combo: "ctrl + shift + p",
label: "Add Plant",
2017-07-07 00:49:21 -06:00
onKeyDown: () => push("/app/designer/plants/crop_search")
},
{
2017-07-07 02:16:40 -06:00
combo: "ctrl + shift + e",
label: "Add Farm Event",
2017-07-07 02:08:43 -06:00
onKeyDown: () => push("/app/designer/farm_events/add")
},
2017-07-07 11:54:09 -06:00
{
combo: "ctrl + shift + /",
label: "Toggle Guide",
onKeyDown: () => this.toggle("guideOpen")()
},
2017-07-07 00:00:20 -06:00
];
2017-07-07 00:01:30 -06:00
return hotkeyMap;
2017-07-06 21:26:37 -06:00
}
2017-07-07 00:00:20 -06:00
renderHotkeys() {
2017-08-04 10:36:32 -06:00
let slug = history.getCurrentLocation().pathname.split("/")[2];
return <Hotkeys>
{
this.hotkeys(this.props.dispatch, slug)
.map(({ combo, label, onKeyDown }: IHotkeyProps, index: number) => {
return <Hotkey
key={index}
global={true}
combo={combo}
label={label}
2017-08-23 16:26:09 -06:00
onKeyDown={onKeyDown} />;
2017-08-04 10:36:32 -06:00
})
}
</Hotkeys>;
2017-07-06 21:26:37 -06:00
}
}