@ckeditor/ckeditor5-upload 40.0.0 → 40.1.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/LICENSE.md +2 -2
- package/package.json +4 -4
- package/src/adapters/base64uploadadapter.d.ts +33 -33
- package/src/adapters/base64uploadadapter.js +81 -81
- package/src/adapters/simpleuploadadapter.d.ts +48 -48
- package/src/adapters/simpleuploadadapter.js +175 -175
- package/src/augmentation.d.ts +20 -20
- package/src/augmentation.js +5 -5
- package/src/filereader.d.ts +56 -56
- package/src/filereader.js +71 -71
- package/src/filerepository.d.ts +342 -342
- package/src/filerepository.js +383 -383
- package/src/index.d.ts +13 -13
- package/src/index.js +12 -12
- package/src/ui/filedialogbuttonview.d.ts +84 -84
- package/src/ui/filedialogbuttonview.js +108 -108
- package/src/uploadconfig.d.ts +90 -90
- package/src/uploadconfig.js +5 -5
package/src/filerepository.d.ts
CHANGED
@@ -1,342 +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, PendingActions } 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(): readonly [typeof PendingActions];
|
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>;
|
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, PendingActions } 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(): readonly [typeof PendingActions];
|
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>;
|