Skip to content

Commit

Permalink
Fix SelectAll, Undo, Redo
Browse files Browse the repository at this point in the history
  • Loading branch information
belcherj committed Jul 2, 2020
1 parent 1baaf0a commit 6660e4f
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 50 deletions.
6 changes: 3 additions & 3 deletions desktop/menus/edit-menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ const buildEditMenu = (settings) => {
submenu: [
{
label: '&Undo',
role: 'undo',
click: appCommandSender({ action: 'undo' }),
},
{
label: '&Redo',
role: 'redo',
click: appCommandSender({ action: 'redo' }),
},
{
type: 'separator',
Expand All @@ -31,7 +31,7 @@ const buildEditMenu = (settings) => {
},
{
label: '&Select All',
role: 'selectall',
click: appCommandSender({ action: 'selectAll' }),
},
{ type: 'separator' },
{
Expand Down
105 changes: 58 additions & 47 deletions desktop/spellchecker/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,59 +5,70 @@
*/
const { Menu, MenuItem } = require('electron');

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

module.exports = function (mainWindow) {
mainWindow.webContents.on('context-menu', (event, params) => {
const menu = new Menu();
const { editFlags } = params;

const copy = new MenuItem({ label: 'Copy', role: 'copy' });
// // Add each spelling suggestion
// for (const suggestion of params.dictionarySuggestions) {
// menu.append(
// new MenuItem({
// label: suggestion,
// click: () => mainWindow.webContents.replaceMisspelling(suggestion),
// })
// );
// }

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
// ),
// })
// );
// }

// 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 (params?.dictionarySuggestions?.length > 0) {
// menu.append(new MenuItem({ type: 'separator' }));
// }

// 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({
const template = [
{
id: 'selectAll',
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();
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({});
});
};
14 changes: 14 additions & 0 deletions lib/state/electron/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,20 @@ export const middleware: S.Middleware = ({ dispatch, getState }) => {
dispatch(actions.settings.decreaseFontSize());
return;

case 'redo':
window?.editor.trigger('', 'redo');
return;

case 'resetFontSize':
dispatch(actions.settings.resetFontSize());
return;

case 'selectAll':
window?.editor.setSelection(
window?.editor.getModel().getFullModelRange()
);
return;

case 'setLineLength':
dispatch(actions.settings.setLineLength(command.lineLength));
return;
Expand Down Expand Up @@ -77,6 +87,10 @@ export const middleware: S.Middleware = ({ dispatch, getState }) => {
dispatch(actions.settings.toggleSpellCheck());
return;

case 'undo':
window?.editor.trigger('', 'undo');
return;

default:
console.log(command);
}
Expand Down

0 comments on commit 6660e4f

Please sign in to comment.