import * as React from "react"; import { getLinks } from "./nav/nav_links"; import { sync } from "./devices/actions"; import { push, getPathArray } from "./history"; import { Row, Col } from "./ui/index"; import { Hotkey, Hotkeys, HotkeysTarget, IHotkeyProps, Overlay, Classes, } from "@blueprintjs/core"; import { findIndex } from "lodash"; import { t } from "./i18next_wrapper"; import { unselectPlant } from "./farm_designer/map/actions"; interface Props { dispatch: Function; } interface State { guideOpen: boolean; } const hotkeyGuideClasses = [ "hotkey-guide", Classes.CARD, Classes.ELEVATION_4, ].join(" "); @HotkeysTarget export class HotKeys extends React.Component> { state: State = { guideOpen: false }; render() { return

{t("Hotkeys")}

{this.hotkeys(this.props.dispatch, "") .map(hotkey => {hotkey.combo} )}
; } toggle = (property: keyof State) => () => this.setState({ [property]: !this.state[property] }); hotkeys(dispatch: Function, slug: string) { const links = getLinks(); 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[] = [ { combo: "ctrl + shift + s", label: "Sync", onKeyDown: () => dispatch(sync()) }, { combo: "ctrl + shift + right", label: "Navigate Right", onKeyDown: () => push(right) }, { combo: "ctrl + shift + left", label: "Navigate Left", onKeyDown: () => push(left) }, { combo: "ctrl + shift + p", label: "Add Plant", onKeyDown: () => push("/app/designer/plants/crop_search") }, { combo: "ctrl + shift + e", label: "Add Event", onKeyDown: () => push("/app/designer/events/add") }, { combo: "esc", label: "Back to plant overview", onKeyDown: () => { push("/app/designer/plants"); dispatch(unselectPlant(dispatch)); } }, { combo: "ctrl + shift + /", label: "Toggle Guide", onKeyDown: () => this.toggle("guideOpen")() }, ]; return hotkeyMap; } public renderHotkeys() { const slug = getPathArray()[2]; return {this.hotkeys(this.props.dispatch, slug) .map(({ combo, label, onKeyDown }: IHotkeyProps, index: number) => )} ; } }