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';
|
2020-10-13 21:11:06 +00:00
|
|
|
import { IPC_MESSAGES } from '../common/ipc-messages';
|
2019-02-26 02:10:26 +00:00
|
|
|
|
|
|
|
window.onload = function () {
|
|
|
|
// Use menu API to show context menu.
|
2020-03-20 20:28:31 +00:00
|
|
|
window.InspectorFrontendHost!.showContextMenuAtPoint = createMenu;
|
2019-02-26 02:10:26 +00:00
|
|
|
|
|
|
|
// correct for Chromium returning undefined for filesystem
|
2020-03-20 20:28:31 +00:00
|
|
|
window.Persistence!.FileSystemWorkspaceBinding.completeURL = completeURL;
|
2019-02-26 02:10:26 +00:00
|
|
|
|
|
|
|
// Use dialog API to override file chooser dialog.
|
2020-03-20 20:28:31 +00:00
|
|
|
window.UI!.createFileSelectorElement = createFileSelectorElement;
|
|
|
|
};
|
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') {
|
2020-03-20 20:28:31 +00:00
|
|
|
window.DevToolsAPI!.contextMenuItemSelected(id);
|
2019-03-19 17:37:43 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
window.DevToolsAPI!.contextMenuCleared();
|
|
|
|
});
|
|
|
|
};
|
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;
|
|
|
|
};
|