Skip to content

Commit

Permalink
fetch from upstream (#2)
Browse files Browse the repository at this point in the history
* Add `showSelectAll` option (sindresorhus#160)

Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>

* 3.4.0

* Fix typo in readme example (sindresorhus#162)

* Meta tweaks

* Add menu items to save and copy video (sindresorhus#161)

* 3.5.0

Co-authored-by: hns258 <ncz258@gmail.com>
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
Co-authored-by: tauraloke <tauraloke@gmail.com>
Co-authored-by: andy wu <and80506@163.com>
  • Loading branch information
5 people committed Aug 25, 2022
1 parent 05e3490 commit 389aa88
Show file tree
Hide file tree
Showing 8 changed files with 165 additions and 9 deletions.
6 changes: 6 additions & 0 deletions fixtures/fixture-menu.js
Expand Up @@ -17,6 +17,12 @@ contextMenu({
label: 'Unicorn'
},
actions.separator(),
actions.saveVideo(),
actions.saveVideoAs(),
actions.copyVideoAddress({
transform: content => `modified_copy_${content}`
}),
actions.separator(),
actions.copy({
transform: content => `modified_copy_${content}`
}),
Expand Down
5 changes: 5 additions & 0 deletions fixtures/fixture.html
Expand Up @@ -22,6 +22,11 @@ <h1>Fixture</h1>
<br>
<img src="fixture.jpg">
<br>
<br>
<video controls width="250">
<source src="flower.mp4" type="video/mp4">
Sorry, your browser doesn't support embedded videos.
</video>
<div contenteditable="true"><strong>Editable text</strong></div>
<br>
<a href="https://github.com">Link</a>
Expand Down
3 changes: 3 additions & 0 deletions fixtures/fixture.js
Expand Up @@ -36,8 +36,11 @@ contextMenu({
}
],
append: () => {},
showSelectAll: true,
showCopyImageAddress: true,
showSaveImageAs: true,
showCopyVideoAddress: true,
showSaveVideoAs: true,
showInspectElement: false,
showSaveLinkAs: true
});
Expand Down
Binary file added fixtures/flower.mp4
Binary file not shown.
75 changes: 69 additions & 6 deletions index.d.ts
Expand Up @@ -40,6 +40,11 @@ declare namespace contextMenu {
*/
readonly paste?: string;

/**
@default 'Select All'
*/
readonly selectAll?: string;

/**
@default 'Save Image'
*/
Expand All @@ -50,6 +55,16 @@ declare namespace contextMenu {
*/
readonly saveImageAs?: string;

/**
@default 'Save Video'
*/
readonly saveVideo?: string;

/**
@default 'Save Video As…'
*/
readonly saveVideoAs?: string;

/**
@default 'Copy Link'
*/
Expand All @@ -70,6 +85,11 @@ declare namespace contextMenu {
*/
readonly copyImageAddress?: string;

/**
@default 'Copy Video Address'
*/
readonly copyVideoAddress?: string;

/**
@default 'Inspect Element'
*/
Expand Down Expand Up @@ -98,11 +118,15 @@ declare namespace contextMenu {
readonly cut: (options: ActionOptions) => MenuItemConstructorOptions;
readonly copy: (options: ActionOptions) => MenuItemConstructorOptions;
readonly paste: (options: ActionOptions) => MenuItemConstructorOptions;
readonly selectAll: (options: ActionOptions) => MenuItemConstructorOptions;
readonly saveImage: (options: ActionOptions) => MenuItemConstructorOptions;
readonly saveImageAs: (options: ActionOptions) => MenuItemConstructorOptions;
readonly saveVideo: (options: ActionOptions) => MenuItemConstructorOptions;
readonly saveVideoAs: (options: ActionOptions) => MenuItemConstructorOptions;
readonly copyLink: (options: ActionOptions) => MenuItemConstructorOptions;
readonly copyImage: (options: ActionOptions) => MenuItemConstructorOptions;
readonly copyImageAddress: (options: ActionOptions) => MenuItemConstructorOptions;
readonly copyVideoAddress: (options: ActionOptions) => MenuItemConstructorOptions;
readonly inspect: () => MenuItemConstructorOptions;
readonly services: () => MenuItemConstructorOptions;
}
Expand Down Expand Up @@ -161,6 +185,13 @@ declare namespace contextMenu {
*/
readonly showSearchWithGoogle?: boolean;

/**
Show the `Select All` menu item when right-clicking in a window.
Default: `false` on macOS, `true` on Windows and Linux
*/
readonly showSelectAll?: boolean;

/**
Show the `Copy Image` menu item when right-clicking on an image.
Expand Down Expand Up @@ -189,6 +220,27 @@ declare namespace contextMenu {
*/
readonly showSaveImageAs?: boolean;

/**
Show the `Copy Video Address` menu item when right-clicking on a video.
@default false
*/
readonly showCopyVideoAddress?: boolean;

/**
Show the `Save Video` menu item when right-clicking on a video.
@default false
*/
readonly showSaveVideo?: boolean;

/**
Show the `Save Video As…` menu item when right-clicking on a video.
@default false
*/
readonly showSaveVideoAs?: boolean;

/**
Show the `Save Link As…` menu item when right-clicking on a link.
Expand Down Expand Up @@ -266,16 +318,20 @@ declare namespace contextMenu {
- `showLearnSpelling`
- `showLookUpSelection`
- `showSearchWithGoogle`
- `showSelectAll`
- `showCopyImage`
- `showCopyImageAddress`
- `showSaveImageAs`
- `showCopyVideoAddress`
- `showSaveVideo`
- `showSaveVideoAs`
- `showSaveLinkAs`
- `showInspectElement`
- `showServices`
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.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.saveLinkAs(), defaultActions.separator(), defaultActions.inspect()]
@default [...dictionarySuggestions, defaultActions.separator(), defaultActions.separator(), defaultActions.learnSpelling(), defaultActions.separator(), defaultActions.lookUpSelection(), defaultActions.separator(),defaultActions.searchWithGoogle(), defaultActions.cut(), defaultActions.copy(), defaultActions.paste(), defaultActions.selectAll(), defaultActions.separator(), defaultActions.saveImage(), defaultActions.saveImageAs(), defaultActions.saveVideo(), defaultActions.saveVideoAs(), defaultActions.copyLink(), defaultActions.copyImage(), defaultActions.copyImageAddress(), defaultActions.separator(), defaultActions.copyLink(), defaultActions.saveLinkAs(), defaultActions.separator(), defaultActions.inspect()]
*/
readonly menu?: (
defaultActions: Actions,
Expand Down Expand Up @@ -304,8 +360,8 @@ You can use this module directly in both the main and renderer process.
@example
```
const {app, BrowserWindow} = require('electron');
const contextMenu = require('electron-context-menu');
import {app, BrowserWindow} = require('electron');
import contextMenu = require('electron-context-menu');
contextMenu({
showSaveImageAs: true
Expand All @@ -325,7 +381,7 @@ let mainWindow;
@example
```
import {app, BrowserWindow} from 'electron';
import {app, BrowserWindow} = require('electron');
import contextMenu = require('electron-context-menu');
contextMenu({
Expand All @@ -334,6 +390,14 @@ contextMenu({
label: 'Rainbow',
// Only show it when right-clicking images
visible: parameters.mediaType === 'image'
},
{
label: 'Search Google for “{selection}”',
// Only show it when right-clicking text
visible: parameters.selectionText.trim().length > 0,
click: () => {
shell.openExternal(`https://google.com/search?q=${encodeURIComponent(parameters.selectionText)}`);
}
}
]
});
Expand All @@ -347,7 +411,7 @@ let mainWindow;
spellcheck: true
}
});
});
})();
```
The return value of `contextMenu()` is a function that disposes of the created event listeners:
Expand All @@ -358,7 +422,6 @@ const dispose = contextMenu();
dispose();
```
*/
declare function contextMenu(options?: contextMenu.Options): () => void; // eslint-disable-line no-redeclare

Expand Down
43 changes: 43 additions & 0 deletions index.js
Expand Up @@ -120,6 +120,13 @@ const create = (win, options) => {
paste(target)
}
}),
selectAll: decorateMenuItem({
id: 'selectAll',
label: 'Select &All',
click() {
webContents(win).selectAll();
}
}),
saveImage: decorateMenuItem({
id: 'saveImage',
label: 'Save I&mage',
Expand All @@ -138,6 +145,24 @@ const create = (win, options) => {
download(win, props.srcURL, {saveAs: true});
}
}),
saveVideo: decorateMenuItem({
id: 'saveVideo',
label: 'Save Vide&o',
visible: props.mediaType === 'video',
click(menuItem) {
props.srcURL = menuItem.transform ? menuItem.transform(props.srcURL) : props.srcURL;
download(win, props.srcURL);
}
}),
saveVideoAs: decorateMenuItem({
id: 'saveVideoAs',
label: 'Save Video& As…',
visible: props.mediaType === 'video',
click(menuItem) {
props.srcURL = menuItem.transform ? menuItem.transform(props.srcURL) : props.srcURL;
download(win, props.srcURL, {saveAs: true});
}
}),
copyLink: decorateMenuItem({
id: 'copyLink',
label: 'Copy Lin&k',
Expand Down Expand Up @@ -181,6 +206,19 @@ const create = (win, options) => {
});
}
}),
copyVideoAddress: decorateMenuItem({
id: 'copyVideoAddress',
label: 'Copy Video Ad&dress',
visible: props.mediaType === 'video',
click(menuItem) {
props.srcURL = menuItem.transform ? menuItem.transform(props.srcURL) : props.srcURL;

electron.clipboard.write({
bookmark: props.srcURL,
text: props.srcURL
});
}
}),
inspect: () => ({
id: 'inspect',
label: 'I&nspect Element',
Expand All @@ -201,6 +239,7 @@ const create = (win, options) => {
};

const shouldShowInspectElement = typeof options.showInspectElement === 'boolean' ? options.showInspectElement : isDev;
const shouldShowSelectAll = options.showSelectAll || (options.showSelectAll !== false && process.platform !== 'darwin');

function word(suggestion) {
return {
Expand Down Expand Up @@ -241,11 +280,15 @@ const create = (win, options) => {
defaultActions.cut(),
defaultActions.copy(),
defaultActions.paste(),
shouldShowSelectAll && defaultActions.selectAll(),
defaultActions.separator(),
options.showSaveImage && defaultActions.saveImage(),
options.showSaveImageAs && defaultActions.saveImageAs(),
options.showCopyImage !== false && defaultActions.copyImage(),
options.showCopyImageAddress && defaultActions.copyImageAddress(),
options.showSaveVideo && defaultActions.saveVideo(),
options.showSaveVideoAs && defaultActions.saveVideoAs(),
options.showCopyVideoAddress && defaultActions.copyVideoAddress(),
defaultActions.separator(),
defaultActions.copyLink(),
options.showSaveLinkAs && defaultActions.saveLinkAs(),
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "electron-context-menu",
"version": "3.3.0",
"version": "3.5.0",
"description": "Context menu for your Electron app",
"license": "MIT",
"repository": "sindresorhus/electron-context-menu",
Expand Down
40 changes: 38 additions & 2 deletions readme.md
Expand Up @@ -41,7 +41,7 @@ let mainWindow;
Advanced example:

```js
const {app, BrowserWindow} = require('electron');
const {app, BrowserWindow, shell} = require('electron');
const contextMenu = require('electron-context-menu');

contextMenu({
Expand Down Expand Up @@ -143,6 +143,13 @@ Default: `true`

Show the `Search with Google` menu item when right-clicking text.

#### showSelectAll

Type: `boolean`\
Default: `false` on macOS, `true` on Windows and Linux

Show the `Select All` menu item when right-clicking in a window.

#### showCopyImage

Type: `boolean`\
Expand Down Expand Up @@ -171,6 +178,27 @@ Default: `false`

Show the `Save Image As…` menu item when right-clicking on an image.

#### showCopyVideoAddress

Type: `boolean`\
Default: `false`

Show the `Copy Video Address` menu item when right-clicking on a video.

#### showSaveVideo

Type: `boolean`\
Default: `false`

Show the `Save Video` menu item when right-clicking on a video.

#### showSaveVideoAs

Type: `boolean`\
Default: `false`

Show the `Save Video As…` menu item when right-clicking on a video.

#### showSaveLinkAs

Type: `boolean`\
Expand Down Expand Up @@ -249,13 +277,17 @@ To get spellchecking, “Correct Automatically”, and “Learn Spelling” in t
The following options are ignored when `menu` is used:

- `showLookUpSelection`
- `showSearchWithGoogle`
- `showSelectAll`
- `showCopyImage`
- `showCopyImageAddress`
- `showSaveImageAs`
- `showCopyVideoAddress`
- `showSaveVideo`
- `showSaveVideoAs`
- `showSaveLinkAs`
- `showInspectElement`
- `showServices`
- `showSearchWithGoogle`

Default actions:

Expand All @@ -267,10 +299,14 @@ Default actions:
- `cut`
- `copy`
- `paste`
- `selectAll`
- `saveImage`
- `saveImageAs`
- `saveVideo`
- `saveVideoAs`
- `copyImage`
- `copyImageAddress`
- `copyVideoAddress`
- `copyLink`
- `saveLinkAs`
- `inspect`
Expand Down

0 comments on commit 389aa88

Please sign in to comment.