-
Notifications
You must be signed in to change notification settings - Fork 1
/
copy-paste.js
68 lines (62 loc) · 1.96 KB
/
copy-paste.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
function Menu(targetElement, cutLabel, copyLabel, pasteLabel) {
var selectionType = window.getSelection().type;
var hasSelection = !(selectionType === "Caret" || selectionType === "None");
var isAnchor = targetElement.tagName === 'A' && targetElement.href;
var isEditable = targetElement.isContentEditable || targetElement.tagName === 'INPUT' || targetElement.tagName === 'TEXTAREA';
var isCopyable = isAnchor || hasSelection;
var gui = window.nwDispatcher.requireNwGui();
menu = new gui.Menu(),
cut = new gui.MenuItem({
label : cutLabel || "Cut",
click : function () {
window.document.execCommand("cut");
console.log('Menu:', 'cutted to clipboard');
}
}),
copy = new gui.MenuItem({
label : copyLabel || "Copy",
click : function () {
if (!hasSelection && isAnchor) {
console.log(targetElement.href);
gui.Clipboard.get().set(targetElement.href, 'text');
} else {
window.document.execCommand("copy");
}
console.log('Menu:', 'copied to clipboard');
}
}),
paste = new gui.MenuItem({
label : pasteLabel || "Paste",
click : function () {
window.document.execCommand("paste");
console.log('Menu:', 'pasted to textarea');
}
});
if (isCopyable) {
menu.append(copy);
}
if (isCopyable && isEditable) {
menu.append(cut);
}
if (isEditable) {
menu.append(paste);
}
return menu;
}
var targetElement = null;
var hasSelection = false;
var isAnchor = false;
var oncontextmenu = function (e) {
e.preventDefault();
var menu = new Menu(e.target);
if (menu.items.length > 0) {
menu.popup(e.x, e.y);
}
}
exports.init = function(window) {
if (window.document.addEventListener) {
window.document.addEventListener("contextmenu", oncontextmenu, false);
} else {
window.document.attachEvent('contextmenu', oncontextmenu);
}
}