@ckeditor/ckeditor5-upload 39.0.1 → 40.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (76) hide show
  1. package/CHANGELOG.md +2 -2
  2. package/LICENSE.md +1 -1
  3. package/README.md +3 -3
  4. package/lang/translations/ar.po +1 -0
  5. package/lang/translations/az.po +1 -0
  6. package/lang/translations/bg.po +1 -0
  7. package/lang/translations/bn.po +1 -0
  8. package/lang/translations/ca.po +1 -0
  9. package/lang/translations/cs.po +1 -0
  10. package/lang/translations/da.po +1 -0
  11. package/lang/translations/de-ch.po +1 -0
  12. package/lang/translations/de.po +1 -0
  13. package/lang/translations/el.po +1 -0
  14. package/lang/translations/en-au.po +1 -0
  15. package/lang/translations/en-gb.po +1 -0
  16. package/lang/translations/en.po +1 -0
  17. package/lang/translations/es-co.po +1 -0
  18. package/lang/translations/es.po +1 -0
  19. package/lang/translations/et.po +1 -0
  20. package/lang/translations/fa.po +1 -0
  21. package/lang/translations/fi.po +1 -0
  22. package/lang/translations/fr.po +1 -0
  23. package/lang/translations/gl.po +1 -0
  24. package/lang/translations/he.po +1 -0
  25. package/lang/translations/hi.po +1 -0
  26. package/lang/translations/hr.po +1 -0
  27. package/lang/translations/hu.po +1 -0
  28. package/lang/translations/id.po +1 -0
  29. package/lang/translations/it.po +1 -0
  30. package/lang/translations/ja.po +1 -0
  31. package/lang/translations/ko.po +1 -0
  32. package/lang/translations/ku.po +1 -0
  33. package/lang/translations/lt.po +1 -0
  34. package/lang/translations/lv.po +1 -0
  35. package/lang/translations/ms.po +1 -0
  36. package/lang/translations/nb.po +1 -0
  37. package/lang/translations/ne.po +1 -0
  38. package/lang/translations/nl.po +1 -0
  39. package/lang/translations/no.po +1 -0
  40. package/lang/translations/pl.po +1 -0
  41. package/lang/translations/pt-br.po +1 -0
  42. package/lang/translations/pt.po +1 -0
  43. package/lang/translations/ro.po +1 -0
  44. package/lang/translations/ru.po +1 -0
  45. package/lang/translations/sk.po +1 -0
  46. package/lang/translations/sq.po +1 -0
  47. package/lang/translations/sr-latn.po +1 -0
  48. package/lang/translations/sr.po +1 -0
  49. package/lang/translations/sv.po +1 -0
  50. package/lang/translations/th.po +1 -0
  51. package/lang/translations/tk.po +1 -0
  52. package/lang/translations/tr.po +1 -0
  53. package/lang/translations/ug.po +1 -0
  54. package/lang/translations/uk.po +1 -0
  55. package/lang/translations/ur.po +1 -0
  56. package/lang/translations/uz.po +1 -0
  57. package/lang/translations/vi.po +1 -0
  58. package/lang/translations/zh-cn.po +1 -0
  59. package/lang/translations/zh.po +1 -0
  60. package/package.json +4 -8
  61. package/src/adapters/base64uploadadapter.d.ts +33 -33
  62. package/src/adapters/base64uploadadapter.js +81 -81
  63. package/src/adapters/simpleuploadadapter.d.ts +48 -48
  64. package/src/adapters/simpleuploadadapter.js +175 -175
  65. package/src/augmentation.d.ts +20 -20
  66. package/src/augmentation.js +5 -5
  67. package/src/filereader.d.ts +56 -56
  68. package/src/filereader.js +71 -71
  69. package/src/filerepository.d.ts +342 -342
  70. package/src/filerepository.js +383 -383
  71. package/src/index.d.ts +13 -13
  72. package/src/index.js +12 -12
  73. package/src/ui/filedialogbuttonview.d.ts +84 -84
  74. package/src/ui/filedialogbuttonview.js +108 -108
  75. package/src/uploadconfig.d.ts +90 -90
  76. package/src/uploadconfig.js +5 -5
