refactor: implement inspector APIs without the remote module (#16607)
This commit is contained in:
parent
392458b252
commit
7dc565fc2e
7 changed files with 205 additions and 96 deletions
|
@ -1,6 +1,7 @@
|
|||
'use strict'
|
||||
|
||||
const { getRemote, potentiallyRemoteRequire } = require('@electron/internal/renderer/remote')
|
||||
const ipcRenderer = require('@electron/internal/renderer/ipc-renderer-internal')
|
||||
const ipcRendererUtils = require('@electron/internal/renderer/ipc-renderer-internal-utils')
|
||||
|
||||
window.onload = function () {
|
||||
// Use menu API to show context menu.
|
||||
|
@ -20,64 +21,16 @@ function completeURL (project, path) {
|
|||
}
|
||||
|
||||
window.confirm = function (message, title) {
|
||||
const dialog = getRemote('dialog')
|
||||
if (title == null) {
|
||||
title = ''
|
||||
}
|
||||
return !dialog.showMessageBox({
|
||||
message: message,
|
||||
title: title,
|
||||
buttons: ['OK', 'Cancel'],
|
||||
cancelId: 1
|
||||
})
|
||||
return ipcRendererUtils.invokeSync('ELECTRON_INSPECTOR_CONFIRM', message, title)
|
||||
}
|
||||
|
||||
const convertToMenuTemplate = function (items) {
|
||||
return items.map(function (item) {
|
||||
const transformed = item.type === 'subMenu' ? {
|
||||
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
|
||||
}
|
||||
ipcRenderer.on('ELECTRON_INSPECTOR_CONTEXT_MENU_CLICK', function (event, id) {
|
||||
window.DevToolsAPI.contextMenuItemSelected(id)
|
||||
})
|
||||
|
||||
if (item.id != null) {
|
||||
transformed.click = function () {
|
||||
window.DevToolsAPI.contextMenuItemSelected(item.id)
|
||||
return window.DevToolsAPI.contextMenuCleared()
|
||||
}
|
||||
}
|
||||
|
||||
return transformed
|
||||
})
|
||||
}
|
||||
|
||||
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)
|
||||
|
||||
// The menu is expected to show asynchronously.
|
||||
setTimeout(function () {
|
||||
const getCurrentWindow = getRemote('getCurrentWindow')
|
||||
menu.popup({ window: getCurrentWindow() })
|
||||
})
|
||||
}
|
||||
ipcRenderer.on('ELECTRON_INSPECTOR_CONTEXT_MENU_CLOSE', function () {
|
||||
window.DevToolsAPI.contextMenuCleared()
|
||||
})
|
||||
|
||||
const useEditMenuItems = function (x, y, items) {
|
||||
return items.length === 0 && document.elementsFromPoint(x, y).some(function (element) {
|
||||
|
@ -85,49 +38,21 @@ const useEditMenuItems = function (x, y, items) {
|
|||
})
|
||||
}
|
||||
|
||||
const getEditMenuItems = function () {
|
||||
return [
|
||||
{
|
||||
role: 'undo'
|
||||
},
|
||||
{
|
||||
role: 'redo'
|
||||
},
|
||||
{
|
||||
type: 'separator'
|
||||
},
|
||||
{
|
||||
role: 'cut'
|
||||
},
|
||||
{
|
||||
role: 'copy'
|
||||
},
|
||||
{
|
||||
role: 'paste'
|
||||
},
|
||||
{
|
||||
role: 'pasteAndMatchStyle'
|
||||
},
|
||||
{
|
||||
role: 'delete'
|
||||
},
|
||||
{
|
||||
role: 'selectAll'
|
||||
}
|
||||
]
|
||||
const createMenu = function (x, y, items) {
|
||||
const isEditMenu = useEditMenuItems(x, y, items)
|
||||
ipcRendererUtils.invoke('ELECTRON_INSPECTOR_CONTEXT_MENU', items, isEditMenu)
|
||||
}
|
||||
|
||||
const showFileChooserDialog = function (callback) {
|
||||
const dialog = getRemote('dialog')
|
||||
const files = dialog.showOpenDialog({})
|
||||
if (files != null) {
|
||||
callback(pathToHtml5FileObject(files[0]))
|
||||
}
|
||||
ipcRendererUtils.invoke('ELECTRON_INSPECTOR_SELECT_FILE').then(([path, data]) => {
|
||||
if (path && data) {
|
||||
callback(dataToHtml5FileObject(path, data))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
const pathToHtml5FileObject = function (path) {
|
||||
const fs = potentiallyRemoteRequire('fs')
|
||||
const blob = new Blob([fs.readFileSync(path)])
|
||||
const dataToHtml5FileObject = function (path, data) {
|
||||
const blob = new Blob([data])
|
||||
blob.name = path
|
||||
return blob
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue