forked from fxmontigny/quill-image-upload
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage-upload.js
More file actions
1 lines (1 loc) · 1.93 KB
/
image-upload.js
File metadata and controls
1 lines (1 loc) · 1.93 KB
1
"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=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||"your-url.com",i=this.options.method||"POST",o=this.options.headers||{},l=this.options.callbackOK||this.uploadImageCallbackOK.bind(this),s=this.options.callbackKO||this.uploadImageCallbackKO.bind(this),r=new FormData;r.append("image",t);var u=new XMLHttpRequest;u.open(i,n,true);for(var c in o){u.setRequestHeader(c,o[c])}u.onload=function(){if(u.status===200){l(JSON.parse(u.responseText),a.insert.bind(a))}else{s({code:u.status,type:u.statusText,body:u.responseText})}};u.send(r)}},{key:"insert",value:function e(t){var a=this.quill.getSelection();var n=a?a.index:this.quill.getLength();this.quill.insertEmbed(n,"image",t,"user")}},{key:"uploadImageCallbackOK",value:function e(t,a){a(t)}},{key:"uploadImageCallbackKO",value:function e(t){alert(t)}}]);return e}();exports.default=ImageUpload;