7f007109c3
* refactor: Port inspector to TypeScript * refactor: Add another type to inspector * refactor: Use correct paths * Update lib/renderer/inspector.ts Co-Authored-By: felixrieseberg <felix@felixrieseberg.com> * refactor: Implement feedback <3 * refactor: Don't define blob at all * fix: Correct type
64 lines
2.3 KiB
TypeScript
64 lines
2.3 KiB
TypeScript
import { ipcRendererInternal } from '@electron/internal/renderer/ipc-renderer-internal'
|
|
import { invoke, invokeSync } from '@electron/internal/renderer/ipc-renderer-internal-utils'
|
|
|
|
window.onload = function () {
|
|
// Use menu API to show context menu.
|
|
window.InspectorFrontendHost!.showContextMenuAtPoint = createMenu
|
|
|
|
// correct for Chromium returning undefined for filesystem
|
|
window.Persistence!.FileSystemWorkspaceBinding.completeURL = completeURL
|
|
|
|
// Use dialog API to override file chooser dialog.
|
|
window.UI!.createFileSelectorElement = createFileSelectorElement
|
|
}
|
|
|
|
// Extra / is needed as a result of MacOS requiring absolute paths
|
|
function completeURL (project: string, path: string) {
|
|
project = 'file:///'
|
|
return `${project}${path}`
|
|
}
|
|
|
|
// The DOM implementation expects (message?: string) => boolean
|
|
(window.confirm as any) = function (message: string, title: string) {
|
|
return invokeSync('ELECTRON_INSPECTOR_CONFIRM', message, title) as boolean
|
|
}
|
|
|
|
ipcRendererInternal.on('ELECTRON_INSPECTOR_CONTEXT_MENU_CLICK', function (_event: Electron.Event, id: number) {
|
|
window.DevToolsAPI!.contextMenuItemSelected(id)
|
|
})
|
|
|
|
ipcRendererInternal.on('ELECTRON_INSPECTOR_CONTEXT_MENU_CLOSE', function () {
|
|
window.DevToolsAPI!.contextMenuCleared()
|
|
})
|
|
|
|
const useEditMenuItems = function (x: number, y: number, items: any[]) {
|
|
return items.length === 0 && document.elementsFromPoint(x, y).some(function (element) {
|
|
return element.nodeName === 'INPUT' ||
|
|
element.nodeName === 'TEXTAREA' ||
|
|
(element as HTMLElement).isContentEditable
|
|
})
|
|
}
|
|
|
|
const createMenu = function (x: number, y: number, items: any[]) {
|
|
const isEditMenu = useEditMenuItems(x, y, items)
|
|
invoke('ELECTRON_INSPECTOR_CONTEXT_MENU', items, isEditMenu)
|
|
}
|
|
|
|
const showFileChooserDialog = function (callback: (blob: File) => void) {
|
|
invoke<[ string, any ]>('ELECTRON_INSPECTOR_SELECT_FILE').then(([path, data]) => {
|
|
if (path && data) {
|
|
callback(dataToHtml5FileObject(path, data))
|
|
}
|
|
})
|
|
}
|
|
|
|
const dataToHtml5FileObject = function (path: string, data: any) {
|
|
return new File([data], path)
|
|
}
|
|
|
|
const createFileSelectorElement = function (this: any, callback: () => void) {
|
|
const fileSelectorElement = document.createElement('span')
|
|
fileSelectorElement.style.display = 'none'
|
|
fileSelectorElement.click = showFileChooserDialog.bind(this, callback)
|
|
return fileSelectorElement
|
|
}
|