forked from sindresorhus/electron-context-menu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
186 lines (163 loc) · 4.5 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
'use strict';
const electron = require('electron');
const {download} = require('electron-dl');
const isDev = require('electron-is-dev');
const webContents = win => win.webContents || win.getWebContents();
function create(win, options) {
webContents(win).on('context-menu', (event, props) => {
if (typeof options.shouldShowMenu === 'function' && options.shouldShowMenu(event, props) === false) {
return;
}
const {editFlags} = props;
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 = [{
type: 'separator'
}, {
id: 'save',
label: 'Save Image',
click(item, win) {
download(win, props.srcURL);
}
}, {
type: 'separator'
}];
}
if (props.linkURL && props.mediaType === 'none') {
menuTpl = [{
type: 'separator'
}, {
id: 'copyLink',
label: 'Copy Link',
click() {
electron.clipboard.write({
bookmark: props.linkText,
text: props.linkURL
});
}
}, {
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
});
}
}, {
type: 'separator'
});
}
if (options.prepend) {
const result = options.prepend(props, win);
if (Array.isArray(result)) {
menuTpl.unshift(...result);
}
}
if (options.append) {
const result = options.append(props, win);
if (Array.isArray(result)) {
menuTpl.push(...result);
}
}
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();
}
}
}, {
type: 'separator'
});
}
// Apply custom labels for default menu items
if (options.labels) {
for (const menuItem of menuTpl) {
if (options.labels[menuItem.id]) {
menuItem.label = options.labels[menuItem.id];
}
}
}
// Filter out leading/trailing separators
// TODO: https://github.com/electron/electron/issues/5869
menuTpl = delUnusedElements(menuTpl);
if (menuTpl.length > 0) {
const menu = (electron.remote ? electron.remote.Menu : electron.Menu).buildFromTemplate(menuTpl);
/*
* When electron.remote is not available this runs in the browser process.
* We can safely use win in this case as it refers to the window the
* context-menu should open in.
* When this is being called from a webView, we can't use win as this
* would refere to the webView which is not allowed to render a popup menu.
*/
menu.popup(electron.remote ? electron.remote.getCurrentWindow() : win);
}
});
}
function delUnusedElements(menuTpl) {
let notDeletedPrevEl;
return menuTpl.filter(el => el.visible !== false).filter((el, i, array) => {
const toDelete = el.type === 'separator' && (!notDeletedPrevEl || i === array.length - 1 || array[i + 1].type === 'separator');
notDeletedPrevEl = toDelete ? notDeletedPrevEl : el;
return !toDelete;
});
}
module.exports = (options = {}) => {
if (options.window) {
const win = options.window;
const wc = webContents(win);
// When window is a webview that has not yet finished loading webContents is not available
if (wc === undefined) {
win.addEventListener('dom-ready', () => {
create(win, options);
}, {once: true});
return;
}
return create(win, options);
}
for (const win of (electron.BrowserWindow || electron.remote.BrowserWindow).getAllWindows()) {
create(win, options);
}
(electron.app || electron.remote.app).on('browser-window-created', (event, win) => {
create(win, options);
});
};