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

Expose event parameter #140

Merged
merged 2 commits into from
May 17, 2021
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
12 changes: 8 additions & 4 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ declare namespace contextMenu {
readonly prepend?: (
defaultActions: Actions,
parameters: ContextMenuParams,
browserWindow: BrowserWindow | BrowserView | WebviewTag | WebContents
browserWindow: BrowserWindow | BrowserView | WebviewTag | WebContents,
event: ElectronEvent
) => MenuItemConstructorOptions[];

/**
Expand All @@ -134,7 +135,8 @@ declare namespace contextMenu {
readonly append?: (
defaultActions: Actions,
parameters: ContextMenuParams,
browserWindow: BrowserWindow | BrowserView | WebviewTag | WebContents
browserWindow: BrowserWindow | BrowserView | WebviewTag | WebContents,
event: ElectronEvent
) => MenuItemConstructorOptions[];

/**
Expand Down Expand Up @@ -244,7 +246,8 @@ declare namespace contextMenu {
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` and `inspect`). 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.
The second argument is [this `parameters` object](https://electronjs.org/docs/api/web-contents/#event-context-menu).
The third argument is the [BrowserWindow](https://electronjs.org/docs/api/browser-window/) the context menu was requested for.
The last argument is an Array of menu items for dictionary suggestions. This should be used if you wish to implement spellcheck in your custom menu.
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.
The last argument is the event object passed to the `context-menu` event in web contents.

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.

Expand All @@ -269,7 +272,8 @@ declare namespace contextMenu {
defaultActions: Actions,
parameters: ContextMenuParams,
browserWindow: BrowserWindow | BrowserView | WebviewTag | WebContents,
dictionarySuggestions: MenuItemConstructorOptions[]
dictionarySuggestions: MenuItemConstructorOptions[],
event: ElectronEvent
) => MenuItemConstructorOptions[];
}
}
Expand Down
6 changes: 3 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -255,19 +255,19 @@ const create = (win, options) => {
];

if (options.menu) {
menuTemplate = options.menu(defaultActions, props, win, dictionarySuggestions);
menuTemplate = options.menu(defaultActions, props, win, dictionarySuggestions, event);
}

if (options.prepend) {
const result = options.prepend(defaultActions, props, win);
const result = options.prepend(defaultActions, props, win, event);

if (Array.isArray(result)) {
menuTemplate.unshift(...result);
}
}

if (options.append) {
const result = options.append(defaultActions, props, win);
const result = options.append(defaultActions, props, win, event);

if (Array.isArray(result)) {
menuTemplate.push(...result);
Expand Down
6 changes: 3 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ Type: `Function`

Should return an array of [MenuItem](https://electronjs.org/docs/api/menu-item/)'s to be prepended to the context menu.

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 [BrowserWindow](https://electronjs.org/docs/api/browser-window/) the context menu was requested for.
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 [BrowserWindow](https://electronjs.org/docs/api/browser-window/) the context menu was requested for. The fourth argument is the context menu event.

`MenuItem` labels may contain the placeholder `{selection}` which will be replaced by the currently selected text as described in [`options.labels`](#labels).

Expand All @@ -94,7 +94,7 @@ Type: `Function`

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 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 [BrowserWindow](https://electronjs.org/docs/api/browser-window/) the context menu was requested for.
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 [BrowserWindow](https://electronjs.org/docs/api/browser-window/) the context menu was requested for. The fourth argument is the context menu event.

`MenuItem` labels may contain the placeholder `{selection}` which will be replaced by the currently selected text as described in [`options.labels`](#labels).

Expand Down Expand Up @@ -207,7 +207,7 @@ Type: `Function`

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.

The function passed to this option is expected to return [`MenuItem[]`](https://electronjs.org/docs/api/menu-item/). 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` and `inspect`). 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. The second argument is [this `parameters` object](https://electronjs.org/docs/api/web-contents/#event-context-menu). The third argument is the [BrowserWindow](https://electronjs.org/docs/api/browser-window/) the context menu was requested for. The last argument is an Array of menu items for dictionary suggestions. This should be used if you wish to implement spellcheck in your custom menu.
The function passed to this option is expected to return [`MenuItem[]`](https://electronjs.org/docs/api/menu-item/). 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` and `inspect`). 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. The second argument is [this `parameters` object](https://electronjs.org/docs/api/web-contents/#event-context-menu). The third argument is the [BrowserWindow](https://electronjs.org/docs/api/browser-window/) the context menu was requested for. 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. The last argument is the context menu event.

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.

Expand Down