|
|
@@ -5,10 +5,27 @@
|
|
|
|
|
|
/* globals XMLHttpRequest, FormData */
|
|
|
|
|
|
+/**
|
|
|
+ * @module adapter-ckfinder/uploadadapter
|
|
|
+ */
|
|
|
+
|
|
|
import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
|
|
|
import FileRepository from '@ckeditor/ckeditor5-upload/src/filerepository';
|
|
|
import { getCSRFToken } from './utils';
|
|
|
|
|
|
+/**
|
|
|
+ * Plugin that enables CKFinder uploads in CKEditor 5.
|
|
|
+ * Configure proper upload URL under `ckfinder.uploadUrl` key, for example:
|
|
|
+ *
|
|
|
+ * Editor.create( editorElement, {
|
|
|
+ * plugins: [ ... ],
|
|
|
+ * ckfinder: {
|
|
|
+ * uploadUrl: 'http://example.com/upload'
|
|
|
+ * }
|
|
|
+ * } );
|
|
|
+ *
|
|
|
+ * @extends module:core/plugin~Plugin
|
|
|
+ */
|
|
|
export default class CKFinderUploadAdapter extends Plugin {
|
|
|
/**
|
|
|
* @inheritDoc
|
|
|
@@ -28,15 +45,34 @@ export default class CKFinderUploadAdapter extends Plugin {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+/**
|
|
|
+ * Upload adapter for CKFinder.
|
|
|
+ *
|
|
|
+ * @private
|
|
|
+ * @implements module:upload/filerepository~Adapter
|
|
|
+ */
|
|
|
class Adapter {
|
|
|
- constructor( loader, uploadURL, t ) {
|
|
|
+ /**
|
|
|
+ * Creates new adapter instance.
|
|
|
+ *
|
|
|
+ * @param {module:upload/filerepository~FileLoader} loader
|
|
|
+ * @param {String} url
|
|
|
+ * @param {module:utils/locale~Locale#t} t
|
|
|
+ */
|
|
|
+ constructor( loader, url, t ) {
|
|
|
// Save Loader instance to update upload progress.
|
|
|
this.loader = loader;
|
|
|
- this.uploadURL = uploadURL;
|
|
|
+ this.url = url;
|
|
|
|
|
|
this.t = t;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Starts upload process.
|
|
|
+ *
|
|
|
+ * @see module:upload/filerepository~Adapter#upload
|
|
|
+ * @returns {Promise}
|
|
|
+ */
|
|
|
upload() {
|
|
|
return new Promise( ( resolve, reject ) => {
|
|
|
this._initRequest();
|
|
|
@@ -45,19 +81,37 @@ class Adapter {
|
|
|
} );
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Aborts upload process.
|
|
|
+ *
|
|
|
+ * @see module:upload/filerepository~Adapter#abort
|
|
|
+ * @returns {Promise}
|
|
|
+ */
|
|
|
abort() {
|
|
|
if ( this.xhr ) {
|
|
|
this.xhr.abort();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Initializes XMLHttpRequest object.
|
|
|
+ *
|
|
|
+ * @private
|
|
|
+ */
|
|
|
_initRequest() {
|
|
|
const xhr = this.xhr = new XMLHttpRequest();
|
|
|
|
|
|
- xhr.open( 'POST', this.uploadURL, true );
|
|
|
+ xhr.open( 'POST', this.url, true );
|
|
|
xhr.responseType = 'json';
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Initializes XMLHttpRequest listeners.
|
|
|
+ *
|
|
|
+ * @private
|
|
|
+ * @param {Function} resolve Callback function to be called when request is successful.
|
|
|
+ * @param {Function} reject Callback function to be called when request cannot be completed.
|
|
|
+ */
|
|
|
_initListeners( resolve, reject ) {
|
|
|
const xhr = this.xhr;
|
|
|
const loader = this.loader;
|
|
|
@@ -89,6 +143,11 @@ class Adapter {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Prepares data and sends the request.
|
|
|
+ *
|
|
|
+ * @private
|
|
|
+ */
|
|
|
_sendRequest() {
|
|
|
// Prepare form data.
|
|
|
const data = new FormData();
|