Skip to content

Commit d7bbff3

Browse files
CvXsindresorhus
authored andcommitted
Add TypeScript definition (#62)
1 parent 201fcf7 commit d7bbff3

File tree

5 files changed

+136
-3
lines changed

5 files changed

+136
-3
lines changed

index.d.ts

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
export interface Labels {
2+
/**
3+
* @default 'Cut'
4+
*/
5+
cut?: string;
6+
7+
/**
8+
* @default 'Copy'
9+
*/
10+
copy?: string;
11+
12+
/**
13+
* @default 'Paste'
14+
*/
15+
paste?: string;
16+
17+
/**
18+
* @default 'Save Image'
19+
*/
20+
save?: string;
21+
22+
/**
23+
* @default 'Save Image As…'
24+
*/
25+
saveImageAs?: string;
26+
27+
/**
28+
* @default 'Copy Link'
29+
*/
30+
copyLink?: string;
31+
32+
/**
33+
* @default 'Copy Image Address'
34+
*/
35+
copyImageAddress?: string;
36+
37+
/**
38+
* @default 'Inspect Element'
39+
*/
40+
inspect?: string;
41+
}
42+
43+
export interface Options {
44+
/**
45+
* Window or WebView to add the context menu to.
46+
* When not specified, the context menu will be added to all existing and new windows.
47+
*/
48+
window?: Electron.BrowserWindow | Electron.WebviewTag;
49+
50+
/**
51+
* Should return an array of [menu items](https://electronjs.org/docs/api/menu-item) to be prepended to the context menu.
52+
*/
53+
prepend?: (params: Electron.ContextMenuParams, browserWindow: Electron.BrowserWindow | Electron.WebviewTag) => Electron.MenuItem[];
54+
55+
/**
56+
* Should return an array of [menu items](https://electronjs.org/docs/api/menu-item) to be appended to the context menu.
57+
*/
58+
append?: (param: Electron.ContextMenuParams, browserWindow: Electron.BrowserWindow | Electron.WebviewTag) => Electron.MenuItem[];
59+
60+
/**
61+
* Show the `Copy Image Address` menu item when right-clicking on an image.
62+
*
63+
* @default false
64+
*/
65+
showCopyImageAddress?: boolean;
66+
67+
/**
68+
* Show the `Save Image As…` menu item when right-clicking on an image.
69+
*
70+
* @default false
71+
*/
72+
showSaveImageAs?: boolean;
73+
74+
/**
75+
* Force enable or disable the `Inspect Element` menu item.
76+
*
77+
* Default: [Only in development](https://github.com/sindresorhus/electron-is-dev)
78+
*/
79+
showInspectElement?: boolean;
80+
81+
/**
82+
* Overwrite labels for the default menu items. Useful for i18n.
83+
*
84+
* @default {}
85+
*/
86+
labels?: Labels;
87+
88+
/**
89+
* Determines whether or not to show the menu.
90+
* Can be useful if you for example have other code presenting a context menu in some contexts.
91+
*
92+
* @example
93+
*
94+
* // Doesn't show the menu if the element is editable
95+
* shouldShowMenu: (event, params) => !params.isEditable
96+
*/
97+
shouldShowMenu?: (event: Electron.Event, params: Electron.ContextMenuParams) => boolean;
98+
}
99+
100+
/**
101+
* 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.
102+
*
103+
* You can use this module directly in both the main and renderer process.
104+
*
105+
* @example
106+
*
107+
* import {app, BrowserWindow} from 'electron';
108+
* import contextMenu from 'electron-context-menu';
109+
*
110+
* contextMenu({
111+
* prepend: (params, browserWindow) => [{
112+
* label: 'Rainbow',
113+
* // Only show it when right-clicking images
114+
* visible: params.mediaType === 'image'
115+
* }]
116+
* });
117+
*
118+
* let win;
119+
* (async () => {
120+
* await app.whenReady();
121+
* win = new BrowserWindow();
122+
* });
123+
*/
124+
export default function contextMenu(options?: Options): void;

index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,3 +196,5 @@ module.exports = (options = {}) => {
196196
create(win, options);
197197
});
198198
};
199+
200+
module.exports.default = module.exports;

index.test-d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import {expectType} from 'tsd-check';
2+
import {BrowserWindow} from 'electron';
3+
import contextMenu from '.';
4+
5+
expectType<void>(contextMenu());

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@
1111
},
1212
"scripts": {
1313
"start": "electron fixture.js",
14-
"test": "xo && ava"
14+
"test": "xo && ava && tsd-check"
1515
},
1616
"files": [
17-
"index.js"
17+
"index.js",
18+
"index.d.ts"
1819
],
1920
"keywords": [
2021
"electron",
@@ -33,6 +34,7 @@
3334
"devDependencies": {
3435
"ava": "*",
3536
"electron": "^3.0.6",
37+
"tsd-check": "^0.3.0",
3638
"xo": "*"
3739
},
3840
"xo": {

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ Should return an array of [MenuItem](https://electronjs.org/docs/api/menu-item)'
6868

6969
Type: `Function`
7070

71-
Should return an array of [MenuItem](https://electronjs.org/docs/api/browser-window)'s to be appended to the context menu. The first argument is [this `params` object](https://electronjs.org/docs/api/browser-window). The second argument is the [BrowserWindow](https://electronjs.org/docs/api/browser-window) the context menu was requested for.
71+
Should return an array of [MenuItem](https://electronjs.org/docs/api/menu-item)'s to be appended to the context menu. The first argument is [this `params` object](https://electronjs.org/docs/api/web-contents#event-context-menu). The second argument is the [BrowserWindow](https://electronjs.org/docs/api/browser-window) the context menu was requested for.
7272

7373
#### showCopyImageAddress
7474

0 commit comments

Comments
 (0)