diff --git a/desktop/app.js b/desktop/app.js index 1468a15ba..c1335deb2 100644 --- a/desktop/app.js +++ b/desktop/app.js @@ -18,7 +18,7 @@ const importNotes = require('./evernote-import'); const platform = require('./detect/platform'); const updater = require('./updater'); const { isDev } = require('./env'); -const spellcheck = require('./spellchecker'); +const contextMenu = require('./context-menu'); require('module').globalPaths.push(path.resolve(path.join(__dirname))); @@ -76,7 +76,7 @@ module.exports = function main() { mainWindow.loadUrl(url); } - spellcheck(mainWindow); + contextMenu(mainWindow); if ( 'test' !== process.env.NODE_ENV && diff --git a/desktop/context-menu/index.js b/desktop/context-menu/index.js new file mode 100644 index 000000000..33a7fdc64 --- /dev/null +++ b/desktop/context-menu/index.js @@ -0,0 +1,45 @@ +'use strict'; + +/** + * External dependencies + */ +const { Menu } = require('electron'); + +const { appCommandSender } = require('../menus/utils'); + +module.exports = function (mainWindow) { + mainWindow.webContents.on('context-menu', (event, params) => { + const { editFlags } = params; + const template = [ + { + id: 'selectAll', + label: 'Select All', + click: appCommandSender({ action: 'selectAll' }), + enabled: editFlags.canSelectAll, + }, + { + id: 'cut', + label: 'Cut', + role: 'cut', + enabled: editFlags.canCut, + }, + { + id: 'copy', + label: 'Copy', + role: 'copy', + enabled: editFlags.canCopy, + }, + { + id: 'paste', + label: 'Paste', + role: 'paste', + enabled: editFlags.canPaste, + }, + { + type: 'separator', + }, + ]; + const menu = Menu.buildFromTemplate(template); + menu.popup({}); + }); +}; diff --git a/desktop/menus/edit-menu.js b/desktop/menus/edit-menu.js index fd4a14867..bd745e542 100644 --- a/desktop/menus/edit-menu.js +++ b/desktop/menus/edit-menu.js @@ -9,11 +9,11 @@ const buildEditMenu = (settings, isAuthenticated) => { submenu: [ { label: '&Undo', - role: 'undo', + click: appCommandSender({ action: 'undo' }), }, { label: '&Redo', - role: 'redo', + click: appCommandSender({ action: 'redo' }), }, { type: 'separator', @@ -32,7 +32,7 @@ const buildEditMenu = (settings, isAuthenticated) => { }, { label: '&Select All', - role: 'selectall', + click: appCommandSender({ action: 'selectAll' }), }, { type: 'separator' }, { diff --git a/desktop/spellchecker/index.js b/desktop/spellchecker/index.js deleted file mode 100644 index 8a6829ec6..000000000 --- a/desktop/spellchecker/index.js +++ /dev/null @@ -1,63 +0,0 @@ -'use strict'; - -/** - * External dependencies - */ -const { Menu, MenuItem } = require('electron'); - -module.exports = function (mainWindow) { - mainWindow.webContents.on('context-menu', (event, params) => { - const menu = new Menu(); - - const copy = new MenuItem({ label: 'Copy', role: 'copy' }); - - if (!params.isEditable) { - // If text is not editable, only permit the `Copy` action - menu.append(copy); - } else { - // Add each spelling suggestion - for (const suggestion of params.dictionarySuggestions) { - menu.append( - new MenuItem({ - label: suggestion, - click: () => mainWindow.webContents.replaceMisspelling(suggestion), - }) - ); - } - - // Allow users to add the misspelled word to the dictionary - if (params.misspelledWord) { - menu.append(new MenuItem({ type: 'separator' })); - menu.append( - new MenuItem({ - label: 'Add to Dictionary', - click: () => - mainWindow.webContents.session.addWordToSpellCheckerDictionary( - params.misspelledWord - ), - }) - ); - } - - // If text is editable, permit the Select All, Cut, Copy and Paste actions - const cut = new MenuItem({ label: 'Cut', role: 'cut' }); - const paste = new MenuItem({ label: 'Paste', role: 'paste' }); - const selectAll = new MenuItem({ - label: 'Select All', - role: 'selectAll', - }); - - const menuItems = [selectAll, cut, copy, paste]; - - if (params?.dictionarySuggestions?.length > 0) { - menu.append(new MenuItem({ type: 'separator' })); - } - - for (const item of menuItems) { - menu.append(item); - } - } - - menu.popup(); - }); -}; diff --git a/lib/note-content-editor.tsx b/lib/note-content-editor.tsx index 75b0dda50..6ae98025f 100644 --- a/lib/note-content-editor.tsx +++ b/lib/note-content-editor.tsx @@ -100,6 +100,7 @@ class NoteContentEditor extends Component { if (this.bootTimer) { clearTimeout(this.bootTimer); } + window?.electron.removeListener('editorCommand'); window.removeEventListener('keydown', this.handleKeys, true); } @@ -189,6 +190,20 @@ class NoteContentEditor extends Component { this.editor = editor; this.monaco = monaco; + window?.electron.receive('editorCommand', (command) => { + switch (command.action) { + case 'redo': + editor.trigger('', 'redo'); + return; + case 'selectAll': + editor.setSelection(editor.getModel().getFullModelRange()); + return; + case 'undo': + editor.trigger('', 'undo'); + return; + } + }); + const titleDecoration = (line: number) => ({ range: new monaco.Range(line, 1, line, 1), options: {