@ckeditor/ckeditor5-upload 35.2.1 → 35.3.1

Sign up to get free protection for your applications and to get access to all the features.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ckeditor/ckeditor5-upload",
3
- "version": "35.2.1",
3
+ "version": "35.3.1",
4
4
  "description": "Upload feature for CKEditor 5.",
5
5
  "keywords": [
6
6
  "ckeditor",
@@ -11,9 +11,14 @@
11
11
  ],
12
12
  "main": "src/index.js",
13
13
  "dependencies": {
14
- "@ckeditor/ckeditor5-core": "^35.2.1",
15
- "@ckeditor/ckeditor5-utils": "^35.2.1",
16
- "@ckeditor/ckeditor5-ui": "^35.2.1"
14
+ "@ckeditor/ckeditor5-core": "^35.3.1",
15
+ "@ckeditor/ckeditor5-utils": "^35.3.1",
16
+ "@ckeditor/ckeditor5-ui": "^35.3.1"
17
+ },
18
+ "devDependencies": {
19
+ "typescript": "^4.8.4",
20
+ "webpack": "^5.58.1",
21
+ "webpack-cli": "^4.9.0"
17
22
  },
18
23
  "engines": {
19
24
  "node": ">=14.0.0",
@@ -30,9 +35,14 @@
30
35
  },
31
36
  "files": [
32
37
  "lang",
33
- "src",
38
+ "src/**/*.js",
39
+ "src/**/*.d.ts",
34
40
  "theme",
35
41
  "ckeditor5-metadata.json",
36
42
  "CHANGELOG.md"
37
- ]
43
+ ],
44
+ "scripts": {
45
+ "build": "tsc -p ./tsconfig.release.json",
46
+ "postversion": "npm run build"
47
+ }
38
48
  }
@@ -2,16 +2,12 @@
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/adapters/base64uploadadapter
8
7
  */
9
-
10
8
  /* globals window */
11
-
12
9
  import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
13
10
  import FileRepository from '../filerepository';
14
-
15
11
  /**
16
12
  * A plugin that converts images inserted into the editor into [Base64 strings](https://en.wikipedia.org/wiki/Base64)
17
13
  * in the {@glink installation/advanced/saving-data editor output}.
@@ -25,28 +21,25 @@ import FileRepository from '../filerepository';
25
21
  * @extends module:core/plugin~Plugin
26
22
  */
27
23
  export default class Base64UploadAdapter extends Plugin {
28
- /**
29
- * @inheritDoc
30
- */
31
- static get requires() {
32
- return [ FileRepository ];
33
- }
34
-
35
- /**
36
- * @inheritDoc
37
- */
38
- static get pluginName() {
39
- return 'Base64UploadAdapter';
40
- }
41
-
42
- /**
43
- * @inheritDoc
44
- */
45
- init() {
46
- this.editor.plugins.get( FileRepository ).createUploadAdapter = loader => new Adapter( loader );
47
- }
24
+ /**
25
+ * @inheritDoc
26
+ */
27
+ static get requires() {
28
+ return [FileRepository];
29
+ }
30
+ /**
31
+ * @inheritDoc
32
+ */
33
+ static get pluginName() {
34
+ return 'Base64UploadAdapter';
35
+ }
36
+ /**
37
+ * @inheritDoc
38
+ */
39
+ init() {
40
+ this.editor.plugins.get(FileRepository).createUploadAdapter = loader => new Adapter(loader);
41
+ }
48
42
  }
49
-
50
43
  /**
51
44
  * The upload adapter that converts images inserted into the editor into Base64 strings.
52
45
  *
@@ -54,55 +47,49 @@ export default class Base64UploadAdapter extends Plugin {
54
47
  * @implements module:upload/filerepository~UploadAdapter
55
48
  */
