Skip to content

Commit

Permalink
Meta tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Jan 27, 2019
1 parent d7bbff3 commit ec20a28
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 102 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
language: node_js
node_js:
- 'node'
- '10'
4 changes: 3 additions & 1 deletion fixture.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
<title>Fixture</title>
<script>
window.addEventListener('load', () => {
require('.')({
const contextmenu = require('.');

contextMenu({
window: document.getElementById('webview')
});
});
Expand Down
49 changes: 29 additions & 20 deletions fixture.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
'use strict';
const electron = require('electron');
const {app, BrowserWindow} = require('electron');
const contextMenu = require('.');

require('.')({
contextMenu({
labels: {
cut: 'Configured Cut',
copy: 'Configured Copy',
Expand All @@ -11,26 +12,34 @@ require('.')({
copyLink: 'Configured Copy Link',
inspect: 'Configured Inspect'
},
prepend: () => [{
label: 'Unicorn'
}, {
type: 'separator'
}, {
type: 'separator'
}, {
label: 'Invisible',
visible: false
}, {
type: 'separator'
}, {
type: 'separator'
}],
prepend: () => [
{
label: 'Unicorn'
},
{
type: 'separator'
},
{
type: 'separator'
},
{
label: 'Invisible',
visible: false
},
{
type: 'separator'
},
{
type: 'separator'
}
],
append: () => {},
showCopyImageAddress: true,
showSaveImageAs: true
});

electron.app.on('ready', () => {
(new electron.BrowserWindow())
.loadURL(`file://${__dirname}/fixture.html`);
});
(async () => {
await app.whenReady();

new BrowserWindow().loadURL(`file://${__dirname}/fixture.html`);
})();
165 changes: 93 additions & 72 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,42 +15,51 @@ function create(win, options) {
const hasText = props.selectionText.trim().length > 0;
const can = type => editFlags[`can${type}`] && hasText;

let menuTpl = [{
type: 'separator'
}, {
id: 'cut',
label: 'Cut',
// Needed because of macOS limitation:
// https://github.com/electron/electron/issues/5860
role: can('Cut') ? 'cut' : '',
enabled: can('Cut'),
visible: props.isEditable
}, {
id: 'copy',
label: 'Copy',
role: can('Copy') ? 'copy' : '',
enabled: can('Copy'),
visible: props.isEditable || hasText
}, {
id: 'paste',
label: 'Paste',
role: editFlags.canPaste ? 'paste' : '',
enabled: editFlags.canPaste,
visible: props.isEditable
}, {
type: 'separator'
}];
let menuTpl = [
{
type: 'separator'
},
{
id: 'cut',
label: 'Cut',
// Needed because of macOS limitation:
// https://github.com/electron/electron/issues/5860
role: can('Cut') ? 'cut' : '',
enabled: can('Cut'),
visible: props.isEditable
},
{
id: 'copy',
label: 'Copy',
role: can('Copy') ? 'copy' : '',
enabled: can('Copy'),
visible: props.isEditable || hasText
},
{
id: 'paste',
label: 'Paste',
role: editFlags.canPaste ? 'paste' : '',
enabled: editFlags.canPaste,
visible: props.isEditable
},
{
type: 'separator'
}
];

if (props.mediaType === 'image') {
menuTpl = [{
type: 'separator'
}, {
id: 'save',
label: 'Save Image',
click(item, win) {
download(win, props.srcURL);
menuTpl = [
{
type: 'separator'
},
{
id: 'save',
label: 'Save Image',
click(item, win) {
download(win, props.srcURL);
}
}
}];
];

if (options.showSaveImageAs) {
menuTpl.push({
Expand All @@ -68,37 +77,45 @@ function create(win, options) {
}

if (props.linkURL && props.mediaType === 'none') {
menuTpl = [{
type: 'separator'
}, {
id: 'copyLink',
label: 'Copy Link',
click() {
electron.clipboard.write({
bookmark: props.linkText,
text: props.linkURL
});
menuTpl = [
{
type: 'separator'
},
{
id: 'copyLink',
label: 'Copy Link',
click() {
electron.clipboard.write({
bookmark: props.linkText,
text: props.linkURL
});
}
},
{
type: 'separator'
}
}, {
type: 'separator'
}];
];
}

if (options.showCopyImageAddress && props.mediaType === 'image') {
menuTpl.push({
type: 'separator'
}, {
id: 'copyImageAddress',
label: 'Copy Image Address',
click() {
electron.clipboard.write({
bookmark: props.srcURL,
text: props.srcURL
});
menuTpl.push(
{
type: 'separator'
},
{
id: 'copyImageAddress',
label: 'Copy Image Address',
click() {
electron.clipboard.write({
bookmark: props.srcURL,
text: props.srcURL
});
}
},
{
type: 'separator'
}
}, {
type: 'separator'
});
);
}

if (options.prepend) {
Expand All @@ -118,21 +135,25 @@ function create(win, options) {
}

if (options.showInspectElement || (options.showInspectElement !== false && isDev)) {
menuTpl.push({
type: 'separator'
}, {
id: 'inspect',
label: 'Inspect Element',
click() {
win.inspectElement(props.x, props.y);

if (webContents(win).isDevToolsOpened()) {
webContents(win).devToolsWebContents.focus();
menuTpl.push(
{
type: 'separator'
},
{
id: 'inspect',
label: 'Inspect Element',
click() {
win.inspectElement(props.x, props.y);

if (webContents(win).isDevToolsOpened()) {
webContents(win).devToolsWebContents.focus();
}
}
},
{
type: 'separator'
}
}, {
type: 'separator'
});
);
}

// Apply custom labels for default menu items
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@
"electron-is-dev": "^1.0.1"
},
"devDependencies": {
"ava": "*",
"electron": "^3.0.6",
"ava": "^1.2.0",
"electron": "^4.0.2",
"tsd-check": "^0.3.0",
"xo": "*"
"xo": "^0.24.0"
},
"xo": {
"envs": [
Expand Down
12 changes: 7 additions & 5 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,21 @@ $ npm install electron-context-menu

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

require('electron-context-menu')({
contextMenu({
prepend: (params, browserWindow) => [{
label: 'Rainbow',
// Only show it when right-clicking images
visible: params.mediaType === 'image'
}]
});

let mainWindow;
app.on('ready', () => {
mainWindow = new BrowserWindow();
});
let win;
(async () => {
await app.whenReady();
win = new BrowserWindow();
})();
```


Expand Down

0 comments on commit ec20a28

Please sign in to comment.