electron/lib/renderer/inspector.js

129 lines
3 KiB
JavaScript
Raw Normal View History

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
2016-01-14 18:35:29 +00:00
// Use dialog API to override file chooser dialog.
2017-01-24 23:44:01 +00:00
// Note: It will be moved to UI after Chrome 57.
window.Bindings.createFileSelectorElement = createFileSelectorElement
2016-03-25 19:57:17 +00:00
}
2016-01-12 02:40:23 +00:00
2016-11-27 19:38:48 +00:00
window.confirm = function (message, title) {
const {dialog} = require('electron').remote
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 {remote} = require('electron')
const {Menu} = remote
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 () {
menu.popup(remote.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} = require('electron').remote
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 = require('fs')
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
}