Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add built-in support for spellchecking #94

Merged
merged 16 commits into from
Apr 14, 2020
Merged
6 changes: 5 additions & 1 deletion fixture-menu.js
Original file line number Diff line number Diff line change
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
nautatva marked this conversation as resolved.
Show resolved Hide resolved
}
})).loadFile(path.join(__dirname, 'fixture.html'));
})();
2 changes: 1 addition & 1 deletion fixture.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ contextMenu({

await (new BrowserWindow({
webPreferences: {
nodeIntegration: true
spellcheck: true
}
})).loadFile(path.join(__dirname, 'fixture.html'));
})();
18 changes: 16 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,14 @@ declare namespace contextMenu {
/**
The placeholder `{selection}` will be replaced by the currently selected text.

@default 'Look Up {selection}'
@default 'Look Up "{selection}"'
nautatva marked this conversation as resolved.
Show resolved Hide resolved
*/
readonly lookUpSelection?: string;

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

/**
@default 'Cut'
Expand Down Expand Up @@ -81,6 +86,7 @@ declare namespace contextMenu {
interface Actions {
readonly separator: () => 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 +135,13 @@ declare namespace contextMenu {
*/
readonly showLookUpSelection?: boolean;

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

@default true
*/
readonly searchWithGoogle?: boolean;

/**
Show the `Copy Image` menu item when right-clicking on an image.

Expand Down Expand Up @@ -217,7 +230,8 @@ declare namespace contextMenu {
- `showSaveImageAs`
- `showInspectElement`
- `showServices`

- `dictionary`

nautatva marked this conversation as resolved.
Show resolved Hide resolved
@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()]
*/
readonly menu?: (
Expand Down
60 changes: 60 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ const create = (win, options) => {
}
}
}),
searchWithGoogle: decorateMenuItem({
id: 'searchWithGoogle',
label: 'Search with Google',
visible: hasText,
click() {
require('electron').shell.openExternal('https://www.google.com/search?q=' + props.selectionText);
nautatva marked this conversation as resolved.
Show resolved Hide resolved
}
}),
nautatva marked this conversation as resolved.
Show resolved Hide resolved
cut: decorateMenuItem({
id: 'cut',
label: 'Cut',
Expand Down Expand Up @@ -152,6 +160,24 @@ const create = (win, options) => {
});
}
}),
correctAutomatically: decorateMenuItem({
sindresorhus marked this conversation as resolved.
Show resolved Hide resolved
id: 'correctAutomatically',
label: 'Correct spelling Automatically',
nautatva marked this conversation as resolved.
Show resolved Hide resolved
visible: props.isEditable && hasText && props.misspelledWord && props.dictionarySuggestions.length,
nautatva marked this conversation as resolved.
Show resolved Hide resolved
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);
}
}),
nautatva marked this conversation as resolved.
Show resolved Hide resolved
inspect: () => ({
id: 'inspect',
label: 'Inspect Element',
Expand All @@ -173,10 +199,44 @@ const create = (win, options) => {

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

const dictionarySuggestions = [];
if (hasText && props.misspelledWord && props.dictionarySuggestions.length > 0) {
for (let index = 0; index < props.dictionarySuggestions.length; index++) {
dictionarySuggestions.push(
{
id: 'dictionarySuggestions',
label: props.dictionarySuggestions[index],
visible: props.isEditable && hasText && props.misspelledWord,
click(menuItem) {
const target = webContents(win);
target.insertText(menuItem.label);
}
}
);
}
} else {
dictionarySuggestions.push(
{
id: 'dictionarySuggestions',
label: 'No guesses available',
nautatva marked this conversation as resolved.
Show resolved Hide resolved
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
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
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 All @@ -47,4 +47,4 @@
"browser"
]
}
}
}
nautatva marked this conversation as resolved.
Show resolved Hide resolved
25 changes: 22 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ contextMenu({
let mainWindow;
(async () => {
await app.whenReady();
mainWindow = new BrowserWindow();
mainWindow = new BrowserWindow(
webPreferences: {
spellcheck: true
});
})();
```

Expand Down Expand Up @@ -90,6 +93,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 @@ -185,11 +195,13 @@ The following options are ignored when `menu` is used:
- `showSaveImageAs`
- `showInspectElement`
- `showServices`

- `spellCheck`

nautatva marked this conversation as resolved.
Show resolved Hide resolved
Default actions:

- `separator`
- `lookUpSelection`
- `searchWithGoogle`
- `cut`
- `copy`
- `paste`
Expand All @@ -200,8 +212,15 @@ Default actions:
- `copyLink`
- `inspect`
- `services`

If we have `{webPreferences: {spellcheck: true}}` in `BrowserWindow`, these functions work by default.
nautatva marked this conversation as resolved.
Show resolved Hide resolved

Example:
- `spellCheck`
- `correctAutomatically`
- `learnSpelling`


Example for actions:

```js
{
Expand Down