@ckeditor/ckeditor5-upload 47.6.1-alpha.1 → 48.0.0-alpha.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.
- package/ckeditor5-metadata.json +2 -2
- package/{src → dist}/adapters/simpleuploadadapter.d.ts +1 -1
- package/dist/index.css +3 -0
- package/dist/index.css.map +1 -0
- package/dist/index.js +2 -3
- package/dist/index.js.map +1 -1
- package/package.json +20 -40
- package/lang/contexts.json +0 -3
- package/lang/translations/af.po +0 -16
- package/lang/translations/ar.po +0 -16
- package/lang/translations/ast.po +0 -16
- package/lang/translations/az.po +0 -16
- package/lang/translations/be.po +0 -16
- package/lang/translations/bg.po +0 -16
- package/lang/translations/bn.po +0 -16
- package/lang/translations/bs.po +0 -16
- package/lang/translations/ca.po +0 -16
- package/lang/translations/cs.po +0 -16
- package/lang/translations/da.po +0 -16
- package/lang/translations/de-ch.po +0 -16
- package/lang/translations/de.po +0 -16
- package/lang/translations/el.po +0 -16
- package/lang/translations/en-au.po +0 -16
- package/lang/translations/en-gb.po +0 -16
- package/lang/translations/en.po +0 -16
- package/lang/translations/eo.po +0 -16
- package/lang/translations/es-co.po +0 -16
- package/lang/translations/es.po +0 -16
- package/lang/translations/et.po +0 -16
- package/lang/translations/eu.po +0 -16
- package/lang/translations/fa.po +0 -16
- package/lang/translations/fi.po +0 -16
- package/lang/translations/fr.po +0 -16
- package/lang/translations/gl.po +0 -16
- package/lang/translations/gu.po +0 -16
- package/lang/translations/he.po +0 -16
- package/lang/translations/hi.po +0 -16
- package/lang/translations/hr.po +0 -16
- package/lang/translations/hu.po +0 -16
- package/lang/translations/hy.po +0 -16
- package/lang/translations/id.po +0 -16
- package/lang/translations/it.po +0 -16
- package/lang/translations/ja.po +0 -16
- package/lang/translations/jv.po +0 -16
- package/lang/translations/kk.po +0 -16
- package/lang/translations/km.po +0 -16
- package/lang/translations/kn.po +0 -16
- package/lang/translations/ko.po +0 -16
- package/lang/translations/ku.po +0 -16
- package/lang/translations/lt.po +0 -16
- package/lang/translations/lv.po +0 -16
- package/lang/translations/ms.po +0 -16
- package/lang/translations/nb.po +0 -16
- package/lang/translations/ne.po +0 -16
- package/lang/translations/nl.po +0 -16
- package/lang/translations/no.po +0 -16
- package/lang/translations/oc.po +0 -16
- package/lang/translations/pl.po +0 -16
- package/lang/translations/pt-br.po +0 -16
- package/lang/translations/pt.po +0 -16
- package/lang/translations/ro.po +0 -16
- package/lang/translations/ru.po +0 -16
- package/lang/translations/si.po +0 -16
- package/lang/translations/sk.po +0 -16
- package/lang/translations/sl.po +0 -16
- package/lang/translations/sq.po +0 -16
- package/lang/translations/sr-latn.po +0 -16
- package/lang/translations/sr.po +0 -16
- package/lang/translations/sv.po +0 -16
- package/lang/translations/th.po +0 -16
- package/lang/translations/ti.po +0 -16
- package/lang/translations/tk.po +0 -16
- package/lang/translations/tr.po +0 -16
- package/lang/translations/tt.po +0 -16
- package/lang/translations/ug.po +0 -16
- package/lang/translations/uk.po +0 -16
- package/lang/translations/ur.po +0 -16
- package/lang/translations/uz.po +0 -16
- package/lang/translations/vi.po +0 -16
- package/lang/translations/zh-cn.po +0 -16
- package/lang/translations/zh.po +0 -16
- package/src/adapters/base64uploadadapter.js +0 -104
- package/src/adapters/simpleuploadadapter.js +0 -205
- package/src/augmentation.js +0 -5
- package/src/filereader.js +0 -80
- package/src/filerepository.js +0 -408
- package/src/index.js +0 -12
- package/src/uploadconfig.js +0 -5
- /package/{src → dist}/adapters/base64uploadadapter.d.ts +0 -0
- /package/{src → dist}/augmentation.d.ts +0 -0
- /package/{src → dist}/filereader.d.ts +0 -0
- /package/{src → dist}/filerepository.d.ts +0 -0
- /package/{src → dist}/index.d.ts +0 -0
- /package/{src → dist}/uploadconfig.d.ts +0 -0
package/lang/translations/uz.po
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
# Copyright (c) 2003-2026, CKSource Holding sp. z o.o. All rights reserved.
|
|
2
|
-
#
|
|
3
|
-
# Want to contribute to this file? Submit your changes via a GitHub Pull Request.
|
|
4
|
-
#
|
|
5
|
-
# Check out the official contributor's guide:
|
|
6
|
-
# https://ckeditor.com/docs/ckeditor5/latest/framework/guides/contributing/contributing.html
|
|
7
|
-
#
|
|
8
|
-
msgid ""
|
|
9
|
-
msgstr ""
|
|
10
|
-
"Language: uz\n"
|
|
11
|
-
"Plural-Forms: nplurals=2; plural=(n > 1);\n"
|
|
12
|
-
"Content-Type: text/plain; charset=UTF-8\n"
|
|
13
|
-
|
|
14
|
-
msgctxt "A message that a file upload is in progress."
|
|
15
|
-
msgid "Upload in progress"
|
|
16
|
-
msgstr "Yuklanmoqda"
|
package/lang/translations/vi.po
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
# Copyright (c) 2003-2026, CKSource Holding sp. z o.o. All rights reserved.
|
|
2
|
-
#
|
|
3
|
-
# Want to contribute to this file? Submit your changes via a GitHub Pull Request.
|
|
4
|
-
#
|
|
5
|
-
# Check out the official contributor's guide:
|
|
6
|
-
# https://ckeditor.com/docs/ckeditor5/latest/framework/guides/contributing/contributing.html
|
|
7
|
-
#
|
|
8
|
-
msgid ""
|
|
9
|
-
msgstr ""
|
|
10
|
-
"Language: vi\n"
|
|
11
|
-
"Plural-Forms: nplurals=1; plural=0;\n"
|
|
12
|
-
"Content-Type: text/plain; charset=UTF-8\n"
|
|
13
|
-
|
|
14
|
-
msgctxt "A message that a file upload is in progress."
|
|
15
|
-
msgid "Upload in progress"
|
|
16
|
-
msgstr "Đang tải lên"
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
# Copyright (c) 2003-2026, CKSource Holding sp. z o.o. All rights reserved.
|
|
2
|
-
#
|
|
3
|
-
# Want to contribute to this file? Submit your changes via a GitHub Pull Request.
|
|
4
|
-
#
|
|
5
|
-
# Check out the official contributor's guide:
|
|
6
|
-
# https://ckeditor.com/docs/ckeditor5/latest/framework/guides/contributing/contributing.html
|
|
7
|
-
#
|
|
8
|
-
msgid ""
|
|
9
|
-
msgstr ""
|
|
10
|
-
"Language: zh_CN\n"
|
|
11
|
-
"Plural-Forms: nplurals=1; plural=0;\n"
|
|
12
|
-
"Content-Type: text/plain; charset=UTF-8\n"
|
|
13
|
-
|
|
14
|
-
msgctxt "A message that a file upload is in progress."
|
|
15
|
-
msgid "Upload in progress"
|
|
16
|
-
msgstr "正在上传"
|
package/lang/translations/zh.po
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
# Copyright (c) 2003-2026, CKSource Holding sp. z o.o. All rights reserved.
|
|
2
|
-
#
|
|
3
|
-
# Want to contribute to this file? Submit your changes via a GitHub Pull Request.
|
|
4
|
-
#
|
|
5
|
-
# Check out the official contributor's guide:
|
|
6
|
-
# https://ckeditor.com/docs/ckeditor5/latest/framework/guides/contributing/contributing.html
|
|
7
|
-
#
|
|
8
|
-
msgid ""
|
|
9
|
-
msgstr ""
|
|
10
|
-
"Language: zh_TW\n"
|
|
11
|
-
"Plural-Forms: nplurals=1; plural=0;\n"
|
|
12
|
-
"Content-Type: text/plain; charset=UTF-8\n"
|
|
13
|
-
|
|
14
|
-
msgctxt "A message that a file upload is in progress."
|
|
15
|
-
msgid "Upload in progress"
|
|
16
|
-
msgstr "正在上傳"
|
|
@@ -1,104 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @license Copyright (c) 2003-2026, CKSource Holding sp. z o.o. All rights reserved.
|
|
3
|
-
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-licensing-options
|
|
4
|
-
*/
|
|
5
|
-
/**
|
|
6
|
-
* @module upload/adapters/base64uploadadapter
|
|
7
|
-
*/
|
|
8
|
-
import { Plugin } from '@ckeditor/ckeditor5-core';
|
|
9
|
-
import { FileRepository } from '../filerepository.js';
|
|
10
|
-
/**
|
|
11
|
-
* A plugin that converts images inserted into the editor into [Base64 strings](https://en.wikipedia.org/wiki/Base64)
|
|
12
|
-
* in the {@glink getting-started/setup/getting-and-setting-data editor output}.
|
|
13
|
-
*
|
|
14
|
-
* This kind of image upload does not require server processing – images are stored with the rest of the text and
|
|
15
|
-
* displayed by the web browser without additional requests.
|
|
16
|
-
*
|
|
17
|
-
* Check out the {@glink features/images/image-upload/image-upload comprehensive "Image upload overview"} to learn about
|
|
18
|
-
* other ways to upload images into CKEditor 5.
|
|
19
|
-
*/
|
|
20
|
-
export class Base64UploadAdapter extends Plugin {
|
|
21
|
-
/**
|
|
22
|
-
* @inheritDoc
|
|
23
|
-
*/
|
|
24
|
-
static get requires() {
|
|
25
|
-
return [FileRepository];
|
|
26
|
-
}
|
|
27
|
-
/**
|
|
28
|
-
* @inheritDoc
|
|
29
|
-
*/
|
|
30
|
-
static get pluginName() {
|
|
31
|
-
return 'Base64UploadAdapter';
|
|
32
|
-
}
|
|
33
|
-
/**
|
|
34
|
-
* @inheritDoc
|
|
35
|
-
* @internal
|
|
36
|
-
*/
|
|
37
|
-
static get licenseFeatureCode() {
|
|
38
|
-
return 'B64A';
|
|
39
|
-
}
|
|
40
|
-
/**
|
|
41
|
-
* @inheritDoc
|
|
42
|
-
*/
|
|
43
|
-
static get isOfficialPlugin() {
|
|
44
|
-
return true;
|
|
45
|
-
}
|
|
46
|
-
/**
|
|
47
|
-
* @inheritDoc
|
|
48
|
-
*/
|
|
49
|
-
static get isPremiumPlugin() {
|
|
50
|
-
return true;
|
|
51
|
-
}
|
|
52
|
-
/**
|
|
53
|
-
* @inheritDoc
|
|
54
|
-
*/
|
|
55
|
-
init() {
|
|
56
|
-
this.editor.plugins.get(FileRepository).createUploadAdapter = loader => new Adapter(loader);
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
/**
|
|
60
|
-
* The upload adapter that converts images inserted into the editor into Base64 strings.
|
|
61
|
-
*/
|
|
62
|
-
class Adapter {
|
|
63
|
-
/**
|
|
64
|
-
* `FileLoader` instance to use during the upload.
|
|
65
|
-
*/
|
|
66
|
-
loader;
|
|
67
|
-
reader;
|
|
68
|
-
/**
|
|
69
|
-
* Creates a new adapter instance.
|
|
70
|
-
*/
|
|
71
|
-
constructor(loader) {
|
|
72
|
-
this.loader = loader;
|
|
73
|
-
}
|
|
74
|
-
/**
|
|
75
|
-
* Starts the upload process.
|
|
76
|
-
*
|
|
77
|
-
* @see module:upload/filerepository~UploadAdapter#upload
|
|
78
|
-
*/
|
|
79
|
-
upload() {
|
|
80
|
-
return new Promise((resolve, reject) => {
|
|
81
|
-
const reader = this.reader = new window.FileReader();
|
|
82
|
-
reader.addEventListener('load', () => {
|
|
83
|
-
resolve({ default: reader.result });
|
|
84
|
-
});
|
|
85
|
-
reader.addEventListener('error', err => {
|
|
86
|
-
reject(err);
|
|
87
|
-
});
|
|
88
|
-
reader.addEventListener('abort', () => {
|
|
89
|
-
reject();
|
|
90
|
-
});
|
|
91
|
-
this.loader.file.then(file => {
|
|
92
|
-
reader.readAsDataURL(file);
|
|
93
|
-
});
|
|
94
|
-
});
|
|
95
|
-
}
|
|
96
|
-
/**
|
|
97
|
-
* Aborts the upload process.
|
|
98
|
-
*
|
|
99
|
-
* @see module:upload/filerepository~UploadAdapter#abort
|
|
100
|
-
*/
|
|
101
|
-
abort() {
|
|
102
|
-
this.reader.abort();
|
|
103
|
-
}
|
|
104
|
-
}
|
|
@@ -1,205 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @license Copyright (c) 2003-2026, CKSource Holding sp. z o.o. All rights reserved.
|
|
3
|
-
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-licensing-options
|
|
4
|
-
*/
|
|
5
|
-
/**
|
|
6
|
-
* @module upload/adapters/simpleuploadadapter
|
|
7
|
-
*/
|
|
8
|
-
import { Plugin } from '@ckeditor/ckeditor5-core';
|
|
9
|
-
import { FileRepository } from '../filerepository.js';
|
|
10
|
-
import { logWarning } from '@ckeditor/ckeditor5-utils';
|
|
11
|
-
/**
|
|
12
|
-
* The Simple upload adapter allows uploading images to an application running on your server using
|
|
13
|
-
* the [`XMLHttpRequest`](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest) API with a
|
|
14
|
-
* minimal {@link module:upload/uploadconfig~SimpleUploadConfig editor configuration}.
|
|
15
|
-
*
|
|
16
|
-
* ```ts
|
|
17
|
-
* ClassicEditor
|
|
18
|
-
* .create( document.querySelector( '#editor' ), {
|
|
19
|
-
* simpleUpload: {
|
|
20
|
-
* uploadUrl: 'http://example.com',
|
|
21
|
-
* headers: {
|
|
22
|
-
* ...
|
|
23
|
-
* }
|
|
24
|
-
* }
|
|
25
|
-
* } )
|
|
26
|
-
* .then( ... )
|
|
27
|
-
* .catch( ... );
|
|
28
|
-
* ```
|
|
29
|
-
*
|
|
30
|
-
* See the {@glink features/images/image-upload/simple-upload-adapter "Simple upload adapter"} guide to learn how to
|
|
31
|
-
* learn more about the feature (configuration, server–side requirements, etc.).
|
|
32
|
-
*
|
|
33
|
-
* Check out the {@glink features/images/image-upload/image-upload comprehensive "Image upload overview"} to learn about
|
|
34
|
-
* other ways to upload images into CKEditor 5.
|
|
35
|
-
*/
|
|
36
|
-
export class SimpleUploadAdapter extends Plugin {
|
|
37
|
-
/**
|
|
38
|
-
* @inheritDoc
|
|
39
|
-
*/
|
|
40
|
-
static get requires() {
|
|
41
|
-
return [FileRepository];
|
|
42
|
-
}
|
|
43
|
-
/**
|
|
44
|
-
* @inheritDoc
|
|
45
|
-
*/
|
|
46
|
-
static get pluginName() {
|
|
47
|
-
return 'SimpleUploadAdapter';
|
|
48
|
-
}
|
|
49
|
-
/**
|
|
50
|
-
* @inheritDoc
|
|
51
|
-
* @internal
|
|
52
|
-
*/
|
|
53
|
-
static get licenseFeatureCode() {
|
|
54
|
-
return 'SUA';
|
|
55
|
-
}
|
|
56
|
-
/**
|
|
57
|
-
* @inheritDoc
|
|
58
|
-
*/
|
|
59
|
-
static get isOfficialPlugin() {
|
|
60
|
-
return true;
|
|
61
|
-
}
|
|
62
|
-
/**
|
|
63
|
-
* @inheritDoc
|
|
64
|
-
*/
|
|
65
|
-
static get isPremiumPlugin() {
|
|
66
|
-
return true;
|
|
67
|
-
}
|
|
68
|
-
/**
|
|
69
|
-
* @inheritDoc
|
|
70
|
-
*/
|
|
71
|
-
init() {
|
|
72
|
-
const options = this.editor.config.get('simpleUpload');
|
|
73
|
-
if (!options) {
|
|
74
|
-
return;
|
|
75
|
-
}
|
|
76
|
-
if (!options.uploadUrl) {
|
|
77
|
-
/**
|
|
78
|
-
* The {@link module:upload/uploadconfig~SimpleUploadConfig#uploadUrl `config.simpleUpload.uploadUrl`}
|
|
79
|
-
* configuration required by the {@link module:upload/adapters/simpleuploadadapter~SimpleUploadAdapter `SimpleUploadAdapter`}
|
|
80
|
-
* is missing. Make sure the correct URL is specified for the image upload to work properly.
|
|
81
|
-
*
|
|
82
|
-
* @error simple-upload-adapter-missing-uploadurl
|
|
83
|
-
*/
|
|
84
|
-
logWarning('simple-upload-adapter-missing-uploadurl');
|
|
85
|
-
return;
|
|
86
|
-
}
|
|
87
|
-
this.editor.plugins.get(FileRepository).createUploadAdapter = loader => {
|
|
88
|
-
return new Adapter(loader, options);
|
|
89
|
-
};
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
/**
|
|
93
|
-
* Upload adapter.
|
|
94
|
-
*/
|
|
95
|
-
class Adapter {
|
|
96
|
-
/**
|
|
97
|
-
* FileLoader instance to use during the upload.
|
|
98
|
-
*/
|
|
99
|
-
loader;
|
|
100
|
-
/**
|
|
101
|
-
* The configuration of the adapter.
|
|
102
|
-
*/
|
|
103
|
-
options;
|
|
104
|
-
xhr;
|
|
105
|
-
/**
|
|
106
|
-
* Creates a new adapter instance.
|
|
107
|
-
*/
|
|
108
|
-
constructor(loader, options) {
|
|
109
|
-
this.loader = loader;
|
|
110
|
-
this.options = options;
|
|
111
|
-
}
|
|
112
|
-
/**
|
|
113
|
-
* Starts the upload process.
|
|
114
|
-
*
|
|
115
|
-
* @see module:upload/filerepository~UploadAdapter#upload
|
|
116
|
-
*/
|
|
117
|
-
upload() {
|
|
118
|
-
return this.loader.file
|
|
119
|
-
.then(file => new Promise((resolve, reject) => {
|
|
120
|
-
this._initRequest();
|
|
121
|
-
this._initListeners(resolve, reject, file);
|
|
122
|
-
this._sendRequest(file);
|
|
123
|
-
}));
|
|
124
|
-
}
|
|
125
|
-
/**
|
|
126
|
-
* Aborts the upload process.
|
|
127
|
-
*
|
|
128
|
-
* @see module:upload/filerepository~UploadAdapter#abort
|
|
129
|
-
*/
|
|
130
|
-
abort() {
|
|
131
|
-
if (this.xhr) {
|
|
132
|
-
this.xhr.abort();
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
/**
|
|
136
|
-
* Initializes the `XMLHttpRequest` object using the URL specified as
|
|
137
|
-
* {@link module:upload/uploadconfig~SimpleUploadConfig#uploadUrl `simpleUpload.uploadUrl`} in the editor's
|
|
138
|
-
* configuration.
|
|
139
|
-
*/
|
|
140
|
-
_initRequest() {
|
|
141
|
-
const xhr = this.xhr = new XMLHttpRequest();
|
|
142
|
-
xhr.open('POST', this.options.uploadUrl, true);
|
|
143
|
-
xhr.responseType = 'json';
|
|
144
|
-
}
|
|
145
|
-
/**
|
|
146
|
-
* Initializes XMLHttpRequest listeners
|
|
147
|
-
*
|
|
148
|
-
* @param resolve Callback function to be called when the request is successful.
|
|
149
|
-
* @param reject Callback function to be called when the request cannot be completed.
|
|
150
|
-
* @param file Native File object.
|
|
151
|
-
*/
|
|
152
|
-
_initListeners(resolve, reject, file) {
|
|
153
|
-
const xhr = this.xhr;
|
|
154
|
-
const loader = this.loader;
|
|
155
|
-
const genericErrorText = `Couldn't upload file: ${file.name}.`;
|
|
156
|
-
xhr.addEventListener('error', () => reject(genericErrorText));
|
|
157
|
-
xhr.addEventListener('abort', () => reject());
|
|
158
|
-
xhr.addEventListener('load', () => {
|
|
159
|
-
const response = xhr.response;
|
|
160
|
-
if (!response || response.error) {
|
|
161
|
-
return reject(response && response.error && response.error.message ? response.error.message : genericErrorText);
|
|
162
|
-
}
|
|
163
|
-
const urls = response.url ? { default: response.url } : response.urls;
|
|
164
|
-
// Resolve with the normalized `urls` property and pass the rest of the response
|
|
165
|
-
// to allow customizing the behavior of features relying on the upload adapters.
|
|
166
|
-
resolve({
|
|
167
|
-
...response,
|
|
168
|
-
urls
|
|
169
|
-
});
|
|
170
|
-
});
|
|
171
|
-
// Upload progress when it is supported.
|
|
172
|
-
/* istanbul ignore else -- @preserve */
|
|
173
|
-
if (xhr.upload) {
|
|
174
|
-
xhr.upload.addEventListener('progress', evt => {
|
|
175
|
-
if (evt.lengthComputable) {
|
|
176
|
-
loader.uploadTotal = evt.total;
|
|
177
|
-
loader.uploaded = evt.loaded;
|
|
178
|
-
}
|
|
179
|
-
});
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
/**
|
|
183
|
-
* Prepares the data and sends the request.
|
|
184
|
-
*
|
|
185
|
-
* @param file File instance to be uploaded.
|
|
186
|
-
*/
|
|
187
|
-
_sendRequest(file) {
|
|
188
|
-
// Set headers if specified.
|
|
189
|
-
let headers = this.options.headers || {};
|
|
190
|
-
if (typeof headers === 'function') {
|
|
191
|
-
headers = headers(file);
|
|
192
|
-
}
|
|
193
|
-
// Use the withCredentials flag if specified.
|
|
194
|
-
const withCredentials = this.options.withCredentials || false;
|
|
195
|
-
for (const headerName of Object.keys(headers)) {
|
|
196
|
-
this.xhr.setRequestHeader(headerName, headers[headerName]);
|
|
197
|
-
}
|
|
198
|
-
this.xhr.withCredentials = withCredentials;
|
|
199
|
-
// Prepare the form data.
|
|
200
|
-
const data = new FormData();
|
|
201
|
-
data.append('upload', file);
|
|
202
|
-
// Send the request.
|
|
203
|
-
this.xhr.send(data);
|
|
204
|
-
}
|
|
205
|
-
}
|
package/src/augmentation.js
DELETED
package/src/filereader.js
DELETED
|
@@ -1,80 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @license Copyright (c) 2003-2026, CKSource Holding sp. z o.o. All rights reserved.
|
|
3
|
-
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-licensing-options
|
|
4
|
-
*/
|
|
5
|
-
/**
|
|
6
|
-
* @module upload/filereader
|
|
7
|
-
*/
|
|
8
|
-
import { ObservableMixin } from '@ckeditor/ckeditor5-utils';
|
|
9
|
-
/**
|
|
10
|
-
* Wrapper over the native `FileReader`.
|
|
11
|
-
*/
|
|
12
|
-
export class FileReader extends /* #__PURE__ */ ObservableMixin() {
|
|
13
|
-
total;
|
|
14
|
-
/**
|
|
15
|
-
* Instance of native FileReader.
|
|
16
|
-
*/
|
|
17
|
-
_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
|
-
_data;
|
|
23
|
-
/**
|
|
24
|
-
* Creates an instance of the FileReader.
|
|
25
|
-
*/
|
|
26
|
-
constructor() {
|
|
27
|
-
super();
|
|
28
|
-
const reader = new window.FileReader();
|
|
29
|
-
this._reader = reader;
|
|
30
|
-
this._data = undefined;
|
|
31
|
-
this.set('loaded', 0);
|
|
32
|
-
reader.onprogress = evt => {
|
|
33
|
-
this.loaded = evt.loaded;
|
|
34
|
-
};
|
|
35
|
-
}
|
|
36
|
-
/**
|
|
37
|
-
* Returns error that occurred during file reading.
|
|
38
|
-
*/
|
|
39
|
-
get error() {
|
|
40
|
-
return this._reader.error;
|
|
41
|
-
}
|
|
42
|
-
/**
|
|
43
|
-
* Holds the data of an already loaded file. The file must be first loaded
|
|
44
|
-
* by using {@link module:upload/filereader~FileReader#read `read()`}.
|
|
45
|
-
*/
|
|
46
|
-
get data() {
|
|
47
|
-
return this._data;
|
|
48
|
-
}
|
|
49
|
-
/**
|
|
50
|
-
* Reads the provided file.
|
|
51
|
-
*
|
|
52
|
-
* @param file Native File object.
|
|
53
|
-
* @returns Returns a promise that will be resolved with file's content.
|
|
54
|
-
* The promise will be rejected in case of an error or when the reading process is aborted.
|
|
55
|
-
*/
|
|
56
|
-
read(file) {
|
|
57
|
-
const reader = this._reader;
|
|
58
|
-
this.total = file.size;
|
|
59
|
-
return new Promise((resolve, reject) => {
|
|
60
|
-
reader.onload = () => {
|
|
61
|
-
const result = reader.result;
|
|
62
|
-
this._data = result;
|
|
63
|
-
resolve(result);
|
|
64
|
-
};
|
|
65
|
-
reader.onerror = () => {
|
|
66
|
-
reject('error');
|
|
67
|
-
};
|
|
68
|
-
reader.onabort = () => {
|
|
69
|
-
reject('aborted');
|
|
70
|
-
};
|
|
71
|
-
this._reader.readAsDataURL(file);
|
|
72
|
-
});
|
|
73
|
-
}
|
|
74
|
-
/**
|
|
75
|
-
* Aborts file reader.
|
|
76
|
-
*/
|
|
77
|
-
abort() {
|
|
78
|
-
this._reader.abort();
|
|
79
|
-
}
|
|
80
|
-
}
|