Skip to content

Commit

Permalink
Meta tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Jun 30, 2019
1 parent 12d60d9 commit 1b389b7
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 14 deletions.
10 changes: 9 additions & 1 deletion index.d.ts
Expand Up @@ -92,6 +92,8 @@ declare namespace contextMenu {

/**
Should return an array of [menu items](https://electronjs.org/docs/api/menu-item) to be prepended to the context menu.
`MenuItem` labels may contain the the placeholder `{selection}` which will be replaced by the currently selected text as described in `options.labels`.
*/
readonly prepend?: (
defaultActions: Actions,
Expand All @@ -101,6 +103,8 @@ declare namespace contextMenu {

/**
Should return an array of [menu items](https://electronjs.org/docs/api/menu-item) to be appended to the context menu.
`MenuItem` labels may contain the the placeholder `{selection}` which will be replaced by the currently selected text as described in `options.labels`.
*/
readonly append?: (
defaultActions: Actions,
Expand Down Expand Up @@ -144,7 +148,9 @@ declare namespace contextMenu {
readonly showServices?: boolean;

/**
Overwrite labels for the default menu items. Useful for i18n.
Override labels for the default menu items. Useful for i18n.
The placeholder `{selection}` may be used in any label, and will be replaced by the currently selected text, trimmed to a maximum of 25 characters length. This is useful when localizing the `Look Up “{selection}”` menu item, but can also be used in custom menu items, for example, to implement a `Search Google for “{selection}”` menu item. If there is no selection, the `{selection}` placeholder will be replaced by an empty string. Normally this placeholder is only useful for menu items which will only be shown when there is text selected. This can be checked using `visible: params.selectionText.trim().length > 0` when implementing a custom menu item.
@default {}
Expand Down Expand Up @@ -184,6 +190,8 @@ declare namespace contextMenu {
Even though you include an action, it will still only be shown/enabled when appropriate. For example, the `saveImage` action is only shown when right-clicking an image.
`MenuItem` labels may contain the the placeholder `{selection}` which will be replaced by the currently selected text as described in `options.labels`.
The following options are ignored when `menu` is used:
- `showCopyImageAddress`
Expand Down
2 changes: 1 addition & 1 deletion index.js
Expand Up @@ -196,7 +196,7 @@ const create = (win, options) => {
}

// Replace placeholders in menu item labels
if (typeof menuItem.label === 'string' && menuItem.label.indexOf('{selection}') !== -1) {
if (typeof menuItem.label === 'string' && menuItem.label.includes('{selection}')) {
const selectionString = typeof props.selectionText === 'string' ? props.selectionText.trim() : '';
menuItem.label = menuItem.label.replace('{selection}', cliTruncate(selectionString, 25));
}
Expand Down
6 changes: 3 additions & 3 deletions package.json
Expand Up @@ -28,13 +28,13 @@
"image"
],
"dependencies": {
"cli-truncate": "^1.1.0",
"cli-truncate": "^2.0.0",
"electron-dl": "^1.2.0",
"electron-is-dev": "^1.0.1"
},
"devDependencies": {
"@types/node": "^11.13.0",
"ava": "^1.4.1",
"@types/node": "^12.0.10",
"ava": "^2.1.0",
"electron": "^5.0.0",
"tsd": "^0.7.2",
"xo": "^0.24.0"
Expand Down
15 changes: 6 additions & 9 deletions readme.md
Expand Up @@ -34,9 +34,9 @@ contextMenu({
{
label: 'Search Google for “{selection}”',
// Only show it when right-clicking text
visible: (params.selectionText.trim().length > 0),
visible: params.selectionText.trim().length > 0,
click: () => {
shell.openExternal(`https://www.google.com/search?q=${encodeURIComponent(params.selectionText)}`);
shell.openExternal(`https://google.com/search?q=${encodeURIComponent(params.selectionText)}`);
}
}
]
Expand All @@ -52,11 +52,11 @@ let mainWindow;

## API

### contextMenu([options])
### contextMenu(options?)

### options

Type: `Object`
Type: `object`

#### window

Expand Down Expand Up @@ -125,15 +125,12 @@ Note: Due to [a bug in the Electron implementation](https://github.com/electron/

#### labels

Type: `Object`<br>
Type: `object`<br>
Default: `{}`

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

The placeholder `{selection}` may be used in any label, and will be replaced by the currently selected text, trimmed to a maximum of 25 characters length.
This is useful when localizing the `Look Up “{selection}”` menu item, but can also be used in custom menu items – for example, to implement a `Search Google for “{selection}”` menu item.

If there is no selection, the `{selection}` placeholder will be replaced by an empty string. Normally this placeholder is only useful for menu items which will only be shown when there is text selected. This can be checked using `visible: (params.selectionText.trim().length > 0)` when implementing a custom menu item, as shown in the usage example above.
The placeholder `{selection}` may be used in any label, and will be replaced by the currently selected text, trimmed to a maximum of 25 characters length. This is useful when localizing the `Look Up “{selection}”` menu item, but can also be used in custom menu items, for example, to implement a `Search Google for “{selection}”` menu item. If there is no selection, the `{selection}` placeholder will be replaced by an empty string. Normally this placeholder is only useful for menu items which will only be shown when there is text selected. This can be checked using `visible: params.selectionText.trim().length > 0` when implementing a custom menu item, as shown in the usage example above.

Format:

Expand Down

0 comments on commit 1b389b7

Please sign in to comment.