Skip to content

Commit

Permalink
Add console warning for duplicated registrations
Browse files Browse the repository at this point in the history
  • Loading branch information
janfaracik committed Jul 12, 2022
1 parent 4dbc2e3 commit 3da7082
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
14 changes: 13 additions & 1 deletion war/src/main/js/keyboard-shortcuts.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import hotkeys from "hotkeys-js"

const registeredShortcuts = {}

window.addEventListener("load", () => {
hotkeys("*", ({key}) => {
if (key === "Meta") {
Expand All @@ -18,11 +20,21 @@ window.addEventListener("load", () => {
})

document.querySelectorAll("[data-keyboard-shortcut]").forEach(function(element) {
hotkeys(translateKeyboardShortcutForOS(element.dataset.keyboardShortcut), () => {
const shortcut = translateKeyboardShortcutForOS(element.dataset.keyboardShortcut)

if (registeredShortcuts[shortcut]) {
console.warn(`Shortcut '${shortcut}' is already registered by`, element)
return
}

registeredShortcuts[shortcut] = element

hotkeys(shortcut, () => {
// Small delay to show animation
setTimeout(function() {
switch (element.tagName) {
case "A":
case "BUTTON":
element.click()
break;
case "INPUT":
Expand Down
2 changes: 1 addition & 1 deletion war/src/main/less/styles.less
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
@import "./modules/content-blocks";
@import "./modules/icon-size";
@import "./modules/icons";
@import './modules/keyboard-shortcuts';
@import "./modules/keyboard-shortcuts";
@import "./modules/page-footer";
@import "./modules/page-header";
@import "./modules/panes-and-bigtable";
Expand Down

0 comments on commit 3da7082

Please sign in to comment.