Skip to content

Commit

Permalink
Add built-in support for spellchecking (sindresorhus#94)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
  • Loading branch information
nautatva and sindresorhus committed Apr 14, 2020
1 parent 2f1bae0 commit 71c5d2e
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 14 deletions.
6 changes: 5 additions & 1 deletion fixture-menu.js
Expand Up @@ -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
Expand Up @@ -45,7 +45,7 @@ contextMenu({

await (new BrowserWindow({
webPreferences: {
nodeIntegration: true
spellcheck: true
}
})).loadFile(path.join(__dirname, 'fixture.html'));
})();
50 changes: 42 additions & 8 deletions index.d.ts
Expand Up @@ -12,11 +12,25 @@ 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'
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -219,13 +243,16 @@ declare namespace contextMenu {
The following options are ignored when `menu` is used:
- `showLookUpSelection`
- `showSearchWithGoogle`
- `showCopyImage`
- `showCopyImageAddress`
- `showSaveImageAs`
- `showInspectElement`
- `showServices`
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 [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()]
@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,
Expand All @@ -246,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
}
});
});
```
*/
Expand Down
62 changes: 62 additions & 0 deletions index.js
Expand Up @@ -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}”',
Expand All @@ -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',
Expand Down Expand Up @@ -173,10 +201,44 @@ 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(),
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -37,7 +37,7 @@
"devDependencies": {
"@types/node": "^12.0.10",
"ava": "^2.1.0",
"electron": "^7.1.1",
"electron": "^8.0.0",
"tsd": "^0.10.0",
"xo": "^0.25.3"
},
Expand Down
25 changes: 22 additions & 3 deletions readme.md
Expand Up @@ -43,7 +43,12 @@ contextMenu({
let mainWindow;
(async () => {
await app.whenReady();
mainWindow = new BrowserWindow();

mainWindow = new BrowserWindow(
webPreferences: {
spellcheck: true
}
);
})();
```

Expand Down Expand Up @@ -90,6 +95,13 @@ Default: `true`

Show the `Look Up {selection}` menu item when right-clicking text on macOS.

#### showSearchWithGoogle

Type: `boolean`\
Default: `true`

Show the `Search with Google` menu item when right-clicking text on macOS.

#### showCopyImage

Type: `boolean`\
Expand Down Expand Up @@ -184,6 +196,8 @@ Even though you include an action, it will still only be shown/enabled when appr

`MenuItem` labels may contain the the placeholder `{selection}` which will be replaced by the currently selected text as described in [`options.labels`](#labels).

To get spellchecking, “Correct Automatically”, and “Learn Spelling” in the menu, please enable the `spellcheck` preference in browser window: `new BrowserWindow({webPreferences: {spellcheck: true}})`

The following options are ignored when `menu` is used:

- `showLookUpSelection`
Expand All @@ -192,11 +206,16 @@ The following options are ignored when `menu` is used:
- `showSaveImageAs`
- `showInspectElement`
- `showServices`

- `showSearchWithGoogle`

Default actions:

- `spellCheck`
- `correctAutomatically`
- `learnSpelling`
- `separator`
- `lookUpSelection`
- `searchWithGoogle`
- `cut`
- `copy`
- `paste`
Expand All @@ -208,7 +227,7 @@ Default actions:
- `inspect`
- `services`

Example:
Example for actions:

```js
{
Expand Down

0 comments on commit 71c5d2e

Please sign in to comment.