Farmbot-Web-App/webpack/hotkeys.tsx

123 lines
3.0 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-10-23 06:58:42 -06:00
import { links } from "./nav/nav_links";
import { sync } from "./devices/actions";
2017-11-22 00:00:28 -07:00
import { push, getPathArray } from "./history";
2017-10-23 06:58:42 -06:00
import { Row, Col } from "./ui/index";
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-07-06 21:26:37 -06:00
interface Props {
dispatch: Function;
}
2017-07-07 11:54:09 -06:00
interface State {
guideOpen: boolean;
}
2017-10-06 01:12:12 -06:00
const hotkeyGuideClasses = [
2017-07-07 11:54:09 -06:00
"hotkey-guide",
2018-05-14 16:29:09 -06:00
Classes.CARD,
2017-07-07 11:54:09 -06:00
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() {
return <div>
<Overlay
isOpen={this.state.guideOpen}
onClose={this.toggle("guideOpen")}>
<div className={hotkeyGuideClasses}>
<h3>{t("Hotkeys")}</h3>
<i
className="fa fa-times"
onClick={this.toggle("guideOpen")} />
{
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
}
2018-03-02 08:25:15 -07:00
private toggle = (property: keyof State) => () =>
2017-07-07 11:54:09 -06:00
this.setState({ [property]: !this.state[property] });
2018-03-02 08:25:15 -07:00
private hotkeys(dispatch: Function, slug: string) {
2017-10-06 01:12:12 -06:00
const idx = _.findIndex(links, { slug });
const right = "/app/" + (links[idx + 1] || links[0]).slug;
const left = "/app/" + (links[idx - 1] || links[links.length - 1]).slug;
const 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
}
2018-03-02 08:25:15 -07:00
public renderHotkeys() {
2017-11-22 00:00:28 -07:00
const slug = getPathArray()[2];
2017-08-04 10:36:32 -06:00
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
}
}