From 63b98b1ea04896111725d49cab4b6cb477af8832 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Tue, 12 Jul 2016 11:31:40 -0700 Subject: [PATCH 1/3] Use const instead of var --- lib/renderer/inspector.js | 60 ++++++++++++++++----------------------- 1 file changed, 25 insertions(+), 35 deletions(-) diff --git a/lib/renderer/inspector.js b/lib/renderer/inspector.js index ca60c84d4be9..962fd12e5a52 100644 --- a/lib/renderer/inspector.js +++ b/lib/renderer/inspector.js @@ -6,12 +6,9 @@ window.onload = function () { window.WebInspector.createFileSelectorElement = createFileSelectorElement } -var convertToMenuTemplate = function (items) { - var fn, i, item, len, template - template = [] - fn = function (item) { - var transformed - transformed = item.type === 'subMenu' ? { +const convertToMenuTemplate = function (items) { + return items.map(function (item) { + const transformed = item.type === 'subMenu' ? { type: 'submenu', label: item.label, enabled: item.enabled, @@ -28,53 +25,46 @@ var convertToMenuTemplate = function (items) { label: item.label, enabled: item.enabled } + if (item.id != null) { transformed.click = function () { window.DevToolsAPI.contextMenuItemSelected(item.id) return window.DevToolsAPI.contextMenuCleared() } } - return template.push(transformed) - } - for (i = 0, len = items.length; i < len; i++) { - item = items[i] - fn(item) - } - return template -} -var createMenu = function (x, y, items) { - const remote = require('electron').remote - const Menu = remote.Menu - const menu = Menu.buildFromTemplate(convertToMenuTemplate(items)) - - // The menu is expected to show asynchronously. - return setTimeout(function () { - return menu.popup(remote.getCurrentWindow()) + return transformed }) } -var showFileChooserDialog = function (callback) { - var dialog, files, remote - remote = require('electron').remote - dialog = remote.dialog - files = dialog.showOpenDialog({}) +const createMenu = function (x, y, items) { + const {remote} = require('electron') + const {Menu} = remote + const menu = Menu.buildFromTemplate(convertToMenuTemplate(items)) + + // The menu is expected to show asynchronously. + setTimeout(function () { + menu.popup(remote.getCurrentWindow()) + }) +} + +const showFileChooserDialog = function (callback) { + const {dialog} = require('electron').remote + const files = dialog.showOpenDialog({}) if (files != null) { - return callback(pathToHtml5FileObject(files[0])) + callback(pathToHtml5FileObject(files[0])) } } -var pathToHtml5FileObject = function (path) { - var blob, fs - fs = require('fs') - blob = new Blob([fs.readFileSync(path)]) +const pathToHtml5FileObject = function (path) { + const fs = require('fs') + const blob = new Blob([fs.readFileSync(path)]) blob.name = path return blob } -var createFileSelectorElement = function (callback) { - var fileSelectorElement - fileSelectorElement = document.createElement('span') +const createFileSelectorElement = function (callback) { + const fileSelectorElement = document.createElement('span') fileSelectorElement.style.display = 'none' fileSelectorElement.click = showFileChooserDialog.bind(this, callback) return fileSelectorElement From fa36d2e8c6b122752f5a58a326f82894f79b8675 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Tue, 12 Jul 2016 11:54:45 -0700 Subject: [PATCH 2/3] Add standard edit items to text context menus --- lib/renderer/inspector.js | 45 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/lib/renderer/inspector.js b/lib/renderer/inspector.js index 962fd12e5a52..3192aa160edb 100644 --- a/lib/renderer/inspector.js +++ b/lib/renderer/inspector.js @@ -40,7 +40,12 @@ const convertToMenuTemplate = function (items) { const createMenu = function (x, y, items) { const {remote} = require('electron') const {Menu} = remote - const menu = Menu.buildFromTemplate(convertToMenuTemplate(items)) + + 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 () { @@ -48,6 +53,44 @@ const createMenu = function (x, y, items) { }) } +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' + }, + { + role: 'pasteandmatchstyle' + }, + { + role: 'delete' + }, + { + role: 'selectall' + } + ] +} + const showFileChooserDialog = function (callback) { const {dialog} = require('electron').remote const files = dialog.showOpenDialog({}) From e02cf5905c39ffd858dc47d6be4d6135b1d9d070 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Tue, 12 Jul 2016 13:24:53 -0700 Subject: [PATCH 3/3] Execute command on devtools web contents when focused --- lib/browser/api/menu-item-roles.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/browser/api/menu-item-roles.js b/lib/browser/api/menu-item-roles.js index 32d0d07f4b92..249e5f4b720f 100644 --- a/lib/browser/api/menu-item-roles.js +++ b/lib/browser/api/menu-item-roles.js @@ -138,7 +138,11 @@ exports.execute = (role, focusedWindow) => { if (webContentsMethod && focusedWindow != null) { const {webContents} = focusedWindow if (webContents) { - webContents[webContentsMethod]() + if (webContents.isDevToolsFocused()) { + webContents.devToolsWebContents[webContentsMethod]() + } else { + webContents[webContentsMethod]() + } } return true }