Skip to content

Commit

Permalink
Add menu item for looking up a word on macOS (#71)
Browse files Browse the repository at this point in the history
  • Loading branch information
caesar authored and sindresorhus committed May 30, 2019
1 parent 2529d65 commit 35f3dd0
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 0 deletions.
8 changes: 8 additions & 0 deletions index.d.ts
Expand Up @@ -67,6 +67,7 @@ declare namespace contextMenu {
readonly saveImage: (options: ActionOptions) => MenuItem;
readonly saveImageAs: (options: ActionOptions) => MenuItem;
readonly copyImageAddress: (options: ActionOptions) => MenuItem;
readonly lookUpSelection: (options: ActionOptions) => MenuItem;
}

interface Options {
Expand Down Expand Up @@ -115,6 +116,13 @@ declare namespace contextMenu {
*/
readonly showInspectElement?: boolean;

/**
Show the `Look Up [selection]` menu item when right-clicking text on macOS.
@default true
*/
readonly showLookUpSelection?: boolean;

/**
Overwrite labels for the default menu items. Useful for i18n.
Expand Down
13 changes: 13 additions & 0 deletions index.js
@@ -1,5 +1,6 @@
'use strict';
const electron = require('electron');
const cliTruncate = require('cli-truncate');
const {download} = require('electron-dl');
const isDev = require('electron-is-dev');

Expand Down Expand Up @@ -126,10 +127,22 @@ const create = (win, options) => {
text: props.srcURL
});
}
}),
lookUpSelection: decorateMenuItem({
id: 'lookUpWord',
label: `Look Up “${cliTruncate(props.selectionText.trim(), 25)}”`,
visible: process.platform === 'darwin' && hasText,
click() {
if (process.platform === 'darwin') {
webContents(win).showDefinitionForSelection();
}
}
})
};

let menuTemplate = [
defaultActions.separator(),
options.showLookUpSelection !== false && defaultActions.lookUpSelection(),
defaultActions.separator(),
defaultActions.cut(),
defaultActions.copy(),
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -28,6 +28,7 @@
"image"
],
"dependencies": {
"cli-truncate": "^1.1.0",
"electron-dl": "^1.2.0",
"electron-is-dev": "^1.0.1"
},
Expand Down
7 changes: 7 additions & 0 deletions readme.md
Expand Up @@ -93,6 +93,13 @@ Default: [Only in development](https://github.com/sindresorhus/electron-is-dev)

Force enable or disable the `Inspect Element` menu item.

#### showLookUpSelection

Type: `boolean`<br>
Default: `true`

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

#### labels

Type: `Object`<br>
Expand Down

0 comments on commit 35f3dd0

Please sign in to comment.