From 3c1db1d7d677497a3f6c577089d73ca590dfaa6c Mon Sep 17 00:00:00 2001 From: Pavel Djundik Date: Tue, 20 Mar 2018 20:57:19 +0200 Subject: [PATCH] Bind formatting hotkeys on input element And bind only one event for all of the hotkeys --- client/js/keybinds.js | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/client/js/keybinds.js b/client/js/keybinds.js index fa2da80e..f171e5aa 100644 --- a/client/js/keybinds.js +++ b/client/js/keybinds.js @@ -95,6 +95,7 @@ Mousetrap.bind([ contextMenuContainer.hide(); }); +const inputTrap = Mousetrap(input.get(0)); const colorsHotkeys = { k: "\x03", b: "\x02", @@ -106,23 +107,16 @@ const colorsHotkeys = { }; for (const hotkey in colorsHotkeys) { - Mousetrap.bind("mod+" + hotkey, function(e) { - const inputElement = input.get(0); - - // Do not handle modifier hotkeys if input is not focused - if (document.activeElement !== inputElement) { - return; - } - - e.preventDefault(); - + inputTrap.bind("mod+" + hotkey, function(e) { const modifier = colorsHotkeys[e.key]; wrapCursor( - inputElement, + e.target, modifier, - inputElement.selectionStart === inputElement.selectionEnd ? "" : modifier + e.target.selectionStart === e.target.selectionEnd ? "" : modifier ); + + return false; }); }