From 42ada7123ce1757965fbba964047ddd02a979d7e Mon Sep 17 00:00:00 2001 From: mic704b Date: Tue, 7 Jan 2020 08:16:39 +1100 Subject: [PATCH] Desktop: Add external editor actions to the note context menu. (#2214) * Add external editor actions to the note context menu. Also start up external editor on note double click. These changes enhance user experience by placing the actions where they feel natural. * Remove double-click behaviour and change menu text. Changes in response to review comments. * Move handling of external editor actions to main screen from note text This is to ensure correct behaviour even when the user launches the action on a note in the list that is under the pointer, but not selected. * Move external edit actions to NoteListUtils from MainScreen. * Reconnect external edit action in main edit menu. --- ElectronClient/app/gui/NoteList.jsx | 1 + ElectronClient/app/gui/NoteText.jsx | 17 ++++----- ElectronClient/app/gui/utils/NoteListUtils.js | 37 +++++++++++++++++++ 3 files changed, 46 insertions(+), 9 deletions(-) diff --git a/ElectronClient/app/gui/NoteList.jsx b/ElectronClient/app/gui/NoteList.jsx index ff4232ed3f..5d8278d7ed 100644 --- a/ElectronClient/app/gui/NoteList.jsx +++ b/ElectronClient/app/gui/NoteList.jsx @@ -87,6 +87,7 @@ class NoteListComponent extends React.Component { const menu = NoteListUtils.makeContextMenu(noteIds, { notes: this.props.notes, dispatch: this.props.dispatch, + watchedNoteFiles: this.props.watchedNoteFiles, }); menu.popup(bridge().window()); diff --git a/ElectronClient/app/gui/NoteText.jsx b/ElectronClient/app/gui/NoteText.jsx index bc045827d5..00e063960b 100644 --- a/ElectronClient/app/gui/NoteText.jsx +++ b/ElectronClient/app/gui/NoteText.jsx @@ -1129,6 +1129,8 @@ class NoteTextComponent extends React.Component { fn = this.commandDateTime; } else if (command.name === 'commandStartExternalEditing') { fn = this.commandStartExternalEditing; + } else if (command.name === 'commandStopExternalEditing') { + fn = this.commandStopExternalEditing; } else if (command.name === 'showLocalSearch') { fn = this.commandShowLocalSearch; } else if (command.name === 'textCode') { @@ -1308,18 +1310,14 @@ class NoteTextComponent extends React.Component { } async commandStartExternalEditing() { - try { - await this.saveIfNeeded(true, { - autoTitle: false, - }); - await ExternalEditWatcher.instance().openAndWatch(this.state.note); - } catch (error) { - bridge().showErrorMessageBox(_('Error opening note in editor: %s', error.message)); - } + await this.saveIfNeeded(true, { + autoTitle: false, + }); + NoteListUtils.startExternalEditing(this.state.note.id); } async commandStopExternalEditing() { - ExternalEditWatcher.instance().stopWatching(this.state.note.id); + NoteListUtils.stopExternalEditing(this.state.note.id); } async commandSetTags() { @@ -1825,6 +1823,7 @@ class NoteTextComponent extends React.Component { const menu = NoteListUtils.makeContextMenu(this.props.selectedNoteIds, { notes: this.props.notes, dispatch: this.props.dispatch, + watchedNoteFiles: this.props.watchedNoteFiles, }); const buttonStyle = Object.assign({}, theme.buttonStyle, { diff --git a/ElectronClient/app/gui/utils/NoteListUtils.js b/ElectronClient/app/gui/utils/NoteListUtils.js index defc75423e..ec7f9a51b0 100644 --- a/ElectronClient/app/gui/utils/NoteListUtils.js +++ b/ElectronClient/app/gui/utils/NoteListUtils.js @@ -7,6 +7,7 @@ const eventManager = require('../../eventManager'); const InteropService = require('lib/services/InteropService'); const InteropServiceHelper = require('../../InteropServiceHelper.js'); const Note = require('lib/models/Note'); +const ExternalEditWatcher = require('lib/services/ExternalEditWatcher'); const { substrWithEllipsis } = require('lib/string-utils'); class NoteListUtils { @@ -49,6 +50,28 @@ class NoteListUtils { }) ); + if (props.watchedNoteFiles.indexOf(noteIds[0]) < 0) { + menu.append( + new MenuItem({ + label: _('Edit in external editor'), + enabled: noteIds.length === 1, + click: async () => { + this.startExternalEditing(noteIds[0]); + }, + }) + ); + } else { + menu.append( + new MenuItem({ + label: _('Stop external editing'), + enabled: noteIds.length === 1, + click: async () => { + this.stopExternalEditing(noteIds[0]); + }, + }) + ); + } + if (noteIds.length <= 1) { menu.append( new MenuItem({ @@ -191,6 +214,20 @@ class NoteListUtils { if (!ok) return; await Note.batchDelete(noteIds); } + + static async startExternalEditing(noteId) { + try { + const note = await Note.load(noteId); + ExternalEditWatcher.instance().openAndWatch(note); + } catch (error) { + bridge().showErrorMessageBox(_('Error opening note in editor: %s', error.message)); + } + } + + static async stopExternalEditing(noteId) { + ExternalEditWatcher.instance().stopWatching(noteId); + } + } module.exports = NoteListUtils;