@ohos-ports-electron/electron-context-menu 0.0.0-stage → 5.1.0-electron-oh.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.d.ts ADDED
@@ -0,0 +1,598 @@
1
+ import {
2
+ type BrowserWindow,
3
+ type BrowserView,
4
+ type ContextMenuParams,
5
+ type MenuItemConstructorOptions,
6
+ type Event as ElectronEvent,
7
+ type WebContents,
8
+ type WebContentsView,
9
+ } from 'electron';
10
+
11
+ export type Labels = {
12
+ /**
13
+ @default 'Learn Spelling'
14
+ */
15
+ readonly learnSpelling?: string;
16
+
17
+ /**
18
+ The placeholder `{selection}` will be replaced by the currently selected text.
19
+ @default 'Look Up “{selection}”'
20
+ */
21
+ readonly lookUpSelection?: string;
22
+
23
+ /**
24
+ @default 'Search with Google'
25
+ */
26
+ readonly searchWithGoogle?: string;
27
+
28
+ /**
29
+ Shown in place of the dictionary suggestions when there are none.
30
+
31
+ @default 'No Guesses Found'
32
+ */
33
+ readonly dictionarySuggestions?: string;
34
+
35
+ /**
36
+ @default 'Cut'
37
+ */
38
+ readonly cut?: string;
39
+
40
+ /**
41
+ @default 'Copy'
42
+ */
43
+ readonly copy?: string;
44
+
45
+ /**
46
+ @default 'Paste'
47
+ */
48
+ readonly paste?: string;
49
+
50
+ /**
51
+ @default 'Paste and Match Style'
52
+ */
53
+ readonly pasteAndMatchStyle?: string;
54
+
55
+ /**
56
+ @default 'Select All'
57
+ */
58
+ readonly selectAll?: string;
59
+
60
+ /**
61
+ @default 'Save Image'
62
+ */
63
+ readonly saveImage?: string;
64
+
65
+ /**
66
+ @default 'Save Image As…'
67
+ */
68
+ readonly saveImageAs?: string;
69
+
70
+ /**
71
+ @default 'Save Video'
72
+ */
73
+ readonly saveVideo?: string;
74
+
75
+ /**
76
+ @default 'Save Video As…'
77
+ */
78
+ readonly saveVideoAs?: string;
79
+
80
+ /**
81
+ @default 'Copy Link'
82
+ */
83
+ readonly copyLink?: string;
84
+
85
+ /**
86
+ @default 'Save Link As…'
87
+ */
88
+ readonly saveLinkAs?: string;
89
+
90
+ /**
91
+ @default 'Copy Image'
92
+ */
93
+ readonly copyImage?: string;
94
+
95
+ /**
96
+ @default 'Copy Image Address'
97
+ */
98
+ readonly copyImageAddress?: string;
99
+
100
+ /**
101
+ @default 'Copy Video Address'
102
+ */
103
+ readonly copyVideoAddress?: string;
104
+
105
+ /**
106
+ @default 'Copy Video Frame'
107
+ */
108
+ readonly copyVideoFrame?: string;
109
+
110
+ /**
111
+ @default 'Save Video Frame As…'
112
+ */
113
+ readonly saveVideoFrameAs?: string;
114
+
115
+ /**
116
+ @default 'Picture in Picture'
117
+ */
118
+ readonly pictureInPicture?: string;
119
+
120
+ /**
121
+ @default 'Inspect Element'
122
+ */
123
+ readonly inspect?: string;
124
+
125
+ /**
126
+ @default 'Services'
127
+ */
128
+ readonly services?: string;
129
+ };
130
+
131
+ export type ActionOptions = {
132
+ /**
133
+ Apply a transformation to the content of the action.
134
+
135
+ If you use this on `cut`, `copy`, or `paste`, they will convert rich text to plain text.
136
+ */
137
+ readonly transform?: (content: string) => string;
138
+ };
139
+
140
+ export type Actions = {
141
+ readonly separator: () => MenuItemConstructorOptions;
142
+ readonly learnSpelling: (options?: ActionOptions) => MenuItemConstructorOptions;
143
+ readonly lookUpSelection: (options?: ActionOptions) => MenuItemConstructorOptions;
144
+ readonly searchWithGoogle: (options?: ActionOptions) => MenuItemConstructorOptions;
145
+ readonly search: (options?: ActionOptions) => MenuItemConstructorOptions;
146
+ readonly cut: (options?: ActionOptions) => MenuItemConstructorOptions;
147
+ readonly copy: (options?: ActionOptions) => MenuItemConstructorOptions;
148
+ readonly paste: (options?: ActionOptions) => MenuItemConstructorOptions;
149
+ readonly pasteAndMatchStyle: (options?: ActionOptions) => MenuItemConstructorOptions;
150
+ readonly selectAll: (options?: ActionOptions) => MenuItemConstructorOptions;
151
+ readonly saveImage: (options?: ActionOptions) => MenuItemConstructorOptions;
152
+ readonly saveImageAs: (options?: ActionOptions) => MenuItemConstructorOptions;
153
+ readonly saveVideo: (options?: ActionOptions) => MenuItemConstructorOptions;
154
+ readonly saveVideoAs: (options?: ActionOptions) => MenuItemConstructorOptions;
155
+ readonly copyLink: (options?: ActionOptions) => MenuItemConstructorOptions;
156
+ readonly saveLinkAs: (options?: ActionOptions) => MenuItemConstructorOptions;
157
+ readonly copyImage: (options?: ActionOptions) => MenuItemConstructorOptions;
158
+ readonly copyImageAddress: (options?: ActionOptions) => MenuItemConstructorOptions;
159
+ readonly copyVideoAddress: (options?: ActionOptions) => MenuItemConstructorOptions;
160
+ readonly copyVideoFrame: (options?: ActionOptions) => MenuItemConstructorOptions;
161
+ readonly saveVideoFrameAs: (options?: ActionOptions) => MenuItemConstructorOptions;
162
+ readonly pictureInPicture: (options?: ActionOptions) => MenuItemConstructorOptions;
163
+ readonly inspect: () => MenuItemConstructorOptions;
164
+ readonly services: () => MenuItemConstructorOptions;
165
+ };
166
+
167
+ export type Options = {
168
+ /**
169
+ Window or view to add the context menu to.
170
+
171
+ To attach it to a `<webview>`, pass its `WebContents`, which you can get in the main process with `webContents.fromId(...)` using the id from `webview.getWebContentsId()`.
172
+
173
+ When not specified, the context menu will be added to all existing and new windows.
174
+ */
175
+ readonly window?: BrowserWindow | BrowserView | WebContents | WebContentsView;
176
+
177
+ /**
178
+ Should return an array of [menu items](https://electronjs.org/docs/api/menu-item) to be prepended to the context menu.
179
+
180
+ The first argument is an array of default actions that can be used. The second argument is [this `parameters` object](https://electronjs.org/docs/api/web-contents/#event-context-menu). The third argument is the window or view the context menu was requested for. The fourth argument is the context menu event.
181
+
182
+ A menu item with an `id` matching one of the default action names (for example `copy`) replaces that default action, and stays where `prepend` put it. The default action is replaced even when the custom item is not visible.
183
+
184
+ `MenuItem` labels may contain the placeholder `{selection}` which will be replaced by the currently selected text as described in `options.labels`.
185
+ */
186
+ readonly prepend?: (
187
+ defaultActions: Actions,
188
+ parameters: ContextMenuParams,
189
+ browserWindow: BrowserWindow | BrowserView | WebContents | WebContentsView,
190
+ event: ElectronEvent,
191
+ ) => MenuItemConstructorOptions[];
192
+
193
+ /**
194
+ Should return an array of [menu items](https://electronjs.org/docs/api/menu-item) to be appended to the context menu.
195
+
196
+ The first argument is an array of default actions that can be used. The second argument is [this `parameters` object](https://electronjs.org/docs/api/web-contents/#event-context-menu). The third argument is the window or view the context menu was requested for. The fourth argument is the context menu event.
197
+
198
+ A menu item with an `id` matching one of the default action names (for example `copy`) replaces that default action, and stays where `append` put it. The default action is replaced even when the custom item is not visible.
199
+
200
+ `MenuItem` labels may contain the placeholder `{selection}` which will be replaced by the currently selected text as described in `options.labels`.
201
+ */
202
+ readonly append?: (
203
+ defaultActions: Actions,
204
+ parameters: ContextMenuParams,
205
+ browserWindow: BrowserWindow | BrowserView | WebContents | WebContentsView,
206
+ event: ElectronEvent,
207
+ ) => MenuItemConstructorOptions[];
208
+
209
+ /**
210
+ Show the `Learn Spelling {selection}` menu item when right-clicking text.
211
+
212
+ The spellcheck will only show when right-clicking misspelled words.
213
+
214
+ @default true
215
+ */
216
+ readonly showLearnSpelling?: boolean;
217
+
218
+ /**
219
+ Show the `Look Up {selection}` menu item when right-clicking text.
220
+
221
+ @default true
222
+ */
223
+ readonly showLookUpSelection?: boolean;
224
+
225
+ /**
226
+ Show the `Search with Google` menu item when right-clicking text.
227
+
228
+ @default true
229
+ */
230
+ readonly showSearchWithGoogle?: boolean;
231
+
232
+ /**
233
+ Add a search menu item when right-clicking text.
234
+
235
+ Set `showSearchWithGoogle` to `false` to use this instead of the built-in `Search with Google` item.
236
+
237
+ When using `menu`, add the item yourself with `defaultActions.search()`.
238
+
239
+ The `url` must contain `%s`, which is replaced by the search query, [like in Chrome](https://support.google.com/chrome/answer/95426?hl=en&co=GENIE.Platform%3DDesktop).
240
+
241
+ `MenuItem` labels may contain the `{selection}` placeholder as described in `options.labels`.
242
+
243
+ @example
244
+ ```
245
+ {
246
+ search: {
247
+ label: 'Search with DuckDuckGo',
248
+ url: 'https://duckduckgo.com/?q=%s'
249
+ },
250
+ showSearchWithGoogle: false
251
+ }
252
+ ```
253
+ */
254
+ readonly search?: {
255
+ /**
256
+ The label of the menu item.
257
+ */
258
+ readonly label: string;
259
+
260
+ /**
261
+ The URL to open, with `%s` as the placeholder for the search query.
262
+ */
263
+ readonly url: string;
264
+ };
265
+
266
+ /**
267
+ Show the dictionary suggestions when right-clicking a misspelled word.
268
+
269
+ @default true
270
+ */
271
+ readonly showDictionarySuggestions?: boolean;
272
+
273
+ /**
274
+ Show the `Select All` menu item when right-clicking in a window.
275
+
276
+ Default: `false` on macOS, `true` on Windows and Linux
277
+ */
278
+ readonly showSelectAll?: boolean;
279
+
280
+ /**
281
+ Show the `Paste and Match Style` menu item when right-clicking in an editable field.
282
+
283
+ @default false
284
+ */
285
+ readonly showPasteAndMatchStyle?: boolean;
286
+
287
+ /**
288
+ Show the `Copy Image` menu item when right-clicking on an image or a `<canvas>`.
289
+
290
+ @default true
291
+ */
292
+ readonly showCopyImage?: boolean;
293
+
294
+ /**
295
+ Show the `Copy Image Address` menu item when right-clicking on an image.
296
+
297
+ Hidden for `file://` URLs by default. See `showFileUrlItems`.
298
+
299
+ @default false
300
+ */
301
+ readonly showCopyImageAddress?: boolean;
302
+
303
+ /**
304
+ Show the `Save Image` menu item when right-clicking on an image.
305
+
306
+ @default false
307
+ */
308
+ readonly showSaveImage?: boolean;
309
+
310
+ /**
311
+ Show the `Save Image As…` menu item when right-clicking on an image.
312
+
313
+ A `<canvas>` cannot be saved, as it has no URL to download.
314
+
315
+ @default false
316
+ */
317
+ readonly showSaveImageAs?: boolean;
318
+
319
+ /**
320
+ Show the `Copy Video Address` menu item when right-clicking on a video.
321
+
322
+ Hidden for `file://` URLs by default. See `showFileUrlItems`.
323
+
324
+ @default false
325
+ */
326
+ readonly showCopyVideoAddress?: boolean;
327
+
328
+ /**
329
+ Show the `Save Video` menu item when right-clicking on a video.
330
+
331
+ @default false
332
+ */
333
+ readonly showSaveVideo?: boolean;
334
+
335
+ /**
336
+ Show the `Save Video As…` menu item when right-clicking on a video.
337
+
338
+ @default false
339
+ */
340
+ readonly showSaveVideoAs?: boolean;
341
+
342
+ /**
343
+ Show the `Copy Video Frame` menu item when right-clicking on a video.
344
+
345
+ Copies the video frame at the click position to the clipboard.
346
+
347
+ @default false
348
+ */
349
+ readonly showCopyVideoFrame?: boolean;
350
+
351
+ /**
352
+ Show the `Save Video Frame As…` menu item when right-clicking on a video.
353
+
354
+ Shows a save dialog for the video frame at the click position.
355
+
356
+ @default false
357
+ */
358
+ readonly showSaveVideoFrameAs?: boolean;
359
+
360
+ /**
361
+ Show the `Picture in Picture` menu item when right-clicking on a video.
362
+
363
+ Toggles [Picture-in-Picture](https://developer.mozilla.org/en-US/docs/Web/API/Picture-in-Picture_API) for the video.
364
+
365
+ @default false
366
+ */
367
+ readonly showPictureInPicture?: boolean;
368
+
369
+ /**
370
+ Show the menu items for `file://` URLs when right-clicking on a link, image, or video.
371
+
372
+ This controls `Copy Link`, `Save Link As…`, `Copy Image Address`, and `Copy Video Address`. You rarely want to expose `file://` URLs to the user.
373
+
374
+ These items used to be shown unconditionally. Set this to `true` to keep that behavior.
375
+
376
+ @default false
377
+ */
378
+ readonly showFileUrlItems?: boolean;
379
+
380
+ /**
381
+ Show the `Copy Link` menu item when right-clicking on a link.
382
+
383
+ Hidden for `file://` URLs by default. See `showFileUrlItems`.
384
+
385
+ @default true
386
+ */
387
+ readonly showCopyLink?: boolean;
388
+
389
+ /**
390
+ Show the `Save Link As…` menu item when right-clicking on a link.
391
+
392
+ Hidden for `file://` URLs by default. See `showFileUrlItems`.
393
+
394
+ @default false
395
+ */
396
+ readonly showSaveLinkAs?: boolean;
397
+
398
+ /**
399
+ Force enable or disable the `Inspect Element` menu item.
400
+
401
+ Default: [Only in development](https://github.com/sindresorhus/electron-is-dev)
402
+ */
403
+ readonly showInspectElement?: boolean;
404
+
405
+ /**
406
+ Show the system `Services` submenu when right-clicking text on macOS.
407
+
408
+ Note: Due to [a bug in the Electron implementation](https://github.com/electron/electron/issues/18476), this menu is not identical to the “Services” submenu in the context menus of native apps. Instead, it looks the same as the “Services” menu in the main App Menu. For this reason, it is currently disabled by default.
409
+
410
+ @default false
411
+ */
412
+ readonly showServices?: boolean;
413
+
414
+ /**
415
+ Override labels for the default menu items. Useful for i18n.
416
+
417
+ 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, as shown in the usage example above.
418
+
419
+ @default {}
420
+
421
+ @example
422
+ ```
423
+ {
424
+ labels: {
425
+ copy: 'Copiar',
426
+ saveImageAs: 'Guardar imagen como…',
427
+ lookUpSelection: 'Consultar “{selection}”',
428
+ dictionarySuggestions: 'No se encontraron sugerencias'
429
+ }
430
+ }
431
+ ```
432
+ */
433
+ readonly labels?: Labels;
434
+
435
+ /**
436
+ Determines whether or not to show the menu.
437
+ Can be useful if you for example have other code presenting a context menu in some contexts.
438
+
439
+ The first argument is the context menu event. The second argument is [this `parameters` object](https://electronjs.org/docs/api/web-contents/#event-context-menu).
440
+
441
+ @example
442
+ ```
443
+ {
444
+ // Doesn't show the menu if the element is editable
445
+ shouldShowMenu: (event, parameters) => !parameters.isEditable
446
+ }
447
+ ```
448
+ */
449
+ readonly shouldShowMenu?: (
450
+ event: ElectronEvent,
451
+ parameters: ContextMenuParams,
452
+ ) => boolean;
453
+
454
+ /**
455
+ 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.
456
+
457
+ The function passed to this option is expected to return an array of [`MenuItem` constructor options](https://electronjs.org/docs/api/menu-item/). If it returns anything else, the default menu is used.
458
+
459
+ 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`, `inspect`, and `services`). 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.
460
+ The second argument is [this `parameters` object](https://electronjs.org/docs/api/web-contents/#event-context-menu).
461
+ The third argument is the window or view the context menu was requested for.
462
+ 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.
463
+ The last argument is the context menu event.
464
+
465
+ 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.
466
+
467
+ `MenuItem` labels may contain the placeholder `{selection}` which will be replaced by the currently selected text as described in `options.labels`.
468
+
469
+ The following options are ignored when `menu` is used:
470
+
471
+ - `showLearnSpelling`
472
+ - `showLookUpSelection`
473
+ - `showSearchWithGoogle`
474
+ - `showDictionarySuggestions`
475
+ - `showSelectAll`
476
+ - `showPasteAndMatchStyle`
477
+ - `showCopyImage`
478
+ - `showCopyImageAddress`
479
+ - `showSaveImage`
480
+ - `showSaveImageAs`
481
+ - `showCopyVideoAddress`
482
+ - `showSaveVideo`
483
+ - `showSaveVideoAs`
484
+ - `showCopyVideoFrame`
485
+ - `showSaveVideoFrameAs`
486
+ - `showPictureInPicture`
487
+ - `showCopyLink`
488
+ - `showSaveLinkAs`
489
+ - `showInspectElement`
490
+ - `showServices`
491
+
492
+ To get spellchecking, “Correct Automatically”, and “Learn Spelling” in the menu, make sure you have not disabled the `spellcheck` option (it's `true` by default) in `BrowserWindow`.
493
+
494
+ @default [defaultActions.separator(), ...dictionarySuggestions, defaultActions.separator(), defaultActions.learnSpelling(), defaultActions.separator(), defaultActions.lookUpSelection(), defaultActions.separator(), defaultActions.searchWithGoogle(), defaultActions.search(), defaultActions.separator(), defaultActions.cut(), defaultActions.copy(), defaultActions.paste(), defaultActions.pasteAndMatchStyle(), defaultActions.selectAll(), defaultActions.separator(), defaultActions.saveImage(), defaultActions.saveImageAs(), defaultActions.copyImage(), defaultActions.copyImageAddress(), defaultActions.saveVideo(), defaultActions.saveVideoAs(), defaultActions.copyVideoAddress(), defaultActions.copyVideoFrame(), defaultActions.saveVideoFrameAs(), defaultActions.pictureInPicture(), defaultActions.separator(), defaultActions.copyLink(), defaultActions.saveLinkAs(), defaultActions.separator(), defaultActions.inspect(), defaultActions.services(), defaultActions.separator()]
495
+ */
496
+ readonly menu?: (
497
+ defaultActions: Actions,
498
+ parameters: ContextMenuParams,
499
+ browserWindow: BrowserWindow | BrowserView | WebContents | WebContentsView,
500
+ dictionarySuggestions: MenuItemConstructorOptions[],
501
+ event: ElectronEvent,
502
+ ) => MenuItemConstructorOptions[];
503
+
504
+ /**
505
+ Called with the final menu template just before the menu is shown, so you can modify it.
506
+
507
+ Return a new template to replace the one passed in, or modify the array in place. Any other return value is ignored.
508
+
509
+ Menu items added here do not get the `labels` overrides or the `{selection}` placeholder replaced. Use `prepend` or `append` to add items.
510
+
511
+ @example
512
+ ```
513
+ {
514
+ // Removes the `Copy Image Address` menu item
515
+ updateMenu: menuTemplate => menuTemplate.filter(menuItem => menuItem.id !== 'copyImageAddress')
516
+ }
517
+ ```
518
+ */
519
+ readonly updateMenu?: (menuTemplate: MenuItemConstructorOptions[]) => MenuItemConstructorOptions[] | void;
520
+
521
+ /**
522
+ Called when the context menu is shown.
523
+
524
+ The function receives the [Electron `Event` object](https://electronjs.org/docs/api/structures/event).
525
+ */
526
+ readonly onShow?: (event: ElectronEvent) => void;
527
+
528
+ /**
529
+ Called when the context menu is closed.
530
+
531
+ The function receives the [Electron `Event` object](https://electronjs.org/docs/api/structures/event).
532
+ */
533
+ readonly onClose?: (event: ElectronEvent) => void;
534
+ };
535
+
536
+ /**
537
+ 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.
538
+
539
+ This package can only be used in the main process.
540
+
541
+ @example
542
+ ```
543
+ import {app, BrowserWindow} from 'electron';
544
+ import contextMenu from 'electron-context-menu';
545
+
546
+ contextMenu({
547
+ showSaveImageAs: true
548
+ });
549
+
550
+ let mainWindow;
551
+ (async () => {
552
+ await app.whenReady();
553
+
554
+ mainWindow = new BrowserWindow();
555
+ })();
556
+ ```
557
+
558
+ @example
559
+ ```
560
+ import {app, BrowserWindow, shell} from 'electron';
561
+ import contextMenu from 'electron-context-menu';
562
+
563
+ contextMenu({
564
+ prepend: (defaultActions, parameters, browserWindow) => [
565
+ {
566
+ label: 'Rainbow',
567
+ // Only show it when right-clicking images
568
+ visible: parameters.mediaType === 'image'
569
+ },
570
+ {
571
+ label: 'Search Google for “{selection}”',
572
+ // Only show it when right-clicking text
573
+ visible: parameters.selectionText.trim().length > 0,
574
+ click: () => {
575
+ shell.openExternal(`https://google.com/search?q=${encodeURIComponent(parameters.selectionText)}`);
576
+ }
577
+ }
578
+ ]
579
+ });
580
+
581
+ let mainWindow;
582
+ (async () => {
583
+ await app.whenReady();
584
+
585
+ mainWindow = new BrowserWindow();
586
+ })();
587
+ ```
588
+
589
+ The return value of `contextMenu()` is a function that disposes of the created event listeners:
590
+
591
+ @example
592
+ ```
593
+ const dispose = contextMenu();
594
+
595
+ dispose();
596
+ ```
597
+ */
598
+ export default function contextMenu(options?: Options): () => void;