2018-04-04 16:36:49 +02:00
|
|
|
/*
|
|
|
|
See hotkey.js for handlers that are more app-wide.
|
|
|
|
*/
|
|
|
|
|
2021-05-26 19:51:07 +02:00
|
|
|
export const vim_left = "h";
|
|
|
|
export const vim_down = "j";
|
|
|
|
export const vim_up = "k";
|
|
|
|
export const vim_right = "l";
|
2018-04-04 16:36:49 +02:00
|
|
|
|
2021-05-26 19:59:12 +02:00
|
|
|
export function handle(opts: {
|
|
|
|
elem: JQuery;
|
|
|
|
handlers: {
|
|
|
|
[handler: string]: (() => boolean) | undefined;
|
|
|
|
};
|
|
|
|
}): void {
|
2020-07-20 21:26:58 +02:00
|
|
|
opts.elem.on("keydown", (e) => {
|
2018-12-04 19:15:31 +01:00
|
|
|
if (e.altKey || e.ctrlKey || e.shiftKey) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-05-26 19:51:07 +02:00
|
|
|
const {key} = e;
|
|
|
|
const handler = opts.handlers[key];
|
|
|
|
if (!handler) {
|
2018-04-04 16:36:49 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-05-26 19:51:07 +02:00
|
|
|
const handled = handler();
|
2018-04-04 16:36:49 +02:00
|
|
|
if (handled) {
|
|
|
|
e.preventDefault();
|
2018-06-09 08:06:49 +02:00
|
|
|
e.stopPropagation();
|
2018-04-04 16:36:49 +02:00
|
|
|
}
|
|
|
|
});
|
2021-02-28 00:36:50 +01:00
|
|
|
}
|