From 7b7980cefb735e3fc1d0bad3f4293c0c63ca391a Mon Sep 17 00:00:00 2001 From: Elian Doran Date: Thu, 19 Dec 2024 21:03:38 +0200 Subject: [PATCH] chore(client/ts): port services/shortcuts --- .../app/services/{shortcuts.js => shortcuts.ts} | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) rename src/public/app/services/{shortcuts.js => shortcuts.ts} (68%) diff --git a/src/public/app/services/shortcuts.js b/src/public/app/services/shortcuts.ts similarity index 68% rename from src/public/app/services/shortcuts.js rename to src/public/app/services/shortcuts.ts index 9c99ef0d1..05886bac2 100644 --- a/src/public/app/services/shortcuts.js +++ b/src/public/app/services/shortcuts.ts @@ -1,14 +1,17 @@ import utils from "./utils.js"; -function removeGlobalShortcut(namespace) { +type ElementType = HTMLElement | Document; +type Handler = (e: JQuery.TriggeredEvent) => void; + +function removeGlobalShortcut(namespace: string) { 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); } -function bindElShortcut($el, keyboardShortcut, handler, namespace = null) { +function bindElShortcut($el: JQuery, keyboardShortcut: string, handler: Handler | null, namespace: string | null = null) { if (utils.isDesktop()) { 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) if (keyboardShortcut) { $el.bind(eventName, keyboardShortcut, e => { - handler(e); + if (handler) { + handler(e); + } e.preventDefault(); e.stopPropagation(); @@ -36,7 +41,7 @@ function bindElShortcut($el, keyboardShortcut, handler, namespace = null) { /** * Normalize to the form expected by the jquery.hotkeys.js */ -function normalizeShortcut(shortcut) { +function normalizeShortcut(shortcut: string): string { if (!shortcut) { return shortcut; }