2018-04-04 16:36:49 +02:00
|
|
|
/*
|
|
|
|
See hotkey.js for handlers that are more app-wide.
|
|
|
|
*/
|
|
|
|
|
2021-05-10 18:25:06 +02:00
|
|
|
// Note that these keycodes differ from those in hotkey.js, because
|
|
|
|
// we're using keydown rather than keypress. It's unclear whether
|
|
|
|
// there's a good reason for this difference.
|
2019-11-02 00:06:25 +01:00
|
|
|
const keys = {
|
2021-05-10 18:25:06 +02:00
|
|
|
13: "enter_key", // Enter
|
|
|
|
37: "left_arrow", // // Left arrow
|
|
|
|
38: "up_arrow", // Up arrow
|
|
|
|
39: "right_arrow", // Right arrow
|
|
|
|
40: "down_arrow", // Down arrow
|
|
|
|
72: "vim_left", // 'H'
|
|
|
|
74: "vim_down", // 'J'
|
|
|
|
75: "vim_up", // 'K'
|
|
|
|
76: "vim_right", // 'L'
|
2018-04-04 16:36:49 +02:00
|
|
|
};
|
|
|
|
|
2021-02-28 00:36:50 +01:00
|
|
|
export function handle(opts) {
|
2020-07-20 21:26:58 +02:00
|
|
|
opts.elem.on("keydown", (e) => {
|
2019-11-02 00:06:25 +01:00
|
|
|
const key = e.which || e.keyCode;
|
2018-04-04 16:36:49 +02:00
|
|
|
|
2018-12-04 19:15:31 +01:00
|
|
|
if (e.altKey || e.ctrlKey || e.shiftKey) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const key_name = keys[key];
|
2018-04-04 16:36:49 +02:00
|
|
|
|
|
|
|
if (!key_name) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!opts.handlers[key_name]) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const handled = opts.handlers[key_name]();
|
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
|
|
|
}
|