Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
shortcut improvements
zadamb55c2d3dbc40
diff --git a/src/public/app/services/frontend_script_api.js b/src/public/app/services/frontend_script_api.jsindex c34e0492d3..2eea0abb5f 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.jsindex 764e34e8df..5513ce6409 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.jsindex 0a617d0bdd..a38476ee6c 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.jsindex 0ae7bf1dbc..80ddaabf9d 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 = `<kbd ></kbd></li>- <li class="dropdown-item" data-trigger-command="showLaunchBarShortcuts">+ <li class="dropdown-item" data-trigger-command="showLaunchBarSubtree"><span class="bx bx-sidebar"></span>- Configure launchbar shortcuts+ Configure launchbar</li><li class="dropdown-item" data-trigger-command="showShareSubtree">diff --git a/src/routes/api/keys.js b/src/routes/api/keys.jsindex 1089544cad..ea793c50f3 100644--- a/src/routes/api/keys.js+++ b/src/routes/api/keys.js@@ -13,6 +13,13 @@ function getShortcutsForNotes() {const map = {};for (const attr of attrs) {+ const note = becca.getNote(attr.noteId);++ if (note?.type === 'launcher') {+ // launchers have different handling+ continue;+ }+map[attr.value] = attr.noteId;}