forked from steemit/slate
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransfer.js
More file actions
249 lines (195 loc) · 4.46 KB
/
transfer.js
File metadata and controls
249 lines (195 loc) · 4.46 KB
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
import Base64 from '../serializers/base-64'
import TYPES from '../constants/types'
/**
* Fragment matching regexp for HTML nodes.
*
* @type {RegExp}
*/
const FRAGMENT_MATCHER = /data-slate-fragment="([^\s]+)"/
/**
* Data transfer helper.
*
* @type {Transfer}
*/
class Transfer {
/**
* Constructor.
*
* @param {DataTransfer} data
*/
constructor(data) {
this.data = data
this.cache = {}
}
/**
* Get a data object representing the transfer's primary content type.
*
* @return {Object}
*/
getData() {
const type = this.getType()
const data = {}
data.type = type
switch (type) {
case 'files':
data.files = this.getFiles()
break
case 'fragment':
data.fragment = this.getFragment()
break
case 'html':
data.html = this.getHtml()
data.text = this.getText()
break
case 'node':
data.node = this.getNode()
break
case 'text':
data.text = this.getText()
break
}
return data
}
/**
* Get the Files content of the data transfer.
*
* @return {Array || Void}
*/
getFiles() {
if ('files' in this.cache) return this.cache.files
const { data } = this
let files
if (data.items && data.items.length) {
const fileItems = Array.from(data.items)
.map(item => item.kind == 'file' ? item.getAsFile() : null)
.filter(exists => exists)
if (fileItems.length) files = fileItems
}
if (data.files && data.files.length) {
files = Array.from(data.files)
}
this.cache.files = files
return files
}
/**
* Get the Slate document fragment content of the data transfer.
*
* @return {Document || Void}
*/
getFragment() {
if ('fragment' in this.cache) return this.cache.fragment
const html = this.getHtml()
let encoded = this.data.getData(TYPES.FRAGMENT)
let fragment
// If there's html content, and the html includes a `data-fragment`
// attribute, it's actually a Base64-serialized fragment from a cut/copy.
if (!encoded && html && ~html.indexOf('<span data-slate-fragment="')) {
const matches = FRAGMENT_MATCHER.exec(html)
const [ full, attribute ] = matches
encoded = attribute
}
if (encoded) {
fragment = Base64.deserializeNode(encoded)
}
this.cache.fragment = fragment
return fragment
}
/**
* Get the HTML content of the data transfer.
*
* @return {String || Void}
*/
getHtml() {
if ('html' in this.cache) return this.cache.html
let html
const string = this.data.getData('text/html')
if (string != '') html = string
this.cache.html = html
return html
}
/**
* Get the Slate node content of the data transfer.
*
* @return {Node || Void}
*/
getNode() {
if ('node' in this.cache) return this.cache.node
const encoded = this.data.getData(TYPES.NODE)
let node
if (encoded) {
node = Base64.deserializeNode(encoded)
}
this.cache.node = node
return node
}
/**
* Get the text content of the data transfer.
*
* @return {String || Void}
*/
getText() {
if ('text' in this.cache) return this.cache.text
let text
const string = this.data.getData('text/plain')
if (string != '') text = string
this.cache.text = text
return text
}
/**
* Get the primary type of the data transfer.
*
* @return {String}
*/
getType() {
if (this.hasFragment()) return 'fragment'
if (this.hasNode()) return 'node'
if (this.hasFiles()) return 'files'
if (this.hasHtml()) return 'html'
if (this.hasText()) return 'text'
return 'unknown'
}
/**
* Check whether the data transfer has File content.
*
* @return {Boolean}
*/
hasFiles() {
return this.getFiles() != null
}
/**
* Check whether the data transfer has HTML content.
*
* @return {Boolean}
*/
hasHtml() {
return this.getHtml() != null
}
/**
* Check whether the data transfer has text content.
*
* @return {Boolean}
*/
hasText() {
return this.getText() != null
}
/**
* Check whether the data transfer has a Slate document fragment as content.
*
* @return {Boolean}
*/
hasFragment() {
return this.getFragment() != null
}
/**
* Check whether the data transfer has a Slate node as content.
*
* @return {Boolean}
*/
hasNode() {
return this.getNode() != null
}
}
/**
* Export.
*/
export default Transfer