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: {
|
2022-01-25 11:36:19 +01:00
|
|
|
$elem: JQuery;
|
2021-05-26 19:59:12 +02:00
|
|
|
handlers: {
|
|
|
|
[handler: string]: (() => boolean) | undefined;
|
|
|
|
};
|
|
|
|
}): void {
|
2022-01-25 11:36:19 +01: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
|
|
|
}
|
2022-09-28 08:27:24 +02:00
|
|
|
|
|
|
|
export function is_enter_event(event: JQuery.KeyDownEvent): boolean {
|
2022-09-28 08:28:21 +02:00
|
|
|
// In addition to checking whether the key pressed was an Enter
|
|
|
|
// key, we need to check whether the keypress was part of an IME
|
|
|
|
// composing session, such as selecting a character using a
|
|
|
|
// phonetic input method like ZhuYin in a character-based
|
|
|
|
// language. See #22062 for details. Further reading:
|
|
|
|
// https://developer.mozilla.org/en-US/docs/Glossary/Input_method_editor
|
|
|
|
const isComposing: boolean = event.originalEvent?.isComposing || false;
|
|
|
|
return !isComposing && event.key === "Enter";
|
2022-09-28 08:27:24 +02:00
|
|
|
}
|