Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: sindresorhus/electron-context-menu
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v0.16.0
Choose a base ref
...
head repository: sindresorhus/electron-context-menu
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v1.0.0
Choose a head ref
  • 4 commits
  • 8 files changed
  • 3 contributors

Commits on Mar 27, 2020

  1. Copy the full SHA
    2f1bae0 View commit details

Commits on Apr 14, 2020

  1. Add built-in support for spellchecking (#94)

    Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
    nautatva and sindresorhus authored Apr 14, 2020
    Copy the full SHA
    71c5d2e View commit details
  2. Require Electron 8

    sindresorhus committed Apr 14, 2020
    Copy the full SHA
    4de32a9 View commit details
  3. 1.0.0

    sindresorhus committed Apr 14, 2020
    Copy the full SHA
    1286195 View commit details
Showing with 174 additions and 28 deletions.
  1. +5 −1 fixture-menu.js
  2. +1 −1 fixture.js
  3. +49 −8 index.d.ts
  4. +63 −1 index.js
  5. +4 −4 index.test-d.ts
  6. +1 −1 license
  7. +21 −8 package.json
  8. +30 −4 readme.md
6 changes: 5 additions & 1 deletion fixture-menu.js
Original file line number Diff line number Diff line change
@@ -33,5 +33,9 @@ contextMenu({
(async () => {
await app.whenReady();

await (new BrowserWindow()).loadFile(path.join(__dirname, 'fixture.html'));
await (new BrowserWindow({
webPreferences: {
spellcheck: true
}
})).loadFile(path.join(__dirname, 'fixture.html'));
})();
2 changes: 1 addition & 1 deletion fixture.js
Original file line number Diff line number Diff line change
@@ -45,7 +45,7 @@ contextMenu({

await (new BrowserWindow({
webPreferences: {
nodeIntegration: true
spellcheck: true
}
})).loadFile(path.join(__dirname, 'fixture.html'));
})();
57 changes: 49 additions & 8 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -12,12 +12,26 @@ import {
declare namespace contextMenu {
interface Labels {
/**
The placeholder `{selection}` will be replaced by the currently selected text.
@default 'Correct Automatically'
*/
readonly correctAutomatically?: string;

/**
@default 'Learn Spelling'
*/
readonly learnSpelling?: string;

/**
The placeholder `{selection}` will be replaced by the currently selected text.
@default 'Look Up “{selection}”'
*/
readonly lookUpSelection?: string;

/**
@default 'Search with Google'
*/
readonly searchWithGoogle?: string;

/**
@default 'Cut'
*/
@@ -80,7 +94,10 @@ declare namespace contextMenu {

interface Actions {
readonly separator: () => MenuItemConstructorOptions;
readonly correctAutomatically: (options: ActionOptions) => MenuItemConstructorOptions;
readonly learnSpelling: (options: ActionOptions) => MenuItemConstructorOptions;
readonly lookUpSelection: (options: ActionOptions) => MenuItemConstructorOptions;
readonly searchWithGoogle: (options: ActionOptions) => MenuItemConstructorOptions;
readonly cut: (options: ActionOptions) => MenuItemConstructorOptions;
readonly copy: (options: ActionOptions) => MenuItemConstructorOptions;
readonly paste: (options: ActionOptions) => MenuItemConstructorOptions;
@@ -129,6 +146,13 @@ declare namespace contextMenu {
*/
readonly showLookUpSelection?: boolean;

/**
Show the `Search with Google` menu item when right-clicking text on macOS.
@default true
*/
readonly showSearchWithGoogle?: boolean;

/**
Show the `Copy Image` menu item when right-clicking on an image.
@@ -143,6 +167,13 @@ declare namespace contextMenu {
*/
readonly showCopyImageAddress?: boolean;

/**
Show the `Save Image` menu item when right-clicking on an image.
@default false
*/
readonly showSaveImage?: boolean;

/**
Show the `Save Image As…` menu item when right-clicking on an image.
@@ -212,13 +243,16 @@ declare namespace contextMenu {
The following options are ignored when `menu` is used:
- `showLookUpSelection`
- `showSearchWithGoogle`
- `showCopyImage`
- `showCopyImageAddress`
- `showSaveImageAs`
- `showInspectElement`
- `showServices`
@default [defaultActions.cut(), defaultActions.copy(), defaultActions.paste(), defaultActions.separator(), defaultActions.saveImage(), defaultActions.saveImageAs(), defaultActions.copyLink(), defaultActions.copyImage(), defaultActions.copyImageAddress(), defaultActions.separator(), defaultActions.copyLink(), defaultActions.separator(), defaultActions.inspect()]
To get spellchecking, “Correct Automatically”, and “Learn Spelling” in the menu, please enable the `spellcheck` preference in browser window: `new BrowserWindow({webPreferences: {spellcheck: true}})`
@default [...dictionarySuggestions, defaultActions.separator(), defaultActions.correctAutomatically(), defaultActions.separator(), defaultActions.learnSpelling(), defaultActions.separator(), defaultActions.lookUpSelection(), defaultActions.separator(),defaultActions.searchWithGoogle(), defaultActions.cut(), defaultActions.copy(), defaultActions.paste(), defaultActions.separator(), defaultActions.saveImage(), defaultActions.saveImageAs(), defaultActions.copyLink(), defaultActions.copyImage(), defaultActions.copyImageAddress(), defaultActions.separator(), defaultActions.copyLink(), defaultActions.separator(), defaultActions.inspect()]
*/
readonly menu?: (
defaultActions: Actions,
@@ -239,17 +273,24 @@ import {app, BrowserWindow} from 'electron';
import contextMenu = require('electron-context-menu');
contextMenu({
prepend: (params, browserWindow) => [{
label: 'Rainbow',
// Only show it when right-clicking images
visible: params.mediaType === 'image'
}]
prepend: (defaultActions, params, browserWindow) => [
{
label: 'Rainbow',
// Only show it when right-clicking images
visible: params.mediaType === 'image'
}
]
});
let mainWindow;
(async () => {
await app.whenReady();
mainWindow = new BrowserWindow();
mainWindow = new BrowserWindow({
webPreferences: {
spellcheck: true
}
});
});
```
*/
64 changes: 63 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -41,6 +41,24 @@ const create = (win, options) => {

const defaultActions = {
separator: () => ({type: 'separator'}),
correctAutomatically: decorateMenuItem({
id: 'correctAutomatically',
label: 'Correct Spelling Automatically',
visible: props.isEditable && hasText && props.misspelledWord && props.dictionarySuggestions.length > 0,
click() {
const target = webContents(win);
target.insertText(props.dictionarySuggestions[0]);
}
}),
learnSpelling: decorateMenuItem({
id: 'learnSpelling',
label: 'Learn Spelling',
visible: props.isEditable && hasText && props.misspelledWord,
click() {
const target = webContents(win);
target.session.addWordToSpellCheckerDictionary(props.misspelledWord);
}
}),
lookUpSelection: decorateMenuItem({
id: 'lookUpSelection',
label: 'Look Up “{selection}”',
@@ -51,6 +69,16 @@ const create = (win, options) => {
}
}
}),
searchWithGoogle: decorateMenuItem({
id: 'searchWithGoogle',
label: 'Search with Google',
visible: hasText,
click() {
const url = new URL('https://www.google.com/search');
url.searchParams.set('q', props.selectionText);
electron.shell.openExternal(url.toString());
}
}),
cut: decorateMenuItem({
id: 'cut',
label: 'Cut',
@@ -173,15 +201,49 @@ const create = (win, options) => {

const shouldShowInspectElement = typeof options.showInspectElement === 'boolean' ? options.showInspectElement : isDev;

function word(suggestion) {
return {
id: 'dictionarySuggestions',
label: suggestion,
visible: props.isEditable && hasText && props.misspelledWord,
click(menuItem) {
const target = webContents(win);
target.insertText(menuItem.label);
}
};
}

let dictionarySuggestions = [];
if (hasText && props.misspelledWord && props.dictionarySuggestions.length > 0) {
dictionarySuggestions = props.dictionarySuggestions.map(word);
} else {
dictionarySuggestions.push(
{
id: 'dictionarySuggestions',
label: 'No Guesses Found',
visible: hasText && props.misspelledWord,
enabled: false
}
);
}

let menuTemplate = [
defaultActions.separator(),
...dictionarySuggestions,
defaultActions.separator(),
defaultActions.correctAutomatically(),
defaultActions.separator(),
defaultActions.learnSpelling(),
defaultActions.separator(),
options.showLookUpSelection !== false && defaultActions.lookUpSelection(),
defaultActions.separator(),
options.showSearchWithGoogle !== false && defaultActions.searchWithGoogle(),
defaultActions.separator(),
defaultActions.cut(),
defaultActions.copy(),
defaultActions.paste(),
defaultActions.separator(),
defaultActions.saveImage(),
options.showSaveImage && defaultActions.saveImage(),
options.showSaveImageAs && defaultActions.saveImageAs(),
options.showCopyImage !== false && defaultActions.copyImage(),
options.showCopyImageAddress && defaultActions.copyImageAddress(),
8 changes: 4 additions & 4 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {expectType} from 'tsd';
import {app, BrowserWindow, shell} from 'electron';
import {app} from 'electron';
import contextMenu = require('.');

expectType<void>(contextMenu());
@@ -15,10 +15,10 @@ contextMenu({

app.on('web-contents-created', (event, webContents) => {
contextMenu({
prepend: (defaultActions, params) => [{
prepend: (defaultActions, parameters) => [{
label: 'Rainbow',
visible: params.mediaType === 'image'
visible: parameters.mediaType === 'image'
}],
window: webContents
});
})
});
2 changes: 1 addition & 1 deletion license
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

29 changes: 21 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"name": "electron-context-menu",
"version": "0.16.0",
"version": "1.0.0",
"description": "Context menu for your Electron app",
"license": "MIT",
"repository": "sindresorhus/electron-context-menu",
"funding": "https://github.com/sponsors/sindresorhus",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
"url": "https://sindresorhus.com"
},
"scripts": {
"start": "electron fixture.js",
@@ -27,24 +27,37 @@
"menu",
"extensible",
"save",
"image"
"image",
"spellchecking",
"spellcheck",
"spelling",
"spell",
"check",
"correct",
"word",
"words",
"dictionary"
],
"dependencies": {
"cli-truncate": "^2.0.0",
"electron-dl": "^1.2.0",
"electron-dl": "^3.0.0",
"electron-is-dev": "^1.0.1"
},
"devDependencies": {
"@types/node": "^12.0.10",
"ava": "^2.1.0",
"electron": "^7.1.1",
"tsd": "^0.10.0",
"xo": "^0.25.3"
"electron": "^8.2.2",
"tsd": "^0.11.0",
"xo": "^0.29.1"
},
"xo": {
"envs": [
"node",
"browser"
]
],
"rules": {
"@typescript-eslint/no-unsafe-call": "off",
"@typescript-eslint/no-unsafe-member-access": "off"
}
}
}
Loading