This repository has been archived by the owner on Jul 21, 2024. It is now read-only.
forked from sindresorhus/electron-context-menu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.d.ts
439 lines (352 loc) · 13.2 KB
/
index.d.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
import {
BrowserWindow,
BrowserView,
ContextMenuParams,
MenuItemConstructorOptions,
Event as ElectronEvent,
WebContents
} from 'electron';
declare namespace contextMenu {
interface Labels {
/**
@default 'Learn Spelling'
*/
readonly learnSpelling?: string;
/**
The placeholder `{selection}` will be replaced by the currently selected text.
@default 'Look Up “{selection}”'
*/
readonly lookUpSelection?: string;
/**
@default 'Search with Google'
*/
readonly searchWithGoogle?: string;
/**
@default 'Search with DuckDuckGo'
*/
readonly searchWithDuckDuckGo?: string;
/**
@default 'Cut'
*/
readonly cut?: string;
/**
@default 'Copy'
*/
readonly copy?: string;
/**
@default 'Paste'
*/
readonly paste?: string;
/**
@default 'Select All'
*/
readonly selectAll?: string;
/**
@default 'Save Image'
*/
readonly saveImage?: string;
/**
@default 'Save Image As…'
*/
readonly saveImageAs?: string;
/**
@default 'Save Video'
*/
readonly saveVideo?: string;
/**
@default 'Save Video As…'
*/
readonly saveVideoAs?: string;
/**
@default 'Copy Link'
*/
readonly copyLink?: string;
/**
@default 'Save Link As…'
*/
readonly saveLinkAs?: string;
/**
@default 'Copy Image'
*/
readonly copyImage?: string;
/**
@default 'Copy Image Address'
*/
readonly copyImageAddress?: string;
/**
@default 'Copy Video Address'
*/
readonly copyVideoAddress?: string;
/**
@default 'Inspect Element'
*/
readonly inspect?: string;
/**
@default 'Services'
*/
readonly services?: string;
}
interface ActionOptions {
/**
Apply a transformation to the content of the action.
If you use this on `cut`, `copy`, or `paste`, they will convert rich text to plain text.
*/
readonly transform?: (content: string) => string;
}
interface Actions {
readonly separator: () => MenuItemConstructorOptions;
readonly learnSpelling: (options: ActionOptions) => MenuItemConstructorOptions;
readonly lookUpSelection: (options: ActionOptions) => MenuItemConstructorOptions;
readonly searchWithGoogle: (options: ActionOptions) => MenuItemConstructorOptions;
readonly searchWithDuckDuckGo: (options: ActionOptions) => MenuItemConstructorOptions;
readonly cut: (options: ActionOptions) => MenuItemConstructorOptions;
readonly copy: (options: ActionOptions) => MenuItemConstructorOptions;
readonly paste: (options: ActionOptions) => MenuItemConstructorOptions;
readonly selectAll: (options: ActionOptions) => MenuItemConstructorOptions;
readonly saveImage: (options: ActionOptions) => MenuItemConstructorOptions;
readonly saveImageAs: (options: ActionOptions) => MenuItemConstructorOptions;
readonly saveVideo: (options: ActionOptions) => MenuItemConstructorOptions;
readonly saveVideoAs: (options: ActionOptions) => MenuItemConstructorOptions;
readonly copyLink: (options: ActionOptions) => MenuItemConstructorOptions;
readonly copyImage: (options: ActionOptions) => MenuItemConstructorOptions;
readonly copyImageAddress: (options: ActionOptions) => MenuItemConstructorOptions;
readonly copyVideoAddress: (options: ActionOptions) => MenuItemConstructorOptions;
readonly inspect: () => MenuItemConstructorOptions;
readonly services: () => MenuItemConstructorOptions;
}
interface Options {
/**
Window or WebView to add the context menu to.
When not specified, the context menu will be added to all existing and new windows.
*/
readonly window?: BrowserWindow | BrowserView | Electron.WebviewTag | WebContents;
/**
Should return an array of [menu items](https://electronjs.org/docs/api/menu-item) to be prepended to the context menu.
`MenuItem` labels may contain the placeholder `{selection}` which will be replaced by the currently selected text as described in `options.labels`.
*/
readonly prepend?: (
defaultActions: Actions,
parameters: ContextMenuParams,
browserWindow: BrowserWindow | BrowserView | Electron.WebviewTag | WebContents,
event: ElectronEvent
) => MenuItemConstructorOptions[];
/**
Should return an array of [menu items](https://electronjs.org/docs/api/menu-item) to be appended to the context menu.
`MenuItem` labels may contain the placeholder `{selection}` which will be replaced by the currently selected text as described in `options.labels`.
*/
readonly append?: (
defaultActions: Actions,
parameters: ContextMenuParams,
browserWindow: BrowserWindow | BrowserView | Electron.WebviewTag | WebContents,
event: ElectronEvent
) => MenuItemConstructorOptions[];
/**
Show the `Learn Spelling {selection}` menu item when right-clicking text.
Even if `true`, the `spellcheck` preference in browser window must still be enabled. It will also only show when right-clicking misspelled words.
@default true
*/
readonly showLearnSpelling?: boolean;
/**
Show the `Look Up {selection}` menu item when right-clicking text.
@default true
*/
readonly showLookUpSelection?: boolean;
/**
Show the `Search with Google` menu item when right-clicking text.
@default true
*/
readonly showSearchWithGoogle?: boolean;
/**
Show the `Search with DuckDuckGo` menu item when right-clicking text.
@default false
*/
readonly showSearchWithDuckDuckGo?: boolean;
/**
Show the `Select All` menu item when right-clicking in a window.
Default: `false` on macOS, `true` on Windows and Linux
*/
readonly showSelectAll?: boolean;
/**
Show the `Copy Image` menu item when right-clicking on an image.
@default true
*/
readonly showCopyImage?: boolean;
/**
Show the `Copy Image Address` menu item when right-clicking on an image.
@default false
*/
readonly showCopyImageAddress?: boolean;
/**
Show the `Save Image` menu item when right-clicking on an image.
@default false
*/
readonly showSaveImage?: boolean;
/**
Show the `Save Image As…` menu item when right-clicking on an image.
@default false
*/
readonly showSaveImageAs?: boolean;
/**
Show the `Copy Video Address` menu item when right-clicking on a video.
@default false
*/
readonly showCopyVideoAddress?: boolean;
/**
Show the `Save Video` menu item when right-clicking on a video.
@default false
*/
readonly showSaveVideo?: boolean;
/**
Show the `Save Video As…` menu item when right-clicking on a video.
@default false
*/
readonly showSaveVideoAs?: boolean;
/**
Show the `Save Link As…` menu item when right-clicking on a link.
@default false
*/
readonly showSaveLinkAs?: boolean;
/**
Force enable or disable the `Inspect Element` menu item.
Default: [Only in development](https://github.com/sindresorhus/electron-is-dev)
*/
readonly showInspectElement?: boolean;
/**
Show the system `Services` submenu on macOS.
@default false
*/
readonly showServices?: boolean;
/**
Override labels for the default menu items. Useful for i18n.
The placeholder `{selection}` may be used in any label, and will be replaced by the currently selected text, trimmed to a maximum of 25 characters length. This is useful when localizing the `Look Up “{selection}”` menu item, but can also be used in custom menu items, for example, to implement a `Search Google for “{selection}”` menu item. If there is no selection, the `{selection}` placeholder will be replaced by an empty string. Normally this placeholder is only useful for menu items which will only be shown when there is text selected. This can be checked using `visible: parameters.selectionText.trim().length > 0` when implementing a custom menu item.
@default {}
@example
```
{
labels: {
copy: 'Configured Copy',
saveImageAs: 'Configured Save Image As…'
}
}
```
*/
readonly labels?: Labels;
/**
Determines whether or not to show the menu.
Can be useful if you for example have other code presenting a context menu in some contexts.
@example
```
{
// Doesn't show the menu if the element is editable
shouldShowMenu: (event, parameters) => !parameters.isEditable
}
```
*/
readonly shouldShowMenu?: (
event: ElectronEvent,
parameters: ContextMenuParams
) => boolean;
/**
This option lets you manually pick what menu items to include. It's meant for advanced needs. The default menu with the other options should be enough for most use-cases, and it ensures correct behavior, for example, correct order of menu items. So prefer the `append` and `prepend` option instead of `menu` whenever possible.
The function passed to this option is expected to return an array of [`MenuItem` constructor options](https://electronjs.org/docs/api/menu-item/).
The first argument the function receives is an array of default actions that can be used. These actions are functions that can take an object with a transform property (except for `separator` and `inspect`). The transform function will be passed the content of the action and can modify it if needed. If you use `transform` on `cut`, `copy`, or `paste`, they will convert rich text to plain text.
The second argument is [this `parameters` object](https://electronjs.org/docs/api/web-contents/#event-context-menu).
The third argument is the [BrowserWindow](https://electronjs.org/docs/api/browser-window/) the context menu was requested for.
The fourth argument is an Array of menu items for dictionary suggestions. This should be used if you wish to implement spellcheck in your custom menu.
The last argument is the event object passed to the `context-menu` event in web contents.
Even though you include an action, it will still only be shown/enabled when appropriate. For example, the `saveImage` action is only shown when right-clicking an image.
`MenuItem` labels may contain the placeholder `{selection}` which will be replaced by the currently selected text as described in `options.labels`.
The following options are ignored when `menu` is used:
- `showLearnSpelling`
- `showLookUpSelection`
- `showSearchWithGoogle`
- `showSelectAll`
- `showCopyImage`
- `showCopyImageAddress`
- `showSaveImageAs`
- `showCopyVideoAddress`
- `showSaveVideo`
- `showSaveVideoAs`
- `showSaveLinkAs`
- `showInspectElement`
- `showServices`
To get spellchecking, “Correct Automatically”, and “Learn Spelling” in the menu, please enable the `spellcheck` preference in browser window: `new BrowserWindow({webPreferences: {spellcheck: true}})`
@default [...dictionarySuggestions, defaultActions.separator(), defaultActions.separator(), defaultActions.learnSpelling(), defaultActions.separator(), defaultActions.lookUpSelection(), defaultActions.separator(),defaultActions.searchWithGoogle(), defaultActions.cut(), defaultActions.copy(), defaultActions.paste(), defaultActions.selectAll(), defaultActions.separator(), defaultActions.saveImage(), defaultActions.saveImageAs(), defaultActions.saveVideo(), defaultActions.saveVideoAs(), defaultActions.copyLink(), defaultActions.copyImage(), defaultActions.copyImageAddress(), defaultActions.separator(), defaultActions.copyLink(), defaultActions.saveLinkAs(), defaultActions.separator(), defaultActions.inspect()]
*/
readonly menu?: (
defaultActions: Actions,
parameters: ContextMenuParams,
browserWindow: BrowserWindow | BrowserView | Electron.WebviewTag | WebContents,
dictionarySuggestions: MenuItemConstructorOptions[],
event: ElectronEvent
) => MenuItemConstructorOptions[];
/**
Called when the context menu is shown.
*/
readonly onShow?: (event: ElectronEvent) => void;
/**
Called when the context menu is closed.
*/
readonly onClose?: (event: ElectronEvent) => void;
}
}
/**
This module gives you a nice extensible context menu with items like `Cut`/`Copy`/`Paste` for text, `Save Image` for images, and `Copy Link` for links. It also adds an `Inspect Element` menu item when in development to quickly view items in the inspector like in Chrome.
You can use this module directly in both the main and renderer process.
@example
```
import {app, BrowserWindow} = require('electron');
import contextMenu = require('electron-context-menu');
contextMenu({
showSaveImageAs: true
});
let mainWindow;
(async () => {
await app.whenReady();
mainWindow = new BrowserWindow({
webPreferences: {
spellcheck: true
}
});
})();
```
@example
```
import {app, BrowserWindow} = require('electron');
import contextMenu = require('electron-context-menu');
contextMenu({
prepend: (defaultActions, parameters, browserWindow) => [
{
label: 'Rainbow',
// Only show it when right-clicking images
visible: parameters.mediaType === 'image'
},
{
label: 'Search Google for “{selection}”',
// Only show it when right-clicking text
visible: parameters.selectionText.trim().length > 0,
click: () => {
shell.openExternal(`https://google.com/search?q=${encodeURIComponent(parameters.selectionText)}`);
}
}
]
});
let mainWindow;
(async () => {
await app.whenReady();
mainWindow = new BrowserWindow({
webPreferences: {
spellcheck: true
}
});
})();
```
The return value of `contextMenu()` is a function that disposes of the created event listeners:
@example
```
const dispose = contextMenu();
dispose();
```
*/
declare function contextMenu(options?: contextMenu.Options): () => void; // eslint-disable-line no-redeclare
export = contextMenu;