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