From 720fb0f73eacd413344562159dc313fe9732fe2a Mon Sep 17 00:00:00 2001 From: zadam Date: Thu, 1 Dec 2022 00:17:15 +0100 Subject: [PATCH] refactorted shortcut handling into a service --- .../app/services/frontend_script_api.js | 3 +- src/public/app/services/keyboard_actions.js | 10 +++--- src/public/app/services/mac_init.js | 15 ++++---- src/public/app/services/shortcuts.js | 36 +++++++++++++++++++ src/public/app/services/utils.js | 32 ----------------- .../attribute_widgets/attribute_detail.js | 5 +-- .../app/widgets/dialogs/jump_to_note.js | 3 +- .../app/widgets/dialogs/markdown_import.js | 3 +- src/public/app/widgets/note_title.js | 6 ++-- src/public/app/widgets/note_tree.js | 3 +- src/public/app/widgets/quick_search.js | 14 ++++---- .../widgets/search_options/search_string.js | 4 +-- 12 files changed, 72 insertions(+), 62 deletions(-) create mode 100644 src/public/app/services/shortcuts.js diff --git a/src/public/app/services/frontend_script_api.js b/src/public/app/services/frontend_script_api.js index 0b40b0333..c34e0492d 100644 --- a/src/public/app/services/frontend_script_api.js +++ b/src/public/app/services/frontend_script_api.js @@ -13,6 +13,7 @@ import appContext from "./app_context.js"; import NoteContextAwareWidget from "../widgets/note_context_aware_widget.js"; import BasicWidget from "../widgets/basic_widget.js"; import SpacedUpdate from "./spaced_update.js"; +import shortcutService from "./shortcuts.js"; /** * This is the main frontend API interface for scripts. It's published in the local "api" object. @@ -509,7 +510,7 @@ function FrontendScriptApi(startNote, currentNote, originEntity = null, $contain * @param {string} keyboardShortcut - e.g. "ctrl+shift+a" * @param {function} handler */ - this.bindGlobalShortcut = utils.bindGlobalShortcut; + this.bindGlobalShortcut = shortcutService.bindGlobalShortcut; /** * Trilium runs in backend and frontend process, when something is changed on the backend from script, diff --git a/src/public/app/services/keyboard_actions.js b/src/public/app/services/keyboard_actions.js index fecebca3f..6c7670092 100644 --- a/src/public/app/services/keyboard_actions.js +++ b/src/public/app/services/keyboard_actions.js @@ -1,6 +1,6 @@ import server from "./server.js"; -import utils from "./utils.js"; import appContext from "./app_context.js"; +import shortcutService from "./shortcuts.js"; const keyboardActionRepo = {}; @@ -31,7 +31,7 @@ async function setupActionsForElement(scope, $el, component) { for (const action of actions) { for (const shortcut of action.effectiveShortcuts) { - utils.bindElShortcut($el, shortcut, () => component.triggerCommand(action.actionName, {ntxId: appContext.tabManager.activeNtxId})); + shortcutService.bindElShortcut($el, shortcut, () => component.triggerCommand(action.actionName, {ntxId: appContext.tabManager.activeNtxId})); } } } @@ -39,14 +39,14 @@ async function setupActionsForElement(scope, $el, component) { getActionsForScope("window").then(actions => { for (const action of actions) { for (const shortcut of action.effectiveShortcuts) { - utils.bindGlobalShortcut(shortcut, () => appContext.triggerCommand(action.actionName, {ntxId: appContext.tabManager.activeNtxId})); + shortcutService.bindGlobalShortcut(shortcut, () => appContext.triggerCommand(action.actionName, {ntxId: appContext.tabManager.activeNtxId})); } } }); server.get('keyboard-shortcuts-for-notes').then(shortcutForNotes => { for (const shortcut in shortcutForNotes) { - utils.bindGlobalShortcut(shortcut, async () => { + shortcutService.bindGlobalShortcut(shortcut, async () => { appContext.tabManager.getActiveContext().setNote(shortcutForNotes[shortcut]); }); } @@ -64,7 +64,7 @@ function setElementActionHandler($el, actionName, handler) { for (const shortcut of action.effectiveShortcuts) { if (shortcut) { - utils.bindElShortcut($el, shortcut, handler); + shortcutService.bindElShortcut($el, shortcut, handler); } } }); diff --git a/src/public/app/services/mac_init.js b/src/public/app/services/mac_init.js index 624c4858f..10fba8cbb 100644 --- a/src/public/app/services/mac_init.js +++ b/src/public/app/services/mac_init.js @@ -2,15 +2,16 @@ * Mac specific initialization */ import utils from "./utils.js"; +import shortcutService from "./shortcuts.js"; function init() { if (utils.isElectron() && utils.isMac()) { - utils.bindGlobalShortcut('meta+c', () => exec("copy")); - utils.bindGlobalShortcut('meta+v', () => exec('paste')); - utils.bindGlobalShortcut('meta+x', () => exec('cut')); - utils.bindGlobalShortcut('meta+a', () => exec('selectAll')); - utils.bindGlobalShortcut('meta+z', () => exec('undo')); - utils.bindGlobalShortcut('meta+y', () => exec('redo')); + shortcutService.bindGlobalShortcut('meta+c', () => exec("copy")); + shortcutService.bindGlobalShortcut('meta+v', () => exec('paste')); + shortcutService.bindGlobalShortcut('meta+x', () => exec('cut')); + shortcutService.bindGlobalShortcut('meta+a', () => exec('selectAll')); + shortcutService.bindGlobalShortcut('meta+z', () => exec('undo')); + shortcutService.bindGlobalShortcut('meta+y', () => exec('redo')); } } @@ -22,4 +23,4 @@ function exec(cmd) { export default { init -} \ No newline at end of file +} diff --git a/src/public/app/services/shortcuts.js b/src/public/app/services/shortcuts.js new file mode 100644 index 000000000..0a617d0bd --- /dev/null +++ b/src/public/app/services/shortcuts.js @@ -0,0 +1,36 @@ +import utils from "./utils.js"; + +function bindGlobalShortcut(keyboardShortcut, handler) { + bindElShortcut($(document), keyboardShortcut, handler); +} + +function bindElShortcut($el, keyboardShortcut, handler) { + if (utils.isDesktop()) { + keyboardShortcut = normalizeShortcut(keyboardShortcut); + + $el.bind('keydown', keyboardShortcut, e => { + handler(e); + + e.preventDefault(); + e.stopPropagation(); + }); + } +} + +/** + * Normalize to the form expected by the jquery.hotkeys.js + */ +function normalizeShortcut(shortcut) { + return shortcut + .toLowerCase() + .replace("enter", "return") + .replace("delete", "del") + .replace("ctrl+alt", "alt+ctrl") + .replace("meta+alt", "alt+meta"); // alt needs to be first; +} + +export default { + bindGlobalShortcut, + bindElShortcut, + normalizeShortcut +} diff --git a/src/public/app/services/utils.js b/src/public/app/services/utils.js index 21f79ceb7..6d2fe7c2e 100644 --- a/src/public/app/services/utils.js +++ b/src/public/app/services/utils.js @@ -132,35 +132,6 @@ function randomString(len) { return text; } -function bindGlobalShortcut(keyboardShortcut, handler) { - bindElShortcut($(document), keyboardShortcut, handler); -} - -function bindElShortcut($el, keyboardShortcut, handler) { - if (isDesktop()) { - keyboardShortcut = normalizeShortcut(keyboardShortcut); - - $el.bind('keydown', keyboardShortcut, e => { - handler(e); - - e.preventDefault(); - e.stopPropagation(); - }); - } -} - -/** - * Normalize to the form expected by the jquery.hotkeys.js - */ -function normalizeShortcut(shortcut) { - return shortcut - .toLowerCase() - .replace("enter", "return") - .replace("delete", "del") - .replace("ctrl+alt", "alt+ctrl") - .replace("meta+alt", "alt+meta"); // alt needs to be first; -} - function isMobile() { return window.device === "mobile" // window.device is not available in setup @@ -387,8 +358,6 @@ export default { formatLabel, toObject, randomString, - bindGlobalShortcut, - bindElShortcut, isMobile, isDesktop, setCookie, @@ -402,7 +371,6 @@ export default { focusSavedElement, isHtmlEmpty, clearBrowserCache, - normalizeShortcut, copySelectionToClipboard, dynamicRequire, timeLimit, diff --git a/src/public/app/widgets/attribute_widgets/attribute_detail.js b/src/public/app/widgets/attribute_widgets/attribute_detail.js index 027fa5429..84d0cf057 100644 --- a/src/public/app/widgets/attribute_widgets/attribute_detail.js +++ b/src/public/app/widgets/attribute_widgets/attribute_detail.js @@ -8,6 +8,7 @@ import promotedAttributeDefinitionParser from '../../services/promoted_attribute import NoteContextAwareWidget from "../note_context_aware_widget.js"; import SpacedUpdate from "../../services/spaced_update.js"; import utils from "../../services/utils.js"; +import shortcutService from "../../services/shortcuts.js"; const TPL = `
@@ -271,8 +272,8 @@ export default class AttributeDetailWidget extends NoteContextAwareWidget { this.$widget = $(TPL); - utils.bindElShortcut(this.$widget, 'ctrl+return', () => this.saveAndClose()); - utils.bindElShortcut(this.$widget, 'esc', () => this.cancelAndClose()); + shortcutService.bindElShortcut(this.$widget, 'ctrl+return', () => this.saveAndClose()); + shortcutService.bindElShortcut(this.$widget, 'esc', () => this.cancelAndClose()); this.$title = this.$widget.find('.attr-detail-title'); diff --git a/src/public/app/widgets/dialogs/jump_to_note.js b/src/public/app/widgets/dialogs/jump_to_note.js index 4db606c01..68d2ec2f4 100644 --- a/src/public/app/widgets/dialogs/jump_to_note.js +++ b/src/public/app/widgets/dialogs/jump_to_note.js @@ -2,6 +2,7 @@ import noteAutocompleteService from '../../services/note_autocomplete.js'; import utils from "../../services/utils.js"; import appContext from "../../services/app_context.js"; import BasicWidget from "../basic_widget.js"; +import shortcutService from "../../services/shortcuts.js"; const TPL = `