Browse Source

Add standard edit items to text context menus

Kevin Sawicki 8 years ago
parent
commit
fa36d2e8c6
1 changed files with 44 additions and 1 deletions
  1. 44 1
      lib/renderer/inspector.js

+ 44 - 1
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({})