forked from fxmontigny/quill-image-upload
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage-upload.min.js
5 lines (5 loc) · 2.12 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 a=0;a<t.length;a++){var n=t[a];n.enumerable=n.enumerable||false;n.configurable=true;if("value"in n)n.writable=true;Object.defineProperty(e,n.key,n)}}return function(t,a,n){if(a)e(t.prototype,a);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 a=arguments.length>1&&arguments[1]!==undefined?arguments[1]:{};_classCallCheck(this,e);this.quill=t;this.options=a;this.quill.getModule("toolbar").addHandler("image",this.selectLocalImage.bind(this))}_createClass(e,[{key:"selectLocalImage",value:function e(){var t=this;var a=document.createElement("input");a.setAttribute("type","file");a.click();a.onchange=function(){var e=a.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,a){a(t)}},{key:"sendToServer",value:function e(t){var a=this;var n=this.options.url,i=this.options.method||"POST",l=this.options.headers||{},o=this.options.callbackOK||this.uploadImageCallbackOK.bind(this),s=this.options.callbackKO||this.uploadImageCallbackKO.bind(this);if(n){var r=new FormData;r.append("image",t);var u=new XMLHttpRequest;u.open(i,n,true);for(var c in l){u.setRequestHeader(c,l[c])}u.onload=function(){if(u.status===200){o(JSON.parse(u.responseText),a.insert.bind(a))}else{s({code:u.status,type:u.statusText,body:u.responseText})}};u.send(r)}else{var d=new FileReader;d.onload=function(e){o(e.target.result,a.insert.bind(a))};d.readAsDataURL(t)}}},{key:"insert",value:function e(t){var a=(this.quill.getSelection()||{}).index||this.quill.getLength();this.quill.insertEmbed(a,"image",t,"user")}},{key:"uploadImageCallbackOK",value:function e(t,a){a(t)}},{key:"uploadImageCallbackKO",value:function e(t){alert(t)}}]);return e}();
window.Quill.register('modules/imageUpload', exports.ImageUpload);
})();