electron/lib/renderer/inspector.js

141 lines
3.3 KiB
JavaScript
Raw Normal View History

'use strict'
const { getRemote, potentiallyRemoteRequire } = require('@electron/internal/renderer/remote')
2016-03-25 19:57:17 +00:00
window.onload = function () {
2016-01-14 18:35:29 +00:00
// Use menu API to show context menu.
2016-03-30 21:56:30 +00:00
window.InspectorFrontendHost.showContextMenuAtPoint = createMenu
2016-01-12 02:40:23 +00:00
// correct for Chromium returning undefined for filesystem
window.Persistence.FileSystemWorkspaceBinding.completeURL = completeURL
2016-01-14 18:35:29 +00:00
// Use dialog API to override file chooser dialog.
window.UI.createFileSelectorElement = createFileSelectorElement
2016-03-25 19:57:17 +00:00
}
2016-01-12 02:40:23 +00:00
// Extra / is needed as a result of MacOS requiring absolute paths
function completeURL (project, path) {
project = 'file:///'
return `${project}${path}`
}
2016-11-27 19:38:48 +00:00
window.confirm = function (message, title) {
const dialog = getRemote('dialog')
2016-11-27 19:38:48 +00:00
if (title == null) {
title = ''
}
return !dialog.showMessageBox({
message: message,
title: title,
2016-11-29 21:30:28 +00:00
buttons: ['OK', 'Cancel'],
cancelId: 1
2016-11-27 19:38:48 +00:00
})
}
2016-07-12 18:31:40 +00:00
const convertToMenuTemplate = function (items) {
return items.map(function (item) {
const transformed = item.type === 'subMenu' ? {
2016-01-12 02:40:23 +00:00
type: 'submenu',
label: item.label,
enabled: item.enabled,
submenu: convertToMenuTemplate(item.subItems)
} : item.type === 'separator' ? {
type: 'separator'
} : item.type === 'checkbox' ? {
type: 'checkbox',
label: item.label,
enabled: item.enabled,
checked: item.checked
} : {
type: 'normal',
label: item.label,
enabled: item.enabled
2016-03-25 19:57:17 +00:00
}
2016-07-12 18:31:40 +00:00
2016-01-12 02:40:23 +00:00
if (item.id != null) {
2016-03-25 19:57:17 +00:00
transformed.click = function () {
2016-03-30 21:56:30 +00:00
window.DevToolsAPI.contextMenuItemSelected(item.id)
return window.DevToolsAPI.contextMenuCleared()
2016-03-25 19:57:17 +00:00
}
2016-01-12 02:40:23 +00:00
}
2016-07-12 18:31:40 +00:00
return transformed
})
2016-03-25 19:57:17 +00:00
}
2016-01-12 02:40:23 +00:00
2016-07-12 18:31:40 +00:00
const createMenu = function (x, y, items) {
const Menu = getRemote('Menu')
let template = convertToMenuTemplate(items)
if (useEditMenuItems(x, y, template)) {
template = getEditMenuItems()
}
const menu = Menu.buildFromTemplate(template)
2016-01-12 02:40:23 +00:00
2016-01-14 18:35:29 +00:00
// The menu is expected to show asynchronously.
2016-07-12 18:31:40 +00:00
setTimeout(function () {
const getCurrentWindow = getRemote('getCurrentWindow')
menu.popup({ window: getCurrentWindow() })
2016-03-25 19:57:17 +00:00
})
}
2016-01-12 02:40:23 +00:00
const useEditMenuItems = function (x, y, items) {
return items.length === 0 && document.elementsFromPoint(x, y).some(function (element) {
return element.nodeName === 'INPUT' || element.nodeName === 'TEXTAREA' || element.isContentEditable
})
}
const getEditMenuItems = function () {
return [
{
role: 'undo'
},
{
role: 'redo'
},
{
type: 'separator'
},
{
role: 'cut'
},
{
role: 'copy'
},
{
role: 'paste'
},
{
2017-12-28 05:22:39 +00:00
role: 'pasteAndMatchStyle'
},
{
role: 'delete'
},
{
2017-12-28 05:22:39 +00:00
role: 'selectAll'
}
]
}
2016-07-12 18:31:40 +00:00
const showFileChooserDialog = function (callback) {
const dialog = getRemote('dialog')
2016-07-12 18:31:40 +00:00
const files = dialog.showOpenDialog({})
2016-01-12 02:40:23 +00:00
if (files != null) {
2016-07-12 18:31:40 +00:00
callback(pathToHtml5FileObject(files[0]))
2016-01-12 02:40:23 +00:00
}
2016-03-25 19:57:17 +00:00
}
2016-01-12 02:40:23 +00:00
2016-07-12 18:31:40 +00:00
const pathToHtml5FileObject = function (path) {
const fs = potentiallyRemoteRequire('fs')
2016-07-12 18:31:40 +00:00
const blob = new Blob([fs.readFileSync(path)])
2016-03-25 19:57:17 +00:00
blob.name = path
return blob
}
2016-01-12 02:40:23 +00:00
2016-07-12 18:31:40 +00:00
const createFileSelectorElement = function (callback) {
const fileSelectorElement = document.createElement('span')
2016-03-25 19:57:17 +00:00
fileSelectorElement.style.display = 'none'
fileSelectorElement.click = showFileChooserDialog.bind(this, callback)
return fileSelectorElement
}