@ckeditor/ckeditor5-upload 35.2.0 → 35.3.0

Sign up to get free protection for your applications and to get access to all the features.
package/src/filereader.js CHANGED
@@ -2,107 +2,87 @@
2
2
  * @license Copyright (c) 2003-2022, CKSource Holding sp. z o.o. All rights reserved.
3
3
  * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
4
  */
5
-
6
5
  /**
7
6
  * @module upload/filereader
8
7
  */
9
-
10
8
  /* globals window */
11
-
12
- import ObservableMixin from '@ckeditor/ckeditor5-utils/src/observablemixin';
13
- import mix from '@ckeditor/ckeditor5-utils/src/mix';
14
-
9
+ import { Observable } from '@ckeditor/ckeditor5-utils/src/observablemixin';
15
10
  /**
16
11
  * Wrapper over the native `FileReader`.
17
12
  */
18
- export default class FileReader {
19
- /**
20
- * Creates an instance of the FileReader.
21
- */
22
- constructor() {
23
- const reader = new window.FileReader();
24
-
25
- /**
26
- * Instance of native FileReader.
27
- *
28
- * @private
29
- * @member {FileReader} #_reader
30
- */
31
- this._reader = reader;
32
-
33
- this._data = undefined;
34
-
35
- /**
36
- * Number of bytes loaded.
37
- *
38
- * @readonly
39
- * @observable
40
- * @member {Number} #loaded
41
- */
42
- this.set( 'loaded', 0 );
43
-
44
- reader.onprogress = evt => {
45
- this.loaded = evt.loaded;
46
- };
47
- }
48
-
49
- /**
50
- * Returns error that occurred during file reading.
51
- *
52
- * @returns {Error}
53
- */
54
- get error() {
55
- return this._reader.error;
56
- }
57
-
58
- /**
59
- * Holds the data of an already loaded file. The file must be first loaded
60
- * by using {@link module:upload/filereader~FileReader#read `read()`}.
61
- *
62
- * @type {File|undefined}
63
- */
64
- get data() {
65
- return this._data;
66
- }
67
-
68
- /**
69
- * Reads the provided file.
70
- *
71
- * @param {File} file Native File object.
72
- * @returns {Promise.<String>} Returns a promise that will be resolved with file's content.
73
- * The promise will be rejected in case of an error or when the reading process is aborted.
74
- */
75
- read( file ) {
76
- const reader = this._reader;
77
- this.total = file.size;
78
-
79
- return new Promise( ( resolve, reject ) => {
80
- reader.onload = () => {
81
- const result = reader.result;
82
-
83
- this._data = result;
84
-
85
- resolve( result );
86
- };
87
-
88
- reader.onerror = () => {
89
- reject( 'error' );
90
- };
91
-
92
- reader.onabort = () => {
93
- reject( 'aborted' );
94
- };
95
-
96
- this._reader.readAsDataURL( file );
97
- } );
98
- }
99
-
100
- /**
101
- * Aborts file reader.
102
- */
103
- abort() {
104
- this._reader.abort();
105
- }
13
+ export default class FileReader extends Observable {
14
+ /**
15
+ * Creates an instance of the FileReader.
16
+ */
17
+ constructor() {
18
+ super();
19
+ const reader = new window.FileReader();
20
+ /**
21
+ * Instance of native FileReader.
22
+ *
23
+ * @private
24
+ * @member {FileReader} #_reader
25
+ */
26
+ this._reader = reader;
27
+ this._data = undefined;
28
+ /**
29
+ * Number of bytes loaded.
30
+ *
31
+ * @readonly
32
+ * @observable
33
+ * @member {Number} #loaded
34
+ */
35
+ this.set('loaded', 0);
36
+ reader.onprogress = evt => {
37
+ this.loaded = evt.loaded;
38
+ };
39
+ }
40
+ /**
41
+ * Returns error that occurred during file reading.
42
+ *
43
+ * @returns {Error}
44
+ */
45
+ get error() {
46
+ return this._reader.error;
47
+ }
48
+ /**
49
+ * Holds the data of an already loaded file. The file must be first loaded
50
+ * by using {@link module:upload/filereader~FileReader#read `read()`}.
51
+ *
52
+ * @type {File|undefined}
53
+ */
54
+ get data() {
55
+ return this._data;
56
+ }
57
+ /**
58
+ * Reads the provided file.
59
+ *
60
+ * @param {File} file Native File object.
61
+ * @returns {Promise.<String>} Returns a promise that will be resolved with file's content.
62
+ * The promise will be rejected in case of an error or when the reading process is aborted.
63
+ */
64
+ read(file) {
65
+ const reader = this._reader;
66
+ this.total = file.size;
67
+ return new Promise((resolve, reject) => {
68
+ reader.onload = () => {
69
+ const result = reader.result;
70
+ this._data = result;
71
+ resolve(result);
72
+ };
73
+ reader.onerror = () => {
74
+ reject('error');
75
+ };
76
+ reader.onabort = () => {
77
+ reject('aborted');
78
+ };
79
+ this._reader.readAsDataURL(file);
80
+ });
81
+ }
82
+ /**
83
+ * Aborts file reader.
84
+ */
85
+ abort() {
86
+ this._reader.abort();
87
+ }
106
88
  }
107
-
108
- mix( FileReader, ObservableMixin );