56
49
  class Adapter {
57
- /**
58
- * Creates a new adapter instance.
59
- *
60
- * @param {module:upload/filerepository~FileLoader} loader
61
- */
62
- constructor( loader ) {
63
- /**
64
- * `FileLoader` instance to use during the upload.
65
- *
66
- * @member {module:upload/filerepository~FileLoader} #loader
67
- */
68
- this.loader = loader;
69
- }
70
-
71
- /**
72
- * Starts the upload process.
73
- *
74
- * @see module:upload/filerepository~UploadAdapter#upload
75
- * @returns {Promise}
76
- */
77
- upload() {
78
- return new Promise( ( resolve, reject ) => {
79
- const reader = this.reader = new window.FileReader();
80
-
81
- reader.addEventListener( 'load', () => {
82
- resolve( { default: reader.result } );
83
- } );
84
-
85
- reader.addEventListener( 'error', err => {
86
- reject( err );
87
- } );
88
-
89
- reader.addEventListener( 'abort', () => {
90
- reject();
91
- } );
92
-
93
- this.loader.file.then( file => {
94
- reader.readAsDataURL( file );
95
- } );
96
- } );
97
- }
98
-
99
- /**
100
- * Aborts the upload process.
101
- *
102
- * @see module:upload/filerepository~UploadAdapter#abort
103
- * @returns {Promise}
104
- */
105
- abort() {
106
- this.reader.abort();
107
- }
50
+ /**
51
+ * Creates a new adapter instance.
52
+ *
53
+ * @param {module:upload/filerepository~FileLoader} loader
54
+ */
55
+ constructor(loader) {
56
+ /**
57
+ * `FileLoader` instance to use during the upload.
58
+ *
59
+ * @member {module:upload/filerepository~FileLoader} #loader
60
+ */
61
+ this.loader = loader;
62
+ }
63
+ /**
64
+ * Starts the upload process.
65
+ *
66
+ * @see module:upload/filerepository~UploadAdapter#upload
67
+ * @returns {Promise}
68
+ */
69
+ upload() {
70
+ return new Promise((resolve, reject) => {
71
+ const reader = this.reader = new window.FileReader();
72
+ reader.addEventListener('load', () => {
73
+ resolve({ default: reader.result });
74
+ });
75
+ reader.addEventListener('error', err => {
76
+ reject(err);
77
+ });
78
+ reader.addEventListener('abort', () => {
79
+ reject();
80
+ });
81
+ this.loader.file.then(file => {
82
+ reader.readAsDataURL(file);
83
+ });
84
+ });
85
+ }
86
+ /**
87
+ * Aborts the upload process.
88
+ *
89
+ * @see module:upload/filerepository~UploadAdapter#abort
90
+ * @returns {Promise}
91
+ */
92
+ abort() {
93
+ this.reader.abort();
94
+ }
108
95
  }
@@ -2,17 +2,13 @@
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/adapters/simpleuploadadapter
8
7
  */
9
-
10
8
  /* globals XMLHttpRequest, FormData */
11
-
12
9
  import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
13
10
  import FileRepository from '../filerepository';
14
11
  import { logWarning } from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
15
-
16
12
  /**
17
13
  * The Simple upload adapter allows uploading images to an application running on your server using
18
14
  * the [`XMLHttpRequest`](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest) API with a
@@ -39,49 +35,42 @@ import { logWarning } from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
39
35
  * @extends module:core/plugin~Plugin
40
36
  */
41
37
  export default class SimpleUploadAdapter extends Plugin {
42
- /**
43
- * @inheritDoc
44
- */
45
- static get requires() {
46
- return [ FileRepository ];
47
- }
48
-
49
- /**
50
- * @inheritDoc
51
- */
52
- static get pluginName() {
53
- return 'SimpleUploadAdapter';
54
- }
55
-
56
- /**
57
- * @inheritDoc
58
- */
59
- init() {
60
- const options = this.editor.config.get( 'simpleUpload' );
61
-
62
- if ( !options ) {
63
- return;
64
- }
65
-
66
- if ( !options.uploadUrl ) {
67
- /**
68
- * The {@link module:upload/adapters/simpleuploadadapter~SimpleUploadConfig#uploadUrl `config.simpleUpload.uploadUrl`}
69
- * configuration required by the {@link module:upload/adapters/simpleuploadadapter~SimpleUploadAdapter `SimpleUploadAdapter`}
70
- * is missing. Make sure the correct URL is specified for the image upload to work properly.
71
- *
72
- * @error simple-upload-adapter-missing-uploadurl
73
- */
74
- logWarning( 'simple-upload-adapter-missing-uploadurl' );
75
-
76
- return;
77
- }
78
-
79
- this.editor.plugins.get( FileRepository ).createUploadAdapter = loader => {
80
- return new Adapter( loader, options );
81
- };
82
- }
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/adapters/simpleuploadadapter~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
+ }
83
73
  }
