Farmbot-Web-App/frontend/hotkeys.tsx

124 lines
3.1 KiB
TypeScript
Raw Permalink Normal View History

2017-07-06 21:26:37 -06:00
import * as React from "react";
2019-08-23 15:18:28 -06:00
import { getLinks } from "./nav/nav_links";
2017-10-23 06:58:42 -06:00
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,
2020-02-28 09:35:32 -07:00
Classes,
2017-07-07 00:00:20 -06:00
} from "@blueprintjs/core";
2019-02-04 07:32:26 -07:00
import { findIndex } from "lodash";
2019-04-02 13:59:37 -06:00
import { t } from "./i18next_wrapper";
2019-11-22 12:57:22 -07:00
import { unselectPlant } from "./farm_designer/map/actions";
2017-07-07 00:00:20 -06:00
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,
2020-02-28 09:35:32 -07:00
Classes.ELEVATION_4,
2017-07-07 11:54:09 -06:00
].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() {
2019-03-04 15:14:35 -07:00
return <Overlay
isOpen={this.state.guideOpen}
onClose={this.toggle("guideOpen")}>
<div className={hotkeyGuideClasses}>
<h3>{t("Hotkeys")}</h3>
2019-08-26 12:20:46 -06:00
<i className="fa fa-times"
title={t("Close")}
2019-03-04 15:14:35 -07:00
onClick={this.toggle("guideOpen")} />
{this.hotkeys(this.props.dispatch, "")
.map(hotkey => <Row key={hotkey.combo}>
<Col xs={5}>
<label>{hotkey.label}</label>
</Col>
<Col xs={7}>
<code>{hotkey.combo}</code>
</Col>
</Row>)}
</div>
</Overlay>;
2017-07-07 00:00:20 -06:00
}
2019-09-03 13:55:56 -06:00
toggle = (property: keyof State) => () =>
2017-07-07 11:54:09 -06:00
this.setState({ [property]: !this.state[property] });
2019-09-03 13:55:56 -06:00
hotkeys(dispatch: Function, slug: string) {
2019-08-23 15:18:28 -06:00
const links = getLinks();
2019-02-04 07:32:26 -07:00
const idx = findIndex(links, { slug });
2017-10-06 01:12:12 -06:00
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",
2019-04-09 19:52:12 -06:00
label: "Add Event",
onKeyDown: () => push("/app/designer/events/add")
2017-07-07 02:08:43 -06:00
},
{
combo: "esc",
label: "Back to plant overview",
2019-10-13 10:41:29 -06:00
onKeyDown: () => {
push("/app/designer/plants");
dispatch(unselectPlant(dispatch));
}
},
2017-07-07 11:54:09 -06:00
{
combo: "ctrl + shift + /",
label: "Toggle Guide",
onKeyDown: () => this.toggle("guideOpen")()
2020-02-28 09:35:32 -07:00
},
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>
2019-03-04 15:14:35 -07:00
{this.hotkeys(this.props.dispatch, slug)
.map(({ combo, label, onKeyDown }: IHotkeyProps, index: number) =>
<Hotkey
key={index}
global={true}
combo={combo}
label={label}
onKeyDown={onKeyDown} />)}
2017-08-04 10:36:32 -06:00
</Hotkeys>;
2017-07-06 21:26:37 -06:00
}
}