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

Add menu option #44

Merged
merged 10 commits into from Apr 9, 2019
Merged
Show file tree
Hide file tree
Changes from 3 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
32 changes: 17 additions & 15 deletions fixture.js
Expand Up @@ -10,21 +10,23 @@ require('.')({
copyLink: 'Configured Copy Link',
inspect: 'Configured Inspect'
},
prepend: () => [{
label: 'Unicorn'
}, {
type: 'separator'
}, {
type: 'separator'
}, {
label: 'Invisible',
visible: false
}, {
type: 'separator'
}, {
type: 'separator'
}],
append: () => {}
prepend: (actions) => [actions.cut({transform: (content) => "modified_cut_" + content})],
menu: (actions) => [
actions.separator(),
actions.copyLink({transform: (content) => "modified_link_" + content}),
actions.separator(),
{
label: 'Unicorn'
},
actions.separator(),
actions.copy({transform: (content) => "modified_copy_" + content}),
{
label: 'Invisible',
visible: false
},
actions.paste({transform: (content) => "modified_paste_" + content})
],
append: (actions) => [actions.saveImage()]
});

electron.app.on('ready', () => {
Expand Down
165 changes: 105 additions & 60 deletions index.js
Expand Up @@ -15,96 +15,131 @@ function create(win, opts) {
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'
}];

if (props.mediaType === 'image') {
menuTpl = [{
const defaultActions = {
cut: decorateMenuItem({
id: 'cut',
label: 'Cut',
enabled: can('Cut'),
visible: props.isEditable,
click(menuItem) {
props.selectionText = menuItem.transform ? menuItem.transform(props.selectionText) : props.selectionText;

if (process.platform === 'darwin') {
electron.clipboard.writeBookmark(props.selectionText);
} else {
electron.clipboard.writeText(props.selectionText);
}

win.webContents.delete();
}
}),
copy: decorateMenuItem({
id: 'copy',
label: 'Copy',
enabled: can('Copy'),
visible: props.isEditable || hasText,
click(menuItem) {
props.selectionText = menuItem.transform ? menuItem.transform(props.selectionText) : props.selectionText;

if (process.platform === 'darwin') {
electron.clipboard.writeBookmark(props.selectionText);
} else {
electron.clipboard.writeText(props.selectionText);
}
}
}),
paste: decorateMenuItem({
id: 'paste',
label: 'Paste',
enabled: editFlags.canPaste,
visible: props.isEditable,
click(menuItem) {
let clipboardContent;
if (process.platform === 'darwin') {
clipboardContent = electron.clipboard.readBookmark();
} else {
clipboardContent = electron.clipboard.readText(props.selectionText);
}

clipboardContent = menuItem.transform ? menuItem.transform(clipboardContent) : clipboardContent;

win.webContents.insertText(clipboardContent);
}

}),
inspect: decorateMenuItem({
id: 'inspect',
label: 'Inspect Element',
click() {
win.inspectElement(props.x, props.y);

if (webContents(win).isDevToolsOpened()) {
webContents(win).devToolsWebContents.focus();
}
}
}),
separator: decorateMenuItem({
type: 'separator'
}, {
}),
saveImage: decorateMenuItem({
id: 'save',
label: 'Save Image',
click() {
visible: props.mediaType === 'image',
click(menuItem) {
props.srcURL = menuItem.transform ? menuItem.transform(props.srcURL) : props.srcURL;
download(win, props.srcURL);
}
}, {
type: 'separator'
}];
}

if (props.linkURL && props.mediaType === 'none') {
menuTpl = [{
type: 'separator'
}, {
}),
copyLink: decorateMenuItem({
id: 'copyLink',
label: 'Copy Link',
click() {
visible: props.linkURL.length !== 0 && props.mediaType === 'none',
click(menuItem) {
props.linkURL = menuItem.transform ? menuItem.transform(props.linkURL) : props.linkURL;

if (process.platform === 'darwin') {
electron.clipboard.writeBookmark(props.linkText, props.linkURL);
} else {
electron.clipboard.writeText(props.linkURL);
}
}
}, {
type: 'separator'
}];
})
};

let menuTpl = [
defaultActions.separator(),
defaultActions.cut(),
defaultActions.copy(),
defaultActions.paste(),
defaultActions.separator(),
defaultActions.saveImage(),
defaultActions.separator(),
defaultActions.copyLink(),
defaultActions.separator()
];

if (opts.menu) {
menuTpl = opts.menu(defaultActions, props, win);
}

if (opts.prepend) {
const result = opts.prepend(props, win);
const result = opts.prepend(defaultActions, props, win);

if (Array.isArray(result)) {
menuTpl.unshift(...result);
}
}

if (opts.append) {
const result = opts.append(props, win);
const result = opts.append(defaultActions, props, win);

if (Array.isArray(result)) {
menuTpl.push(...result);
}
}

if (opts.showInspectElement || (opts.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();
}
}
}, {
type: 'separator'
});
menuTpl.push(defaultActions.separator(), defaultActions.inspect(), defaultActions.separator());
}

// Apply custom labels for default menu items
Expand Down Expand Up @@ -135,6 +170,16 @@ function create(win, opts) {
});
}

