Skip to content

Instantly share code, notes, and snippets.

@diedona
Created April 12, 2019 11:27
Show Gist options
  • Save diedona/16444e367c069b8b7639b85899b957f7 to your computer and use it in GitHub Desktop.
Save diedona/16444e367c069b8b7639b85899b957f7 to your computer and use it in GitHub Desktop.
/**
* @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
* Minor edits 2019 - Diego Doná
* For licensing, see LICENSE.md.
*/
export default function Base64UploaderPlugin( editor ) {
editor.plugins.get( 'FileRepository' ).createUploadAdapter = ( loader ) => {
// Configure the URL to the upload script in your back-end here!
return new UploadAdapter(loader, editor.t);
};
}
/**
* Upload adapter for Base64.
*
* @private
* @implements module:upload/filerepository~UploadAdapter
*/
class UploadAdapter {
/**
* Creates a new adapter instance.
*
* @param {module:upload/filerepository~FileLoader} loader
* @param {module:utils/locale~Locale#t} t
*/
constructor(loader, t) {
/**
* FileLoader instance to use during the upload.
*
* @member {module:upload/filerepository~FileLoader} #loader
*/
this.loader = loader;
/**
* Locale translation method.
*
* @member {module:utils/locale~Locale#t} #t
*/
this.t = t;
}
/**
* Starts the upload process.
*
* @see module:upload/filerepository~UploadAdapter#upload
* @returns {Promise}
*/
upload() {
return new Promise((resolve, reject) => {
const reader = this.reader = new FileReader();
reader.onload = function () {
resolve({ default: reader.result });
};
reader.onerror = function (error) {
reject(error);
};
reader.onabort = function () {
reject();
};
this.loader.file.then(file => {
reader.readAsDataURL(file);
});
});
}
/**
* Aborts the upload process.
*
* @see module:upload/filerepository~UploadAdapter#abort
* @returns {Promise}
*/
abort() {
if (this.reader) {
this.reader.abort();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment