From a0ca4425c526f01f88ac46f5467e6074455de376 Mon Sep 17 00:00:00 2001 From: nautatva Date: Thu, 6 Feb 2020 02:14:10 +0530 Subject: [PATCH 01/16] Feature:Builtin support for spellcheck suggestions --- fixture-menu.js | 7 ++++++- fixture.js | 7 ++++--- index.d.ts | 3 ++- index.js | 31 ++++++++++++++++++++++++------- package.json | 4 ++-- readme.md | 4 +++- 6 files changed, 41 insertions(+), 15 deletions(-) diff --git a/fixture-menu.js b/fixture-menu.js index a3b023c..7bb1fda 100644 --- a/fixture-menu.js +++ b/fixture-menu.js @@ -33,5 +33,10 @@ contextMenu({ (async () => { await app.whenReady(); - await (new BrowserWindow()).loadFile(path.join(__dirname, 'fixture.html')); + await (new BrowserWindow({ + webPreferences: { + nodeIntegration: true, + spellcheck: true + } + })).loadFile(path.join(__dirname, 'fixture.html')); })(); diff --git a/fixture.js b/fixture.js index 97b4f73..3c25e34 100644 --- a/fixture.js +++ b/fixture.js @@ -1,6 +1,6 @@ 'use strict'; const path = require('path'); -const {app, BrowserWindow} = require('electron'); +const { app, BrowserWindow } = require('electron'); const contextMenu = require('.'); contextMenu({ @@ -34,7 +34,7 @@ contextMenu({ type: 'separator' } ], - append: () => {}, + append: () => { }, showCopyImageAddress: true, showSaveImageAs: true, showInspectElement: false @@ -45,7 +45,8 @@ contextMenu({ await (new BrowserWindow({ webPreferences: { - nodeIntegration: true + nodeIntegration: true, + spellcheck: true } })).loadFile(path.join(__dirname, 'fixture.html')); })(); diff --git a/index.d.ts b/index.d.ts index 44f3592..c3b7af7 100644 --- a/index.d.ts +++ b/index.d.ts @@ -217,7 +217,8 @@ declare namespace contextMenu { - `showSaveImageAs` - `showInspectElement` - `showServices` - + - `dictionary` + @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?: ( diff --git a/index.js b/index.js index 1d6184b..2137bd8 100644 --- a/index.js +++ b/index.js @@ -1,7 +1,7 @@ 'use strict'; const electron = require('electron'); const cliTruncate = require('cli-truncate'); -const {download} = require('electron-dl'); +const { download } = require('electron-dl'); const isDev = require('electron-is-dev'); const webContents = win => win.webContents || (win.getWebContents && win.getWebContents()); @@ -34,13 +34,13 @@ const create = (win, options) => { return; } - const {editFlags} = props; + const { editFlags } = props; const hasText = props.selectionText.trim().length > 0; const isLink = Boolean(props.linkURL); const can = type => editFlags[`can${type}`] && hasText; const defaultActions = { - separator: () => ({type: 'separator'}), + separator: () => ({ type: 'separator' }), lookUpSelection: decorateMenuItem({ id: 'lookUpSelection', label: 'Look Up “{selection}”', @@ -115,7 +115,7 @@ const create = (win, options) => { visible: props.mediaType === 'image', click(menuItem) { props.srcURL = menuItem.transform ? menuItem.transform(props.srcURL) : props.srcURL; - download(win, props.srcURL, {saveAs: true}); + download(win, props.srcURL, { saveAs: true }); } }), copyLink: decorateMenuItem({ @@ -173,7 +173,24 @@ const create = (win, options) => { const shouldShowInspectElement = typeof options.showInspectElement === 'boolean' ? options.showInspectElement : isDev; + let dictSuggestions = [] + for (let index = 0; index < props.dictionarySuggestions.length; index++) { + dictSuggestions.push( + { + id: 'dictSuggestions', + label: props.dictionarySuggestions[index], + visible: props.isEditable && hasText && props.misspelledWord, + click(menuItem) { + const target = webContents(win); + target.insertText(menuItem.label); + } + } + ) + } + let menuTemplate = [ + defaultActions.separator(), + ...dictSuggestions, defaultActions.separator(), options.showLookUpSelection !== false && defaultActions.lookUpSelection(), defaultActions.separator(), @@ -235,10 +252,10 @@ const create = (win, options) => { /* When `electron.remote` is not available, this runs in the browser process. - + We can safely use `win` in this case as it refers to the window the context-menu should open in. - + When this is being called from a web view, we can't use `win` as this would refer to the web view which is not allowed to render a popup menu. */ @@ -255,7 +272,7 @@ module.exports = (options = {}) => { if (webContents(win) === undefined) { win.addEventListener('dom-ready', () => { create(win, options); - }, {once: true}); + }, { once: true }); return; } diff --git a/package.json b/package.json index fbbf97a..3eec1aa 100644 --- a/package.json +++ b/package.json @@ -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" }, @@ -47,4 +47,4 @@ "browser" ] } -} +} \ No newline at end of file diff --git a/readme.md b/readme.md index 03de5e0..8abf05e 100644 --- a/readme.md +++ b/readme.md @@ -185,7 +185,8 @@ The following options are ignored when `menu` is used: - `showSaveImageAs` - `showInspectElement` - `showServices` - +- `dictionary` + Default actions: - `separator` @@ -200,6 +201,7 @@ Default actions: - `copyLink` - `inspect` - `services` +- `dictionary` Example: From b6d98541f5075d92de3f2e7ff05bc90f395080b4 Mon Sep 17 00:00:00 2001 From: nautatva Date: Thu, 6 Feb 2020 02:21:40 +0530 Subject: [PATCH 02/16] linting errors --- fixture.js | 2 +- index.js | 18 +++++++++--------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/fixture.js b/fixture.js index 3c25e34..d2d7773 100644 --- a/fixture.js +++ b/fixture.js @@ -1,6 +1,6 @@ 'use strict'; const path = require('path'); -const { app, BrowserWindow } = require('electron'); +const {app, BrowserWindow} = require('electron'); const contextMenu = require('.'); contextMenu({ diff --git a/index.js b/index.js index 2137bd8..83b934f 100644 --- a/index.js +++ b/index.js @@ -1,7 +1,7 @@ 'use strict'; const electron = require('electron'); const cliTruncate = require('cli-truncate'); -const { download } = require('electron-dl'); +const {download} = require('electron-dl'); const isDev = require('electron-is-dev'); const webContents = win => win.webContents || (win.getWebContents && win.getWebContents()); @@ -34,13 +34,13 @@ const create = (win, options) => { return; } - const { editFlags } = props; + const {editFlags} = props; const hasText = props.selectionText.trim().length > 0; const isLink = Boolean(props.linkURL); const can = type => editFlags[`can${type}`] && hasText; const defaultActions = { - separator: () => ({ type: 'separator' }), + separator: () => ({type: 'separator'}), lookUpSelection: decorateMenuItem({ id: 'lookUpSelection', label: 'Look Up “{selection}”', @@ -115,7 +115,7 @@ const create = (win, options) => { visible: props.mediaType === 'image', click(menuItem) { props.srcURL = menuItem.transform ? menuItem.transform(props.srcURL) : props.srcURL; - download(win, props.srcURL, { saveAs: true }); + download(win, props.srcURL, {saveAs: true}); } }), copyLink: decorateMenuItem({ @@ -173,7 +173,7 @@ const create = (win, options) => { const shouldShowInspectElement = typeof options.showInspectElement === 'boolean' ? options.showInspectElement : isDev; - let dictSuggestions = [] + const dictSuggestions = []; for (let index = 0; index < props.dictionarySuggestions.length; index++) { dictSuggestions.push( { @@ -185,7 +185,7 @@ const create = (win, options) => { target.insertText(menuItem.label); } } - ) + ); } let menuTemplate = [ @@ -252,10 +252,10 @@ const create = (win, options) => { /* When `electron.remote` is not available, this runs in the browser process. - + We can safely use `win` in this case as it refers to the window the context-menu should open in. - + When this is being called from a web view, we can't use `win` as this would refer to the web view which is not allowed to render a popup menu. */ @@ -272,7 +272,7 @@ module.exports = (options = {}) => { if (webContents(win) === undefined) { win.addEventListener('dom-ready', () => { create(win, options); - }, { once: true }); + }, {once: true}); return; } From 4ffb76ab2437523c441687b19a4c0af3296887c7 Mon Sep 17 00:00:00 2001 From: nautatva Date: Fri, 14 Feb 2020 05:49:05 +0530 Subject: [PATCH 03/16] SearchWithGoogle,AutoCorrection,learnSpell support --- fixture-menu.js | 1 - fixture.js | 1 - index.d.ts | 15 +++++++++++- index.js | 65 ++++++++++++++++++++++++++++++++++++++++--------- readme.md | 21 ++++++++++++++-- 5 files changed, 87 insertions(+), 16 deletions(-) diff --git a/fixture-menu.js b/fixture-menu.js index 7bb1fda..7bb09c9 100644 --- a/fixture-menu.js +++ b/fixture-menu.js @@ -35,7 +35,6 @@ contextMenu({ await (new BrowserWindow({ webPreferences: { - nodeIntegration: true, spellcheck: true } })).loadFile(path.join(__dirname, 'fixture.html')); diff --git a/fixture.js b/fixture.js index d2d7773..b1c3e57 100644 --- a/fixture.js +++ b/fixture.js @@ -45,7 +45,6 @@ contextMenu({ await (new BrowserWindow({ webPreferences: { - nodeIntegration: true, spellcheck: true } })).loadFile(path.join(__dirname, 'fixture.html')); diff --git a/index.d.ts b/index.d.ts index c3b7af7..861e8e5 100644 --- a/index.d.ts +++ b/index.d.ts @@ -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}"' */ readonly lookUpSelection?: string; + + /** + @default 'Search with Google' + */ + readonly searchWithGoogle?: string; /** @default 'Cut' @@ -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; @@ -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. diff --git a/index.js b/index.js index 83b934f..2882589 100644 --- a/index.js +++ b/index.js @@ -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); + } + }), cut: decorateMenuItem({ id: 'cut', label: 'Cut', @@ -152,6 +160,24 @@ const create = (win, options) => { }); } }), + correctAutomatically: decorateMenuItem({ + id: 'correctAutomatically', + label: 'Correct spelling Automatically', + visible: props.isEditable && hasText && props.misspelledWord && props.dictionarySuggestions.length, + 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); + } + }), inspect: () => ({ id: 'inspect', label: 'Inspect Element', @@ -173,27 +199,44 @@ const create = (win, options) => { const shouldShowInspectElement = typeof options.showInspectElement === 'boolean' ? options.showInspectElement : isDev; - const dictSuggestions = []; - for (let index = 0; index < props.dictionarySuggestions.length; index++) { - dictSuggestions.push( - { - id: 'dictSuggestions', - label: props.dictionarySuggestions[index], - visible: props.isEditable && hasText && props.misspelledWord, - click(menuItem) { - const target = webContents(win); - target.insertText(menuItem.label); + 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', + visible: hasText && props.misspelledWord, + enabled: false } ); } let menuTemplate = [ defaultActions.separator(), - ...dictSuggestions, + ...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(), diff --git a/readme.md b/readme.md index 8abf05e..f3b2d3c 100644 --- a/readme.md +++ b/readme.md @@ -43,7 +43,10 @@ contextMenu({ let mainWindow; (async () => { await app.whenReady(); - mainWindow = new BrowserWindow(); + mainWindow = new BrowserWindow( + webPreferences: { + spellcheck: true + }); })(); ``` @@ -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`\ @@ -191,6 +201,7 @@ Default actions: - `separator` - `lookUpSelection` +- `searchWithGoogle` - `cut` - `copy` - `paste` @@ -201,9 +212,15 @@ Default actions: - `copyLink` - `inspect` - `services` + +If we have `{webPreferences: {spellcheck: true}}` in `BrowserWindow`, these functions work by default. + - `dictionary` +- `correctAutomatically` +- `learnSpelling` -Example: + +Example for actions: ```js { From 57c95150d357e75be51ef415aad6109b2ec89baa Mon Sep 17 00:00:00 2001 From: nautatva Date: Fri, 14 Feb 2020 05:54:57 +0530 Subject: [PATCH 04/16] extra space removed --- fixture.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fixture.js b/fixture.js index b1c3e57..6cd4754 100644 --- a/fixture.js +++ b/fixture.js @@ -34,7 +34,7 @@ contextMenu({ type: 'separator' } ], - append: () => { }, + append: () => {}, showCopyImageAddress: true, showSaveImageAs: true, showInspectElement: false From 4efc985d747e869c072f9b14becd5d4a66969d91 Mon Sep 17 00:00:00 2001 From: nautatva Date: Fri, 14 Feb 2020 05:59:29 +0530 Subject: [PATCH 05/16] better nomenclature --- readme.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/readme.md b/readme.md index f3b2d3c..ea4e145 100644 --- a/readme.md +++ b/readme.md @@ -195,7 +195,7 @@ The following options are ignored when `menu` is used: - `showSaveImageAs` - `showInspectElement` - `showServices` -- `dictionary` +- `spellCheck` Default actions: @@ -215,7 +215,7 @@ Default actions: If we have `{webPreferences: {spellcheck: true}}` in `BrowserWindow`, these functions work by default. -- `dictionary` +- `spellCheck` - `correctAutomatically` - `learnSpelling` From 1e6fe1e58a3fe94b425c4c0c82483ece7649e775 Mon Sep 17 00:00:00 2001 From: nautatva Date: Mon, 24 Feb 2020 18:14:38 +0530 Subject: [PATCH 06/16] Refactored code --- index.d.ts | 6 +++--- index.js | 3 ++- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/index.d.ts b/index.d.ts index 861e8e5..d3ad21d 100644 --- a/index.d.ts +++ b/index.d.ts @@ -14,7 +14,7 @@ declare namespace contextMenu { /** The placeholder `{selection}` will be replaced by the currently selected text. - @default 'Look Up "{selection}"' + @default 'Look Up “{selection}”' */ readonly lookUpSelection?: string; @@ -136,7 +136,7 @@ declare namespace contextMenu { readonly showLookUpSelection?: boolean; /** - Show the `Look Up {selection}` menu item when right-clicking text on macOS. + Show the `Search with Google` menu item when right-clicking text on macOS. @default true */ @@ -232,7 +232,7 @@ declare namespace contextMenu { - `showServices` - `dictionary` - @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 [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(), defaultActions.correctAutomatically, defaultActions.learnSpelling] */ readonly menu?: ( defaultActions: Actions, diff --git a/index.js b/index.js index 2882589..9520d48 100644 --- a/index.js +++ b/index.js @@ -56,7 +56,8 @@ const create = (win, options) => { label: 'Search with Google', visible: hasText, click() { - require('electron').shell.openExternal('https://www.google.com/search?q=' + props.selectionText); + const query = 'https://www.google.com/search?q='; + electron.shell.openExternal(query + props.selectionText); } }), cut: decorateMenuItem({ From f4ebfdd6468bf8590b4122ad306029c12f073c6e Mon Sep 17 00:00:00 2001 From: nautatva Date: Wed, 11 Mar 2020 01:22:16 +0530 Subject: [PATCH 07/16] Docs updated to be in sync --- index.d.ts | 24 ++++++++++++++++++++---- index.js | 36 ++++++++++++++++++------------------ readme.md | 15 ++++++++------- 3 files changed, 46 insertions(+), 29 deletions(-) diff --git a/index.d.ts b/index.d.ts index d3ad21d..80b1e49 100644 --- a/index.d.ts +++ b/index.d.ts @@ -23,6 +23,16 @@ declare namespace contextMenu { */ readonly searchWithGoogle?: string; + /** + @default 'Correct Automatically' + */ + readonly correctAutomatically?: string; + + /** + @default 'Learn Spelling' + */ + readonly learnSpelling?: string; + /** @default 'Cut' */ @@ -87,6 +97,8 @@ declare namespace contextMenu { readonly separator: () => MenuItemConstructorOptions; readonly lookUpSelection: (options: ActionOptions) => MenuItemConstructorOptions; readonly searchWithGoogle: (options: ActionOptions) => MenuItemConstructorOptions; + readonly correctAutomatically: (options: ActionOptions) => MenuItemConstructorOptions; + readonly learnSpelling: (options: ActionOptions) => MenuItemConstructorOptions; readonly cut: (options: ActionOptions) => MenuItemConstructorOptions; readonly copy: (options: ActionOptions) => MenuItemConstructorOptions; readonly paste: (options: ActionOptions) => MenuItemConstructorOptions; @@ -140,7 +152,7 @@ declare namespace contextMenu { @default true */ - readonly searchWithGoogle?: boolean; + readonly showSearchWithGoogle?: boolean; /** Show the `Copy Image` menu item when right-clicking on an image. @@ -230,9 +242,13 @@ declare namespace contextMenu { - `showSaveImageAs` - `showInspectElement` - `showServices` - - `dictionary` + - `showSearchWithGoogle` - @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(), defaultActions.correctAutomatically, defaultActions.learnSpelling] + + To get spellcheck, 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, @@ -263,7 +279,7 @@ contextMenu({ let mainWindow; (async () => { await app.whenReady(); - mainWindow = new BrowserWindow(); + mainWindow = new BrowserWindow({ webPreferences: {spellcheck: true}}); }); ``` */ diff --git a/index.js b/index.js index 9520d48..bbb3799 100644 --- a/index.js +++ b/index.js @@ -56,8 +56,8 @@ const create = (win, options) => { label: 'Search with Google', visible: hasText, click() { - const query = 'https://www.google.com/search?q='; - electron.shell.openExternal(query + props.selectionText); + const url = new URL('https://www.google.com/search?q=' + props.selectionText).href; + electron.shell.openExternal(url); } }), cut: decorateMenuItem({ @@ -163,7 +163,7 @@ const create = (win, options) => { }), correctAutomatically: decorateMenuItem({ id: 'correctAutomatically', - label: 'Correct spelling Automatically', + label: 'Correct Spelling Automatically', visible: props.isEditable && hasText && props.misspelledWord && props.dictionarySuggestions.length, click() { const target = webContents(win); @@ -200,22 +200,22 @@ const create = (win, options) => { const shouldShowInspectElement = typeof options.showInspectElement === 'boolean' ? options.showInspectElement : isDev; - const dictionarySuggestions = []; + 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) { - 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 = props.dictionarySuggestions.map(word); + } else { dictionarySuggestions.push( { id: 'dictionarySuggestions', diff --git a/readme.md b/readme.md index ea4e145..4935b09 100644 --- a/readme.md +++ b/readme.md @@ -195,10 +195,17 @@ The following options are ignored when `menu` is used: - `showSaveImageAs` - `showInspectElement` - `showServices` -- `spellCheck` +- `showSearchWithGoogle` Default actions: +To get spellcheck, Correct Automatically and Learn Spelling in the menu, please enable the `spellcheck` preference in browser window. +Format: +`new BrowserWindow({ webPreferences: {spellcheck: true}})` + +- `spellCheck` +- `correctAutomatically` +- `learnSpelling` - `separator` - `lookUpSelection` - `searchWithGoogle` @@ -212,12 +219,6 @@ Default actions: - `copyLink` - `inspect` - `services` - -If we have `{webPreferences: {spellcheck: true}}` in `BrowserWindow`, these functions work by default. - -- `spellCheck` -- `correctAutomatically` -- `learnSpelling` Example for actions: From 085c3044977496966d03cd51172a670c43da348a Mon Sep 17 00:00:00 2001 From: nautatva Date: Wed, 11 Mar 2020 13:14:20 +0530 Subject: [PATCH 08/16] Encoding the URLqueryparams --- index.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index bbb3799..bcb2d93 100644 --- a/index.js +++ b/index.js @@ -56,7 +56,9 @@ const create = (win, options) => { label: 'Search with Google', visible: hasText, click() { - const url = new URL('https://www.google.com/search?q=' + props.selectionText).href; + const queryParams = {q: props.selectionText}; + const queryString = new URLSearchParams(queryParams).toString(); + const url = new URL('https://www.google.com/search?' + queryString).href; electron.shell.openExternal(url); } }), From 8e77f66d6bff722574ac67c6e3d46fb3af42f772 Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Wed, 11 Mar 2020 18:53:21 +0800 Subject: [PATCH 09/16] Update package.json --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 3eec1aa..cea2415 100644 --- a/package.json +++ b/package.json @@ -47,4 +47,4 @@ "browser" ] } -} \ No newline at end of file +} From fc8f0590f0bfc69607e69f3433097d6ff6337fff Mon Sep 17 00:00:00 2001 From: nautatva Date: Wed, 11 Mar 2020 17:25:53 +0530 Subject: [PATCH 10/16] encoding the search parameter --- index.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index bcb2d93..fb77f30 100644 --- a/index.js +++ b/index.js @@ -56,10 +56,9 @@ const create = (win, options) => { label: 'Search with Google', visible: hasText, click() { - const queryParams = {q: props.selectionText}; - const queryString = new URLSearchParams(queryParams).toString(); - const url = new URL('https://www.google.com/search?' + queryString).href; - electron.shell.openExternal(url); + const url = new URL('https://www.google.com/search'); + url.searchParams.set('q', props.selectionText); + electron.shell.openExternal(url.toString()); } }), cut: decorateMenuItem({ From 66afb4df6c8e4ebd126ac91fa8228078bdd2ba08 Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Mon, 6 Apr 2020 22:44:45 +0800 Subject: [PATCH 11/16] Update readme.md --- readme.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/readme.md b/readme.md index 4935b09..3e3d5e0 100644 --- a/readme.md +++ b/readme.md @@ -43,10 +43,12 @@ contextMenu({ let mainWindow; (async () => { await app.whenReady(); + mainWindow = new BrowserWindow( webPreferences: { spellcheck: true - }); + } + ); })(); ``` From 36899d7a8765d0888f706942b94bc137dbcafdc3 Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Mon, 6 Apr 2020 22:49:09 +0800 Subject: [PATCH 12/16] Update index.d.ts --- index.d.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/index.d.ts b/index.d.ts index 80b1e49..2d6c79f 100644 --- a/index.d.ts +++ b/index.d.ts @@ -244,9 +244,7 @@ declare namespace contextMenu { - `showServices` - `showSearchWithGoogle` - - To get spellcheck, Correct Automatically and Learn Spelling in the menu, please enable the `spellcheck` preference in browser window. - `new BrowserWindow({ webPreferences: {spellcheck: true}})` + 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()] */ From 6b867d610349fb4c4366099daee8c3b9e8977b7d Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Mon, 6 Apr 2020 22:51:00 +0800 Subject: [PATCH 13/16] Update index.d.ts --- index.d.ts | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/index.d.ts b/index.d.ts index 2d6c79f..d87fc2d 100644 --- a/index.d.ts +++ b/index.d.ts @@ -267,17 +267,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({ webPreferences: {spellcheck: true}}); + + mainWindow = new BrowserWindow({ + webPreferences: { + spellcheck: true + } + }); }); ``` */ From 4160cda3bd39aa9325a30d101fcdb1bfcbd8ba90 Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Mon, 6 Apr 2020 22:55:34 +0800 Subject: [PATCH 14/16] Update readme.md --- readme.md | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/readme.md b/readme.md index 3e3d5e0..c911901 100644 --- a/readme.md +++ b/readme.md @@ -189,6 +189,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` @@ -201,10 +203,6 @@ The following options are ignored when `menu` is used: Default actions: -To get spellcheck, Correct Automatically and Learn Spelling in the menu, please enable the `spellcheck` preference in browser window. -Format: -`new BrowserWindow({ webPreferences: {spellcheck: true}})` - - `spellCheck` - `correctAutomatically` - `learnSpelling` @@ -222,7 +220,6 @@ Format: - `inspect` - `services` - Example for actions: ```js From f27ffa7a76ad80ac1dd5e8c69e56fe3af848cae9 Mon Sep 17 00:00:00 2001 From: nautatva Date: Tue, 7 Apr 2020 19:10:27 +0530 Subject: [PATCH 15/16] Arranged in same order as they appear in menu. --- index.d.ts | 26 +++++++++++++------------- index.js | 38 +++++++++++++++++++------------------- 2 files changed, 32 insertions(+), 32 deletions(-) diff --git a/index.d.ts b/index.d.ts index d87fc2d..5fec214 100644 --- a/index.d.ts +++ b/index.d.ts @@ -11,27 +11,27 @@ import { declare namespace contextMenu { interface Labels { - /** - The placeholder `{selection}` will be replaced by the currently selected text. - @default 'Look Up “{selection}”' + /** + @default 'Correct Automatically' */ - readonly lookUpSelection?: string; + readonly correctAutomatically?: string; /** - @default 'Search with Google' + @default 'Learn Spelling' */ - readonly searchWithGoogle?: string; + readonly learnSpelling?: string; /** - @default 'Correct Automatically' + The placeholder `{selection}` will be replaced by the currently selected text. + @default 'Look Up “{selection}”' */ - readonly correctAutomatically?: string; + readonly lookUpSelection?: string; /** - @default 'Learn Spelling' + @default 'Search with Google' */ - readonly learnSpelling?: string; + readonly searchWithGoogle?: string; /** @default 'Cut' @@ -95,10 +95,10 @@ declare namespace contextMenu { interface Actions { readonly separator: () => MenuItemConstructorOptions; - readonly lookUpSelection: (options: ActionOptions) => MenuItemConstructorOptions; - readonly searchWithGoogle: (options: ActionOptions) => 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; @@ -237,12 +237,12 @@ declare namespace contextMenu { The following options are ignored when `menu` is used: - `showLookUpSelection` + - `showSearchWithGoogle` - `showCopyImage` - `showCopyImageAddress` - `showSaveImageAs` - `showInspectElement` - `showServices` - - `showSearchWithGoogle` To get spellchecking, “Correct Automatically”, and “Learn Spelling” in the menu, please enable the `spellcheck` preference in browser window: `new BrowserWindow({webPreferences: {spellcheck: true}})` diff --git a/index.js b/index.js index fb77f30..6401eca 100644 --- a/index.js +++ b/index.js @@ -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}”', @@ -162,24 +180,6 @@ const create = (win, options) => { }); } }), - correctAutomatically: decorateMenuItem({ - id: 'correctAutomatically', - label: 'Correct Spelling Automatically', - visible: props.isEditable && hasText && props.misspelledWord && props.dictionarySuggestions.length, - 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); - } - }), inspect: () => ({ id: 'inspect', label: 'Inspect Element', @@ -220,7 +220,7 @@ const create = (win, options) => { dictionarySuggestions.push( { id: 'dictionarySuggestions', - label: 'No guesses available', + label: 'No Guesses Found', visible: hasText && props.misspelledWord, enabled: false } From 4a4ebb2388e10bfe5c5736ce08b92e505489aec8 Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Tue, 14 Apr 2020 15:06:14 +0800 Subject: [PATCH 16/16] Update index.d.ts --- index.d.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/index.d.ts b/index.d.ts index 5fec214..93a80ad 100644 --- a/index.d.ts +++ b/index.d.ts @@ -11,7 +11,6 @@ import { declare namespace contextMenu { interface Labels { - /** @default 'Correct Automatically' */