84
-
85
74
  /**
86
75
  * Upload adapter.
87
76
  *
@@ -89,228 +78,120 @@ export default class SimpleUploadAdapter extends Plugin {
89
78
  * @implements module:upload/filerepository~UploadAdapter
90
79
  */
91
80
  class Adapter {
92
- /**
93
- * Creates a new adapter instance.
94
- *
95
- * @param {module:upload/filerepository~FileLoader} loader
96
- * @param {module:upload/adapters/simpleuploadadapter~SimpleUploadConfig} options
97
- */
98
- constructor( loader, options ) {
99
- /**
100
- * FileLoader instance to use during the upload.
101
- *
102
- * @member {module:upload/filerepository~FileLoader} #loader
103
- */
104
- this.loader = loader;
105
-
106
- /**
107
- * The configuration of the adapter.
108
- *
109
- * @member {module:upload/adapters/simpleuploadadapter~SimpleUploadConfig} #options
110
- */
111
- this.options = options;
112
- }
113
-
114
- /**
115
- * Starts the upload process.
116
- *
117
- * @see module:upload/filerepository~UploadAdapter#upload
118
- * @returns {Promise}
119
- */
120
- upload() {
121
- return this.loader.file
122
- .then( file => new Promise( ( resolve, reject ) => {
123
- this._initRequest();
124
- this._initListeners( resolve, reject, file );
125
- this._sendRequest( file );
126
- } ) );
127
- }
128
-
129
- /**
130
- * Aborts the upload process.
131
- *
132
- * @see module:upload/filerepository~UploadAdapter#abort
133
- * @returns {Promise}
134
- */
135
- abort() {
136
- if ( this.xhr ) {
137
- this.xhr.abort();
138
- }
139
- }
140
-
141
- /**
142
- * Initializes the `XMLHttpRequest` object using the URL specified as
143
- * {@link module:upload/adapters/simpleuploadadapter~SimpleUploadConfig#uploadUrl `simpleUpload.uploadUrl`} in the editor's
144
- * configuration.
145
- *
146
- * @private
147
- */
148
- _initRequest() {
149
- const xhr = this.xhr = new XMLHttpRequest();
150
-
151
- xhr.open( 'POST', this.options.uploadUrl, true );
152
- xhr.responseType = 'json';
153
- }
154
-
155
- /**
156
- * Initializes XMLHttpRequest listeners
157
- *
158
- * @private
159
- * @param {Function} resolve Callback function to be called when the request is successful.
160
- * @param {Function} reject Callback function to be called when the request cannot be completed.
161
- * @param {File} file Native File object.
162
- */
163
- _initListeners( resolve, reject, file ) {
164
- const xhr = this.xhr;
165
- const loader = this.loader;
166
- const genericErrorText = `Couldn't upload file: ${ file.name }.`;
167
-
168
- xhr.addEventListener( 'error', () => reject( genericErrorText ) );
169
- xhr.addEventListener( 'abort', () => reject() );
170
- xhr.addEventListener( 'load', () => {
171
- const response = xhr.response;
172
-
173
- if ( !response || response.error ) {
174
- return reject( response && response.error && response.error.message ? response.error.message : genericErrorText );
175
- }
176
-
177
- const urls = response.url ? { default: response.url } : response.urls;
178
-
179
- // Resolve with the normalized `urls` property and pass the rest of the response
180
- // to allow customizing the behavior of features relying on the upload adapters.
181
- resolve( {
182
- ...response,
183
- urls
184
- } );
185
- } );
186
-
187
- // Upload progress when it is supported.
188
- /* istanbul ignore else */
189
- if ( xhr.upload ) {
190
- xhr.upload.addEventListener( 'progress', evt => {
191
- if ( evt.lengthComputable ) {
192
- loader.uploadTotal = evt.total;
193
- loader.uploaded = evt.loaded;
194
- }
195
- } );
196
- }
197
- }
198
-
199
- /**
200
- * Prepares the data and sends the request.
201
- *
202
- * @private
203
- * @param {File} file File instance to be uploaded.
204
- */
205
- _sendRequest( file ) {
206
- // Set headers if specified.
207
- const headers = this.options.headers || {};
208
-
209
- // Use the withCredentials flag if specified.
210
- const withCredentials = this.options.withCredentials || false;
211
-
212
- for ( const headerName of Object.keys( headers ) ) {
213
- this.xhr.setRequestHeader( headerName, headers[ headerName ] );
214
- }
215
-
216
- this.xhr.withCredentials = withCredentials;
217
-
218
- // Prepare the form data.
219
- const data = new FormData();
220
-
221
- data.append( 'upload', file );
222
-
223
- // Send the request.
224
- this.xhr.send( data );
225
- }
81
+ /**
82
+ * Creates a new adapter instance.
83
+ *
84
+ * @param {module:upload/filerepository~FileLoader} loader
85
+ * @param {module:upload/adapters/simpleuploadadapter~SimpleUploadConfig} options
86
+ */
87
+ constructor(loader, options) {
88
+ /**
89
+ * FileLoader instance to use during the upload.
90
+ *
91
+ * @member {module:upload/filerepository~FileLoader} #loader
92
+ */
93
+ this.loader = loader;
94
+ /**
95
+ * The configuration of the adapter.
96
+ *
97
+ * @member {module:upload/adapters/simpleuploadadapter~SimpleUploadConfig} #options
98
+ */
99
+ this.options = options;
100
+ }
101
+ /**
102
+ * Starts the upload process.
103
+ *
104
+ * @see module:upload/filerepository~UploadAdapter#upload
105
+ * @returns {Promise}
106
+ */
107
+ upload() {
108
+ return this.loader.file
109
+ .then(file => new Promise((resolve, reject) => {
110
+ this._initRequest();
111
+ this._initListeners(resolve, reject, file);
112
+ this._sendRequest(file);
113
+ }));
114
+ }
115
+ /**
116
+ * Aborts the upload process.
117
+ *
118
+ * @see module:upload/filerepository~UploadAdapter#abort
119
+ * @returns {Promise}
120
+ */
121
+ abort() {
122
+ if (this.xhr) {
123
+ this.xhr.abort();
124
+ }
125
+ }
126
+ /**
127
+ * Initializes the `XMLHttpRequest` object using the URL specified as
128
+ * {@link module:upload/adapters/simpleuploadadapter~SimpleUploadConfig#uploadUrl `simpleUpload.uploadUrl`} in the editor's
129
+ * configuration.
130
+ *
131
+ * @private
132
+ */
133
+ _initRequest() {
134
+ const xhr = this.xhr = new XMLHttpRequest();
135
+ xhr.open('POST', this.options.uploadUrl, true);
136
+ xhr.responseType = 'json';
137
+ }
138
+ /**
139
+ * Initializes XMLHttpRequest listeners
140
+ *
141
+ * @private
142
+ * @param {Function} resolve Callback function to be called when the request is successful.
143
+ * @param {Function} reject Callback function to be called when the request cannot be completed.
144
+ * @param {File} file Native File object.
145
+ */
146
+ _initListeners(resolve, reject, file) {
147
+ const xhr = this.xhr;
148
+ const loader = this.loader;
149
+ const genericErrorText = `Couldn't upload file: ${file.name}.`;
150
+ xhr.addEventListener('error', () => reject(genericErrorText));
151
+ xhr.addEventListener('abort', () => reject());
152
+ xhr.addEventListener('load', () => {
153
+ const response = xhr.response;
154
+ if (!response || response.error) {
155
+ return reject(response && response.error && response.error.message ? response.error.message : genericErrorText);
156
+ }
157
+ const urls = response.url ? { default: response.url } : response.urls;
158
+ // Resolve with the normalized `urls` property and pass the rest of the response
159
+ // to allow customizing the behavior of features relying on the upload adapters.
160
+ resolve({
161
+ ...response,
162
+ urls
163
+ });
164
+ });
165
+ // Upload progress when it is supported.
166
+ /* istanbul ignore else */
167
+ if (xhr.upload) {
168
+ xhr.upload.addEventListener('progress', evt => {
169
+ if (evt.lengthComputable) {
170
+ loader.uploadTotal = evt.total;
171
+ loader.uploaded = evt.loaded;
172
+ }
173
+ });
174
+ }
175
+ }
176
+ /**
177
+ * Prepares the data and sends the request.
178
+ *
179
+ * @private
180
+ * @param {File} file File instance to be uploaded.
181
+ */
182
+ _sendRequest(file) {
183
+ // Set headers if specified.
184
+ const headers = this.options.headers || {};
185
+ // Use the withCredentials flag if specified.
186
+ const withCredentials = this.options.withCredentials || false;
187
+ for (const headerName of Object.keys(headers)) {
188
+ this.xhr.setRequestHeader(headerName, headers[headerName]);
189
+ }
190
+ this.xhr.withCredentials = withCredentials;
191
+ // Prepare the form data.
192
+ const data = new FormData();
193
+ data.append('upload', file);
194
+ // Send the request.
195
+ this.xhr.send(data);
196
+ }
226
197
  }
