@@ -20,7 +20,17 @@ import FileReader from './filereader.js';
20
20
import uid from '@ckeditor/ckeditor5-utils/src/uid.js' ;
21
21
22
22
/**
23
- * FileRepository plugin.
23
+ * File repository plugin. A central point for managing file upload.
24
+ *
25
+ * To use it, first you need an upload adapter. Upload adapter's job is to handle communication with the server
26
+ * (sending the file and handling server's response). You can use one of the existing plugins introducing upload adapters
27
+ * (e.g. {@link module:easy-image/cloudservicesuploadadapter~CloudServicesUploadAdapter} or
28
+ * {@link module:adapter-ckfinder/uploadadapter~CKFinderUploadAdapter}) or write your own one
29
+ * (which boils down to setting the {@link ~FileRepository#createAdapter} factory function – see
30
+ * {@link ~Adapter `Adapter` interface} documentation).
31
+ *
32
+ * Then, you can use {@link ~FileRepository#createLoader `createLoader()`} and the returned {@link ~FileLoader} instance to
33
+ * load and upload files.
24
34
*
25
35
* @extends module:core/plugin~Plugin
26
36
*/
@@ -44,15 +54,15 @@ export default class FileRepository extends Plugin {
44
54
this . loaders = new Collection ( ) ;
45
55
46
56
/**
47
- * Function that should be defined before using FileRepository. It should return new instance of
48
- * {@link module:upload/filerepository~Adapter Adapter} that will be used to upload files.
49
- * {@link module:upload/filerepository~FileLoader FileLoader} instance associated with the adapter
57
+ * A factory function which should be defined before using `FileRepository`.
58
+ *
59
+ * It should return a new instance of {@link module:upload/filerepository~Adapter} that will be used to upload files.
60
+ * {@link module:upload/filerepository~FileLoader} instance associated with the adapter
50
61
* will be passed to that function.
51
- * For more information and example see {@link module:upload/filerepository~Adapter Adapter}.
52
62
*
53
- * @abstract
54
- * @function
55
- * @name #createAdapter
63
+ * For more information and example see { @link module:upload/filerepository~Adapter}.
64
+ *
65
+ * @member {Function} #createAdapter
56
66
*/
57
67
58
68
/**
@@ -66,6 +76,7 @@ export default class FileRepository extends Plugin {
66
76
67
77
/**
68
78
* Number of total bytes to upload.
79
+ *
69
80
* It might be different than the file size because of headers and additional data.
70
81
* It contains `null` if value is not available yet, so it's better to use {@link #uploadedPercent} to monitor
71
82
* the progress.
@@ -88,11 +99,49 @@ export default class FileRepository extends Plugin {
88
99
} ) ;
89
100
}
90
101
102
+ afterInit ( ) {
103
+ if ( ! this . createAdapter ) {
104
+ /**
105
+ * You need to enable an upload adapter in order to be able to upload files.
106
+ *
107
+ * This warning shows up when {@link module:upload/filerepository~FileRepository} is being used
108
+ * without {@link #createAdapter definining an upload adapter}.
109
+ *
110
+ * If you see this warning when using one of the {@glink builds/index CKEditor 5 Builds}
111
+ * it means that you did not configure any of the upload adapters available by default in those builds.
112
+ * See:
113
+ *
114
+ * * {@link module:core/editor/editorconfig~EditorConfig#cloudServices `config.cloudServices`} for
115
+ * Easy Image with Cloud Services integration,
116
+ * * {@link module:core/editor/editorconfig~EditorConfig#ckfinder `config.ckfinder`} for CKFinder
117
+ * file upload integration.
118
+ *
119
+ * If you do not need file upload functionality at all and you use one of the builds, you can disable the built-in
120
+ * upload adapters to hide this warning:
121
+ *
122
+ * ClassicEditor
123
+ * .create( document.querySelector( '#editor' ), {
124
+ * removePlugins: [ 'EasyImage', 'CKFinderUploadAdapter' ]
125
+ * } )
126
+ * .then( ... )
127
+ * .catch( ... );
128
+ *
129
+ * If you wish to implement your own upload adapter refer to the {@link ~Adapter `Adapter` interface} documentation.
130
+ *
131
+ * @error filerepository-no-adapter
132
+ */
133
+ log . warn ( 'filerepository-no-adapter: Upload adapter is not defined.' ) ;
134
+
135
+ return null ;
136
+ }
137
+ }
138
+
91
139
/**
92
140
* Returns the loader associated with specified file.
141
+ *
93
142
* To get loader by id use `fileRepository.loaders.get( id )`.
94
143
*
95
- * @param {File } file Native File object .
144
+ * @param {File } file Native file handle .
96
145
* @returns {module:upload/filerepository~FileLoader|null }
97
146
*/
98
147
getLoader ( file ) {
@@ -106,15 +155,16 @@ export default class FileRepository extends Plugin {
106
155
}
107
156
108
157
/**
109
- * Creates loader for specified file.
110
- * Shows console warning and returns `null` if {@link #createAdapter} method is not defined.
158
+ * Creates a loader instance for the given file.
159
+ *
160
+ * Requires {@link #createAdapter} factory to be defined.
111
161
*
112
162
* @param {File } file Native File object.
113
163
* @returns {module:upload/filerepository~FileLoader|null }
114
164
*/
115
165
createLoader ( file ) {
116
166
if ( ! this . createAdapter ) {
117
- log . warn ( 'FileRepository: no createAdapter method found. Please define it before creating a loader .' ) ;
167
+ log . error ( 'filerepository-no-adapter: Upload adapter is not defined .' ) ;
118
168
119
169
return null ;
120
170
}
@@ -150,7 +200,7 @@ export default class FileRepository extends Plugin {
150
200
}
151
201
152
202
/**
153
- * Destroys loader.
203
+ * Destroys the given loader.
154
204
*
155
205
* @param {File|module:upload/filerepository~FileLoader } fileOrLoader File associated with that loader or loader
156
206
* itself.
@@ -172,9 +222,9 @@ mix( FileRepository, ObservableMixin );
172
222
*/
173
223
class FileLoader {
174
224
/**
175
- * Creates instance of FileLoader.
225
+ * Creates a new instance of ` FileLoader` .
176
226
*
177
- * @param {File } file
227
+ * @param {File } file A native file instance.
178
228
* @param {module:upload/filerepository~Adapter } adapter
179
229
*/
180
230
constructor ( file , adapter ) {
@@ -187,15 +237,15 @@ class FileLoader {
187
237
this . id = uid ( ) ;
188
238
189
239
/**
190
- * File instance associated with FileLoader .
240
+ * A ` File` instance associated with this file loader .
191
241
*
192
242
* @readonly
193
243
* @member {File}
194
244
*/
195
245
this . file = file ;
196
246
197
247
/**
198
- * Adapter instance associated with FileLoader .
248
+ * Adapter instance associated with this file loader .
199
249
*
200
250
* @private
201
251
* @member {module:upload/filerepository~Adapter}
@@ -212,18 +262,21 @@ class FileLoader {
212
262
213
263
/**
214
264
* Current status of FileLoader. It can be one of the following:
265
+ *
215
266
* * 'idle',
216
267
* * 'reading',
217
268
* * 'uploading',
218
269
* * 'aborted',
219
270
* * 'error'.
220
271
*
221
272
* When reading status can change in a following way:
273
+ *
222
274
* `idle` -> `reading` -> `idle`
223
275
* `idle` -> `reading -> `aborted`
224
276
* `idle` -> `reading -> `error`
225
277
*
226
278
* When uploading status can change in a following way:
279
+ *
227
280
* `idle` -> `uploading` -> `idle`
228
281
* `idle` -> `uploading` -> `aborted`
229
282
* `idle` -> `uploading` -> `error`
@@ -275,17 +328,19 @@ class FileLoader {
275
328
276
329
/**
277
330
* Reads file using {@link module:upload/filereader~FileReader}.
331
+ *
278
332
* Throws {@link module:utils/ckeditorerror~CKEditorError CKEditorError} `filerepository-read-wrong-status` when status
279
333
* is different than `idle`.
334
+ *
280
335
* Example usage:
281
336
*
282
337
* fileLoader.read()
283
338
* .then( data => { ... } )
284
- * .catch( e => {
285
- * if ( e === 'aborted' ) {
339
+ * .catch( err => {
340
+ * if ( err === 'aborted' ) {
286
341
* console.log( 'Reading aborted.' );
287
342
* } else {
288
- * console.log( 'Reading error.', e );
343
+ * console.log( 'Reading error.', err );
289
344
* }
290
345
* } );
291
346
*
@@ -317,7 +372,8 @@ class FileLoader {
317
372
}
318
373
319
374
/**
320
- * Reads file using provided {@link module:upload/filerepository~Adapter}.
375
+ * Reads file using the provided {@link module:upload/filerepository~Adapter}.
376
+ *
321
377
* Throws {@link module:utils/ckeditorerror~CKEditorError CKEditorError} `filerepository-upload-wrong-status` when status
322
378
* is different than `idle`.
323
379
* Example usage:
@@ -398,39 +454,40 @@ mix( FileLoader, ObservableMixin );
398
454
* handles file uploads. It should contain logic necessary to initiate upload process and monitor its progress.
399
455
*
400
456
* It should implement two methods:
401
- * * {@link module:upload/filerepository~Adapter#upload upload()},
402
- * * {@link module:upload/filerepository~Adapter#abort abort()}.
457
+ *
458
+ * * {@link module:upload/filerepository~Adapter#upload `upload()`},
459
+ * * {@link module:upload/filerepository~Adapter#abort `abort()`}.
403
460
*
404
461
* Example adapter implementation:
405
462
*
406
- * class Adapter {
407
- * constructor( loader ) {
408
- * // Save Loader instance to update upload progress.
409
- * this.loader = loader;
410
- * }
463
+ * class Adapter {
464
+ * constructor( loader ) {
465
+ * // Save Loader instance to update upload progress.
466
+ * this.loader = loader;
467
+ * }
411
468
*
412
- * upload() {
413
- * // Update loader's progress.
414
- * server.onUploadProgress( data => {
415
- * loader.uploadTotal = data.total;
416
- * loader.uploaded = data.uploaded;
417
- * } ):
469
+ * upload() {
470
+ * // Update loader's progress.
471
+ * server.onUploadProgress( data => {
472
+ * loader.uploadTotal = data.total;
473
+ * loader.uploaded = data.uploaded;
474
+ * } ):
418
475
*
419
- * // Return promise that will be resolved when file is uploaded.
420
- * return server.upload( loader.file );
421
- * }
476
+ * // Return promise that will be resolved when file is uploaded.
477
+ * return server.upload( loader.file );
478
+ * }
422
479
*
423
- * abort() {
424
- * // Reject promise returned from upload() method.
425
- * server.abortUpload();
480
+ * abort() {
481
+ * // Reject promise returned from upload() method.
482
+ * server.abortUpload();
483
+ * }
426
484
* }
427
- * }
428
485
*
429
486
* Then adapter can be set to be used by {@link module:upload/filerepository~FileRepository FileRepository }:
430
487
*
431
- * editor.plugins.get( 'FileRepository' ).createAdapter = function( loader ) {
432
- * return new Adapter( loader );
433
- * };
488
+ * editor.plugins.get( 'FileRepository' ).createAdapter = function( loader ) {
489
+ * return new Adapter( loader );
490
+ * };
434
491
*
435
492
* @interface Adapter
436
493
*/
@@ -440,18 +497,18 @@ mix( FileLoader, ObservableMixin );
440
497
* This method should return a promise that will resolve when data will be uploaded to server. Promise should be
441
498
* resolved with an object containing information about uploaded file:
442
499
*
443
- * {
444
- * default: 'http://server/default-size.image.png'
445
- * }
500
+ * {
501
+ * default: 'http://server/default-size.image.png'
502
+ * }
446
503
*
447
504
* Additionally, other image sizes can be provided:
448
505
*
449
- * {
450
- * default: 'http://server/default-size.image.png',
451
- * '160': 'http://server/size-160.image.png',
452
- * '500': 'http://server/size-500.image.png',
453
- * '1000': 'http://server/size-1000.image.png'
454
- * }
506
+ * {
507
+ * default: 'http://server/default-size.image.png',
508
+ * '160': 'http://server/size-160.image.png',
509
+ * '500': 'http://server/size-500.image.png',
510
+ * '1000': 'http://server/size-1000.image.png'
511
+ * }
455
512
*
456
513
* Take a look at {@link module:upload/filerepository~Adapter example Adapter implementation} and
457
514
* {@link module:upload/filerepository~FileRepository#createAdapter createAdapter method}.
0 commit comments