Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save emanoelqueiroz/65fa3ca3194e0e48608e05635be21b88 to your computer and use it in GitHub Desktop.
Save emanoelqueiroz/65fa3ca3194e0e48608e05635be21b88 to your computer and use it in GitHub Desktop.
[ELECTRON] Menu-contexto com opções de copiar, colar e cortar em inputs e divs
(function () {
'use strict';
const remote = require('electron').remote;
const Menu = remote.Menu;
const MenuItem = remote.MenuItem;
const isAnyTextSelected = function () {
return window.getSelection().toString() !== '';
};
const cut = new MenuItem({
label: 'Cortar',
click: function () {
document.execCommand("cut");
}
});
const copy = new MenuItem({
label: 'Copiar',
click: function () {
document.execCommand("copy");
}
});
const paste = new MenuItem({
label: 'Colar',
click: function () {
document.execCommand("paste");
}
});
const normalMenu = new Menu();
normalMenu.append(copy);
const textEditingMenu = new Menu();
textEditingMenu.append(cut);
textEditingMenu.append(copy);
textEditingMenu.append(paste);
document.addEventListener('contextmenu', function (e) {
switch (e.target.nodeName) {
case 'TEXTAREA':
case 'INPUT':
e.preventDefault();
textEditingMenu.popup(remote.getCurrentWindow());
break;
case 'DIV':
e.preventDefault();
textEditingMenu.popup(remote.getCurrentWindow());
break;
default:
if (isAnyTextSelected()) {
e.preventDefault();
normalMenu.popup(remote.getCurrentWindow());
}
}
}, false);
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment