From aebfc22a82ca47cfcb4621a932e08e6d518ffcc2 Mon Sep 17 00:00:00 2001 From: Kat Hagan Date: Wed, 30 Sep 2020 13:28:38 -0700 Subject: [PATCH] Electron: Handle all keyboard shortcuts that are in the menu, from the menu (#2370) * separate listeners * restore shift+cmd+g * extra whitespace --- desktop/menus/edit-menu.js | 2 +- lib/app.tsx | 27 +++++++++++++++++++++------ lib/note-content-editor.tsx | 3 ++- 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/desktop/menus/edit-menu.js b/desktop/menus/edit-menu.js index ab58170ae..319837cb6 100644 --- a/desktop/menus/edit-menu.js +++ b/desktop/menus/edit-menu.js @@ -66,7 +66,7 @@ const buildEditMenu = (settings, isAuthenticated, editMode) => { { label: '&Find in Note', visible: isAuthenticated, - click: editorCommandSender({ action: 'find' }), + click: appCommandSender({ action: 'focusSearchField' }), accelerator: 'CommandOrControl+F', }, { diff --git a/lib/app.tsx b/lib/app.tsx index 510e7af62..38ffb7de1 100644 --- a/lib/app.tsx +++ b/lib/app.tsx @@ -92,6 +92,27 @@ class AppComponent extends Component { return false; } + if (('Escape' === key || 'Esc' === key) && this.props.isSearchActive) { + this.props.clearSearch(); + } + + if (!window.electron) { + this.handleBrowserShortcut(event); + } + + return true; + }; + + // handle all keyboard shortcuts that are duplicated in the Electron menus + // this listener is only called in browsers, as otherwise the + // menu will trigger them via the provided Accelerator, so we don't need a listener + handleBrowserShortcut = (event: KeyboardEvent) => { + const { ctrlKey, metaKey, shiftKey } = event; + const key = event.key.toLowerCase(); + + // Is either cmd or ctrl pressed? (But not both) + const cmdOrCtrl = (ctrlKey || metaKey) && ctrlKey !== metaKey; + if ( (cmdOrCtrl && shiftKey && 's' === key) || (cmdOrCtrl && !shiftKey && 'f' === key) @@ -103,10 +124,6 @@ class AppComponent extends Component { return false; } - if (('Escape' === key || 'Esc' === key) && this.props.isSearchActive) { - this.props.clearSearch(); - } - if (cmdOrCtrl && shiftKey && 'f' === key) { this.props.toggleFocusMode(); @@ -128,8 +145,6 @@ class AppComponent extends Component { event.stopPropagation(); event.preventDefault(); } - - return true; }; toggleShortcuts = (doEnable: boolean) => { diff --git a/lib/note-content-editor.tsx b/lib/note-content-editor.tsx index 0952bbcb5..75178d53f 100644 --- a/lib/note-content-editor.tsx +++ b/lib/note-content-editor.tsx @@ -178,7 +178,8 @@ class NoteContentEditor extends Component { return false; } - if (cmdOrCtrl && !shiftKey && key === 'g') { + // Electron can trigger this from the menu + if (!window.electron && cmdOrCtrl && !shiftKey && key === 'g') { this.setNextSearchSelection(); event.stopPropagation(); event.preventDefault();