/
clipboard.js
146 lines (136 loc) · 3.39 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
(function(Daisy, $) {
/**
* 模拟的剪贴板,在firefox下不能直接向系统剪贴板中写入数据。
* 使用单例模式。
*/
Daisy.Clipboard = function() {
this.inner_text = "";
this.inner_data = null;
this.saved_event = null;
this.saved_callback = null;
this._image_delegate = $.createDelegate(this, this._imageLoad);
this._text_delegate = $.createDelegate(this, this._textLoad);
};
var C = Daisy.Clipboard;
C.prototype = {
getData : function(e, callback) {
if(e == null || typeof callback !== 'function') {
return null;
}
this.saved_event = e;
this.saved_callback = callback;
if(e && e.clipboardData && e.clipboardData.types) {
/**
* chrome safari
*/
var d_type = e.clipboardData.types[0];
//$.log(e.clipboardData.types);
//$.log(e.clipboardData.items);
if(d_type === "text/html") {
this.data = {
type : 'html',
value : e.clipboardData.getData("text/html")
}
this._checkData();
} else if(d_type === "text/uri-list") {
this.data = {
type : 'url',
value : e.clipboardData.getData("text/uri-list")
}
this._checkData();
} else if(/image/.test(d_type)) {
var reader = new FileReader();
reader.onload = this._image_delegate;
reader.readAsDataURL(e.clipboardData.items[0].getAsFile());
} else {
this.data = {
type : 'text',
value : e.clipboardData.getData("text/plain")
}
this._checkData();
}
} else if(window.clipboardData) {
/**
* IE 9
*/
this.data = {
type : 'text',
value : window.clipboardData.getData('text')
};
this._checkData();
} else {
/*
* firefox
*/
if(this.inner_text!=null && this.inner_text !== ""){
this.data = {
type : 'item',
value : this.inner_data
};
this._checkData();
}else{
//直接返回,不要stopEvent,使得文本可以复制到caret中,然后会触发 input事件,从而将文本插入。
return;
}
}
/*
* stopEvent阻止文本复制到caret中。
*/
$.stopEvent(e);
},
_textLoad : function() {
this.data = {
type : 'text',
value : this.saved_event.target.value
};
this._checkData();
},
_imageLoad : function(evt) {
this.data = {
type : 'image',
value : evt.target.result
};
this._checkData();
},
_checkData : function() {
if(this.data.type === "text") {
if(this.data.value!=="" && this.data.value === this.inner_text) {
this.data.type = "item";
this.data.value = this.inner_data;
}
}
this.saved_callback(this.data);
},
_item2Text : function(items) {
var txt = "";
for(var i = 0; i < items.length; i++) {
var it = items[i];
if(it.type === Daisy._Element.Type.HANDWORD) {
txt += "\uFFFC";
} else {
txt += it.toString();
}
}
return txt;
},
setData : function(type, value, e) {
if(type === 'item') {
this.inner_data = value;
this.inner_text = this._item2Text(value);
if(e && e.clipboardData) {
e.clipboardData.setData("text/plain", this.inner_text);
} else if(window.clipboardData) {
window.clipboardData.setData("text", this.inner_text);
}
} else if(type === 'image') {
return;
}
}
}
C.__instance__ = null;
C.getInstance = function() {
if(C.__instance__ === null)
C.__instance__ = new C();
return C.__instance__;
}
})(Daisy, Daisy.$);