Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add TypeScript definition #62

Merged
merged 6 commits into from Jan 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
124 changes: 124 additions & 0 deletions index.d.ts
@@ -0,0 +1,124 @@
export interface Labels {
/**
* @default 'Cut'
*/
cut?: string;

/**
* @default 'Copy'
*/
copy?: string;

/**
* @default 'Paste'
*/
paste?: string;

/**
* @default 'Save Image'
*/
save?: string;

/**
* @default 'Save Image As…'
*/
saveImageAs?: string;

/**
* @default 'Copy Link'
*/
copyLink?: string;

/**
* @default 'Copy Image Address'
*/
copyImageAddress?: string;

/**
* @default 'Inspect Element'
*/
inspect?: string;
}

export 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.
*/
window?: Electron.BrowserWindow | Electron.WebviewTag;

/**
* Should return an array of [menu items](https://electronjs.org/docs/api/menu-item) to be prepended to the context menu.
*/
prepend?: (params: Electron.ContextMenuParams, browserWindow: Electron.BrowserWindow | Electron.WebviewTag) => Electron.MenuItem[];

/**
* Should return an array of [menu items](https://electronjs.org/docs/api/menu-item) to be appended to the context menu.
*/
append?: (param: Electron.ContextMenuParams, browserWindow: Electron.BrowserWindow | Electron.WebviewTag) => Electron.MenuItem[];

/**
* Show the `Copy Image Address` menu item when right-clicking on an image.
*
* @default false
*/
showCopyImageAddress?: boolean;

/**
* Show the `Save Image As…` menu item when right-clicking on an image.
*
* @default false
*/
showSaveImageAs?: boolean;

/**
* Force enable or disable the `Inspect Element` menu item.
*
* Default: [Only in development](https://github.com/sindresorhus/electron-is-dev)
*/
showInspectElement?: boolean;

/**
* Overwrite labels for the default menu items. Useful for i18n.
*
* @default {}
*/
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, params) => !params.isEditable
*/
shouldShowMenu?: (event: Electron.Event, params: Electron.ContextMenuParams) => boolean;
}

/**
* 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} from 'electron';
* import contextMenu from 'electron-context-menu';
*
* contextMenu({
* prepend: (params, browserWindow) => [{
* label: 'Rainbow',
* // Only show it when right-clicking images
* visible: params.mediaType === 'image'
* }]
* });
*
* let win;
* (async () => {
* await app.whenReady();
* win = new BrowserWindow();
* });
*/
export default function contextMenu(options?: Options): void;
2 changes: 2 additions & 0 deletions index.js
Expand Up @@ -196,3 +196,5 @@ module.exports = (options = {}) => {
create(win, options);
});
};

module.exports.default = module.exports;
5 changes: 5 additions & 0 deletions index.test-d.ts
@@ -0,0 +1,5 @@
import {expectType} from 'tsd-check';
import {BrowserWindow} from 'electron';
import contextMenu from '.';

expectType<void>(contextMenu());
6 changes: 4 additions & 2 deletions package.json
Expand Up @@ -11,10 +11,11 @@
},
"scripts": {
"start": "electron fixture.js",
"test": "xo && ava"
"test": "xo && ava && tsd-check"
},
"files": [
"index.js"
"index.js",
"index.d.ts"
],
"keywords": [
"electron",
Expand All @@ -33,6 +34,7 @@
"devDependencies": {
"ava": "*",
"electron": "^3.0.6",
"tsd-check": "^0.3.0",
"xo": "*"
},
"xo": {
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Expand Up @@ -68,7 +68,7 @@ Should return an array of [MenuItem](https://electronjs.org/docs/api/menu-item)'

Type: `Function`

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.
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.

#### showCopyImageAddress

Expand Down