@@ -1,175 +1,175 @@
1
- /**
2
- * @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
3
- * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
- */
5
- /**
6
- * @module upload/adapters/simpleuploadadapter
7
- */
8
- /* globals XMLHttpRequest, FormData */
9
- import { Plugin } from '@ckeditor/ckeditor5-core';
10
- import FileRepository from '../filerepository';
11
- import { logWarning } from '@ckeditor/ckeditor5-utils';
12
- /**
13
- * The Simple upload adapter allows uploading images to an application running on your server using
14
- * the [`XMLHttpRequest`](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest) API with a
15
- * minimal {@link module:upload/uploadconfig~SimpleUploadConfig editor configuration}.
16
- *
17
- * ```ts
18
- * ClassicEditor
19
- * .create( document.querySelector( '#editor' ), {
20
- * simpleUpload: {
21
- * uploadUrl: 'http://example.com',
22
- * headers: {
23
- * ...
24
- * }
25
- * }
26
- * } )
27
- * .then( ... )
28
- * .catch( ... );
29
- * ```
30
- *
31
- * See the {@glink features/images/image-upload/simple-upload-adapter "Simple upload adapter"} guide to learn how to
32
- * learn more about the feature (configuration, server–side requirements, etc.).
33
- *
34
- * Check out the {@glink features/images/image-upload/image-upload comprehensive "Image upload overview"} to learn about
35
- * other ways to upload images into CKEditor 5.
36
- */
37
- export default class SimpleUploadAdapter extends Plugin {
38
- /**
39
- * @inheritDoc
40
- */
41
- static get requires() {
42
- return [FileRepository];
43
- }
44
- /**
45
- * @inheritDoc
46
- */
47
- static get pluginName() {
48
- return 'SimpleUploadAdapter';
49
- }
50
- /**
51
- * @inheritDoc
52
- */
53
- init() {
54
- const options = this.editor.config.get('simpleUpload');
55
- if (!options) {
56
- return;
57
- }
58
- if (!options.uploadUrl) {
59
- /**
60
- * The {@link module:upload/uploadconfig~SimpleUploadConfig#uploadUrl `config.simpleUpload.uploadUrl`}
61
- * configuration required by the {@link module:upload/adapters/simpleuploadadapter~SimpleUploadAdapter `SimpleUploadAdapter`}
62
- * is missing. Make sure the correct URL is specified for the image upload to work properly.
63
- *
64
- * @error simple-upload-adapter-missing-uploadurl
65
- */
66
- logWarning('simple-upload-adapter-missing-uploadurl');
67
- return;
68
- }
69
- this.editor.plugins.get(FileRepository).createUploadAdapter = loader => {
70
- return new Adapter(loader, options);
71
- };
72
- }
73
- }
74
- /**
75
- * Upload adapter.
76
- */
77
- class Adapter {
78
- /**
79
- * Creates a new adapter instance.
80
- */
81
- constructor(loader, options) {
82
- this.loader = loader;
83
- this.options = options;
84
- }
85
- /**
86
- * Starts the upload process.
87
- *
88
- * @see module:upload/filerepository~UploadAdapter#upload
89
- */
90
- upload() {
91
- return this.loader.file
92
- .then(file => new Promise((resolve, reject) => {
93
- this._initRequest();
94
- this._initListeners(resolve, reject, file);
95
- this._sendRequest(file);
96
- }));
97
- }
98
- /**
99
- * Aborts the upload process.
100
- *
101
- * @see module:upload/filerepository~UploadAdapter#abort
102
- */
103
- abort() {
104
- if (this.xhr) {
105
- this.xhr.abort();
106
- }
107
- }
108
- /**
109
- * Initializes the `XMLHttpRequest` object using the URL specified as
110
- * {@link module:upload/uploadconfig~SimpleUploadConfig#uploadUrl `simpleUpload.uploadUrl`} in the editor's
111
- * configuration.
112
- */
113
- _initRequest() {
114
- const xhr = this.xhr = new XMLHttpRequest();
115
- xhr.open('POST', this.options.uploadUrl, true);
116
- xhr.responseType = 'json';
117
- }
118
- /**
119
- * Initializes XMLHttpRequest listeners
120
- *
121
- * @param resolve Callback function to be called when the request is successful.
122
- * @param reject Callback function to be called when the request cannot be completed.
123
- * @param file Native File object.
124
- */
125
- _initListeners(resolve, reject, file) {
126
- const xhr = this.xhr;
127
- const loader = this.loader;
128
- const genericErrorText = `Couldn't upload file: ${file.name}.`;
129
- xhr.addEventListener('error', () => reject(genericErrorText));
130
- xhr.addEventListener('abort', () => reject());
131
- xhr.addEventListener('load', () => {
132
- const response = xhr.response;
133
- if (!response || response.error) {
134
- return reject(response && response.error && response.error.message ? response.error.message : genericErrorText);
135
- }
136
- const urls = response.url ? { default: response.url } : response.urls;
137
- // Resolve with the normalized `urls` property and pass the rest of the response
138
- // to allow customizing the behavior of features relying on the upload adapters.
139
- resolve({
140
- ...response,
141
- urls
142
- });
143
- });
144
- // Upload progress when it is supported.
145
- /* istanbul ignore else -- @preserve */
146
- if (xhr.upload) {
147
- xhr.upload.addEventListener('progress', evt => {
148
- if (evt.lengthComputable) {
149
- loader.uploadTotal = evt.total;
150
- loader.uploaded = evt.loaded;
151
- }
152
- });
153
- }
154
- }
155
- /**
156
- * Prepares the data and sends the request.
157
- *
158
- * @param file File instance to be uploaded.
159
- */
160
- _sendRequest(file) {
161
- // Set headers if specified.
162
- const headers = this.options.headers || {};
163
- // Use the withCredentials flag if specified.
164
- const withCredentials = this.options.withCredentials || false;
165
- for (const headerName of Object.keys(headers)) {
166
- this.xhr.setRequestHeader(headerName, headers[headerName]);
167
- }
168
- this.xhr.withCredentials = withCredentials;
169
- // Prepare the form data.
170
- const data = new FormData();
171
- data.append('upload', file);
172
- // Send the request.
173
- this.xhr.send(data);
174
- }
175
- }
1
+ /**
2
+ * @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
3
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
+ */
5
+ /**
6
+ * @module upload/adapters/simpleuploadadapter
7
+ */
8
+ /* globals XMLHttpRequest, FormData */
9
+ import { Plugin } from '@ckeditor/ckeditor5-core';
10
+ import FileRepository from '../filerepository';
11
+ import { logWarning } from '@ckeditor/ckeditor5-utils';
12
+ /**
13
+ * The Simple upload adapter allows uploading images to an application running on your server using
14
+ * the [`XMLHttpRequest`](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest) API with a
15
+ * minimal {@link module:upload/uploadconfig~SimpleUploadConfig editor configuration}.
16
+ *
17
+ * ```ts
18
+ * ClassicEditor
19
+ * .create( document.querySelector( '#editor' ), {
20
+ * simpleUpload: {
21
+ * uploadUrl: 'http://example.com',
22
+ * headers: {
23
+ * ...
24
+ * }
25
+ * }
26
+ * } )
27
+ * .then( ... )
28
+ * .catch( ... );
29
+ * ```
30
+ *
31
+ * See the {@glink features/images/image-upload/simple-upload-adapter "Simple upload adapter"} guide to learn how to
32
+ * learn more about the feature (configuration, server–side requirements, etc.).
33
+ *
34
+ * Check out the {@glink features/images/image-upload/image-upload comprehensive "Image upload overview"} to learn about
35
+ * other ways to upload images into CKEditor 5.
36
+ */
37
+ export default class SimpleUploadAdapter extends Plugin {
38
+ /**
39
+ * @inheritDoc
40
+ */
41
+ static get requires() {
42
+ return [FileRepository];
43
+ }
44
+ /**
45
+ * @inheritDoc
46
+ */
47
+ static get pluginName() {
48
+ return 'SimpleUploadAdapter';
49
+ }
50
+ /**
51
+ * @inheritDoc
52
+ */
53
+ init() {
54
+ const options = this.editor.config.get('simpleUpload');
55
+ if (!options) {
56
+ return;
57
+ }
58
+ if (!options.uploadUrl) {
59
+ /**
60
+ * The {@link module:upload/uploadconfig~SimpleUploadConfig#uploadUrl `config.simpleUpload.uploadUrl`}
61
+ * configuration required by the {@link module:upload/adapters/simpleuploadadapter~SimpleUploadAdapter `SimpleUploadAdapter`}
62
+ * is missing. Make sure the correct URL is specified for the image upload to work properly.
63
+ *
64
+ * @error simple-upload-adapter-missing-uploadurl
65
+ */
66
+ logWarning('simple-upload-adapter-missing-uploadurl');
67
+ return;
68
+ }
69
+ this.editor.plugins.get(FileRepository).createUploadAdapter = loader => {
70
+ return new Adapter(loader, options);
71
+ };
72
+ }
73
+ }
74
+ /**
75
+ * Upload adapter.
76
+ */
77
+ class Adapter {
78
+ /**
79
+ * Creates a new adapter instance.
80
+ */
81
+ constructor(loader, options) {
82
+ this.loader = loader;
83
+ this.options = options;
84
+ }
85
+ /**
86
+ * Starts the upload process.
87
+ *
88
+ * @see module:upload/filerepository~UploadAdapter#upload
89
+ */
90
+ upload() {
91
+ return this.loader.file
92
+ .then(file => new Promise((resolve, reject) => {
93
+ this._initRequest();
94
+ this._initListeners(resolve, reject, file);
95
+ this._sendRequest(file);
96
+ }));
97
+ }
98
+ /**
99
+ * Aborts the upload process.
100
+ *
101
+ * @see module:upload/filerepository~UploadAdapter#abort
102
+ */
103
+ abort() {
104
+ if (this.xhr) {
105
+ this.xhr.abort();
106
+ }
107
+ }
108
+ /**
109
+ * Initializes the `XMLHttpRequest` object using the URL specified as
110
+ * {@link module:upload/uploadconfig~SimpleUploadConfig#uploadUrl `simpleUpload.uploadUrl`} in the editor's
111
+ * configuration.
112
+ */
113
+ _initRequest() {
114
+ const xhr = this.xhr = new XMLHttpRequest();
115
+ xhr.open('POST', this.options.uploadUrl, true);
116
+ xhr.responseType = 'json';
117
+ }
118
+ /**
119
+ * Initializes XMLHttpRequest listeners
120
+ *
121
+ * @param resolve Callback function to be called when the request is successful.
122
+ * @param reject Callback function to be called when the request cannot be completed.
123
+ * @param file Native File object.
124
+ */
125
+ _initListeners(resolve, reject, file) {
126
+ const xhr = this.xhr;
127
+ const loader = this.loader;
128
+ const genericErrorText = `Couldn't upload file: ${file.name}.`;
129
+ xhr.addEventListener('error', () => reject(genericErrorText));
130
+ xhr.addEventListener('abort', () => reject());
131
+ xhr.addEventListener('load', () => {
132
+ const response = xhr.response;
133
+ if (!response || response.error) {
134
+ return reject(response && response.error && response.error.message ? response.error.message : genericErrorText);
135
+ }
136
+ const urls = response.url ? { default: response.url } : response.urls;
137
+ // Resolve with the normalized `urls` property and pass the rest of the response
138
+ // to allow customizing the behavior of features relying on the upload adapters.
139
+ resolve({
140
+ ...response,
141
+ urls
142
+ });
143
+ });
144
+ // Upload progress when it is supported.
145
+ /* istanbul ignore else -- @preserve */
146
+ if (xhr.upload) {
147
+ xhr.upload.addEventListener('progress', evt => {
148
+ if (evt.lengthComputable) {
149
+ loader.uploadTotal = evt.total;
150
+ loader.uploaded = evt.loaded;
151
+ }
152
+ });
153
+ }
154
+ }
155
+ /**
156
+ * Prepares the data and sends the request.
157
+ *
158
+ * @param file File instance to be uploaded.
159
+ */
160
+ _sendRequest(file) {
161
+ // Set headers if specified.
162
+ const headers = this.options.headers || {};
163
+ // Use the withCredentials flag if specified.
164
+ const withCredentials = this.options.withCredentials || false;
165
+ for (const headerName of Object.keys(headers)) {
166
+ this.xhr.setRequestHeader(headerName, headers[headerName]);
167
+ }
168
+ this.xhr.withCredentials = withCredentials;
169
+ // Prepare the form data.
170
+ const data = new FormData();
171
+ data.append('upload', file);
172
+ // Send the request.
173
+ this.xhr.send(data);
174
+ }
175
+ }
@@ -1,20 +1,20 @@
1
- /**
2
- * @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
3
- * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
- */
5
- import type { SimpleUploadConfig, FileRepository, SimpleUploadAdapter, Base64UploadAdapter } from './index';
6
- declare module '@ckeditor/ckeditor5-core' {
7
- interface EditorConfig {
8
- /**
9
- * The configuration of the {@link module:upload/adapters/simpleuploadadapter~SimpleUploadAdapter simple upload adapter}.
10
- *
11
- * Read more in {@link module:upload/uploadconfig~SimpleUploadConfig}.
12
- */
13
- simpleUpload?: SimpleUploadConfig;
14
- }
15
- interface PluginsMap {
16
- [FileRepository.pluginName]: FileRepository;
17
- [SimpleUploadAdapter.pluginName]: SimpleUploadAdapter;
18
- [Base64UploadAdapter.pluginName]: Base64UploadAdapter;
19
- }
20
- }
1
+ /**
2
+ * @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
3
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
+ */
5
+ import type { SimpleUploadConfig, FileRepository, SimpleUploadAdapter, Base64UploadAdapter } from './index';
6
+ declare module '@ckeditor/ckeditor5-core' {
7
+ interface EditorConfig {
8
+ /**
9
+ * The configuration of the {@link module:upload/adapters/simpleuploadadapter~SimpleUploadAdapter simple upload adapter}.
10
+ *
11
+ * Read more in {@link module:upload/uploadconfig~SimpleUploadConfig}.
12
+ */
13
+ simpleUpload?: SimpleUploadConfig;
14
+ }
15
+ interface PluginsMap {
16
+ [FileRepository.pluginName]: FileRepository;
17
+ [SimpleUploadAdapter.pluginName]: SimpleUploadAdapter;
18
+ [Base64UploadAdapter.pluginName]: Base64UploadAdapter;
19
+ }
20
+ }
@@ -1,5 +1,5 @@
1
- /**
2
- * @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
3
- * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
- */
5
- export {};
1
+ /**
2
+ * @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
3
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
+ */
5
+ export {};
@@ -1,56 +1,56 @@
1
- /**
2
- * @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
3
- * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
- */
5
- declare const FileReader_base: {
6
- new (): import("@ckeditor/ckeditor5-utils").Observable;
7
- prototype: import("@ckeditor/ckeditor5-utils").Observable;
8
- };
9
- /**
10
- * Wrapper over the native `FileReader`.
11
- */
12
- export default class FileReader extends FileReader_base {
13
- total: number;
14
- /**
15
- * Instance of native FileReader.
16
- */
17
- private readonly _reader;
18
- /**
19
- * Holds the data of an already loaded file. The file must be first loaded
20
- * by using {@link module:upload/filereader~FileReader#read `read()`}.
21
- */
22
- private _data?;
23
- /**
24
- * Number of bytes loaded.
25
- *
26
- * @readonly
27
- * @observable
28
- */
29
- loaded: number;
30
- /**
31
- * Creates an instance of the FileReader.
32
- */
33
- constructor();
34
- /**
35
- * Returns error that occurred during file reading.
36
- */
37
- get error(): DOMException | null;
38
- /**
39
- * Holds the data of an already loaded file. The file must be first loaded
40
- * by using {@link module:upload/filereader~FileReader#read `read()`}.
41
- */
42
- get data(): string | undefined;
43
- /**
44
- * Reads the provided file.
45
- *
46
- * @param file Native File object.
47
- * @returns Returns a promise that will be resolved with file's content.
48
- * The promise will be rejected in case of an error or when the reading process is aborted.
49
- */
50
- read(file: File): Promise<string>;
51
- /**
52
- * Aborts file reader.
53
- */
54
- abort(): void;
55
- }
56
- export {};
1
+ /**
2
+ * @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
3
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
+ */
5
+ declare const FileReader_base: {
6
+ new (): import("@ckeditor/ckeditor5-utils").Observable;
7
+ prototype: import("@ckeditor/ckeditor5-utils").Observable;
8
+ };
9
+ /**
10
+ * Wrapper over the native `FileReader`.
11
+ */
12
+ export default class FileReader extends FileReader_base {
13
+ total: number;
14
+ /**
15
+ * Instance of native FileReader.
16
+ */
17
+ private readonly _reader;
18
+ /**
19
+ * Holds the data of an already loaded file. The file must be first loaded
20
+ * by using {@link module:upload/filereader~FileReader#read `read()`}.
21
+ */
22
+ private _data?;
23
+ /**
24
+ * Number of bytes loaded.
25
+ *
26
+ * @readonly
27
+ * @observable
28
+ */
29
+ loaded: number;
30
+ /**
31
+ * Creates an instance of the FileReader.
32
+ */
33
+ constructor();
34
+ /**
35
+ * Returns error that occurred during file reading.
36
+ */
37
+ get error(): DOMException | null;
38
+ /**
39
+ * Holds the data of an already loaded file. The file must be first loaded
40
+ * by using {@link module:upload/filereader~FileReader#read `read()`}.
41
+ */
42
+ get data(): string | undefined;
43
+ /**
44
+ * Reads the provided file.
45
+ *
46
+ * @param file Native File object.
47
+ * @returns Returns a promise that will be resolved with file's content.
48
+ * The promise will be rejected in case of an error or when the reading process is aborted.
49
+ */
50
+ read(file: File): Promise<string>;
51
+ /**
52
+ * Aborts file reader.
53
+ */
54
+ abort(): void;
55
+ }
56
+ export {};
package/src/filereader.js CHANGED
@@ -1,71 +1,71 @@
1
- /**
2
- * @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
3
- * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
- */
5
- /**
6
- * @module upload/filereader
7
- */
8
- /* globals window */
9
- import { ObservableMixin } from '@ckeditor/ckeditor5-utils';
10
- /**
11
- * Wrapper over the native `FileReader`.
12
- */
13
- export default class FileReader extends ObservableMixin() {
14
- /**
15
- * Creates an instance of the FileReader.
16
- */
17
- constructor() {
18
- super();
19
- const reader = new window.FileReader();
20
- this._reader = reader;
21
- this._data = undefined;
22
- this.set('loaded', 0);
23
- reader.onprogress = evt => {
24
- this.loaded = evt.loaded;
25
- };
26
- }
27
- /**
28
- * Returns error that occurred during file reading.
29
- */
30
- get error() {
31
- return this._reader.error;
32
- }
33
- /**
34
- * Holds the data of an already loaded file. The file must be first loaded
35
- * by using {@link module:upload/filereader~FileReader#read `read()`}.
36
- */
37
- get data() {
38
- return this._data;
39
- }
40
- /**
41
- * Reads the provided file.
42
- *
43
- * @param file Native File object.
44
- * @returns Returns a promise that will be resolved with file's content.
45
- * The promise will be rejected in case of an error or when the reading process is aborted.
46
- */
47
- read(file) {
48
- const reader = this._reader;
49
- this.total = file.size;
50
- return new Promise((resolve, reject) => {
51
- reader.onload = () => {
52
- const result = reader.result;
53
- this._data = result;
54
- resolve(result);
55
- };
56
- reader.onerror = () => {
57
- reject('error');
58
- };
59
- reader.onabort = () => {
60
- reject('aborted');
61
- };
62
- this._reader.readAsDataURL(file);
63
- });
64
- }
65
- /**
66
- * Aborts file reader.
67
- */
68
- abort() {
69
- this._reader.abort();
70
- }
71
- }
1
+ /**
2
+ * @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
3
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
+ */
5
+ /**
6
+ * @module upload/filereader
7
+ */
8
+ /* globals window */
9
+ import { ObservableMixin } from '@ckeditor/ckeditor5-utils';
10
+ /**
11
+ * Wrapper over the native `FileReader`.
12
+ */
13
+ export default class FileReader extends ObservableMixin() {
14
+ /**
15
+ * Creates an instance of the FileReader.
16
+ */
17
+ constructor() {
18
+ super();
19
+ const reader = new window.FileReader();
20
+ this._reader = reader;
21
+ this._data = undefined;
22
+ this.set('loaded', 0);
23
+ reader.onprogress = evt => {
24
+ this.loaded = evt.loaded;
25
+ };
26
+ }
27
+ /**
28
+ * Returns error that occurred during file reading.
29
+ */
30
+ get error() {
31
+ return this._reader.error;
32
+ }
33
+ /**
34
+ * Holds the data of an already loaded file. The file must be first loaded
35
+ * by using {@link module:upload/filereader~FileReader#read `read()`}.
36
+ */
37
+ get data() {
38
+ return this._data;
39
+ }
40
+ /**
41
+ * Reads the provided file.
42
+ *
43
+ * @param file Native File object.
44
+ * @returns Returns a promise that will be resolved with file's content.
45
+ * The promise will be rejected in case of an error or when the reading process is aborted.
46
+ */
47
+ read(file) {
48
+ const reader = this._reader;
49
+ this.total = file.size;
50
+ return new Promise((resolve, reject) => {
51
+ reader.onload = () => {
52
+ const result = reader.result;
53
+ this._data = result;
54
+ resolve(result);
55
+ };
56
+ reader.onerror = () => {
57
+ reject('error');
58
+ };
59
+ reader.onabort = () => {
60
+ reject('aborted');
61
+ };
62
+ this._reader.readAsDataURL(file);
63
+ });
64
+ }
65
+ /**
66
+ * Aborts file reader.
67
+ */
68
+ abort() {
69
+ this._reader.abort();
70
+ }
71
+ }