From d447280b037ba5417c8c9f6cd96a22947fec4252 Mon Sep 17 00:00:00 2001 From: Dimitri Benin Date: Tue, 9 Apr 2019 17:33:54 +0000 Subject: [PATCH] Refactor TypeScript definition to CommonJS compatible export (#64) --- index.d.ts | 323 ++++++++++++++++++++++++++---------------------- index.js | 1 + index.test-d.ts | 4 +- package.json | 9 +- 4 files changed, 185 insertions(+), 152 deletions(-) diff --git a/index.d.ts b/index.d.ts index 627e5d4..1993e60 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,3 +1,5 @@ +/// +/// import { BrowserWindow, WebviewTag, @@ -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; diff --git a/index.js b/index.js index bc453f1..fff7a28 100644 --- a/index.js +++ b/index.js @@ -217,4 +217,5 @@ module.exports = (options = {}) => { }); }; +// TODO: Remove this for the next major release module.exports.default = module.exports; diff --git a/index.test-d.ts b/index.test-d.ts index bd83b58..8184b7f 100644 --- a/index.test-d.ts +++ b/index.test-d.ts @@ -1,4 +1,4 @@ -import {expectType} from 'tsd-check'; -import contextMenu from '.'; +import {expectType} from 'tsd'; +import contextMenu = require('.'); expectType(contextMenu()); diff --git a/package.json b/package.json index 34bc956..74fdd7f 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,7 @@ }, "scripts": { "start": "electron fixture.js", - "test": "xo && ava && tsd-check" + "test": "xo && ava && tsd" }, "files": [ "index.js", @@ -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": {