This repository has been archived by the owner on Jun 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathdatatransfer.js
76 lines (68 loc) · 1.9 KB
/
datatransfer.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
/**
* @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/
/**
* @module clipboard/datatransfer
*/
/**
* Facade over the native [`DataTransfer`](https://developer.mozilla.org/en-US/docs/Web/API/DataTransfer) object.
*/
export default class DataTransfer {
constructor( nativeDataTransfer ) {
/**
* The array of files created from the native `DataTransfer#files` or `DataTransfer#items`.
*
* @readonly
* @member {Array.<File>} #files
*/
this.files = getFiles( nativeDataTransfer );
/**
* The native DataTransfer object.
*
* @private
* @member {DataTransfer} #_native
*/
this._native = nativeDataTransfer;
}
/**
* Returns an array of available native content types.
*
* @returns {Array.<String>}
*/
get types() {
return this._native.types;
}
/**
* Gets data from the data transfer by its mime type.
*
* dataTransfer.getData( 'text/plain' );
*
* @param {String} type The mime type. E.g. `text/html` or `text/plain`.
* @returns {String}
*/
getData( type ) {
return this._native.getData( type );
}
/**
* Sets data in the data transfer.
*
* @param {String} type The mime type. E.g. `text/html` or `text/plain`.
* @param {String} data
*/
setData( type, data ) {
this._native.setData( type, data );
}
}
function getFiles( nativeDataTransfer ) {
// DataTransfer.files and items are Array-like and might not have an iterable interface.
const files = nativeDataTransfer.files ? Array.from( nativeDataTransfer.files ) : [];
const items = nativeDataTransfer.items ? Array.from( nativeDataTransfer.items ) : [];
if ( files.length ) {
return files;
}
// Chrome have empty DataTransfer.files, but let get files through the items interface.
return items
.filter( item => item.kind === 'file' )
.map( item => item.getAsFile() );
}