Skip to content

Commit

Permalink
Refactor TypeScript definition to CommonJS compatible export (#64)
Browse files Browse the repository at this point in the history
  • Loading branch information
BendingBender authored and sindresorhus committed Apr 9, 2019
1 parent b075c7f commit d447280
Show file tree
Hide file tree
Showing 4 changed files with 185 additions and 152 deletions.
323 changes: 177 additions & 146 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/// <reference lib="dom"/>
/// <reference types="node"/>
import {
BrowserWindow,
WebviewTag,
Expand All @@ -6,151 +8,180 @@ import {
Event as ElectronEvent
} from 'electron';

export interface Labels {
/**
* @default 'Cut'
*/
readonly cut?: string;

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

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

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

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

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

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

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

export interface ActionOptions {
/**
* Apply a transformation to the content of the action.
*/
readonly transform?: (content: string) => string;
}

export interface Actions {
readonly separator: () => MenuItem;
readonly inspect: () => MenuItem;
readonly cut: (options: ActionOptions) => MenuItem;
readonly copy: (options: ActionOptions) => MenuItem;
readonly paste: (options: ActionOptions) => MenuItem;
readonly saveImage: (options: ActionOptions) => MenuItem;
readonly saveImageAs: (options: ActionOptions) => MenuItem;
readonly copyImageAddress: (options: ActionOptions) => MenuItem;
}

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

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

/**
* Should return an array of [menu items](https://electronjs.org/docs/api/menu-item) to override the default context menu.
* @default [defaultActions.cut(), defaultActions.copy(), defaultActions.paste(), defaultActions.separator(), defaultActions.saveImage(), defaultActions.saveImageAs(), defaultActions.copyImageAddress(), defaultActions.separator(), defaultActions.copyLink(), defaultActions.separator(), defaultActions.inspect()]
*/
readonly menu?: (defaultActions: Actions, params: ContextMenuParams, browserWindow: BrowserWindow | WebviewTag) => MenuItem[];

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

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

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

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

/**
* Overwrite labels for the default menu items. Useful for i18n.
*
* @default {}
*/
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, params) => !params.isEditable
*/
readonly shouldShowMenu?: (event: ElectronEvent, params: ContextMenuParams) => boolean;
declare namespace contextMenu {
interface Labels {
/**
@default 'Cut'
*/
readonly cut?: string;

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

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

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

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

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

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

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

interface ActionOptions {
/**
Apply a transformation to the content of the action.
*/
readonly transform?: (content: string) => string;
}

interface Actions {
readonly separator: () => MenuItem;
readonly inspect: () => MenuItem;
readonly cut: (options: ActionOptions) => MenuItem;
readonly copy: (options: ActionOptions) => MenuItem;
readonly paste: (options: ActionOptions) => MenuItem;
readonly saveImage: (options: ActionOptions) => MenuItem;
readonly saveImageAs: (options: ActionOptions) => MenuItem;
readonly copyImageAddress: (options: ActionOptions) => MenuItem;
}

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 | WebviewTag;

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

/**
Should return an array of [menu items](https://electronjs.org/docs/api/menu-item) to override the default context menu.
@default [defaultActions.cut(), defaultActions.copy(), defaultActions.paste(), defaultActions.separator(), defaultActions.saveImage(), defaultActions.saveImageAs(), defaultActions.copyImageAddress(), defaultActions.separator(), defaultActions.copyLink(), defaultActions.separator(), defaultActions.inspect()]
*/
readonly menu?: (
defaultActions: Actions,
params: ContextMenuParams,
browserWindow: BrowserWindow | WebviewTag
) => MenuItem[];

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

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

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

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

/**
Overwrite labels for the default menu items. Useful for i18n.
@default {}
*/
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, params) => !params.isEditable
```
*/
readonly shouldShowMenu?: (
event: ElectronEvent,
params: 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;
declare const contextMenu: {
/**
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 = require('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();
});
```
*/
(options?: contextMenu.Options): void;

// TODO: Remove this for the next major release, refactor the whole definition to:
// declare function contextMenu(options?: contextMenu.Options): void;
// export = contextMenu;
default: typeof contextMenu;
};

export = contextMenu;
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,4 +217,5 @@ module.exports = (options = {}) => {
});
};

// TODO: Remove this for the next major release
module.exports.default = module.exports;
4 changes: 2 additions & 2 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {expectType} from 'tsd-check';
import contextMenu from '.';
import {expectType} from 'tsd';
import contextMenu = require('.');

expectType<void>(contextMenu());
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
},
"scripts": {
"start": "electron fixture.js",
"test": "xo && ava && tsd-check"
"test": "xo && ava && tsd"
},
"files": [
"index.js",
Expand All @@ -32,9 +32,10 @@
"electron-is-dev": "^1.0.1"
},
"devDependencies": {
"ava": "^1.2.0",
"electron": "^4.0.2",
"tsd-check": "^0.3.0",
"@types/node": "^11.13.0",
"ava": "^1.4.1",
"electron": "^4.1.4",
"tsd": "^0.7.2",
"xo": "^0.24.0"
},
"xo": {
Expand Down

0 comments on commit d447280

Please sign in to comment.