diff --git a/src/public/app/services/frontend_script_api.js b/src/public/app/services/frontend_script_api.js index c34e0492d..2eea0abb5 100644 --- a/src/public/app/services/frontend_script_api.js +++ b/src/public/app/services/frontend_script_api.js @@ -509,6 +509,8 @@ function FrontendScriptApi(startNote, currentNote, originEntity = null, $contain * @method * @param {string} keyboardShortcut - e.g. "ctrl+shift+a" * @param {function} handler + * @param {string} [namespace] - specify namespace of the handler for the cases where call for bind may be repeated. + * If a handler with this ID exists, it's replaced by the new handler. */ this.bindGlobalShortcut = shortcutService.bindGlobalShortcut; diff --git a/src/public/app/services/root_command_executor.js b/src/public/app/services/root_command_executor.js index 764e34e8d..5513ce640 100644 --- a/src/public/app/services/root_command_executor.js +++ b/src/public/app/services/root_command_executor.js @@ -72,7 +72,7 @@ export default class RootCommandExecutor extends Component { options.toggle('leftPaneVisible'); } - async showLaunchBarShortcutsCommand() { + async showLaunchBarSubtreeCommand() { await appContext.tabManager.openContextWithNote('lb_root', true, null, 'lb_root'); } diff --git a/src/public/app/services/shortcuts.js b/src/public/app/services/shortcuts.js index 0a617d0bd..a38476ee6 100644 --- a/src/public/app/services/shortcuts.js +++ b/src/public/app/services/shortcuts.js @@ -1,19 +1,31 @@ import utils from "./utils.js"; -function bindGlobalShortcut(keyboardShortcut, handler) { +function bindGlobalShortcut(keyboardShortcut, handler, namespace = null) { bindElShortcut($(document), keyboardShortcut, handler); } -function bindElShortcut($el, keyboardShortcut, handler) { +function bindElShortcut($el, keyboardShortcut, handler, namespace = null) { if (utils.isDesktop()) { keyboardShortcut = normalizeShortcut(keyboardShortcut); - $el.bind('keydown', keyboardShortcut, e => { - handler(e); + let eventName = 'keydown'; - e.preventDefault(); - e.stopPropagation(); - }); + if (namespace) { + eventName += "." + namespace; + + // if there's a namespace then we replace the existing event handler with the new one + $el.off(eventName); + } + + // method can be called to remove the shortcut (e.g. when keyboardShortcut label is deleted) + if (keyboardShortcut) { + $el.on(eventName, keyboardShortcut, e => { + handler(e); + + e.preventDefault(); + e.stopPropagation(); + }); + } } } @@ -21,6 +33,10 @@ function bindElShortcut($el, keyboardShortcut, handler) { * Normalize to the form expected by the jquery.hotkeys.js */ function normalizeShortcut(shortcut) { + if (!shortcut) { + return shortcut; + } + return shortcut .toLowerCase() .replace("enter", "return") diff --git a/src/public/app/widgets/buttons/global_menu.js b/src/public/app/widgets/buttons/global_menu.js index 0ae7bf1db..80ddaabf9 100644 --- a/src/public/app/widgets/buttons/global_menu.js +++ b/src/public/app/widgets/buttons/global_menu.js @@ -124,9 +124,9 @@ const TPL = ` -