Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
janfaracik committed Jul 21, 2022
1 parent f2a6e56 commit 9654bfb
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions war/src/main/js/keyboard-shortcuts.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import hotkeys from "hotkeys-js"

window.addEventListener("load", () => {
const searchBar = document.querySelector("#search-box")
searchBar.placeholder = searchBar.placeholder + ` (${translateKeyboardShortcutForOS("CMD+K")
searchBar.placeholder = searchBar.placeholder + ` (${translateModifierKeysForUsersPlatform("CMD+K")
.replace("CMD", "⌘")})`

hotkeys(translateKeyboardShortcutForOS("CMD+K"), () => {
hotkeys(translateModifierKeysForUsersPlatform("CMD+K"), () => {
searchBar.focus()

// Returning false stops the event and prevents default browser events
Expand All @@ -14,11 +14,12 @@ window.addEventListener("load", () => {
})

/**
* Translates a given keyboard shortcut, e.g. CMD+K, into an OS neutral version, e.g. CTRL+K
* @param {string} keyboardShortcut The shortcut for translation
* Given a keyboard shortcut, e.g. CMD+K, replace any included modifier keys for the user's
* platform e.g. output will be CMD+K for macOS/iOS, CTRL+K for Windows/Linux
* @param {string} keyboardShortcut The shortcut to translate
*/
function translateKeyboardShortcutForOS(keyboardShortcut) {
function translateModifierKeysForUsersPlatform(keyboardShortcut) {
const useCmdKey = navigator.platform.toUpperCase().indexOf("MAC") >= 0 ||
navigator.platform.toUpperCase() === "IPHONE"
return keyboardShortcut.replace("CMD", useCmdKey ? "CMD" : "CTRL")
return keyboardShortcut.replaceAll(/CMD|CTRL/ig, useCmdKey ? "CMD" : "CTRL")
}

0 comments on commit 9654bfb

Please sign in to comment.