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;
|
2023-04-19 23:46:25 +02:00
|
|
|
handlers: Record<string, (() => boolean) | undefined>;
|
2021-05-26 19:59:12 +02:00
|
|
|
}): 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
|
|
|
|
2023-10-23 19:18:56 +02:00
|
|
|
export function is_enter_event(event: JQuery.KeyboardEventBase): 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
|
2023-04-20 00:50:38 +02:00
|
|
|
const isComposing = event.originalEvent?.isComposing ?? false;
|
2022-09-28 08:28:21 +02:00
|
|
|
return !isComposing && event.key === "Enter";
|
2022-09-28 08:27:24 +02:00
|
|
|
}
|