forked from samuelmaddock/electron-browser-shell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
249 lines (218 loc) · 6.46 KB
/
index.ts
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
import { app, BrowserWindow, clipboard, Menu, MenuItem } from 'electron'
const LABELS = {
openInNewTab: (type: 'link' | Electron.ContextMenuParams['mediaType']) =>
`Open ${type} in new tab`,
openInNewWindow: (type: 'link' | Electron.ContextMenuParams['mediaType']) =>
`Open ${type} in new window`,
copyAddress: (type: 'link' | Electron.ContextMenuParams['mediaType']) => `Copy ${type} address`,
undo: 'Undo',
redo: 'Redo',
cut: 'Cut',
copy: 'Copy',
delete: 'Delete',
paste: 'Paste',
selectAll: 'Select All',
back: 'Back',
forward: 'Forward',
reload: 'Reload',
inspect: 'Inspect',
addToDictionary: 'Add to dictionary',
exitFullScreen: 'Exit full screen',
emoji: 'Emoji',
}
const getBrowserWindowFromWebContents = (webContents: Electron.WebContents) => {
return BrowserWindow.getAllWindows().find((win) => {
if (win.webContents === webContents) return true
let browserViews: Electron.BrowserView[]
if ('getBrowserViews' in win) {
browserViews = win.getBrowserViews()
} else if ('getBrowserView' in win) {
// @ts-ignore
browserViews = [win.getBrowserView()]
} else {
browserViews = []
}
return browserViews.some((view) => view.webContents === webContents)
})
}
type ChromeContextMenuLabels = typeof LABELS
interface ChromeContextMenuOptions {
/** Context menu parameters emitted from the WebContents 'context-menu' event. */
params: Electron.ContextMenuParams
/** WebContents which emitted the 'context-menu' event. */
webContents: Electron.WebContents
/** Handler for opening links. */
openLink: (
url: string,
disposition: 'default' | 'foreground-tab' | 'background-tab' | 'new-window',
params: Electron.ContextMenuParams
) => void
/** Chrome extension menu items. */
extensionMenuItems?: MenuItem[]
/** Labels used to create menu items. Replace this if localization is needed. */
labels?: ChromeContextMenuLabels
/**
* @deprecated Use 'labels' instead.
*/
strings?: ChromeContextMenuLabels
}
export const buildChromeContextMenu = (opts: ChromeContextMenuOptions): Menu => {
const { params, webContents, openLink, extensionMenuItems } = opts
const labels = opts.labels || opts.strings || LABELS
const menu = new Menu()
const append = (opts: Electron.MenuItemConstructorOptions) => menu.append(new MenuItem(opts))
const appendSeparator = () => menu.append(new MenuItem({ type: 'separator' }))
if (params.linkURL) {
append({
label: labels.openInNewTab('link'),
click: () => {
openLink(params.linkURL, 'default', params)
},
})
append({
label: labels.openInNewWindow('link'),
click: () => {
openLink(params.linkURL, 'new-window', params)
},
})
appendSeparator()
append({
label: labels.copyAddress('link'),
click: () => {
clipboard.writeText(params.linkURL)
},
})
appendSeparator()
} else if (params.mediaType !== 'none') {
// TODO: Loop, Show controls
append({
label: labels.openInNewTab(params.mediaType),
click: () => {
openLink(params.srcURL, 'default', params)
},
})
append({
label: labels.copyAddress(params.mediaType),
click: () => {
clipboard.writeText(params.srcURL)
},
})
appendSeparator()
}
if (params.isEditable) {
if (params.misspelledWord) {
for (const suggestion of params.dictionarySuggestions) {
append({
label: suggestion,
click: () => webContents.replaceMisspelling(suggestion),
})
}
if (params.dictionarySuggestions.length > 0) appendSeparator()
append({
label: labels.addToDictionary,
click: () => webContents.session.addWordToSpellCheckerDictionary(params.misspelledWord),
})
} else {
if (
app.isEmojiPanelSupported() &&
!['number', 'tel', 'other'].includes(params.inputFieldType)
) {
append({
label: labels.emoji,
click: () => app.showEmojiPanel(),
})
appendSeparator()
}
append({
label: labels.redo,
enabled: params.editFlags.canRedo,
click: () => webContents.redo(),
})
append({
label: labels.undo,
enabled: params.editFlags.canUndo,
click: () => webContents.undo(),
})
}
appendSeparator()
append({
label: labels.cut,
enabled: params.editFlags.canCut,
click: () => webContents.cut(),
})
append({
label: labels.copy,
enabled: params.editFlags.canCopy,
click: () => webContents.copy(),
})
append({
label: labels.paste,
enabled: params.editFlags.canPaste,
click: () => webContents.paste(),
})
append({
label: labels.delete,
enabled: params.editFlags.canDelete,
click: () => webContents.delete(),
})
appendSeparator()
if (params.editFlags.canSelectAll) {
append({
label: labels.selectAll,
click: () => webContents.selectAll(),
})
appendSeparator()
}
} else if (params.selectionText) {
append({
label: labels.copy,
click: () => {
clipboard.writeText(params.selectionText)
},
})
appendSeparator()
}
if (menu.items.length === 0) {
const browserWindow = getBrowserWindowFromWebContents(webContents)
// TODO: Electron needs a way to detect whether we're in HTML5 full screen.
// Also need to properly exit full screen in Blink rather than just exiting
// the Electron BrowserWindow.
if (browserWindow?.fullScreen) {
append({
label: labels.exitFullScreen,
click: () => browserWindow.setFullScreen(false),
})
appendSeparator()
}
append({
label: labels.back,
enabled: webContents.canGoBack(),
click: () => webContents.goBack(),
})
append({
label: labels.forward,
enabled: webContents.canGoForward(),
click: () => webContents.goForward(),
})
append({
label: labels.reload,
click: () => webContents.reload(),
})
appendSeparator()
}
if (extensionMenuItems) {
extensionMenuItems.forEach((item) => menu.append(item))
if (extensionMenuItems.length > 0) appendSeparator()
}
append({
label: labels.inspect,
click: () => {
webContents.inspectElement(params.x, params.y)
if (!webContents.isDevToolsFocused()) {
webContents.devToolsWebContents?.focus()
}
},
})
return menu
}
export default buildChromeContextMenu