@ckeditor/ckeditor5-upload 36.0.1 → 37.0.0-alpha.1
Sign up to get free protection for your applications and to get access to all the features.
- package/package.json +7 -6
- package/src/adapters/base64uploadadapter.d.ts +32 -0
- package/src/adapters/base64uploadadapter.js +0 -14
- package/src/adapters/simpleuploadadapter.d.ts +47 -0
- package/src/adapters/simpleuploadadapter.js +20 -42
- package/src/augmentation.d.ts +20 -0
- package/src/augmentation.js +5 -0
- package/src/filereader.d.ts +56 -0
- package/src/filereader.js +2 -19
- package/src/filerepository.d.ts +342 -0
- package/src/filerepository.js +48 -175
- package/src/index.d.ts +13 -0
- package/src/index.js +1 -0
- package/src/ui/filedialogbuttonview.d.ts +84 -0
- package/src/ui/filedialogbuttonview.js +17 -73
- package/src/uploadconfig.d.ts +90 -0
- package/src/uploadconfig.js +5 -0
@@ -0,0 +1,342 @@
|
|
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/filerepository
|
7
|
+
*/
|
8
|
+
import { Plugin, type PluginDependencies } from '@ckeditor/ckeditor5-core';
|
9
|
+
import { Collection } from '@ckeditor/ckeditor5-utils';
|
10
|
+
/**
|
11
|
+
* File repository plugin. A central point for managing file upload.
|
12
|
+
*
|
13
|
+
* To use it, first you need an upload adapter. Upload adapter's job is to handle communication with the server
|
14
|
+
* (sending the file and handling server's response). You can use one of the existing plugins introducing upload adapters
|
15
|
+
* (e.g. {@link module:easy-image/cloudservicesuploadadapter~CloudServicesUploadAdapter} or
|
16
|
+
* {@link module:adapter-ckfinder/uploadadapter~CKFinderUploadAdapter}) or write your own one – see
|
17
|
+
* the {@glink framework/deep-dive/upload-adapter Custom image upload adapter deep-dive} guide.
|
18
|
+
*
|
19
|
+
* Then, you can use {@link module:upload/filerepository~FileRepository#createLoader `createLoader()`} and the returned
|
20
|
+
* {@link module:upload/filerepository~FileLoader} instance to load and upload files.
|
21
|
+
*/
|
22
|
+
export default class FileRepository extends Plugin {
|
23
|
+
/**
|
24
|
+
* Collection of loaders associated with this repository.
|
25
|
+
*/
|
26
|
+
loaders: Collection<FileLoader>;
|
27
|
+
/**
|
28
|
+
* A factory function which should be defined before using `FileRepository`.
|
29
|
+
*
|
30
|
+
* It should return a new instance of {@link module:upload/filerepository~UploadAdapter} that will be used to upload files.
|
31
|
+
* {@link module:upload/filerepository~FileLoader} instance associated with the adapter
|
32
|
+
* will be passed to that function.
|
33
|
+
*
|
34
|
+
* For more information and example see {@link module:upload/filerepository~UploadAdapter}.
|
35
|
+
*/
|
36
|
+
createUploadAdapter?: (loader: FileLoader) => UploadAdapter;
|
37
|
+
/**
|
38
|
+
* Loaders mappings used to retrieve loaders references.
|
39
|
+
*/
|
40
|
+
private _loadersMap;
|
41
|
+
/**
|
42
|
+
* Reference to a pending action registered in a {@link module:core/pendingactions~PendingActions} plugin
|
43
|
+
* while upload is in progress. When there is no upload then value is `null`.
|
44
|
+
*/
|
45
|
+
private _pendingAction;
|
46
|
+
/**
|
47
|
+
* Number of bytes uploaded.
|
48
|
+
*
|
49
|
+
* @readonly
|
50
|
+
* @observable
|
51
|
+
*/
|
52
|
+
uploaded: number;
|
53
|
+
/**
|
54
|
+
* Number of total bytes to upload.
|
55
|
+
*
|
56
|
+
* It might be different than the file size because of headers and additional data.
|
57
|
+
* It contains `null` if value is not available yet, so it's better to use {@link #uploadedPercent} to monitor
|
58
|
+
* the progress.
|
59
|
+
*
|
60
|
+
* @readonly
|
61
|
+
* @observable
|
62
|
+
*/
|
63
|
+
uploadTotal: number | null;
|
64
|
+
/**
|
65
|
+
* Upload progress in percents.
|
66
|
+
*
|
67
|
+
* @readonly
|
68
|
+
* @observable
|
69
|
+
*/
|
70
|
+
uploadedPercent: number;
|
71
|
+
/**
|
72
|
+
* @inheritDoc
|
73
|
+
*/
|
74
|
+
static get pluginName(): 'FileRepository';
|
75
|
+
/**
|
76
|
+
* @inheritDoc
|
77
|
+
*/
|
78
|
+
static get requires(): PluginDependencies;
|
79
|
+
/**
|
80
|
+
* @inheritDoc
|
81
|
+
*/
|
82
|
+
init(): void;
|
83
|
+
/**
|
84
|
+
* Returns the loader associated with specified file or promise.
|
85
|
+
*
|
86
|
+
* To get loader by id use `fileRepository.loaders.get( id )`.
|
87
|
+
*
|
88
|
+
* @param fileOrPromise Native file or promise handle.
|
89
|
+
*/
|
90
|
+
getLoader(fileOrPromise: File | Promise<File>): FileLoader | null;
|
91
|
+
/**
|
92
|
+
* Creates a loader instance for the given file.
|
93
|
+
*
|
94
|
+
* Requires {@link #createUploadAdapter} factory to be defined.
|
95
|
+
*
|
96
|
+
* @param fileOrPromise Native File object or native Promise object which resolves to a File.
|
97
|
+
*/
|
98
|
+
createLoader(fileOrPromise: File | Promise<File>): FileLoader | null;
|
99
|
+
/**
|
100
|
+
* Destroys the given loader.
|
101
|
+
*
|
102
|
+
* @param fileOrPromiseOrLoader File or Promise associated with that loader or loader itself.
|
103
|
+
*/
|
104
|
+
destroyLoader(fileOrPromiseOrLoader: File | Promise<File> | FileLoader): void;
|
105
|
+
/**
|
106
|
+
* Registers or deregisters pending action bound with upload progress.
|
107
|
+
*/
|
108
|
+
private _updatePendingAction;
|
109
|
+
}
|
110
|
+
declare const FileLoader_base: {
|
111
|
+
new (): import("@ckeditor/ckeditor5-utils").Observable;
|
112
|
+
prototype: import("@ckeditor/ckeditor5-utils").Observable;
|
113
|
+
};
|
114
|
+
/**
|
115
|
+
* File loader class.
|
116
|
+
*
|
117
|
+
* It is used to control the process of reading the file and uploading it using the specified upload adapter.
|
118
|
+
*/
|
119
|
+
declare class FileLoader extends FileLoader_base {
|
120
|
+
/**
|
121
|
+
* Unique id of FileLoader instance.
|
122
|
+
*
|
123
|
+
* @readonly
|
124
|
+
*/
|
125
|
+
readonly id: string;
|
126
|
+
/**
|
127
|
+
* Additional wrapper over the initial file promise passed to this loader.
|
128
|
+
*/
|
129
|
+
private _filePromiseWrapper;
|
130
|
+
/**
|
131
|
+
* Adapter instance associated with this file loader.
|
132
|
+
*/
|
133
|
+
private _adapter;
|
134
|
+
/**
|
135
|
+
* FileReader used by FileLoader.
|
136
|
+
*/
|
137
|
+
private _reader;
|
138
|
+
/**
|
139
|
+
* Current status of FileLoader. It can be one of the following:
|
140
|
+
*
|
141
|
+
* * 'idle',
|
142
|
+
* * 'reading',
|
143
|
+
* * 'uploading',
|
144
|
+
* * 'aborted',
|
145
|
+
* * 'error'.
|
146
|
+
*
|
147
|
+
* When reading status can change in a following way:
|
148
|
+
*
|
149
|
+
* `idle` -> `reading` -> `idle`
|
150
|
+
* `idle` -> `reading -> `aborted`
|
151
|
+
* `idle` -> `reading -> `error`
|
152
|
+
*
|
153
|
+
* When uploading status can change in a following way:
|
154
|
+
*
|
155
|
+
* `idle` -> `uploading` -> `idle`
|
156
|
+
* `idle` -> `uploading` -> `aborted`
|
157
|
+
* `idle` -> `uploading` -> `error`
|
158
|
+
*
|
159
|
+
* @readonly
|
160
|
+
* @observable
|
161
|
+
*/
|
162
|
+
status: 'idle' | 'reading' | 'uploading' | 'aborted' | 'error';
|
163
|
+
/**
|
164
|
+
* Number of bytes uploaded.
|
165
|
+
*
|
166
|
+
* @readonly
|
167
|
+
* @observable
|
168
|
+
*/
|
169
|
+
uploaded: number;
|
170
|
+
/**
|
171
|
+
* Number of total bytes to upload.
|
172
|
+
*
|
173
|
+
* @readonly
|
174
|
+
* @observable
|
175
|
+
*/
|
176
|
+
uploadTotal: number | null;
|
177
|
+
/**
|
178
|
+
* Upload progress in percents.
|
179
|
+
*
|
180
|
+
* @readonly
|
181
|
+
* @observable
|
182
|
+
*/
|
183
|
+
uploadedPercent: number;
|
184
|
+
/**
|
185
|
+
* Response of the upload.
|
186
|
+
*
|
187
|
+
* @readonly
|
188
|
+
* @observable
|
189
|
+
*/
|
190
|
+
uploadResponse?: UploadResponse | null;
|
191
|
+
/**
|
192
|
+
* Creates a new instance of `FileLoader`.
|
193
|
+
*
|
194
|
+
* @param filePromise A promise which resolves to a file instance.
|
195
|
+
* @param uploadAdapterCreator The function which returns {@link module:upload/filerepository~UploadAdapter} instance.
|
196
|
+
*/
|
197
|
+
constructor(filePromise: Promise<File>, uploadAdapterCreator: (loader: FileLoader) => UploadAdapter);
|
198
|
+
/**
|
199
|
+
* A `Promise` which resolves to a `File` instance associated with this file loader.
|
200
|
+
*/
|
201
|
+
get file(): Promise<File | null>;
|
202
|
+
/**
|
203
|
+
* Returns the file data. To read its data, you need for first load the file
|
204
|
+
* by using the {@link module:upload/filerepository~FileLoader#read `read()`} method.
|
205
|
+
*/
|
206
|
+
get data(): string | undefined;
|
207
|
+
/**
|
208
|
+
* Reads file using {@link module:upload/filereader~FileReader}.
|
209
|
+
*
|
210
|
+
* Throws {@link module:utils/ckeditorerror~CKEditorError CKEditorError} `filerepository-read-wrong-status` when status
|
211
|
+
* is different than `idle`.
|
212
|
+
*
|
213
|
+
* Example usage:
|
214
|
+
*
|
215
|
+
* ```ts
|
216
|
+
* fileLoader.read()
|
217
|
+
* .then( data => { ... } )
|
218
|
+
* .catch( err => {
|
219
|
+
* if ( err === 'aborted' ) {
|
220
|
+
* console.log( 'Reading aborted.' );
|
221
|
+
* } else {
|
222
|
+
* console.log( 'Reading error.', err );
|
223
|
+
* }
|
224
|
+
* } );
|
225
|
+
* ```
|
226
|
+
*
|
227
|
+
* @returns Returns promise that will be resolved with read data. Promise will be rejected if error
|
228
|
+
* occurs or if read process is aborted.
|
229
|
+
*/
|
230
|
+
read(): Promise<string>;
|
231
|
+
/**
|
232
|
+
* Reads file using the provided {@link module:upload/filerepository~UploadAdapter}.
|
233
|
+
*
|
234
|
+
* Throws {@link module:utils/ckeditorerror~CKEditorError CKEditorError} `filerepository-upload-wrong-status` when status
|
235
|
+
* is different than `idle`.
|
236
|
+
* Example usage:
|
237
|
+
*
|
238
|
+
* ```ts
|
239
|
+
* fileLoader.upload()
|
240
|
+
* .then( data => { ... } )
|
241
|
+
* .catch( e => {
|
242
|
+
* if ( e === 'aborted' ) {
|
243
|
+
* console.log( 'Uploading aborted.' );
|
244
|
+
* } else {
|
245
|
+
* console.log( 'Uploading error.', e );
|
246
|
+
* }
|
247
|
+
* } );
|
248
|
+
* ```
|
249
|
+
*
|
250
|
+
* @returns Returns promise that will be resolved with response data. Promise will be rejected if error
|
251
|
+
* occurs or if read process is aborted.
|
252
|
+
*/
|
253
|
+
upload(): Promise<UploadResponse>;
|
254
|
+
/**
|
255
|
+
* Aborts loading process.
|
256
|
+
*/
|
257
|
+
abort(): void;
|
258
|
+
/**
|
259
|
+
* Performs cleanup.
|
260
|
+
*
|
261
|
+
* @internal
|
262
|
+
*/
|
263
|
+
_destroy(): void;
|
264
|
+
/**
|
265
|
+
* Wraps a given file promise into another promise giving additional
|
266
|
+
* control (resolving, rejecting, checking if fulfilled) over it.
|
267
|
+
*
|
268
|
+
* @param filePromise The initial file promise to be wrapped.
|
269
|
+
*/
|
270
|
+
private _createFilePromiseWrapper;
|
271
|
+
}
|
272
|
+
export type { FileLoader };
|
273
|
+
/**
|
274
|
+
* Upload adapter interface used by the {@link module:upload/filerepository~FileRepository file repository}
|
275
|
+
* to handle file upload. An upload adapter is a bridge between the editor and server that handles file uploads.
|
276
|
+
* It should contain a logic necessary to initiate an upload process and monitor its progress.
|
277
|
+
*
|
278
|
+
* Learn how to develop your own upload adapter for CKEditor 5 in the
|
279
|
+
* {@glink framework/deep-dive/upload-adapter "Custom upload adapter"} guide.
|
280
|
+
*
|
281
|
+
* @interface UploadAdapter
|
282
|
+
*/
|
283
|
+
export interface UploadAdapter {
|
284
|
+
/**
|
285
|
+
* Executes the upload process.
|
286
|
+
* This method should return a promise that will resolve when data will be uploaded to server. Promise should be
|
287
|
+
* resolved with an object containing information about uploaded file:
|
288
|
+
*
|
289
|
+
* ```json
|
290
|
+
* {
|
291
|
+
* default: 'http://server/default-size.image.png'
|
292
|
+
* }
|
293
|
+
* ```
|
294
|
+
*
|
295
|
+
* Additionally, other image sizes can be provided:
|
296
|
+
*
|
297
|
+
* ```json
|
298
|
+
* {
|
299
|
+
* default: 'http://server/default-size.image.png',
|
300
|
+
* '160': 'http://server/size-160.image.png',
|
301
|
+
* '500': 'http://server/size-500.image.png',
|
302
|
+
* '1000': 'http://server/size-1000.image.png',
|
303
|
+
* '1052': 'http://server/default-size.image.png'
|
304
|
+
* }
|
305
|
+
* ```
|
306
|
+
*
|
307
|
+
* You can also pass additional properties from the server. In this case you need to wrap URLs
|
308
|
+
* in the `urls` object and pass additional properties along the `urls` property.
|
309
|
+
*
|
310
|
+
* ```json
|
311
|
+
* {
|
312
|
+
* myCustomProperty: 'foo',
|
313
|
+
* urls: {
|
314
|
+
* default: 'http://server/default-size.image.png',
|
315
|
+
* '160': 'http://server/size-160.image.png',
|
316
|
+
* '500': 'http://server/size-500.image.png',
|
317
|
+
* '1000': 'http://server/size-1000.image.png',
|
318
|
+
* '1052': 'http://server/default-size.image.png'
|
319
|
+
* }
|
320
|
+
* }
|
321
|
+
* ```
|
322
|
+
*
|
323
|
+
* NOTE: When returning multiple images, the widest returned one should equal the default one. It is essential to
|
324
|
+
* correctly set `width` attribute of the image. See this discussion:
|
325
|
+
* https://github.com/ckeditor/ckeditor5-easy-image/issues/4 for more information.
|
326
|
+
*
|
327
|
+
* Take a look at {@link module:upload/filerepository~UploadAdapter example Adapter implementation} and
|
328
|
+
* {@link module:upload/filerepository~FileRepository#createUploadAdapter createUploadAdapter method}.
|
329
|
+
*
|
330
|
+
* @returns Promise that should be resolved when data is uploaded.
|
331
|
+
*/
|
332
|
+
upload(): Promise<UploadResponse>;
|
333
|
+
/**
|
334
|
+
* Aborts the upload process.
|
335
|
+
* After aborting it should reject promise returned from {@link #upload upload()}.
|
336
|
+
*
|
337
|
+
* Take a look at {@link module:upload/filerepository~UploadAdapter example Adapter implementation} and
|
338
|
+
* {@link module:upload/filerepository~FileRepository#createUploadAdapter createUploadAdapter method}.
|
339
|
+
*/
|
340
|
+
abort?(): void;
|
341
|
+
}
|
342
|
+
export type UploadResponse = Record<string, unknown>;
|
package/src/filerepository.js
CHANGED
@@ -15,14 +15,28 @@ import FileReader from './filereader';
|
|
15
15
|
* (sending the file and handling server's response). You can use one of the existing plugins introducing upload adapters
|
16
16
|
* (e.g. {@link module:easy-image/cloudservicesuploadadapter~CloudServicesUploadAdapter} or
|
17
17
|
* {@link module:adapter-ckfinder/uploadadapter~CKFinderUploadAdapter}) or write your own one – see
|
18
|
-
* the {@glink framework/
|
18
|
+
* the {@glink framework/deep-dive/upload-adapter Custom image upload adapter deep-dive} guide.
|
19
19
|
*
|
20
20
|
* Then, you can use {@link module:upload/filerepository~FileRepository#createLoader `createLoader()`} and the returned
|
21
21
|
* {@link module:upload/filerepository~FileLoader} instance to load and upload files.
|
22
|
-
*
|
23
|
-
* @extends module:core/plugin~Plugin
|
24
22
|
*/
|
25
23
|
export default class FileRepository extends Plugin {
|
24
|
+
constructor() {
|
25
|
+
super(...arguments);
|
26
|
+
/**
|
27
|
+
* Collection of loaders associated with this repository.
|
28
|
+
*/
|
29
|
+
this.loaders = new Collection();
|
30
|
+
/**
|
31
|
+
* Loaders mappings used to retrieve loaders references.
|
32
|
+
*/
|
33
|
+
this._loadersMap = new Map();
|
34
|
+
/**
|
35
|
+
* Reference to a pending action registered in a {@link module:core/pendingactions~PendingActions} plugin
|
36
|
+
* while upload is in progress. When there is no upload then value is `null`.
|
37
|
+
*/
|
38
|
+
this._pendingAction = null;
|
39
|
+
}
|
26
40
|
/**
|
27
41
|
* @inheritDoc
|
28
42
|
*/
|
@@ -39,67 +53,10 @@ export default class FileRepository extends Plugin {
|
|
39
53
|
* @inheritDoc
|
40
54
|
*/
|
41
55
|
init() {
|
42
|
-
/**
|
43
|
-
* Collection of loaders associated with this repository.
|
44
|
-
*
|
45
|
-
* @member {module:utils/collection~Collection} #loaders
|
46
|
-
*/
|
47
|
-
this.loaders = new Collection();
|
48
56
|
// Keeps upload in a sync with pending actions.
|
49
57
|
this.loaders.on('change', () => this._updatePendingAction());
|
50
|
-
/**
|
51
|
-
* Loaders mappings used to retrieve loaders references.
|
52
|
-
*
|
53
|
-
* @private
|
54
|
-
* @member {Map<File|Promise, FileLoader>} #_loadersMap
|
55
|
-
*/
|
56
|
-
this._loadersMap = new Map();
|
57
|
-
/**
|
58
|
-
* Reference to a pending action registered in a {@link module:core/pendingactions~PendingActions} plugin
|
59
|
-
* while upload is in progress. When there is no upload then value is `null`.
|
60
|
-
*
|
61
|
-
* @private
|
62
|
-
* @member {Object} #_pendingAction
|
63
|
-
*/
|
64
|
-
this._pendingAction = null;
|
65
|
-
/**
|
66
|
-
* A factory function which should be defined before using `FileRepository`.
|
67
|
-
*
|
68
|
-
* It should return a new instance of {@link module:upload/filerepository~UploadAdapter} that will be used to upload files.
|
69
|
-
* {@link module:upload/filerepository~FileLoader} instance associated with the adapter
|
70
|
-
* will be passed to that function.
|
71
|
-
*
|
72
|
-
* For more information and example see {@link module:upload/filerepository~UploadAdapter}.
|
73
|
-
*
|
74
|
-
* @member {Function} #createUploadAdapter
|
75
|
-
*/
|
76
|
-
/**
|
77
|
-
* Number of bytes uploaded.
|
78
|
-
*
|
79
|
-
* @readonly
|
80
|
-
* @observable
|
81
|
-
* @member {Number} #uploaded
|
82
|
-
*/
|
83
58
|
this.set('uploaded', 0);
|
84
|
-
/**
|
85
|
-
* Number of total bytes to upload.
|
86
|
-
*
|
87
|
-
* It might be different than the file size because of headers and additional data.
|
88
|
-
* It contains `null` if value is not available yet, so it's better to use {@link #uploadedPercent} to monitor
|
89
|
-
* the progress.
|
90
|
-
*
|
91
|
-
* @readonly
|
92
|
-
* @observable
|
93
|
-
* @member {Number|null} #uploadTotal
|
94
|
-
*/
|
95
59
|
this.set('uploadTotal', null);
|
96
|
-
/**
|
97
|
-
* Upload progress in percents.
|
98
|
-
*
|
99
|
-
* @readonly
|
100
|
-
* @observable
|
101
|
-
* @member {Number} #uploadedPercent
|
102
|
-
*/
|
103
60
|
this.bind('uploadedPercent').to(this, 'uploaded', this, 'uploadTotal', (uploaded, total) => {
|
104
61
|
return total ? (uploaded / total * 100) : 0;
|
105
62
|
});
|
@@ -109,8 +66,7 @@ export default class FileRepository extends Plugin {
|
|
109
66
|
*
|
110
67
|
* To get loader by id use `fileRepository.loaders.get( id )`.
|
111
68
|
*
|
112
|
-
* @param
|
113
|
-
* @returns {module:upload/filerepository~FileLoader|null}
|
69
|
+
* @param fileOrPromise Native file or promise handle.
|
114
70
|
*/
|
115
71
|
getLoader(fileOrPromise) {
|
116
72
|
return this._loadersMap.get(fileOrPromise) || null;
|
@@ -120,8 +76,7 @@ export default class FileRepository extends Plugin {
|
|
120
76
|
*
|
121
77
|
* Requires {@link #createUploadAdapter} factory to be defined.
|
122
78
|
*
|
123
|
-
* @param
|
124
|
-
* @returns {module:upload/filerepository~FileLoader|null}
|
79
|
+
* @param fileOrPromise Native File object or native Promise object which resolves to a File.
|
125
80
|
*/
|
126
81
|
createLoader(fileOrPromise) {
|
127
82
|
if (!this.createUploadAdapter) {
|
@@ -129,7 +84,7 @@ export default class FileRepository extends Plugin {
|
|
129
84
|
* You need to enable an upload adapter in order to be able to upload files.
|
130
85
|
*
|
131
86
|
* This warning shows up when {@link module:upload/filerepository~FileRepository} is being used
|
132
|
-
* without {@link #createUploadAdapter defining an upload adapter}.
|
87
|
+
* without {@link module:upload/filerepository~FileRepository#createUploadAdapter defining an upload adapter}.
|
133
88
|
*
|
134
89
|
* **If you see this warning when using one of the {@glink installation/getting-started/predefined-builds
|
135
90
|
* CKEditor 5 Builds}**
|
@@ -144,7 +99,7 @@ export default class FileRepository extends Plugin {
|
|
144
99
|
* You can choose one of the existing upload adapters listed in the
|
145
100
|
* {@glink features/images/image-upload/image-upload "Image upload overview"}.
|
146
101
|
*
|
147
|
-
* You can also implement your {@glink framework/
|
102
|
+
* You can also implement your {@glink framework/deep-dive/upload-adapter own image upload adapter}.
|
148
103
|
*
|
149
104
|
* @error filerepository-no-upload-adapter
|
150
105
|
*/
|
@@ -186,8 +141,7 @@ export default class FileRepository extends Plugin {
|
|
186
141
|
/**
|
187
142
|
* Destroys the given loader.
|
188
143
|
*
|
189
|
-
* @param
|
190
|
-
* with that loader or loader itself.
|
144
|
+
* @param fileOrPromiseOrLoader File or Promise associated with that loader or loader itself.
|
191
145
|
*/
|
192
146
|
destroyLoader(fileOrPromiseOrLoader) {
|
193
147
|
const loader = fileOrPromiseOrLoader instanceof FileLoader ? fileOrPromiseOrLoader : this.getLoader(fileOrPromiseOrLoader);
|
@@ -201,8 +155,6 @@ export default class FileRepository extends Plugin {
|
|
201
155
|
}
|
202
156
|
/**
|
203
157
|
* Registers or deregisters pending action bound with upload progress.
|
204
|
-
*
|
205
|
-
* @private
|
206
158
|
*/
|
207
159
|
_updatePendingAction() {
|
208
160
|
const pendingActions = this.editor.plugins.get(PendingActions);
|
@@ -229,104 +181,25 @@ class FileLoader extends ObservableMixin() {
|
|
229
181
|
/**
|
230
182
|
* Creates a new instance of `FileLoader`.
|
231
183
|
*
|
232
|
-
* @param
|
233
|
-
* @param
|
184
|
+
* @param filePromise A promise which resolves to a file instance.
|
185
|
+
* @param uploadAdapterCreator The function which returns {@link module:upload/filerepository~UploadAdapter} instance.
|
234
186
|
*/
|
235
187
|
constructor(filePromise, uploadAdapterCreator) {
|
236
188
|
super();
|
237
|
-
/**
|
238
|
-
* Unique id of FileLoader instance.
|
239
|
-
*
|
240
|
-
* @readonly
|
241
|
-
* @member {Number}
|
242
|
-
*/
|
243
189
|
this.id = uid();
|
244
|
-
/**
|
245
|
-
* Additional wrapper over the initial file promise passed to this loader.
|
246
|
-
*
|
247
|
-
* @protected
|
248
|
-
* @member {module:upload/filerepository~FilePromiseWrapper}
|
249
|
-
*/
|
250
190
|
this._filePromiseWrapper = this._createFilePromiseWrapper(filePromise);
|
251
|
-
/**
|
252
|
-
* Adapter instance associated with this file loader.
|
253
|
-
*
|
254
|
-
* @private
|
255
|
-
* @member {module:upload/filerepository~UploadAdapter}
|
256
|
-
*/
|
257
191
|
this._adapter = uploadAdapterCreator(this);
|
258
|
-
/**
|
259
|
-
* FileReader used by FileLoader.
|
260
|
-
*
|
261
|
-
* @protected
|
262
|
-
* @member {module:upload/filereader~FileReader}
|
263
|
-
*/
|
264
192
|
this._reader = new FileReader();
|
265
|
-
/**
|
266
|
-
* Current status of FileLoader. It can be one of the following:
|
267
|
-
*
|
268
|
-
* * 'idle',
|
269
|
-
* * 'reading',
|
270
|
-
* * 'uploading',
|
271
|
-
* * 'aborted',
|
272
|
-
* * 'error'.
|
273
|
-
*
|
274
|
-
* When reading status can change in a following way:
|
275
|
-
*
|
276
|
-
* `idle` -> `reading` -> `idle`
|
277
|
-
* `idle` -> `reading -> `aborted`
|
278
|
-
* `idle` -> `reading -> `error`
|
279
|
-
*
|
280
|
-
* When uploading status can change in a following way:
|
281
|
-
*
|
282
|
-
* `idle` -> `uploading` -> `idle`
|
283
|
-
* `idle` -> `uploading` -> `aborted`
|
284
|
-
* `idle` -> `uploading` -> `error`
|
285
|
-
*
|
286
|
-
* @readonly
|
287
|
-
* @observable
|
288
|
-
* @member {String} #status
|
289
|
-
*/
|
290
193
|
this.set('status', 'idle');
|
291
|
-
/**
|
292
|
-
* Number of bytes uploaded.
|
293
|
-
*
|
294
|
-
* @readonly
|
295
|
-
* @observable
|
296
|
-
* @member {Number} #uploaded
|
297
|
-
*/
|
298
194
|
this.set('uploaded', 0);
|
299
|
-
/**
|
300
|
-
* Number of total bytes to upload.
|
301
|
-
*
|
302
|
-
* @readonly
|
303
|
-
* @observable
|
304
|
-
* @member {Number|null} #uploadTotal
|
305
|
-
*/
|
306
195
|
this.set('uploadTotal', null);
|
307
|
-
/**
|
308
|
-
* Upload progress in percents.
|
309
|
-
*
|
310
|
-
* @readonly
|
311
|
-
* @observable
|
312
|
-
* @member {Number} #uploadedPercent
|
313
|
-
*/
|
314
196
|
this.bind('uploadedPercent').to(this, 'uploaded', this, 'uploadTotal', (uploaded, total) => {
|
315
197
|
return total ? (uploaded / total * 100) : 0;
|
316
198
|
});
|
317
|
-
/**
|
318
|
-
* Response of the upload.
|
319
|
-
*
|
320
|
-
* @readonly
|
321
|
-
* @observable
|
322
|
-
* @member {Object|null} #uploadResponse
|
323
|
-
*/
|
324
199
|
this.set('uploadResponse', null);
|
325
200
|
}
|
326
201
|
/**
|
327
202
|
* A `Promise` which resolves to a `File` instance associated with this file loader.
|
328
|
-
*
|
329
|
-
* @type {Promise.<File|null>}
|
330
203
|
*/
|
331
204
|
get file() {
|
332
205
|
if (!this._filePromiseWrapper) {
|
@@ -348,8 +221,6 @@ class FileLoader extends ObservableMixin() {
|
|
348
221
|
/**
|
349
222
|
* Returns the file data. To read its data, you need for first load the file
|
350
223
|
* by using the {@link module:upload/filerepository~FileLoader#read `read()`} method.
|
351
|
-
*
|
352
|
-
* @type {File|undefined}
|
353
224
|
*/
|
354
225
|
get data() {
|
355
226
|
return this._reader.data;
|
@@ -362,17 +233,19 @@ class FileLoader extends ObservableMixin() {
|
|
362
233
|
*
|
363
234
|
* Example usage:
|
364
235
|
*
|
365
|
-
*
|
366
|
-
*
|
367
|
-
*
|
368
|
-
*
|
369
|
-
*
|
370
|
-
*
|
371
|
-
*
|
372
|
-
*
|
373
|
-
*
|
236
|
+
* ```ts
|
237
|
+
* fileLoader.read()
|
238
|
+
* .then( data => { ... } )
|
239
|
+
* .catch( err => {
|
240
|
+
* if ( err === 'aborted' ) {
|
241
|
+
* console.log( 'Reading aborted.' );
|
242
|
+
* } else {
|
243
|
+
* console.log( 'Reading error.', err );
|
244
|
+
* }
|
245
|
+
* } );
|
246
|
+
* ```
|
374
247
|
*
|
375
|
-
* @returns
|
248
|
+
* @returns Returns promise that will be resolved with read data. Promise will be rejected if error
|
376
249
|
* occurs or if read process is aborted.
|
377
250
|
*/
|
378
251
|
read() {
|
@@ -412,17 +285,19 @@ class FileLoader extends ObservableMixin() {
|
|
412
285
|
* is different than `idle`.
|
413
286
|
* Example usage:
|
414
287
|
*
|
415
|
-
*
|
416
|
-
*
|
417
|
-
*
|
418
|
-
*
|
419
|
-
*
|
420
|
-
*
|
421
|
-
*
|
422
|
-
*
|
423
|
-
*
|
288
|
+
* ```ts
|
289
|
+
* fileLoader.upload()
|
290
|
+
* .then( data => { ... } )
|
291
|
+
* .catch( e => {
|
292
|
+
* if ( e === 'aborted' ) {
|
293
|
+
* console.log( 'Uploading aborted.' );
|
294
|
+
* } else {
|
295
|
+
* console.log( 'Uploading error.', e );
|
296
|
+
* }
|
297
|
+
* } );
|
298
|
+
* ```
|
424
299
|
*
|
425
|
-
* @returns
|
300
|
+
* @returns Returns promise that will be resolved with response data. Promise will be rejected if error
|
426
301
|
* occurs or if read process is aborted.
|
427
302
|
*/
|
428
303
|
upload() {
|
@@ -486,9 +361,7 @@ class FileLoader extends ObservableMixin() {
|
|
486
361
|
* Wraps a given file promise into another promise giving additional
|
487
362
|
* control (resolving, rejecting, checking if fulfilled) over it.
|
488
363
|
*
|
489
|
-
* @private
|
490
364
|
* @param filePromise The initial file promise to be wrapped.
|
491
|
-
* @returns {module:upload/filerepository~FilePromiseWrapper}
|
492
365
|
*/
|
493
366
|
_createFilePromiseWrapper(filePromise) {
|
494
367
|
const wrapper = {};
|
package/src/index.d.ts
ADDED
@@ -0,0 +1,13 @@
|
|
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
|
7
|
+
*/
|
8
|
+
export { default as FileRepository, type UploadAdapter, type UploadResponse, type FileLoader } from './filerepository';
|
9
|
+
export { default as FileDialogButtonView } from './ui/filedialogbuttonview';
|
10
|
+
export { default as Base64UploadAdapter } from './adapters/base64uploadadapter';
|
11
|
+
export { default as SimpleUploadAdapter } from './adapters/simpleuploadadapter';
|
12
|
+
export type { SimpleUploadConfig } from './uploadconfig';
|
13
|
+
import './augmentation';
|