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 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
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: (x) => [x.CUT({transform: (content) => "modified_cut_" + content})],
menu: (x) => [
sindresorhus marked this conversation as resolved.
Show resolved Hide resolved
x.SEPARATOR(),
x.COPY_LINK({transform: (content) => "modified_link_" + content}),
x.SEPARATOR(),
{
label: 'Unicorn'
},
x.SEPARATOR(),
x.COPY({transform: (content) => "modified_copy_" + content}),
{
label: 'Invisible',
visible: false
},
x.PASTE({transform: (content) => "modified_paste_" + content})
sindresorhus marked this conversation as resolved.
Show resolved Hide resolved
],
append: (x) => [x.SAVE_IMAGE()]
});

electron.app.on('ready', () => {
Expand Down
169 changes: 109 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'
}, {
}),
SAVE_IMAGE: 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'
}, {
}),
COPY_LINK: 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'
}];
})
};

const defaultMenu = [defaultActions.SEPARATOR(), defaultActions.CUT(), defaultActions.COPY(), defaultActions.PASTE(), defaultActions.SEPARATOR()];

let menuTpl = defaultMenu;

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

if (!opts.menu && props.mediaType === 'image') {
menuTpl = [defaultActions.SEPARATOR(), defaultActions.SAVE_IMAGE(), defaultActions.SEPARATOR()];
sindresorhus marked this conversation as resolved.
Show resolved Hide resolved
}

if (!opts.menu && props.linkURL && props.mediaType === 'none') {
menuTpl = [defaultActions.SEPARATOR(), defaultActions.COPY_LINK(), defaultActions.SEPARATOR()];
}

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,20 @@ function create(win, opts) {
});
}

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

if (opts.click) {
menuItem.click = opts.click;
}

return menuItem;
};
}

function delUnusedElements(menuTpl) {
let notDeletedPrevEl;
return menuTpl.filter(el => el.visible !== false).filter((el, i, arr) => {
Expand Down
9 changes: 8 additions & 1 deletion package.json
Expand Up @@ -39,6 +39,13 @@
"envs": [
"node",
"browser"
]
],
"rules": {
"new-cap": ["error", {
"newIsCap": true,
"capIsNew": true,
"properties": false
}]
sindresorhus marked this conversation as resolved.
Show resolved Hide resolved
}
}
}