shortcut improvements

This commit is contained in:
zadam 2022-12-01 10:03:04 +01:00
parent 720fb0f73e
commit b55c2d3dbc
5 changed files with 35 additions and 10 deletions

View File

@ -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;

View File

@ -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');
}

View File

@ -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")

View File

@ -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">

View File

@ -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;
}