Skip to content

Commit

Permalink
[Rewrite Branch] Fixes undo redo selectall (#2181)
Browse files Browse the repository at this point in the history
This fixes the undo, redo, and selectall in the Electron menu
  • Loading branch information
belcherj authored Aug 18, 2020
1 parent 4f1c795 commit 6011d2a
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 73 deletions.
4 changes: 2 additions & 2 deletions desktop/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)));

Expand Down Expand Up @@ -76,7 +76,7 @@ module.exports = function main() {
mainWindow.loadUrl(url);
}

spellcheck(mainWindow);
contextMenu(mainWindow);

if (
'test' !== process.env.NODE_ENV &&
Expand Down
43 changes: 43 additions & 0 deletions desktop/context-menu/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
'use strict';

/**
* External dependencies
*/
const { Menu } = require('electron');

const { editorCommandSender } = require('../menus/utils');

module.exports = function (mainWindow) {
mainWindow.webContents.on('context-menu', (event, params) => {
const { editFlags } = params;
Menu.buildFromTemplate([
{
id: 'selectAll',
label: 'Select All',
click: editorCommandSender({ 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',
},
]).popup({});
});
};
8 changes: 4 additions & 4 deletions desktop/menus/edit-menu.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { appCommandSender } = require('./utils');
const { appCommandSender, editorCommandSender } = require('./utils');

const buildEditMenu = (settings, isAuthenticated) => {
settings = settings || {};
Expand All @@ -9,11 +9,11 @@ const buildEditMenu = (settings, isAuthenticated) => {
submenu: [
{
label: '&Undo',
role: 'undo',
click: editorCommandSender({ action: 'undo' }),
},
{
label: '&Redo',
role: 'redo',
click: editorCommandSender({ action: 'redo' }),
},
{
type: 'separator',
Expand All @@ -32,7 +32,7 @@ const buildEditMenu = (settings, isAuthenticated) => {
},
{
label: '&Select All',
role: 'selectall',
click: editorCommandSender({ action: 'selectAll' }),
},
{ type: 'separator' },
{
Expand Down
5 changes: 4 additions & 1 deletion desktop/menus/help-menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ const buildHelpMenu = (mainWindow, isAuthenticated) => {
{
label: '&Keyboard Shortcuts',
visible: isAuthenticated,
click: appCommandSender({ action: 'showDialog', dialog: 'KEYBINDINGS' }),
click: appCommandSender({
action: 'showDialog',
dialog: 'KEYBINDINGS',
}),
},
{ type: 'separator' },
{
Expand Down
11 changes: 10 additions & 1 deletion desktop/menus/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,23 @@ const buildRadioGroup = ({ action, propName, settings }) => {
};

const appCommandSender = (arg) => {
return commandSender('appCommand', arg);
};

const editorCommandSender = (arg) => {
return commandSender('editorCommand', arg);
};

const commandSender = (commandName, arg) => {
return (item, focusedWindow) => {
if (focusedWindow) {
focusedWindow.webContents.send('appCommand', arg);
focusedWindow.webContents.send(commandName, arg);
}
};
};

module.exports = {
buildRadioGroup,
appCommandSender,
editorCommandSender,
};
8 changes: 6 additions & 2 deletions desktop/menus/view-menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ const buildViewMenu = (settings, isAuthenticated) => {
label: '&Reversed',
type: 'checkbox',
checked: settings.sortReversed,
click: appCommandSender({ action: 'toggleSortOrder' }),
click: appCommandSender({
action: 'toggleSortOrder',
}),
},
]),
},
Expand Down Expand Up @@ -96,7 +98,9 @@ const buildViewMenu = (settings, isAuthenticated) => {
label: '&Sort Alphabetically',
type: 'checkbox',
checked: settings.sortTagsAlpha,
click: appCommandSender({ action: 'toggleSortTagsAlpha' }),
click: appCommandSender({
action: 'toggleSortTagsAlpha',
}),
},
],
},
Expand Down
1 change: 1 addition & 0 deletions desktop/preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const { contextBridge, ipcRenderer, remote } = require('electron');
const validChannels = [
'appCommand',
'clearCookies',
'editorCommand',
'importNotes',
'noteImportChannel',
'setAutoHideMenuBar',
Expand Down
63 changes: 0 additions & 63 deletions desktop/spellchecker/index.js

This file was deleted.

1 change: 1 addition & 0 deletions lib/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ declare global {
confirmLogout(changes: string): 'logout' | 'reconsider' | 'export';
isMac: boolean;
receive(command: 'appCommand', callback: (event: any) => any);
receive(command: 'editorCommand', callback: (event: any) => any);
receive(command: 'noteImportChannel', callback: (event: any) => any);
receive(command: 'wpLogin', callback: (event: any) => any);
removeListener(command: 'noteImportChannel');
Expand Down
15 changes: 15 additions & 0 deletions lib/note-content-editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ class NoteContentEditor extends Component<Props> {
if (this.bootTimer) {
clearTimeout(this.bootTimer);
}
window.electron?.removeListener('editorCommand');
window.removeEventListener('keydown', this.handleKeys, true);
}

Expand Down Expand Up @@ -189,6 +190,20 @@ class NoteContentEditor extends Component<Props> {
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: {
Expand Down

0 comments on commit 6011d2a

Please sign in to comment.