Farmbot-Web-App/src/hotkeys.tsx

67 lines
1.6 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";
import {
Hotkey,
Hotkeys,
HotkeysTarget,
IHotkeyProps
} from "@blueprintjs/core";
import { links } from "./nav/links";
import { sync } from "./devices/actions";
import { lastUrlChunk } from "./util";
import { history, push } from "./history";
2017-07-06 21:26:37 -06:00
interface Props {
dispatch: Function;
}
@HotkeysTarget
2017-07-07 00:00:20 -06:00
export class HotKeys extends React.Component<Props, {}> {
render() {
return <span />;
}
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
{
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)
},
];
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() {
let slug = history.getCurrentLocation().pathname.split("/")[2];
2017-07-06 21:26:37 -06:00
return <Hotkeys>
2017-07-07 00:00:20 -06:00
{
this.hotkeys(this.props.dispatch, slug)
.map(({ combo, label, onKeyDown }: IHotkeyProps, index: number) => {
return <Hotkey
key={index}
global={true}
combo={combo}
label={label}
onKeyDown={onKeyDown}
/>;
})
}
2017-07-06 21:26:37 -06:00
</Hotkeys>;
}
}