-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclipboard.js
166 lines (139 loc) · 4.81 KB
/
clipboard.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/**
* Clipboard Module for the Cloud9 IDE
*
* @copyright 2010, Ajax.org B.V.
* @license GPLv3 <http://www.gnu.org/licenses/gpl.txt>
*/
define(function(require, exports, module) {
"use strict";
var ide = require("core/ide");
var ext = require("core/ext");
var menus = require("ext/menus/menus");
var commands = require("ext/commands/commands");
var editors = require("ext/editors/editors");
var aceClipboardText = "";
module.exports = ext.register("ext/clipboard/clipboard", {
dev : "Ajax.org",
name : "Clipboard",
alone : true,
type : ext.GENERAL,
nodes : [],
text : "",
range : null,
hook : function(){
var _self = this;
var isAvailable = function(editor, event){
if (apf.activeElement && apf.activeElement.localName == "codeeditor") {
var type = event && event.type;
return type != "keypress" && type != "keydown" && !ide.readonly;
}
return self.trFiles && apf.activeElement == trFiles && !trFiles.renaming && !ide.readonly;
};
commands.addCommand({
name: "cut",
bindKey: {mac: "Command-X", win: "Ctrl-X"},
isAvailable : isAvailable,
exec: function(){ _self.cut(); }
});
commands.addCommand({
name: "copy",
bindKey: {mac: "Command-C", win: "Ctrl-C"},
isAvailable : isAvailable,
exec: function(){ _self.copy(); }
});
commands.addCommand({
name: "paste",
bindKey: {mac: "Command-V", win: "Ctrl-V"},
isAvailable : isAvailable,
exec: function(){ _self.paste(); }
});
commands.addCommand({
name: "clearcut",
bindKey: {mac: "ESC", win: "ESC"},
isAvailable : function(){
return self.trFiles && apf.activeElement == trFiles
&& apf.clipboard.store && !apf.clipboard.copied;
},
exec: function(){
var nodes = apf.clipboard.store;
if (!nodes) return false;
apf.clipboard.$highlightSelection(trFiles, nodes, true);
apf.clipboard.clear();
return false;
}
});
this.nodes.push(
menus.addItemByPath("Edit/~", new apf.divider(), 300),
menus.addItemByPath("Edit/Cut", new apf.item({
command : "cut"
}), 400),
menus.addItemByPath("Edit/Copy", new apf.item({
command : "copy"
}), 500),
menus.addItemByPath("Edit/Paste", new apf.item({
command : "paste"
}), 600)
);
},
cut: function() {
if (self.trFiles && apf.document.activeElement == trFiles) {
apf.clipboard.cutSelection(trFiles);
}
else {
var ace = this.$getAce();
ace.focus();
// try-catch is needed because firefox throws error instead of returning false
try {
// due to some bug in chrome "cut" is very slow
document.execCommand("copy");
} catch(e) {}
aceClipboardText = ace.getCopyText() || aceClipboardText;
var cutCommand = ace.$nativeCommands.commands.cut;
ace.commands.exec(cutCommand, ace);
}
},
copy: function() {
if (self.trFiles && apf.document.activeElement == trFiles) {
apf.clipboard.put(trFiles.getSelection().map(function (node) {
return apf.xmldb.cleanNode(node.cloneNode(false))
}));
apf.clipboard.copied = true;
}
else {
var ace = this.$getAce();
ace.focus();
try {
if (document.execCommand("copy")) return;
} catch(e) {}
aceClipboardText = ace.getCopyText() || aceClipboardText;
}
},
paste: function() {
if (self.trFiles && apf.document.activeElement == trFiles) {
apf.clipboard.pasteSelection(trFiles);
}
else {
var ace = this.$getAce();
ace.focus();
try {
if (document.execCommand("paste")) return;
} catch(e) {}
ace.onPaste(aceClipboardText);
}
},
$getAce : function() {
var editor = editors.currentEditor;
if (!editor || !editor.amlEditor)
return;
var amlEditor = editor.amlEditor;
return amlEditor.$editor;
},
destroy : function(){
menus.remove("Edit/~", 300);
menus.remove("Edit/Cut");
menus.remove("Edit/Copy");
menus.remove("Edit/Paste");
this.$destroy();
}
});
});