mirror of
https://github.com/codex-team/editor.js
synced 2024-06-27 01:40:22 +02:00
c3ee7560f1
* cover restoring fixed * upd * fetch fixed in safari * updated * plugins * plugins ready * code improved * fixed bug with backspace * improved architecture * Versioning (#102) * start versioning * codex.version * eslint settings * versioning improved * cover restoring and fetch function fixed (#101) * cover restoring fixed * upd * fetch fixed in safari * updated * plugins * plugins ready * code improved * fixed bug with backspace * improved architecture * new sanitize method (#103) * new sanitize method Need to fix caret position * removed console logs * version updated * eslint style * caret position * big fixed on sanitize method * sanitize improved, using observers * sanitize: using html-janitor * fixes * last fixes, code improved after review * updated * new bundle * webpack config improved * upd * upd * upd * upd * clear from conflicts * upd * upd
106 lines
2.4 KiB
JavaScript
106 lines
2.4 KiB
JavaScript
/**
|
|
*
|
|
* Codex.Editor Transport Module
|
|
*
|
|
* @author Codex Team
|
|
* @version 1.0
|
|
*/
|
|
|
|
var transport = (function(transport){
|
|
|
|
transport.input = null;
|
|
|
|
/**
|
|
* @property {Object} arguments - keep plugin settings and defined callbacks
|
|
*/
|
|
transport.arguments = null;
|
|
|
|
transport.prepare = function(){
|
|
|
|
var input = document.createElement('INPUT');
|
|
|
|
input.type = 'file';
|
|
input.addEventListener('change', codex.transport.fileSelected);
|
|
|
|
codex.transport.input = input;
|
|
|
|
};
|
|
|
|
/** Clear input when files is uploaded */
|
|
transport.clearInput = function() {
|
|
|
|
/** Remove old input */
|
|
this.input = null;
|
|
|
|
/** Prepare new one */
|
|
this.prepare();
|
|
};
|
|
|
|
/**
|
|
* Callback for file selection
|
|
*/
|
|
transport.fileSelected = function(event){
|
|
|
|
var input = this,
|
|
files = input.files,
|
|
filesLength = files.length,
|
|
formdData = new FormData(),
|
|
file,
|
|
i;
|
|
|
|
formdData.append('files', files[0], files[0].name);
|
|
|
|
codex.transport.ajax({
|
|
data : formdData,
|
|
beforeSend : codex.transport.arguments.beforeSend,
|
|
success : codex.transport.arguments.success,
|
|
error : codex.transport.arguments.error
|
|
});
|
|
};
|
|
|
|
/**
|
|
* Use plugin callbacks
|
|
* @protected
|
|
*/
|
|
transport.selectAndUpload = function (args) {
|
|
|
|
this.arguments = args;
|
|
this.input.click();
|
|
|
|
};
|
|
|
|
/**
|
|
* Ajax requests module
|
|
*/
|
|
transport.ajax = function(params){
|
|
|
|
var xhr = new XMLHttpRequest(),
|
|
beforeSend = typeof params.beforeSend == 'function' ? params.beforeSend : function(){},
|
|
success = typeof params.success == 'function' ? params.success : function(){},
|
|
error = typeof params.error == 'function' ? params.error : function(){};
|
|
|
|
beforeSend();
|
|
|
|
xhr.open('POST', codex.settings.uploadImagesUrl, true);
|
|
|
|
xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
|
|
|
|
xhr.onload = function () {
|
|
if (xhr.status === 200) {
|
|
success(xhr.responseText);
|
|
} else {
|
|
console.log("request error: %o", xhr);
|
|
error();
|
|
}
|
|
};
|
|
|
|
xhr.send(params.data);
|
|
this.clearInput();
|
|
|
|
};
|
|
|
|
return transport;
|
|
|
|
})({});
|
|
|
|
module.exports = transport; |