forked from guillaumebort/textile-editor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clipboard.js
67 lines (60 loc) · 2.37 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
/** Pretty dumb clipboard implementation **/
Textile.Clipboard = Textile.Utils.makeClass({
constructor: function(editor) {
this.editor = editor;
this.clipboard = document.createElement('textarea');
document.body.appendChild(this.clipboard);
this.clipboard.style.position = 'absolute';
this.clipboard.style.width = '100px';
this.clipboard.style.height = '100px';
this.clipboard.style.top = this.editor.getPosition().top + 'px';
this.clipboard.style.left = '-999em';
this.clipboard.autocomplete = 'off';
this.clipboard.tabIndex = '-1';
},
cut: function() {
var data = this.selected();
if(data) {
this.copyToClipboard(data);
this.editor.model.replace(this.editor.selection.from, this.editor.selection.to, '');
this.editor.cursor.toPosition(this.editor.selection.from);
this.editor.selection = null;
this.editor.cursor.focus();
this.editor.paint();
}
},
copy: function() {
var data = this.selected();
if(data) {
this.copyToClipboard(data);
}
},
paste: function() {
this.clipboard.select();
setTimeout(Textile.Utils.bind(function() {
var data = this.clipboard.value;
if(data) {
if(this.editor.selection) {
this.editor.model.replace(this.editor.selection.from, this.editor.selection.to, data);
this.editor.cursor.toPosition(this.editor.selection.from + data.length);
this.editor.selection = null;
} else {
this.editor.model.insert(this.editor.cursor.getPosition(), data);
this.editor.cursor.toPosition(this.editor.cursor.getPosition() + data.length);
}
this.editor.cursor.focus();
this.editor.paint();
}
}, this), 0);
},
copyToClipboard: function(data) {
this.clipboard.value = data;
this.clipboard.select();
},
selected: function() {
if(!this.editor.selection) {
return '';
}
return this.editor.model.content.substring(this.editor.selection.from, this.editor.selection.to);
}
});