function decorateMenuItem(menuItem) {
return (opts = {}) => {
if (opts.transform && !opts.click) {
menuItem.transform = opts.transform;
}

return menuItem;
};
}

function delUnusedElements(menuTpl) {
let notDeletedPrevEl;
return menuTpl.filter(el => el.visible !== false).filter((el, i, arr) => {
Expand Down
37 changes: 35 additions & 2 deletions readme.md
Expand Up @@ -51,17 +51,50 @@ Window or WebView to add the context menu to.

When not specified, the context menu will be added to all existing and new windows.

#### menu
Type: `Function`

Should return an array of [MenuItem](http://electron.atom.io/docs/api/menu-item/)'s to be shown in the context menu. The first argument is an array of default actions that can be used. These actions are functions that can take an object with a transform property. The transform function will get the content of what will be copied and can modify it if needed. If no menu property is defined, the default menu will be used.

Default actions:
- `cut`
- `copy`
- `paste`
- `cut`
- `inspect`
- `separator`
- `saveImage`
- `copyLink`

```js
menu: (actions) => [
actions.separator(),
actions.copyLink({transform: (content) => "modified_link_" + content}),
actions.separator(),
{
label: 'Unicorn'
},
actions.separator(),
actions.copy({transform: (content) => "modified_copy_" + content}),
{
label: 'Invisible',
visible: false
},
actions.paste({transform: (content) => "modified_paste_" + content})
]
```

#### prepend

Type: `Function`

Should return an array of [MenuItem](http://electron.atom.io/docs/api/menu-item/)'s to be prepended to the context menu. The first argument is [this `params` object](http://electron.atom.io/docs/api/web-contents/#event-context-menu). The second argument is the [BrowserWindow](http://electron.atom.io/docs/api/browser-window/) the context menu was requested for.
Should return an array of [MenuItem](http://electron.atom.io/docs/api/menu-item/)'s to be prepended to the context menu. The first argument is an array of default actions that can be used. The first argument is [this `params` object](http://electron.atom.io/docs/api/web-contents/#event-context-menu). The second argument is the [BrowserWindow](http://electron.atom.io/docs/api/browser-window/) the context menu was requested for.
sindresorhus marked this conversation as resolved.
Show resolved Hide resolved

#### append

Type: `Function`

Should return an array of [MenuItem](http://electron.atom.io/docs/api/menu-item/)'s to be appended to the context menu. The first argument is [this `params` object](http://electron.atom.io/docs/api/web-contents/#event-context-menu). The second argument is the [BrowserWindow](http://electron.atom.io/docs/api/browser-window/) the context menu was requested for.
Should return an array of [MenuItem](http://electron.atom.io/docs/api/menu-item/)'s to be appended to the context menu. The first argument is an array of default actions that can be used. The second argument is [this `params` object](http://electron.atom.io/docs/api/web-contents/#event-context-menu). The third argument is the [BrowserWindow](http://electron.atom.io/docs/api/browser-window/) the context menu was requested for.

#### showInspectElement

Expand Down