[MM-34494] Encapsulate context menu in a class object and add it to each view as necessary (#1544)

This commit is contained in:
Devin Binnie
2021-04-16 12:33:23 -04:00
committed by GitHub
parent 9f31d3c725
commit 62de91b7aa
6 changed files with 42 additions and 39 deletions

View File

@@ -6,8 +6,7 @@ import electronContextMenu from 'electron-context-menu';
import urlUtils from 'common/utils/url';
let disposeCurrent;
let menuOptions = {
const defaultMenuOptions = {
shouldShowMenu: (e, p) => {
const isInternalLink = p.linkURL.endsWith('#') && p.linkURL.slice(0, -1) === p.pageURL;
let isInternalSrc;
@@ -27,38 +26,31 @@ let menuOptions = {
showServices: true,
};
function dispose() {
if (disposeCurrent) {
disposeCurrent();
disposeCurrent = null;
export default class ContextMenu {
constructor(options, view) {
const providedOptions = options || {};
this.menuOptions = Object.assign({}, defaultMenuOptions, providedOptions);
this.view = view;
this.reload();
}
dispose = () => {
if (this.menuDispose) {
this.menuDispose();
this.menuDispose = null;
}
}
reload = () => {
this.dispose();
/**
* Work-around issue with passing `WebContents` to `electron-context-menu` in Electron 11
* @see https://github.com/sindresorhus/electron-context-menu/issues/123
*/
const options = {window: {webContents: this.view.webContents, inspectElement: this.view.webContents.inspectElement.bind(this.view.webContents), isDestroyed: this.view.webContents.isDestroyed.bind(this.view.webContents), off: this.view.webContents.off.bind(this.view.webContents)}, ...this.menuOptions};
this.menuDispose = electronContextMenu(options);
}
}
function saveOptions(options) {
const providedOptions = options || {};
menuOptions = Object.assign({}, menuOptions, providedOptions);
}
function reload(target) {
dispose();
/**
* Work-around issue with passing `WebContents` to `electron-context-menu` in Electron 11
* @see https://github.com/sindresorhus/electron-context-menu/issues/123
*/
const options = target ? {window: {webContents: target, inspectElement: target.inspectElement.bind(target), isDestroyed: target.isDestroyed.bind(target), off: target.off.bind(target)}, ...menuOptions} : menuOptions;
disposeCurrent = electronContextMenu(options);
}
function setup(options) {
saveOptions(options);
dispose();
disposeCurrent = electronContextMenu(menuOptions);
}
export default {
setup,
dispose,
reload,
};