Skip to content

Commit

Permalink
Fix Electron 12 compatibility (#139)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
  • Loading branch information
dusansimic and sindresorhus committed May 2, 2021
1 parent 6323f95 commit 004d476
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 19 deletions.
28 changes: 10 additions & 18 deletions index.js
Expand Up @@ -4,7 +4,7 @@ const cliTruncate = require('cli-truncate');
const {download} = require('electron-dl');
const isDev = require('electron-is-dev');

const webContents = win => win.webContents || (win.getWebContentsId && electron.remote.webContents.fromId(win.getWebContentsId()));
const webContents = win => win.webContents;

const decorateMenuItem = menuItem => {
return (options = {}) => {
Expand Down Expand Up @@ -292,18 +292,8 @@ const create = (win, options) => {
}

if (menuTemplate.length > 0) {
const menu = (electron.remote ? electron.remote.Menu : electron.Menu).buildFromTemplate(menuTemplate);

/*
When `electron.remote` is not available, this runs in the browser process.
We can safely use `win` in this case as it refers to the window the
context-menu should open in.
When this is being called from a web view, we can't use `win` as this
would refer to the web view which is not allowed to render a popup menu.
*/
menu.popup(electron.remote ? electron.remote.getCurrentWindow() : win);
const menu = electron.Menu.buildFromTemplate(menuTemplate);
menu.popup(win);
}
};

Expand All @@ -319,6 +309,10 @@ const create = (win, options) => {
};

module.exports = (options = {}) => {
if (process.type === 'renderer') {
throw new Error('Cannot use electron-context-menu in renderer process!');
}

let isDisposed = false;
const disposables = [];

Expand Down Expand Up @@ -379,19 +373,17 @@ module.exports = (options = {}) => {
return dispose;
}

for (const win of (electron.BrowserWindow || electron.remote.BrowserWindow).getAllWindows()) {
for (const win of electron.BrowserWindow.getAllWindows()) {
init(win);
}

const app = electron.app || electron.remote.app;

const onWindowCreated = (event, win) => {
init(win);
};

app.on('browser-window-created', onWindowCreated);
electron.app.on('browser-window-created', onWindowCreated);
disposables.push(() => {
app.removeListener('browser-window-created', onWindowCreated);
electron.app.removeListener('browser-window-created', onWindowCreated);
});

return dispose;
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Expand Up @@ -6,7 +6,7 @@

Electron doesn't have a built-in context menu. You're supposed to handle that yourself. But it's both tedious and hard to get right. This module gives you a nice extensible context menu with spellchecking and 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.
This package can only be used in the main process.

## Install

Expand Down

0 comments on commit 004d476

Please sign in to comment.