-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
base64uploadadapter.js
108 lines (95 loc) · 2.48 KB
/
base64uploadadapter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/**
* @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/
/**
* @module upload/adapters/base64uploadadapter
*/
/* globals window */
import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
import FileRepository from '../filerepository';
/**
* A plugin that converts images inserted into the editor into [Base64 strings](https://en.wikipedia.org/wiki/Base64)
* in the {@glink builds/guides/integration/saving-data editor output}.
*
* This kind of image upload does not require server processing – images are stored with the rest of the text and
* displayed by the web browser without additional requests.
*
* Check out the {@glink features/image-upload/image-upload comprehensive "Image upload overview"} to learn about
* other ways to upload images into CKEditor 5.
*
* @extends module:core/plugin~Plugin
*/
export default class Base64UploadAdapter extends Plugin {
/**
* @inheritDoc
*/
static get requires() {
return [ FileRepository ];
}
/**
* @inheritDoc
*/
static get pluginName() {
return 'Base64UploadAdapter';
}
/**
* @inheritDoc
*/
init() {
this.editor.plugins.get( FileRepository ).createUploadAdapter = loader => new Adapter( loader );
}
}
/**
* The upload adapter that converts images inserted into the editor into Base64 strings.
*
* @private
* @implements module:upload/filerepository~UploadAdapter
*/
class Adapter {
/**
* Creates a new adapter instance.
*
* @param {module:upload/filerepository~FileLoader} loader
*/
constructor( loader ) {
/**
* `FileLoader` instance to use during the upload.
*
* @member {module:upload/filerepository~FileLoader} #loader
*/
this.loader = loader;
}
/**
* Starts the upload process.
*
* @see module:upload/filerepository~UploadAdapter#upload
* @returns {Promise}
*/
upload() {
return new Promise( ( resolve, reject ) => {
const reader = this.reader = new window.FileReader();
reader.addEventListener( 'load', () => {
resolve( { default: reader.result } );
} );
reader.addEventListener( 'error', err => {
reject( err );
} );
reader.addEventListener( 'abort', () => {
reject();
} );
this.loader.file.then( file => {
reader.readAsDataURL( file );
} );
} );
}
/**
* Aborts the upload process.
*
* @see module:upload/filerepository~UploadAdapter#abort
* @returns {Promise}
*/
abort() {
this.reader.abort();
}
}