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

Improve lookUpSelection implementation #76

Merged
merged 7 commits into from Jun 30, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 9 additions & 2 deletions index.d.ts
Expand Up @@ -10,6 +10,13 @@ import {

declare namespace contextMenu {
interface Labels {
/**
The default label for the `Look Up [selection]` menu item is dynamically generated depending on the selected text when the menu is shown.
If a value is set here, the provided static value will be used instead.
caesar marked this conversation as resolved.
Show resolved Hide resolved
@default 'Look Up'
*/
readonly lookUpSelection?: string;

/**
@default 'Cut'
*/
Expand Down Expand Up @@ -60,14 +67,14 @@ declare namespace contextMenu {

interface Actions {
readonly separator: () => MenuItem;
readonly inspect: () => MenuItem;
readonly lookUpSelection: (options: ActionOptions) => MenuItem;
readonly cut: (options: ActionOptions) => MenuItem;
readonly copy: (options: ActionOptions) => MenuItem;
readonly paste: (options: ActionOptions) => MenuItem;
readonly saveImage: (options: ActionOptions) => MenuItem;
readonly saveImageAs: (options: ActionOptions) => MenuItem;
readonly copyImageAddress: (options: ActionOptions) => MenuItem;
readonly lookUpSelection: (options: ActionOptions) => MenuItem;
readonly inspect: () => MenuItem;
}

interface Options {
Expand Down
38 changes: 19 additions & 19 deletions index.js
Expand Up @@ -39,6 +39,17 @@ const create = (win, options) => {
const can = type => editFlags[`can${type}`] && hasText;

const defaultActions = {
separator: () => ({type: 'separator'}),
lookUpSelection: decorateMenuItem({
id: 'lookUpSelection',
label: `Look Up “${cliTruncate(props.selectionText.trim(), 25)}”`,
visible: process.platform === 'darwin' && hasText,
click() {
if (process.platform === 'darwin') {
webContents(win).showDefinitionForSelection();
}
}
}),
cut: decorateMenuItem({
id: 'cut',
label: 'Cut',
Expand Down Expand Up @@ -71,19 +82,6 @@ const create = (win, options) => {
webContents(win).insertText(clipboardContent);
}
}),
inspect: () => ({
id: 'inspect',
label: 'Inspect Element',
enabled: isDev,
click() {
win.inspectElement(props.x, props.y);

if (webContents(win).isDevToolsOpened()) {
webContents(win).devToolsWebContents.focus();
}
}
}),
separator: () => ({type: 'separator'}),
saveImage: decorateMenuItem({
id: 'save',
label: 'Save Image',
Expand Down Expand Up @@ -128,13 +126,15 @@ const create = (win, options) => {
});
}
}),
lookUpSelection: decorateMenuItem({
id: 'lookUpSelection',
label: `Look Up “${cliTruncate(props.selectionText.trim(), 25)}”`,
visible: process.platform === 'darwin' && hasText,
inspect: () => ({
id: 'inspect',
label: 'Inspect Element',
enabled: isDev,
click() {
if (process.platform === 'darwin') {
webContents(win).showDefinitionForSelection();
win.inspectElement(props.x, props.y);

if (webContents(win).isDevToolsOpened()) {
webContents(win).devToolsWebContents.focus();
}
}
})
Expand Down
6 changes: 5 additions & 1 deletion readme.md
Expand Up @@ -105,7 +105,7 @@ Show the `Look Up [selection]` menu item when right-clicking text on macOS.
Type: `Object`<br>
Default: `{}`

Overwrite labels for the default menu items. Useful for i18n.
Override labels for the default menu items. Useful for i18n.

Format:

Expand All @@ -118,6 +118,10 @@ Format:
}
```

Note that the default label for the `Look Up [selection]` menu item is dynamically generated depending on the selected text when the menu is shown.
For example, if the word `Electron` was selected, the menu item label would be `Look up “Electron”`.<br />
If the label for `lookUpSelection` is overridden via the `labels` option, the provided static label will be used instead, which will not contain the selected text.

#### shouldShowMenu

Type: `Function`
Expand Down