forked from fxmontigny/quill-image-upload
-
Notifications
You must be signed in to change notification settings - Fork 0
/
image-upload.min.js
5 lines (5 loc) · 2.37 KB
/
image-upload.min.js
1
2
3
4
5
(function () {
var exports = {};
"use strict";Object.defineProperty(exports,"__esModule",{value:true});var _createClass=function(){function e(e,t){for(var i=0;i<t.length;i++){var n=t[i];n.enumerable=n.enumerable||false;n.configurable=true;if("value"in n)n.writable=true;Object.defineProperty(e,n.key,n)}}return function(t,i,n){if(i)e(t.prototype,i);if(n)e(t,n);return t}}();function _classCallCheck(e,t){if(!(e instanceof t)){throw new TypeError("Cannot call a class as a function")}}var ImageUpload=exports.ImageUpload=function(){function e(t){var i=arguments.length>1&&arguments[1]!==undefined?arguments[1]:{};_classCallCheck(this,e);this.quill=t;this.options=i;this.quill.getModule("toolbar").addHandler("image",this.selectLocalImage.bind(this))}_createClass(e,[{key:"selectLocalImage",value:function e(){var t=this;var i=document.createElement("input");i.setAttribute("type","file");i.click();i.onchange=function(){var e=i.files[0];if(/^image\//.test(e.type)){var n=t.options.checkBeforeSend||t.checkBeforeSend.bind(t);n(e,t.sendToServer.bind(t))}else{console.warn("You could only upload images.")}}}},{key:"checkBeforeSend",value:function e(t,i){i(t)}},{key:"sendToServer",value:function e(t){var i=this;if(this.options.customUploader){this.options.customUploader(t,function(e){i.insert(e)})}else{var n=this.options.url,a=this.options.method||"POST",s=this.options.name||"image",o=this.options.headers||{},l=this.options.callbackOK||this.uploadImageCallbackOK.bind(this),r=this.options.callbackKO||this.uploadImageCallbackKO.bind(this);if(n){var u=new FormData;u.append(s,t);if(this.options.csrf){u.append(this.options.csrf.token,this.options.csrf.hash)}var c=new XMLHttpRequest;c.open(a,n,true);for(var d in o){c.setRequestHeader(d,o[d])}c.onload=function(){if(c.status===200){l(JSON.parse(c.responseText),i.insert.bind(i))}else{r({code:c.status,type:c.statusText,body:c.responseText})}};if(this.options.withCredentials){c.withCredentials=true}c.send(u)}else{var f=new FileReader;f.onload=function(e){l(e.target.result,i.insert.bind(i))};f.readAsDataURL(t)}}}},{key:"insert",value:function e(t){var i=(this.quill.getSelection()||{}).index||this.quill.getLength();this.quill.insertEmbed(i,"image",t,"user")}},{key:"uploadImageCallbackOK",value:function e(t,i){i(t)}},{key:"uploadImageCallbackKO",value:function e(t){alert(t)}}]);return e}();
window.Quill.register('modules/imageUpload', exports.ImageUpload);
})();