2021-06-24 18:36:22 +00:00
|
|
|
import { internalContextBridge } from '@electron/internal/renderer/api/context-bridge';
|
2020-03-20 20:28:31 +00:00
|
|
|
import { ipcRendererInternal } from '@electron/internal/renderer/ipc-renderer-internal';
|
|
|
|
import * as ipcRendererUtils from '@electron/internal/renderer/ipc-renderer-internal-utils';
|
2021-06-24 18:36:22 +00:00
|
|
|
import { webFrame } from 'electron/renderer';
|
2020-10-13 21:11:06 +00:00
|
|
|
import { IPC_MESSAGES } from '../common/ipc-messages';
|
2019-02-26 02:10:26 +00:00
|
|
|
|
2021-06-24 18:36:22 +00:00
|
|
|
const { contextIsolationEnabled } = internalContextBridge;
|
2019-02-26 02:10:26 +00:00
|
|
|
|
2021-06-24 18:36:22 +00:00
|
|
|
/* Corrects for some Inspector adaptations needed in Electron.
|
|
|
|
* 1) Use menu API to show context menu.
|
|
|
|
* 2) Correct for Chromium returning undefined for filesystem.
|
|
|
|
* 3) Use dialog API to override file chooser dialog.
|
|
|
|
*/
|
|
|
|
window.onload = function () {
|
|
|
|
if (contextIsolationEnabled) {
|
|
|
|
internalContextBridge.overrideGlobalValueFromIsolatedWorld([
|
|
|
|
'InspectorFrontendHost', 'showContextMenuAtPoint'
|
|
|
|
], createMenu);
|
|
|
|
internalContextBridge.overrideGlobalValueFromIsolatedWorld([
|
|
|
|
'Persistence', 'FileSystemWorkspaceBinding', 'completeURL'
|
|
|
|
], completeURL);
|
|
|
|
internalContextBridge.overrideGlobalValueFromIsolatedWorld([
|
|
|
|
'UI', 'createFileSelectorElement'
|
|
|
|
], createFileSelectorElement);
|
|
|
|
} else {
|
|
|
|
window.InspectorFrontendHost!.showContextMenuAtPoint = createMenu;
|
|
|
|
window.Persistence!.FileSystemWorkspaceBinding.completeURL = completeURL;
|
|
|
|
window.UI!.createFileSelectorElement = createFileSelectorElement;
|
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
};
|
2019-02-26 02:10:26 +00:00
|
|
|
|
|
|
|
// Extra / is needed as a result of MacOS requiring absolute paths
|
|
|
|
function completeURL (project: string, path: string) {
|
2020-03-20 20:28:31 +00:00
|
|
|
project = 'file:///';
|
|
|
|
return `${project}${path}`;
|
2019-02-26 02:10:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// The DOM implementation expects (message?: string) => boolean
|
2020-10-08 01:01:23 +00:00
|
|
|
window.confirm = function (message?: string, title?: string) {
|
2020-10-13 21:11:06 +00:00
|
|
|
return ipcRendererUtils.invokeSync(IPC_MESSAGES.INSPECTOR_CONFIRM, message, title) as boolean;
|
2020-03-20 20:28:31 +00:00
|
|
|
};
|
2019-02-26 02:10:26 +00:00
|
|
|
|
2019-07-17 17:23:52 +00:00
|
|
|
const useEditMenuItems = function (x: number, y: number, items: ContextMenuItem[]) {
|
2019-02-26 02:10:26 +00:00
|
|
|
return items.length === 0 && document.elementsFromPoint(x, y).some(function (element) {
|
|
|
|
return element.nodeName === 'INPUT' ||
|
|
|
|
element.nodeName === 'TEXTAREA' ||
|
2020-03-20 20:28:31 +00:00
|
|
|
(element as HTMLElement).isContentEditable;
|
|
|
|
});
|
|
|
|
};
|
2019-02-26 02:10:26 +00:00
|
|
|
|
2019-07-17 17:23:52 +00:00
|
|
|
const createMenu = function (x: number, y: number, items: ContextMenuItem[]) {
|
2020-03-20 20:28:31 +00:00
|
|
|
const isEditMenu = useEditMenuItems(x, y, items);
|
2020-10-13 21:11:06 +00:00
|
|
|
ipcRendererInternal.invoke<number>(IPC_MESSAGES.INSPECTOR_CONTEXT_MENU, items, isEditMenu).then(id => {
|
2019-03-19 17:37:43 +00:00
|
|
|
if (typeof id === 'number') {
|
2021-06-24 18:36:22 +00:00
|
|
|
webFrame.executeJavaScript(`window.DevToolsAPI.contextMenuItemSelected(${JSON.stringify(id)})`);
|
2019-03-19 17:37:43 +00:00
|
|
|
}
|
2021-06-24 18:36:22 +00:00
|
|
|
|
|
|
|
webFrame.executeJavaScript('window.DevToolsAPI.contextMenuCleared()');
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
|
|
|
};
|
2019-02-26 02:10:26 +00:00
|
|
|
|
|
|
|
const showFileChooserDialog = function (callback: (blob: File) => void) {
|
2020-10-13 21:11:06 +00:00
|
|
|
ipcRendererInternal.invoke<[ string, any ]>(IPC_MESSAGES.INSPECTOR_SELECT_FILE).then(([path, data]) => {
|
2019-02-26 02:10:26 +00:00
|
|
|
if (path && data) {
|
2020-03-20 20:28:31 +00:00
|
|
|
callback(dataToHtml5FileObject(path, data));
|
2019-02-26 02:10:26 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
|
|
|
};
|
2019-02-26 02:10:26 +00:00
|
|
|
|
|
|
|
const dataToHtml5FileObject = function (path: string, data: any) {
|
2020-03-20 20:28:31 +00:00
|
|
|
return new File([data], path);
|
|
|
|
};
|
2019-02-26 02:10:26 +00:00
|
|
|
|
|
|
|
const createFileSelectorElement = function (this: any, callback: () => void) {
|
2020-03-20 20:28:31 +00:00
|
|
|
const fileSelectorElement = document.createElement('span');
|
|
|
|
fileSelectorElement.style.display = 'none';
|
|
|
|
fileSelectorElement.click = showFileChooserDialog.bind(this, callback);
|
|
|
|
return fileSelectorElement;
|
|
|
|
};
|