227
-
228
- /**
229
- * The configuration of the {@link module:upload/adapters/simpleuploadadapter~SimpleUploadAdapter simple upload adapter}.
230
- *
231
- * ClassicEditor
232
- * .create( editorElement, {
233
- * simpleUpload: {
234
- * // The URL the images are uploaded to.
235
- * uploadUrl: 'http://example.com',
236
- *
237
- * // Headers sent along with the XMLHttpRequest to the upload server.
238
- * headers: {
239
- * ...
240
- * }
241
- * }
242
- * } );
243
- * .then( ... )
244
- * .catch( ... );
245
- *
246
- * See the {@glink features/images/image-upload/simple-upload-adapter "Simple upload adapter"} guide to learn more.
247
- *
248
- * See {@link module:core/editor/editorconfig~EditorConfig all editor configuration options}.
249
- *
250
- * @interface SimpleUploadConfig
251
- */
252
-
253
- /**
254
- * The configuration of the {@link module:upload/adapters/simpleuploadadapter~SimpleUploadAdapter simple upload adapter}.
255
- *
256
- * Read more in {@link module:upload/adapters/simpleuploadadapter~SimpleUploadConfig}.
257
- *
258
- * @member {module:upload/adapters/simpleuploadadapter~SimpleUploadConfig} module:core/editor/editorconfig~EditorConfig#simpleUpload
259
- */
260
-
261
- /**
262
- * The path (URL) to the server (application) which handles the file upload. When specified, enables the automatic
263
- * upload of resources (images) inserted into the editor content.
264
- *
265
- * Learn more about the server application requirements in the
266
- * {@glink features/images/image-upload/simple-upload-adapter#server-side-configuration "Server-side configuration"} section
267
- * of the feature guide.
268
- *
269
- * @member {String} module:upload/adapters/simpleuploadadapter~SimpleUploadConfig#uploadUrl
270
- */
271
-
272
- /**
273
- * An object that defines additional [headers](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers) sent with
274
- * the request to the server during the upload. This is the right place to implement security mechanisms like
275
- * authentication and [CSRF](https://developer.mozilla.org/en-US/docs/Glossary/CSRF) protection.
276
- *
277
- * ClassicEditor
278
- * .create( editorElement, {
279
- * simpleUpload: {
280
- * headers: {
281
- * 'X-CSRF-TOKEN': 'CSRF-Token',
282
- * Authorization: 'Bearer <JSON Web Token>'
283
- * }
284
- * }
285
- * } );
286
- * .then( ... )
287
- * .catch( ... );
288
- *
289
- * Learn more about the server application requirements in the
290
- * {@glink features/images/image-upload/simple-upload-adapter#server-side-configuration "Server-side configuration"} section
291
- * of the feature guide.
292
- *
293
- * @member {Object.<String, String>} module:upload/adapters/simpleuploadadapter~SimpleUploadConfig#headers
294
- */
295
-
296
- /**
297
- * This flag enables the
298
- * [`withCredentials`](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/withCredentials)
299
- * property of the request sent to the server during the upload. It affects cross-site requests only and, for instance,
300
- * allows credentials such as cookies to be sent along with the request.
301
- *
302
- * ClassicEditor
303
- * .create( editorElement, {
304
- * simpleUpload: {
305
- * withCredentials: true
306
- * }
307
- * } );
308
- * .then( ... )
309
- * .catch( ... );
310
- *
311
- * Learn more about the server application requirements in the
312
- * {@glink features/images/image-upload/simple-upload-adapter#server-side-configuration "Server-side configuration"} section
313
- * of the feature guide.
314
- *
315
- * @member {Boolean} [module:upload/adapters/simpleuploadadapter~SimpleUploadConfig#withCredentials=false]
316
- */