chore(client/ts): port services/shortcuts

This commit is contained in:
Elian Doran 2024-12-19 21:03:38 +02:00
parent e8d1fe4e84
commit 7b7980cefb
No known key found for this signature in database

View File

@ -1,14 +1,17 @@
import utils from "./utils.js"; import utils from "./utils.js";
function removeGlobalShortcut(namespace) { type ElementType = HTMLElement | Document;
type Handler = (e: JQuery.TriggeredEvent<ElementType, string, ElementType, ElementType>) => void;
function removeGlobalShortcut(namespace: string) {
bindGlobalShortcut('', null, namespace); bindGlobalShortcut('', null, namespace);
} }
function bindGlobalShortcut(keyboardShortcut, handler, namespace = null) { function bindGlobalShortcut(keyboardShortcut: string, handler: Handler | null, namespace: string | null = null) {
bindElShortcut($(document), keyboardShortcut, handler, namespace); bindElShortcut($(document), keyboardShortcut, handler, namespace);
} }
function bindElShortcut($el, keyboardShortcut, handler, namespace = null) { function bindElShortcut($el: JQuery<ElementType>, keyboardShortcut: string, handler: Handler | null, namespace: string | null = null) {
if (utils.isDesktop()) { if (utils.isDesktop()) {
keyboardShortcut = normalizeShortcut(keyboardShortcut); keyboardShortcut = normalizeShortcut(keyboardShortcut);
@ -24,7 +27,9 @@ function bindElShortcut($el, keyboardShortcut, handler, namespace = null) {
// method can be called to remove the shortcut (e.g. when keyboardShortcut label is deleted) // method can be called to remove the shortcut (e.g. when keyboardShortcut label is deleted)
if (keyboardShortcut) { if (keyboardShortcut) {
$el.bind(eventName, keyboardShortcut, e => { $el.bind(eventName, keyboardShortcut, e => {
if (handler) {
handler(e); handler(e);
}
e.preventDefault(); e.preventDefault();
e.stopPropagation(); e.stopPropagation();
@ -36,7 +41,7 @@ function bindElShortcut($el, keyboardShortcut, handler, namespace = null) {
/** /**
* Normalize to the form expected by the jquery.hotkeys.js * Normalize to the form expected by the jquery.hotkeys.js
*/ */
function normalizeShortcut(shortcut) { function normalizeShortcut(shortcut: string): string {
if (!shortcut) { if (!shortcut) {
return shortcut; return shortcut;
} }