Add standard edit items to text context menus
This commit is contained in:
parent
63b98b1ea0
commit
fa36d2e8c6
1 changed files with 44 additions and 1 deletions
|
@ -40,7 +40,12 @@ const convertToMenuTemplate = function (items) {
|
||||||
const createMenu = function (x, y, items) {
|
const createMenu = function (x, y, items) {
|
||||||
const {remote} = require('electron')
|
const {remote} = require('electron')
|
||||||
const {Menu} = remote
|
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.
|
// The menu is expected to show asynchronously.
|
||||||
setTimeout(function () {
|
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 showFileChooserDialog = function (callback) {
|
||||||
const {dialog} = require('electron').remote
|
const {dialog} = require('electron').remote
|
||||||
const files = dialog.showOpenDialog({})
|
const files = dialog.showOpenDialog({})
|
||||||
|
|
Loading…
Add table
Reference in a new issue