quill-image-compress 1.2.16 → 1.2.20
Sign up to get free protection for your applications and to get access to all the features.
- package/README.md +17 -9
- package/dist/demo.min.js +1 -1
- package/dist/quill.imageCompressor.min.js +1 -1
- package/package.json +1 -1
- package/src/downscaleImage.js +25 -1
- package/src/options.validation.js +66 -0
- package/src/quill.imageCompressor.js +8 -49
- package/src/quill.imageDrop.js +8 -8
package/README.md
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
# quill-image-compress
|
2
2
|
<!-- [START badges] -->
|
3
|
-
[![NPM Version](https://img.shields.io/npm/v/quill-image-compress.svg)](https://www.npmjs.com/package/quill-image-compress)
|
4
|
-
[![License](https://img.shields.io/npm/l/quill-image-compress.svg)](https://github.com/benwinding/quill-image-compress/blob/master/LICENSE)
|
5
|
-
[![Downloads/week](https://img.shields.io/npm/dm/quill-image-compress.svg)](https://www.npmjs.com/package/quill-image-compress)
|
3
|
+
[![NPM Version](https://img.shields.io/npm/v/quill-image-compress.svg)](https://www.npmjs.com/package/quill-image-compress)
|
4
|
+
[![License](https://img.shields.io/npm/l/quill-image-compress.svg)](https://github.com/benwinding/quill-image-compress/blob/master/LICENSE)
|
5
|
+
[![Downloads/week](https://img.shields.io/npm/dm/quill-image-compress.svg)](https://www.npmjs.com/package/quill-image-compress)
|
6
6
|
[![Github Issues](https://img.shields.io/github/issues/benwinding/quill-image-compress.svg)](https://github.com/benwinding/quill-image-compress)
|
7
7
|
![Build and Publish](https://github.com/benwinding/quill-image-compress/workflows/Build%20and%20Publish/badge.svg)
|
8
8
|
<!-- [END badges] -->
|
9
9
|
|
10
|
-
Quill.js Module which compresses images that are uploaded to the editor
|
10
|
+
Quill.js Module which compresses images that are uploaded to the editor
|
11
11
|
|
12
12
|
- [Live Demo!](https://benwinding.github.io/quill-image-compress/src/demo.html)
|
13
13
|
- [Live Demo! (with script tag)](https://benwinding.github.io/quill-image-compress/src/demo-script-tag.html)
|
@@ -21,7 +21,7 @@ Quill.js Module which compresses images that are uploaded to the editor
|
|
21
21
|
- Drag/Dropped into quill
|
22
22
|
- Pasted into quill
|
23
23
|
- Clicked image load button
|
24
|
-
- Handles most image formats a browser can read:
|
24
|
+
- Handles most image formats a browser can read:
|
25
25
|
- `gif|jpeg|png|svg|webp|bmp|vnd`
|
26
26
|
- Compression options [more info](#options)
|
27
27
|
|
@@ -71,13 +71,21 @@ const quill = new Quill(editor, {
|
|
71
71
|
|
72
72
|
## Options
|
73
73
|
|
74
|
-
- **maxWidth**
|
74
|
+
- **[Integer] maxWidth, maxHeight**
|
75
75
|
- Maximum width of images (in pixels)
|
76
|
-
- **quality**
|
76
|
+
- **[Float] quality**
|
77
77
|
- Image quality range: 0.0 - 1.0
|
78
|
-
- **imageType**
|
78
|
+
- **[String] imageType**
|
79
79
|
- Values: 'image/jpeg' , 'image/png' ... etc
|
80
|
-
- **
|
80
|
+
- **[Array] keepImageTypes**
|
81
|
+
Preserve image type and apply quality, maxWidth, maxHeight options
|
82
|
+
- Values: ['image/jpeg', 'image/png']
|
83
|
+
|
84
|
+
- **[Array] ignoreImageTypes**
|
85
|
+
Image types contained in this array retain their original images, do not compress them.
|
86
|
+
- Values: ['image'/jpeg', 'image/webp']
|
87
|
+
|
88
|
+
- **[Boolean] debug**
|
81
89
|
- Displays console logs: true/false
|
82
90
|
|
83
91
|
## Thanks
|
package/dist/demo.min.js
CHANGED
@@ -1 +1 @@
|
|
1
|
-
!function(e,t){if("object"==typeof exports&&"object"==typeof module)module.exports=t(require("Quill"));else if("function"==typeof define&&define.amd)define(["Quill"],t);else{var r="object"==typeof exports?t(require("Quill")):t(e.Quill);for(var n in r)("object"==typeof exports?exports:e)[n]=r[n]}}(window,(function(e){return function(e){var t={};function r(n){if(t[n])return t[n].exports;var o=t[n]={i:n,l:!1,exports:{}};return e[n].call(o.exports,o,o.exports,r),o.l=!0,o.exports}return r.m=e,r.c=t,r.d=function(e,t,n){r.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:n})},r.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},r.t=function(e,t){if(1&t&&(e=r(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var n=Object.create(null);if(r.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var o in e)r.d(n,o,function(t){return e[t]}.bind(null,o));return n},r.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return r.d(t,"a",t),t},r.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},r.p="/dist/",r(r.s=16)}([function(e,t,r){e.exports=r(7)},function(e,t){function r(e,t,r,n,o,i,a){try{var u=e[i](a),s=u.value}catch(e){return void r(e)}u.done?t(s):Promise.resolve(s).then(n,o)}e.exports=function(e){return function(){var t=this,n=arguments;return new Promise((function(o,i){var a=e.apply(t,n);function u(e){r(a,o,i,u,s,"next",e)}function s(e){r(a,o,i,u,s,"throw",e)}u(void 0)}))}},e.exports.default=e.exports,e.exports.__esModule=!0},function(e,t){e.exports=function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")},e.exports.default=e.exports,e.exports.__esModule=!0},function(e,t){function r(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}e.exports=function(e,t,n){return t&&r(e.prototype,t),n&&r(e,n),e},e.exports.default=e.exports,e.exports.__esModule=!0},function(e,t,r){"use strict";r.r(t),r.d(t,"file2b64",(function(){return u}));var n=r(1),o=r.n(n),i=r(0),a=r.n(i);function u(e){return s.apply(this,arguments)}function s(){return(s=o()(a.a.mark((function e(t){var r,n;return a.a.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=new FileReader,n=new Promise((function(e){r.addEventListener("load",(function(){var t=r.result;e(t)}),!1)})),r.readAsDataURL(t),e.abrupt("return",n);case 4:case"end":return e.stop()}}),e)})))).apply(this,arguments)}},function(e,t,r){var n=r(10),o=r(11),i=r(12),a=r(14);e.exports=function(e,t){return n(e)||o(e,t)||i(e,t)||a()},e.exports.default=e.exports,e.exports.__esModule=!0},function(e,t,r){"use strict";r.r(t),r.d(t,"imageCompressor",(function(){return v}));var n=r(1),o=r.n(n),i=r(2),a=r.n(i),u=r(3),s=r.n(u),l=r(0),c=r.n(l);var f=!0,h=!1,p={prefixString:function(){return"</> quill-image-compress: "},get log(){return f?console.log.bind(console,this.prefixString()):function(){}},get error(){return h?function(){}:console.error.bind(console,this.prefixString())},get warn(){return h?function(){}:console.warn.bind(console,this.prefixString())}},d=r(8).ImageDrop,g=r(4).file2b64,m=r(9).downscaleImage,v=function(){function e(t,r){var n=this;a()(this,e),this.quill=t,this.range=null,this.options=r,f=r&&r.debug,h=r&&r.suppressErrorLogging;var i=function(){var e=o()(c.a.mark((function e(t){var r;return c.a.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return p.log("onImageDrop",{dataUrl:t}),e.next=3,n.downscaleImageFromUrl(t);case 3:r=e.sent,n.insertToEditor(r);case 5:case"end":return e.stop()}}),e)})));return function(t){return e.apply(this,arguments)}}();this.imageDrop=new d(t,i,p),function(e){e.maxWidth=e.maxWidth||1e3,e.maxHeight=e.maxHeight||1e3,e.maxWidth&&"number"!=typeof e.maxWidth&&(p.warn("[config error] 'maxWidth' is required to be a \"number\" (in pixels), \nrecieved: ".concat(e.maxWidth,"\n-> using default 1000")),e.maxWidth=1e3),e.maxHeight&&"number"!=typeof e.maxHeight&&(p.warn("[config error] 'maxHeight' is required to be a \"number\" (in pixels), \nrecieved: ".concat(e.maxHeight,"\n-> using default 1000")),e.maxHeight=1e3),e.quality&&"number"!=typeof e.quality&&(p.warn("quill.imageCompressor: [config error] 'quality' is required to be a \"number\", \nrecieved: ".concat(e.quality,"\n-> using default 0.7")),e.quality=.7),!e.imageType||"string"==typeof e.imageType&&e.imageType.startsWith("image/")||(p.warn('quill.imageCompressor: [config error] \'imageType\' is required be in the form of "image/png" or "image/jpeg" etc ..., \nrecieved: '.concat(e.imageType,"\n-> using default image/jpeg")),e.imageType="image/jpeg")}(r),p.log("fileChanged",{options:r,quill:t,debug:f});var u=this.quill.getModule("toolbar");u?u.addHandler("image",(function(){return n.selectLocalImage()})):p.error("Quill toolbar module not found! need { toolbar: // options } in Quill.modules for image icon to sit in")}var t,r;return s()(e,[{key:"selectLocalImage",value:function(){var e=this;this.range=this.quill.getSelection(),this.fileHolder=document.createElement("input"),this.fileHolder.setAttribute("type","file"),this.fileHolder.setAttribute("accept","image/*"),this.fileHolder.setAttribute("style","visibility:hidden"),this.fileHolder.onchange=function(){return e.fileChanged()},document.body.appendChild(this.fileHolder),this.fileHolder.click(),window.requestAnimationFrame((function(){document.body.removeChild(e.fileHolder)}))}},{key:"fileChanged",value:(r=o()(c.a.mark((function e(){var t,r,n;return c.a.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(t=this.fileHolder.files[0],p.log("fileChanged",{file:t}),t){e.next=4;break}return e.abrupt("return");case 4:return e.next=6,g(t);case 6:return r=e.sent,e.next=9,this.downscaleImageFromUrl(r);case 9:n=e.sent,this.insertToEditor(n);case 11:case"end":return e.stop()}}),e,this)}))),function(){return r.apply(this,arguments)})},{key:"downscaleImageFromUrl",value:(t=o()(c.a.mark((function e(t){var r,n;return c.a.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=p,e.next=3,m(t,this.options.maxWidth,this.options.maxHeight,this.options.imageType,this.options.quality,r);case 3:return n=e.sent,p.log("downscaleImageFromUrl",{dataUrl:t,dataUrlCompressed:n}),e.abrupt("return",n);case 6:case"end":return e.stop()}}),e,this)}))),function(e){return t.apply(this,arguments)})},{key:"insertToEditor",value:function(e){p.log("insertToEditor",{url:e}),this.range=this.quill.getSelection();var t=this.range;this.logFileSize(e),this.quill.insertEmbed(t.index,"image","".concat(e),"user"),t.index++,this.quill.setSelection(t,"api")}},{key:"logFileSize",value:function(e){var t=(Math.round(3*(e.length-"data:image/png;base64,".length)/4)/1024).toFixed(0);p.log("estimated img size: "+t+" kb")}}]),e}();window.imageCompressor=v,t.default=v},function(e,t,r){var n=function(e){"use strict";var t=Object.prototype,r=t.hasOwnProperty,n="function"==typeof Symbol?Symbol:{},o=n.iterator||"@@iterator",i=n.asyncIterator||"@@asyncIterator",a=n.toStringTag||"@@toStringTag";function u(e,t,r){return Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}),e[t]}try{u({},"")}catch(e){u=function(e,t,r){return e[t]=r}}function s(e,t,r,n){var o=t&&t.prototype instanceof f?t:f,i=Object.create(o.prototype),a=new E(n||[]);return i._invoke=function(e,t,r){var n="suspendedStart";return function(o,i){if("executing"===n)throw new Error("Generator is already running");if("completed"===n){if("throw"===o)throw i;return j()}for(r.method=o,r.arg=i;;){var a=r.delegate;if(a){var u=b(a,r);if(u){if(u===c)continue;return u}}if("next"===r.method)r.sent=r._sent=r.arg;else if("throw"===r.method){if("suspendedStart"===n)throw n="completed",r.arg;r.dispatchException(r.arg)}else"return"===r.method&&r.abrupt("return",r.arg);n="executing";var s=l(e,t,r);if("normal"===s.type){if(n=r.done?"completed":"suspendedYield",s.arg===c)continue;return{value:s.arg,done:r.done}}"throw"===s.type&&(n="completed",r.method="throw",r.arg=s.arg)}}}(e,r,a),i}function l(e,t,r){try{return{type:"normal",arg:e.call(t,r)}}catch(e){return{type:"throw",arg:e}}}e.wrap=s;var c={};function f(){}function h(){}function p(){}var d={};u(d,o,(function(){return this}));var g=Object.getPrototypeOf,m=g&&g(g(L([])));m&&m!==t&&r.call(m,o)&&(d=m);var v=p.prototype=f.prototype=Object.create(d);function y(e){["next","throw","return"].forEach((function(t){u(e,t,(function(e){return this._invoke(t,e)}))}))}function x(e,t){var n;this._invoke=function(o,i){function a(){return new t((function(n,a){!function n(o,i,a,u){var s=l(e[o],e,i);if("throw"!==s.type){var c=s.arg,f=c.value;return f&&"object"==typeof f&&r.call(f,"__await")?t.resolve(f.__await).then((function(e){n("next",e,a,u)}),(function(e){n("throw",e,a,u)})):t.resolve(f).then((function(e){c.value=e,a(c)}),(function(e){return n("throw",e,a,u)}))}u(s.arg)}(o,i,n,a)}))}return n=n?n.then(a,a):a()}}function b(e,t){var r=e.iterator[t.method];if(void 0===r){if(t.delegate=null,"throw"===t.method){if(e.iterator.return&&(t.method="return",t.arg=void 0,b(e,t),"throw"===t.method))return c;t.method="throw",t.arg=new TypeError("The iterator does not provide a 'throw' method")}return c}var n=l(r,e.iterator,t.arg);if("throw"===n.type)return t.method="throw",t.arg=n.arg,t.delegate=null,c;var o=n.arg;return o?o.done?(t[e.resultName]=o.value,t.next=e.nextLoc,"return"!==t.method&&(t.method="next",t.arg=void 0),t.delegate=null,c):o:(t.method="throw",t.arg=new TypeError("iterator result is not an object"),t.delegate=null,c)}function w(e){var t={tryLoc:e[0]};1 in e&&(t.catchLoc=e[1]),2 in e&&(t.finallyLoc=e[2],t.afterLoc=e[3]),this.tryEntries.push(t)}function _(e){var t=e.completion||{};t.type="normal",delete t.arg,e.completion=t}function E(e){this.tryEntries=[{tryLoc:"root"}],e.forEach(w,this),this.reset(!0)}function L(e){if(e){var t=e[o];if(t)return t.call(e);if("function"==typeof e.next)return e;if(!isNaN(e.length)){var n=-1,i=function t(){for(;++n<e.length;)if(r.call(e,n))return t.value=e[n],t.done=!1,t;return t.value=void 0,t.done=!0,t};return i.next=i}}return{next:j}}function j(){return{value:void 0,done:!0}}return h.prototype=p,u(v,"constructor",p),u(p,"constructor",h),h.displayName=u(p,a,"GeneratorFunction"),e.isGeneratorFunction=function(e){var t="function"==typeof e&&e.constructor;return!!t&&(t===h||"GeneratorFunction"===(t.displayName||t.name))},e.mark=function(e){return Object.setPrototypeOf?Object.setPrototypeOf(e,p):(e.__proto__=p,u(e,a,"GeneratorFunction")),e.prototype=Object.create(v),e},e.awrap=function(e){return{__await:e}},y(x.prototype),u(x.prototype,i,(function(){return this})),e.AsyncIterator=x,e.async=function(t,r,n,o,i){void 0===i&&(i=Promise);var a=new x(s(t,r,n,o),i);return e.isGeneratorFunction(r)?a:a.next().then((function(e){return e.done?e.value:a.next()}))},y(v),u(v,a,"Generator"),u(v,o,(function(){return this})),u(v,"toString",(function(){return"[object Generator]"})),e.keys=function(e){var t=[];for(var r in e)t.push(r);return t.reverse(),function r(){for(;t.length;){var n=t.pop();if(n in e)return r.value=n,r.done=!1,r}return r.done=!0,r}},e.values=L,E.prototype={constructor:E,reset:function(e){if(this.prev=0,this.next=0,this.sent=this._sent=void 0,this.done=!1,this.delegate=null,this.method="next",this.arg=void 0,this.tryEntries.forEach(_),!e)for(var t in this)"t"===t.charAt(0)&&r.call(this,t)&&!isNaN(+t.slice(1))&&(this[t]=void 0)},stop:function(){this.done=!0;var e=this.tryEntries[0].completion;if("throw"===e.type)throw e.arg;return this.rval},dispatchException:function(e){if(this.done)throw e;var t=this;function n(r,n){return a.type="throw",a.arg=e,t.next=r,n&&(t.method="next",t.arg=void 0),!!n}for(var o=this.tryEntries.length-1;o>=0;--o){var i=this.tryEntries[o],a=i.completion;if("root"===i.tryLoc)return n("end");if(i.tryLoc<=this.prev){var u=r.call(i,"catchLoc"),s=r.call(i,"finallyLoc");if(u&&s){if(this.prev<i.catchLoc)return n(i.catchLoc,!0);if(this.prev<i.finallyLoc)return n(i.finallyLoc)}else if(u){if(this.prev<i.catchLoc)return n(i.catchLoc,!0)}else{if(!s)throw new Error("try statement without catch or finally");if(this.prev<i.finallyLoc)return n(i.finallyLoc)}}}},abrupt:function(e,t){for(var n=this.tryEntries.length-1;n>=0;--n){var o=this.tryEntries[n];if(o.tryLoc<=this.prev&&r.call(o,"finallyLoc")&&this.prev<o.finallyLoc){var i=o;break}}i&&("break"===e||"continue"===e)&&i.tryLoc<=t&&t<=i.finallyLoc&&(i=null);var a=i?i.completion:{};return a.type=e,a.arg=t,i?(this.method="next",this.next=i.finallyLoc,c):this.complete(a)},complete:function(e,t){if("throw"===e.type)throw e.arg;return"break"===e.type||"continue"===e.type?this.next=e.arg:"return"===e.type?(this.rval=this.arg=e.arg,this.method="return",this.next="end"):"normal"===e.type&&t&&(this.next=t),c},finish:function(e){for(var t=this.tryEntries.length-1;t>=0;--t){var r=this.tryEntries[t];if(r.finallyLoc===e)return this.complete(r.completion,r.afterLoc),_(r),c}},catch:function(e){for(var t=this.tryEntries.length-1;t>=0;--t){var r=this.tryEntries[t];if(r.tryLoc===e){var n=r.completion;if("throw"===n.type){var o=n.arg;_(r)}return o}}throw new Error("illegal catch attempt")},delegateYield:function(e,t,r){return this.delegate={iterator:L(e),resultName:t,nextLoc:r},"next"===this.method&&(this.arg=void 0),c}},e}(e.exports);try{regeneratorRuntime=n}catch(e){"object"==typeof globalThis?globalThis.regeneratorRuntime=n:Function("r","regeneratorRuntime = r")(n)}},function(e,t,r){"use strict";r.r(t),r.d(t,"ImageDrop",(function(){return h}));var n=r(1),o=r.n(n),i=r(2),a=r.n(i),u=r(3),s=r.n(u),l=r(0),c=r.n(l),f=r(4).file2b64,h=function(){function e(t,r,n){var o=this;a()(this,e),this.logger=n,this.quill=t,this.onNewDataUrl=r,this.quill.root.addEventListener("drop",(function(e){return o.handleDrop(e)}),!1),this.quill.root.addEventListener("paste",(function(e){return o.handlePaste(e)}),!1)}var t;return s()(e,[{key:"handleNewImageFiles",value:(t=o()(c.a.mark((function e(t){var r,n,o;return c.a.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(Array.isArray(t)){e.next=2;break}return e.abrupt("return");case 2:if(r=t.pop()){e.next=5;break}return e.abrupt("return");case 5:return n=r.getAsFile?r.getAsFile():r,e.next=8,f(n);case 8:o=e.sent,this.logger.log("handlePaste",{base64ImageSrc:o}),this.onNewDataUrl(o);case 11:case"end":return e.stop()}}),e,this)}))),function(e){return t.apply(this,arguments)})},{key:"handleDrop",value:function(e){e.preventDefault();var t=e.dataTransfer&&e.dataTransfer.files&&e.dataTransfer.files.length;if(this.logger.log("handleDrop",{hasFiles:t}),t){if(document.caretRangeFromPoint){var r=document.getSelection(),n=document.caretRangeFromPoint(e.clientX,e.clientY);r&&n&&r.setBaseAndExtent(n.startContainer,n.startOffset,n.startContainer,n.startOffset)}var o=this.getImageFiles(e.dataTransfer.files);this.handleNewImageFiles(o)}}},{key:"handlePaste",value:function(e){var t=e.clipboardData&&e.clipboardData.items&&!!e.clipboardData.items.length;if(this.logger.log("handlePaste",{hasItems:t}),t){var r=this.getImageFiles(e.clipboardData.items);0!==r.length&&(Array.from(e.clipboardData.items).some((function(e){return"text/html"===e.type}))?this.logger.log("detected html, not handling"):(e.preventDefault(),this.handleNewImageFiles(r)))}}},{key:"getImageFiles",value:function(e){var t=Array.from(e);return this.logger.log("readFiles",{files:t}),t.filter((function(e){return!!e.type.match(/^image\/(gif|jpe?g|a?png|svg|webp|bmp|vnd\.microsoft\.icon)/i)}))||[]}}]),e}()},function(e,t,r){"use strict";r.r(t),r.d(t,"downscaleImage",(function(){return l}));var n=r(5),o=r.n(n),i=r(1),a=r.n(i),u=r(0),s=r.n(u);function l(e,t,r,n,o,i){return c.apply(this,arguments)}function c(){return(c=a()(s.a.mark((function e(t,r,n,i,a,u){var l,c,h,p,d,g,m;return s.a.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return i=i||"image/jpeg",a=a||.7,(l=new Image).src=t,e.next=6,new Promise((function(e){l.onload=function(){e()}}));case 6:return c=f(l.width,l.height,r,n),h=o()(c,2),p=h[0],d=h[1],(g=document.createElement("canvas")).width=p,g.height=d,g.getContext("2d").drawImage(l,0,0,p,d),m=g.toDataURL(i,a),u.log("downscaling image...",{args:{dataUrl:t,maxWidth:r,maxHeight:n,imageType:i,imageQuality:a},newHeight:d,newWidth:p}),e.abrupt("return",m);case 15:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function f(e,t,r,n){if(e<=r&&t<=n)return[e,t];if(e>r){var o=r,i=Math.floor(t/e*o);if(i>n){var a=n;return[Math.floor(e/t*a),a]}return[o,i]}if(t>n){var u=n;return[Math.floor(e/t*u),u]}}},function(e,t){e.exports=function(e){if(Array.isArray(e))return e},e.exports.default=e.exports,e.exports.__esModule=!0},function(e,t){e.exports=function(e,t){var r=null==e?null:"undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(null!=r){var n,o,i=[],a=!0,u=!1;try{for(r=r.call(e);!(a=(n=r.next()).done)&&(i.push(n.value),!t||i.length!==t);a=!0);}catch(e){u=!0,o=e}finally{try{a||null==r.return||r.return()}finally{if(u)throw o}}return i}},e.exports.default=e.exports,e.exports.__esModule=!0},function(e,t,r){var n=r(13);e.exports=function(e,t){if(e){if("string"==typeof e)return n(e,t);var r=Object.prototype.toString.call(e).slice(8,-1);return"Object"===r&&e.constructor&&(r=e.constructor.name),"Map"===r||"Set"===r?Array.from(e):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?n(e,t):void 0}},e.exports.default=e.exports,e.exports.__esModule=!0},function(e,t){e.exports=function(e,t){(null==t||t>e.length)&&(t=e.length);for(var r=0,n=new Array(t);r<t;r++)n[r]=e[r];return n},e.exports.default=e.exports,e.exports.__esModule=!0},function(e,t){e.exports=function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")},e.exports.default=e.exports,e.exports.__esModule=!0},function(t,r){t.exports=e},function(e,t,r){"use strict";r.r(t);var n=r(15),o=r.n(n),i=r(6);o.a.register("modules/imageCompressor",i.default);console.log("Demo loaded...");new o.a("#editor",{theme:"snow",modules:{toolbar:{container:[[{header:[1,2,3,!1]}],["bold","italic"],["clean"],["image"]]},imageCompressor:{quality:.9,maxWidth:200,maxHeight:200,imageType:"image/png",debug:!0}}})}])}));
|
1
|
+
!function(e,t){if("object"==typeof exports&&"object"==typeof module)module.exports=t(require("Quill"));else if("function"==typeof define&&define.amd)define(["Quill"],t);else{var r="object"==typeof exports?t(require("Quill")):t(e.Quill);for(var n in r)("object"==typeof exports?exports:e)[n]=r[n]}}(window,(function(e){return function(e){var t={};function r(n){if(t[n])return t[n].exports;var o=t[n]={i:n,l:!1,exports:{}};return e[n].call(o.exports,o,o.exports,r),o.l=!0,o.exports}return r.m=e,r.c=t,r.d=function(e,t,n){r.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:n})},r.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},r.t=function(e,t){if(1&t&&(e=r(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var n=Object.create(null);if(r.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var o in e)r.d(n,o,function(t){return e[t]}.bind(null,o));return n},r.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return r.d(t,"a",t),t},r.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},r.p="/dist/",r(r.s=17)}([function(e,t,r){e.exports=r(7)},function(e,t){function r(e,t,r,n,o,i,a){try{var u=e[i](a),s=u.value}catch(e){return void r(e)}u.done?t(s):Promise.resolve(s).then(n,o)}e.exports=function(e){return function(){var t=this,n=arguments;return new Promise((function(o,i){var a=e.apply(t,n);function u(e){r(a,o,i,u,s,"next",e)}function s(e){r(a,o,i,u,s,"throw",e)}u(void 0)}))}},e.exports.__esModule=!0,e.exports.default=e.exports},function(e,t){e.exports=function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")},e.exports.__esModule=!0,e.exports.default=e.exports},function(e,t){function r(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}e.exports=function(e,t,n){return t&&r(e.prototype,t),n&&r(e,n),Object.defineProperty(e,"prototype",{writable:!1}),e},e.exports.__esModule=!0,e.exports.default=e.exports},function(e,t,r){"use strict";r.r(t),r.d(t,"file2b64",(function(){return u}));var n=r(1),o=r.n(n),i=r(0),a=r.n(i);function u(e){return s.apply(this,arguments)}function s(){return(s=o()(a.a.mark((function e(t){var r,n;return a.a.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=new FileReader,n=new Promise((function(e){r.addEventListener("load",(function(){var t=r.result;e(t)}),!1)})),r.readAsDataURL(t),e.abrupt("return",n);case 4:case"end":return e.stop()}}),e)})))).apply(this,arguments)}},function(e,t,r){var n=r(11),o=r(12),i=r(13),a=r(15);e.exports=function(e,t){return n(e)||o(e,t)||i(e,t)||a()},e.exports.__esModule=!0,e.exports.default=e.exports},function(e,t,r){"use strict";r.r(t),r.d(t,"imageCompressor",(function(){return v}));var n=r(1),o=r.n(n),i=r(2),a=r.n(i),u=r(3),s=r.n(u),l=r(0),c=r.n(l),f=!0,p=!1,h={prefixString:function(){return"</> quill-image-compress: "},get log(){return f?console.log.bind(console,this.prefixString()):function(){}},get error(){return p?function(){}:console.error.bind(console,this.prefixString())},get warn(){return p?function(){}:console.warn.bind(console,this.prefixString())}},g=r(8).ImageDrop,d=r(9).warnAboutOptions,m=r(4).file2b64,y=r(10).downscaleImage,v=function(){function e(t,r){var n=this;a()(this,e),this.quill=t,this.range=null,this.options=r||{},f=r.debug,p=r.suppressErrorLogging,d(r);var i=function(){var e=o()(c.a.mark((function e(t){var r;return c.a.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return h.log("onImageDrop",{dataUrl:t}),e.next=3,n.downscaleImageFromUrl(t);case 3:r=e.sent,n.insertToEditor(r);case 5:case"end":return e.stop()}}),e)})));return function(t){return e.apply(this,arguments)}}();this.imageDrop=new g(t,i,h),h.log("fileChanged",{options:r,quill:t,debug:f});var u=this.quill.getModule("toolbar");u?u.addHandler("image",(function(){return n.selectLocalImage()})):h.error("Quill toolbar module not found! need { toolbar: // options } in Quill.modules for image icon to sit in")}var t,r;return s()(e,[{key:"selectLocalImage",value:function(){var e=this;this.range=this.quill.getSelection(),this.fileHolder=document.createElement("input"),this.fileHolder.setAttribute("type","file"),this.fileHolder.setAttribute("accept","image/*"),this.fileHolder.setAttribute("style","visibility:hidden"),this.fileHolder.onchange=function(){return e.fileChanged()},document.body.appendChild(this.fileHolder),this.fileHolder.click(),window.requestAnimationFrame((function(){document.body.removeChild(e.fileHolder)}))}},{key:"fileChanged",value:(r=o()(c.a.mark((function e(){var t,r,n;return c.a.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(t=this.fileHolder.files[0],h.log("fileChanged",{file:t}),t){e.next=4;break}return e.abrupt("return");case 4:return e.next=6,m(t);case 6:return r=e.sent,e.next=9,this.downscaleImageFromUrl(r);case 9:n=e.sent,this.insertToEditor(n);case 11:case"end":return e.stop()}}),e,this)}))),function(){return r.apply(this,arguments)})},{key:"downscaleImageFromUrl",value:(t=o()(c.a.mark((function e(t){var r;return c.a.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,y(t,this.options.maxWidth,this.options.maxHeight,this.options.imageType,this.options.keepImageTypes,this.options.ignoreImageTypes,this.options.quality,h);case 2:return r=e.sent,h.log("downscaleImageFromUrl",{dataUrl:t,dataUrlCompressed:r}),e.abrupt("return",r);case 5:case"end":return e.stop()}}),e,this)}))),function(e){return t.apply(this,arguments)})},{key:"insertToEditor",value:function(e){h.log("insertToEditor",{url:e}),this.range=this.quill.getSelection();var t=this.range;this.logFileSize(e),this.quill.insertEmbed(t.index,"image","".concat(e),"user"),t.index++,this.quill.setSelection(t,"api")}},{key:"logFileSize",value:function(e){var t=(Math.round(3*(e.length-"data:image/png;base64,".length)/4)/1024).toFixed(0);h.log("estimated img size: "+t+" kb")}}]),e}();window.imageCompressor=v,t.default=v},function(e,t,r){var n=function(e){"use strict";var t=Object.prototype,r=t.hasOwnProperty,n="function"==typeof Symbol?Symbol:{},o=n.iterator||"@@iterator",i=n.asyncIterator||"@@asyncIterator",a=n.toStringTag||"@@toStringTag";function u(e,t,r){return Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}),e[t]}try{u({},"")}catch(e){u=function(e,t,r){return e[t]=r}}function s(e,t,r,n){var o=t&&t.prototype instanceof f?t:f,i=Object.create(o.prototype),a=new T(n||[]);return i._invoke=function(e,t,r){var n="suspendedStart";return function(o,i){if("executing"===n)throw new Error("Generator is already running");if("completed"===n){if("throw"===o)throw i;return k()}for(r.method=o,r.arg=i;;){var a=r.delegate;if(a){var u=b(a,r);if(u){if(u===c)continue;return u}}if("next"===r.method)r.sent=r._sent=r.arg;else if("throw"===r.method){if("suspendedStart"===n)throw n="completed",r.arg;r.dispatchException(r.arg)}else"return"===r.method&&r.abrupt("return",r.arg);n="executing";var s=l(e,t,r);if("normal"===s.type){if(n=r.done?"completed":"suspendedYield",s.arg===c)continue;return{value:s.arg,done:r.done}}"throw"===s.type&&(n="completed",r.method="throw",r.arg=s.arg)}}}(e,r,a),i}function l(e,t,r){try{return{type:"normal",arg:e.call(t,r)}}catch(e){return{type:"throw",arg:e}}}e.wrap=s;var c={};function f(){}function p(){}function h(){}var g={};u(g,o,(function(){return this}));var d=Object.getPrototypeOf,m=d&&d(d(L([])));m&&m!==t&&r.call(m,o)&&(g=m);var y=h.prototype=f.prototype=Object.create(g);function v(e){["next","throw","return"].forEach((function(t){u(e,t,(function(e){return this._invoke(t,e)}))}))}function x(e,t){var n;this._invoke=function(o,i){function a(){return new t((function(n,a){!function n(o,i,a,u){var s=l(e[o],e,i);if("throw"!==s.type){var c=s.arg,f=c.value;return f&&"object"==typeof f&&r.call(f,"__await")?t.resolve(f.__await).then((function(e){n("next",e,a,u)}),(function(e){n("throw",e,a,u)})):t.resolve(f).then((function(e){c.value=e,a(c)}),(function(e){return n("throw",e,a,u)}))}u(s.arg)}(o,i,n,a)}))}return n=n?n.then(a,a):a()}}function b(e,t){var r=e.iterator[t.method];if(void 0===r){if(t.delegate=null,"throw"===t.method){if(e.iterator.return&&(t.method="return",t.arg=void 0,b(e,t),"throw"===t.method))return c;t.method="throw",t.arg=new TypeError("The iterator does not provide a 'throw' method")}return c}var n=l(r,e.iterator,t.arg);if("throw"===n.type)return t.method="throw",t.arg=n.arg,t.delegate=null,c;var o=n.arg;return o?o.done?(t[e.resultName]=o.value,t.next=e.nextLoc,"return"!==t.method&&(t.method="next",t.arg=void 0),t.delegate=null,c):o:(t.method="throw",t.arg=new TypeError("iterator result is not an object"),t.delegate=null,c)}function w(e){var t={tryLoc:e[0]};1 in e&&(t.catchLoc=e[1]),2 in e&&(t.finallyLoc=e[2],t.afterLoc=e[3]),this.tryEntries.push(t)}function I(e){var t=e.completion||{};t.type="normal",delete t.arg,e.completion=t}function T(e){this.tryEntries=[{tryLoc:"root"}],e.forEach(w,this),this.reset(!0)}function L(e){if(e){var t=e[o];if(t)return t.call(e);if("function"==typeof e.next)return e;if(!isNaN(e.length)){var n=-1,i=function t(){for(;++n<e.length;)if(r.call(e,n))return t.value=e[n],t.done=!1,t;return t.value=void 0,t.done=!0,t};return i.next=i}}return{next:k}}function k(){return{value:void 0,done:!0}}return p.prototype=h,u(y,"constructor",h),u(h,"constructor",p),p.displayName=u(h,a,"GeneratorFunction"),e.isGeneratorFunction=function(e){var t="function"==typeof e&&e.constructor;return!!t&&(t===p||"GeneratorFunction"===(t.displayName||t.name))},e.mark=function(e){return Object.setPrototypeOf?Object.setPrototypeOf(e,h):(e.__proto__=h,u(e,a,"GeneratorFunction")),e.prototype=Object.create(y),e},e.awrap=function(e){return{__await:e}},v(x.prototype),u(x.prototype,i,(function(){return this})),e.AsyncIterator=x,e.async=function(t,r,n,o,i){void 0===i&&(i=Promise);var a=new x(s(t,r,n,o),i);return e.isGeneratorFunction(r)?a:a.next().then((function(e){return e.done?e.value:a.next()}))},v(y),u(y,a,"Generator"),u(y,o,(function(){return this})),u(y,"toString",(function(){return"[object Generator]"})),e.keys=function(e){var t=[];for(var r in e)t.push(r);return t.reverse(),function r(){for(;t.length;){var n=t.pop();if(n in e)return r.value=n,r.done=!1,r}return r.done=!0,r}},e.values=L,T.prototype={constructor:T,reset:function(e){if(this.prev=0,this.next=0,this.sent=this._sent=void 0,this.done=!1,this.delegate=null,this.method="next",this.arg=void 0,this.tryEntries.forEach(I),!e)for(var t in this)"t"===t.charAt(0)&&r.call(this,t)&&!isNaN(+t.slice(1))&&(this[t]=void 0)},stop:function(){this.done=!0;var e=this.tryEntries[0].completion;if("throw"===e.type)throw e.arg;return this.rval},dispatchException:function(e){if(this.done)throw e;var t=this;function n(r,n){return a.type="throw",a.arg=e,t.next=r,n&&(t.method="next",t.arg=void 0),!!n}for(var o=this.tryEntries.length-1;o>=0;--o){var i=this.tryEntries[o],a=i.completion;if("root"===i.tryLoc)return n("end");if(i.tryLoc<=this.prev){var u=r.call(i,"catchLoc"),s=r.call(i,"finallyLoc");if(u&&s){if(this.prev<i.catchLoc)return n(i.catchLoc,!0);if(this.prev<i.finallyLoc)return n(i.finallyLoc)}else if(u){if(this.prev<i.catchLoc)return n(i.catchLoc,!0)}else{if(!s)throw new Error("try statement without catch or finally");if(this.prev<i.finallyLoc)return n(i.finallyLoc)}}}},abrupt:function(e,t){for(var n=this.tryEntries.length-1;n>=0;--n){var o=this.tryEntries[n];if(o.tryLoc<=this.prev&&r.call(o,"finallyLoc")&&this.prev<o.finallyLoc){var i=o;break}}i&&("break"===e||"continue"===e)&&i.tryLoc<=t&&t<=i.finallyLoc&&(i=null);var a=i?i.completion:{};return a.type=e,a.arg=t,i?(this.method="next",this.next=i.finallyLoc,c):this.complete(a)},complete:function(e,t){if("throw"===e.type)throw e.arg;return"break"===e.type||"continue"===e.type?this.next=e.arg:"return"===e.type?(this.rval=this.arg=e.arg,this.method="return",this.next="end"):"normal"===e.type&&t&&(this.next=t),c},finish:function(e){for(var t=this.tryEntries.length-1;t>=0;--t){var r=this.tryEntries[t];if(r.finallyLoc===e)return this.complete(r.completion,r.afterLoc),I(r),c}},catch:function(e){for(var t=this.tryEntries.length-1;t>=0;--t){var r=this.tryEntries[t];if(r.tryLoc===e){var n=r.completion;if("throw"===n.type){var o=n.arg;I(r)}return o}}throw new Error("illegal catch attempt")},delegateYield:function(e,t,r){return this.delegate={iterator:L(e),resultName:t,nextLoc:r},"next"===this.method&&(this.arg=void 0),c}},e}(e.exports);try{regeneratorRuntime=n}catch(e){"object"==typeof globalThis?globalThis.regeneratorRuntime=n:Function("r","regeneratorRuntime = r")(n)}},function(e,t,r){"use strict";r.r(t),r.d(t,"ImageDrop",(function(){return p}));var n=r(1),o=r.n(n),i=r(2),a=r.n(i),u=r(3),s=r.n(u),l=r(0),c=r.n(l),f=r(4).file2b64,p=function(){function e(t,r,n){var o=this;a()(this,e),this.logger=n,this.quill=t,this.onNewDataUrl=r,this.quill.root.addEventListener("drop",(function(e){return o.handleDrop(e)}),!1),this.quill.root.addEventListener("paste",(function(e){return o.handlePaste(e)}),!1)}var t;return s()(e,[{key:"handleNewImageFiles",value:(t=o()(c.a.mark((function e(t){var r,n,o;return c.a.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(Array.isArray(t)){e.next=2;break}return e.abrupt("return");case 2:if(r=t.pop()){e.next=5;break}return e.abrupt("return");case 5:return n=r.getAsFile?r.getAsFile():r,e.next=8,f(n);case 8:o=e.sent,this.logger.log("handleNewImageFiles",{base64ImageSrc:o}),this.onNewDataUrl(o);case 11:case"end":return e.stop()}}),e,this)}))),function(e){return t.apply(this,arguments)})},{key:"handleDrop",value:function(e){e.preventDefault();var t=e.dataTransfer&&e.dataTransfer.files&&e.dataTransfer.files.length;if(this.logger.log("handleDrop",{hasFiles:t}),t){if(document.caretRangeFromPoint){var r=document.getSelection(),n=document.caretRangeFromPoint(e.clientX,e.clientY);r&&n&&r.setBaseAndExtent(n.startContainer,n.startOffset,n.startContainer,n.startOffset)}var o=this.getImageFiles(e.dataTransfer.files);this.handleNewImageFiles(o)}}},{key:"handlePaste",value:function(e){var t=e.clipboardData&&e.clipboardData.items&&!!e.clipboardData.items.length;if(this.logger.log("handlePaste",{hasItems:t}),t){var r=this.getImageFiles(e.clipboardData.items),n=r.length>0;if(this.logger.log("handlePaste",{hasImages:n,imageCount:r.length}),n)Array.from(e.clipboardData.items).some((function(e){return"text/html"===e.type}))?this.logger.log("detected html, not handling"):(e.preventDefault(),this.handleNewImageFiles(r))}}},{key:"getImageFiles",value:function(e){var t=Array.from(e);return this.logger.log("getImageFiles",{types:t.map((function(e){return e.type}))}),t.filter((function(e){return!!e.type.match(/^image\/(gif|jpe?g|a?png|svg|webp|bmp|vnd\.microsoft\.icon)/i)}))||[]}}]),e}()},function(e,t,r){"use strict";function n(e){e.maxWidth=e.maxWidth||1e3,e.maxHeight=e.maxHeight||1e3,e.maxWidth&&"number"!=typeof e.maxWidth&&(Logger.warn("[config error] 'maxWidth' is required to be a \"number\" (in pixels), \nreceived: ".concat(e.maxWidth,"\n-> using default 1000")),e.maxWidth=1e3),e.maxHeight&&"number"!=typeof e.maxHeight&&(Logger.warn("[config error] 'maxHeight' is required to be a \"number\" (in pixels), \nreceived: ".concat(e.maxHeight,"\n-> using default 1000")),e.maxHeight=1e3),e.quality&&"number"!=typeof e.quality&&(Logger.warn("quill.imageCompressor: [config error] 'quality' is required to be a \"number\", \nreceived: ".concat(e.quality,"\n-> using default 0.7")),e.quality=.7),!e.imageType||"string"==typeof e.imageType&&e.imageType.startsWith("image/")||(Logger.warn('quill.imageCompressor: [config error] \'imageType\' is required be in the form of "image/png" or "image/jpeg" etc ..., \nreceived: '.concat(e.imageType,"\n-> using default image/jpeg")),e.imageType="image/jpeg"),e.keepImageTypes||(e.keepImageTypes=[]),e.keepImageTypes&&!Array.isArray(e.keepImageTypes)&&(Logger.warn("quill.imageCompressor: [config error] 'keepImageTypes' is required to be a \"array\", received: ".concat(e.keepImageTypes," -> using default []")),e.keepImageTypes=[]),e.ignoreImageTypes||(e.ignoreImageTypes=[]),e.ignoreImageTypes&&!Array.isArray(e.ignoreImageTypes)&&(Logger.warn("quill.imageCompressor: [config error] 'ignoreImageTypes' is required to be a \"array\", received: ".concat(e.ignoreImageTypes," -> using default []")),e.ignoreImageTypes=[])}r.r(t),r.d(t,"warnAboutOptions",(function(){return n}))},function(e,t,r){"use strict";r.r(t),r.d(t,"downscaleImage",(function(){return l}));var n=r(5),o=r.n(n),i=r(1),a=r.n(i),u=r(0),s=r.n(u);function l(e,t,r,n,o,i,a,u){return c.apply(this,arguments)}function c(){return(c=a()(s.a.mark((function e(t,r,n,i,a,u,l,c){var p,h,g,d,m,y,v,x,b;return s.a.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return p=t.split(";")[0].split(":")[1],i=i||"image/jpeg",l=l||.7,(h=new Image).src=t,e.next=7,new Promise((function(e){h.onload=function(){e()}}));case 7:if(g=f(h.width,h.height,r,n),d=o()(g,2),m=d[0],y=d[1],(v=document.createElement("canvas")).width=m,v.height=y,x=v.getContext("2d"),"image/jpeg"===i&&(x.fillStyle="#FFFFFF",x.fillRect(0,0,h.width,h.height)),!u.includes(p)){e.next=15;break}return e.abrupt("return",t);case 15:return a.includes(p)&&(i=p),x.drawImage(h,0,0,m,y),b=v.toDataURL(i,l),c.log("downscaling image...",{args:{dataUrl:t,maxWidth:r,maxHeight:n,imageType:i,ignoreImageTypes:u,keepImageTypes:a,imageQuality:l},newHeight:y,newWidth:m}),e.abrupt("return",b);case 20:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function f(e,t,r,n){if(e<=r&&t<=n)return[e,t];if(e>r){var o=r,i=Math.floor(t/e*o);if(i>n){var a=n;return[Math.floor(e/t*a),a]}return[o,i]}if(t>n){var u=n;return[Math.floor(e/t*u),u]}}},function(e,t){e.exports=function(e){if(Array.isArray(e))return e},e.exports.__esModule=!0,e.exports.default=e.exports},function(e,t){e.exports=function(e,t){var r=null==e?null:"undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(null!=r){var n,o,i=[],a=!0,u=!1;try{for(r=r.call(e);!(a=(n=r.next()).done)&&(i.push(n.value),!t||i.length!==t);a=!0);}catch(e){u=!0,o=e}finally{try{a||null==r.return||r.return()}finally{if(u)throw o}}return i}},e.exports.__esModule=!0,e.exports.default=e.exports},function(e,t,r){var n=r(14);e.exports=function(e,t){if(e){if("string"==typeof e)return n(e,t);var r=Object.prototype.toString.call(e).slice(8,-1);return"Object"===r&&e.constructor&&(r=e.constructor.name),"Map"===r||"Set"===r?Array.from(e):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?n(e,t):void 0}},e.exports.__esModule=!0,e.exports.default=e.exports},function(e,t){e.exports=function(e,t){(null==t||t>e.length)&&(t=e.length);for(var r=0,n=new Array(t);r<t;r++)n[r]=e[r];return n},e.exports.__esModule=!0,e.exports.default=e.exports},function(e,t){e.exports=function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")},e.exports.__esModule=!0,e.exports.default=e.exports},function(t,r){t.exports=e},function(e,t,r){"use strict";r.r(t);var n=r(16),o=r.n(n),i=r(6);o.a.register("modules/imageCompressor",i.default);console.log("Demo loaded...");new o.a("#editor",{theme:"snow",modules:{toolbar:{container:[[{header:[1,2,3,!1]}],["bold","italic"],["clean"],["image"]]},imageCompressor:{quality:.9,maxWidth:200,maxHeight:200,imageType:"image/png",debug:!0}}})}])}));
|
@@ -1 +1 @@
|
|
1
|
-
!function(t,e){if("object"==typeof exports&&"object"==typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var r=e();for(var n in r)("object"==typeof exports?exports:t)[n]=r[n]}}(window,(function(){return function(t){var e={};function r(n){if(e[n])return e[n].exports;var o=e[n]={i:n,l:!1,exports:{}};return t[n].call(o.exports,o,o.exports,r),o.l=!0,o.exports}return r.m=t,r.c=e,r.d=function(t,e,n){r.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:n})},r.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},r.t=function(t,e){if(1&e&&(t=r(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var n=Object.create(null);if(r.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var o in t)r.d(n,o,function(e){return t[e]}.bind(null,o));return n},r.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return r.d(e,"a",e),e},r.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},r.p="/dist/",r(r.s=6)}([function(t,e,r){t.exports=r(7)},function(t,e){function r(t,e,r,n,o,i,a){try{var u=t[i](a),s=u.value}catch(t){return void r(t)}u.done?e(s):Promise.resolve(s).then(n,o)}t.exports=function(t){return function(){var e=this,n=arguments;return new Promise((function(o,i){var a=t.apply(e,n);function u(t){r(a,o,i,u,s,"next",t)}function s(t){r(a,o,i,u,s,"throw",t)}u(void 0)}))}},t.exports.default=t.exports,t.exports.__esModule=!0},function(t,e){t.exports=function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")},t.exports.default=t.exports,t.exports.__esModule=!0},function(t,e){function r(t,e){for(var r=0;r<e.length;r++){var n=e[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,n.key,n)}}t.exports=function(t,e,n){return e&&r(t.prototype,e),n&&r(t,n),t},t.exports.default=t.exports,t.exports.__esModule=!0},function(t,e,r){"use strict";r.r(e),r.d(e,"file2b64",(function(){return u}));var n=r(1),o=r.n(n),i=r(0),a=r.n(i);function u(t){return s.apply(this,arguments)}function s(){return(s=o()(a.a.mark((function t(e){var r,n;return a.a.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=new FileReader,n=new Promise((function(t){r.addEventListener("load",(function(){var e=r.result;t(e)}),!1)})),r.readAsDataURL(e),t.abrupt("return",n);case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}},function(t,e,r){var n=r(10),o=r(11),i=r(12),a=r(14);t.exports=function(t,e){return n(t)||o(t,e)||i(t,e)||a()},t.exports.default=t.exports,t.exports.__esModule=!0},function(t,e,r){"use strict";r.r(e),r.d(e,"imageCompressor",(function(){return v}));var n=r(1),o=r.n(n),i=r(2),a=r.n(i),u=r(3),s=r.n(u),l=r(0),c=r.n(l);var f=!0,h=!1,p={prefixString:function(){return"</> quill-image-compress: "},get log(){return f?console.log.bind(console,this.prefixString()):function(){}},get error(){return h?function(){}:console.error.bind(console,this.prefixString())},get warn(){return h?function(){}:console.warn.bind(console,this.prefixString())}},d=r(8).ImageDrop,g=r(4).file2b64,m=r(9).downscaleImage,v=function(){function t(e,r){var n=this;a()(this,t),this.quill=e,this.range=null,this.options=r,f=r&&r.debug,h=r&&r.suppressErrorLogging;var i=function(){var t=o()(c.a.mark((function t(e){var r;return c.a.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return p.log("onImageDrop",{dataUrl:e}),t.next=3,n.downscaleImageFromUrl(e);case 3:r=t.sent,n.insertToEditor(r);case 5:case"end":return t.stop()}}),t)})));return function(e){return t.apply(this,arguments)}}();this.imageDrop=new d(e,i,p),function(t){t.maxWidth=t.maxWidth||1e3,t.maxHeight=t.maxHeight||1e3,t.maxWidth&&"number"!=typeof t.maxWidth&&(p.warn("[config error] 'maxWidth' is required to be a \"number\" (in pixels), \nrecieved: ".concat(t.maxWidth,"\n-> using default 1000")),t.maxWidth=1e3),t.maxHeight&&"number"!=typeof t.maxHeight&&(p.warn("[config error] 'maxHeight' is required to be a \"number\" (in pixels), \nrecieved: ".concat(t.maxHeight,"\n-> using default 1000")),t.maxHeight=1e3),t.quality&&"number"!=typeof t.quality&&(p.warn("quill.imageCompressor: [config error] 'quality' is required to be a \"number\", \nrecieved: ".concat(t.quality,"\n-> using default 0.7")),t.quality=.7),!t.imageType||"string"==typeof t.imageType&&t.imageType.startsWith("image/")||(p.warn('quill.imageCompressor: [config error] \'imageType\' is required be in the form of "image/png" or "image/jpeg" etc ..., \nrecieved: '.concat(t.imageType,"\n-> using default image/jpeg")),t.imageType="image/jpeg")}(r),p.log("fileChanged",{options:r,quill:e,debug:f});var u=this.quill.getModule("toolbar");u?u.addHandler("image",(function(){return n.selectLocalImage()})):p.error("Quill toolbar module not found! need { toolbar: // options } in Quill.modules for image icon to sit in")}var e,r;return s()(t,[{key:"selectLocalImage",value:function(){var t=this;this.range=this.quill.getSelection(),this.fileHolder=document.createElement("input"),this.fileHolder.setAttribute("type","file"),this.fileHolder.setAttribute("accept","image/*"),this.fileHolder.setAttribute("style","visibility:hidden"),this.fileHolder.onchange=function(){return t.fileChanged()},document.body.appendChild(this.fileHolder),this.fileHolder.click(),window.requestAnimationFrame((function(){document.body.removeChild(t.fileHolder)}))}},{key:"fileChanged",value:(r=o()(c.a.mark((function t(){var e,r,n;return c.a.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(e=this.fileHolder.files[0],p.log("fileChanged",{file:e}),e){t.next=4;break}return t.abrupt("return");case 4:return t.next=6,g(e);case 6:return r=t.sent,t.next=9,this.downscaleImageFromUrl(r);case 9:n=t.sent,this.insertToEditor(n);case 11:case"end":return t.stop()}}),t,this)}))),function(){return r.apply(this,arguments)})},{key:"downscaleImageFromUrl",value:(e=o()(c.a.mark((function t(e){var r,n;return c.a.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=p,t.next=3,m(e,this.options.maxWidth,this.options.maxHeight,this.options.imageType,this.options.quality,r);case 3:return n=t.sent,p.log("downscaleImageFromUrl",{dataUrl:e,dataUrlCompressed:n}),t.abrupt("return",n);case 6:case"end":return t.stop()}}),t,this)}))),function(t){return e.apply(this,arguments)})},{key:"insertToEditor",value:function(t){p.log("insertToEditor",{url:t}),this.range=this.quill.getSelection();var e=this.range;this.logFileSize(t),this.quill.insertEmbed(e.index,"image","".concat(t),"user"),e.index++,this.quill.setSelection(e,"api")}},{key:"logFileSize",value:function(t){var e=(Math.round(3*(t.length-"data:image/png;base64,".length)/4)/1024).toFixed(0);p.log("estimated img size: "+e+" kb")}}]),t}();window.imageCompressor=v,e.default=v},function(t,e,r){var n=function(t){"use strict";var e=Object.prototype,r=e.hasOwnProperty,n="function"==typeof Symbol?Symbol:{},o=n.iterator||"@@iterator",i=n.asyncIterator||"@@asyncIterator",a=n.toStringTag||"@@toStringTag";function u(t,e,r){return Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}),t[e]}try{u({},"")}catch(t){u=function(t,e,r){return t[e]=r}}function s(t,e,r,n){var o=e&&e.prototype instanceof f?e:f,i=Object.create(o.prototype),a=new E(n||[]);return i._invoke=function(t,e,r){var n="suspendedStart";return function(o,i){if("executing"===n)throw new Error("Generator is already running");if("completed"===n){if("throw"===o)throw i;return j()}for(r.method=o,r.arg=i;;){var a=r.delegate;if(a){var u=b(a,r);if(u){if(u===c)continue;return u}}if("next"===r.method)r.sent=r._sent=r.arg;else if("throw"===r.method){if("suspendedStart"===n)throw n="completed",r.arg;r.dispatchException(r.arg)}else"return"===r.method&&r.abrupt("return",r.arg);n="executing";var s=l(t,e,r);if("normal"===s.type){if(n=r.done?"completed":"suspendedYield",s.arg===c)continue;return{value:s.arg,done:r.done}}"throw"===s.type&&(n="completed",r.method="throw",r.arg=s.arg)}}}(t,r,a),i}function l(t,e,r){try{return{type:"normal",arg:t.call(e,r)}}catch(t){return{type:"throw",arg:t}}}t.wrap=s;var c={};function f(){}function h(){}function p(){}var d={};u(d,o,(function(){return this}));var g=Object.getPrototypeOf,m=g&&g(g(L([])));m&&m!==e&&r.call(m,o)&&(d=m);var v=p.prototype=f.prototype=Object.create(d);function y(t){["next","throw","return"].forEach((function(e){u(t,e,(function(t){return this._invoke(e,t)}))}))}function x(t,e){var n;this._invoke=function(o,i){function a(){return new e((function(n,a){!function n(o,i,a,u){var s=l(t[o],t,i);if("throw"!==s.type){var c=s.arg,f=c.value;return f&&"object"==typeof f&&r.call(f,"__await")?e.resolve(f.__await).then((function(t){n("next",t,a,u)}),(function(t){n("throw",t,a,u)})):e.resolve(f).then((function(t){c.value=t,a(c)}),(function(t){return n("throw",t,a,u)}))}u(s.arg)}(o,i,n,a)}))}return n=n?n.then(a,a):a()}}function b(t,e){var r=t.iterator[e.method];if(void 0===r){if(e.delegate=null,"throw"===e.method){if(t.iterator.return&&(e.method="return",e.arg=void 0,b(t,e),"throw"===e.method))return c;e.method="throw",e.arg=new TypeError("The iterator does not provide a 'throw' method")}return c}var n=l(r,t.iterator,e.arg);if("throw"===n.type)return e.method="throw",e.arg=n.arg,e.delegate=null,c;var o=n.arg;return o?o.done?(e[t.resultName]=o.value,e.next=t.nextLoc,"return"!==e.method&&(e.method="next",e.arg=void 0),e.delegate=null,c):o:(e.method="throw",e.arg=new TypeError("iterator result is not an object"),e.delegate=null,c)}function w(t){var e={tryLoc:t[0]};1 in t&&(e.catchLoc=t[1]),2 in t&&(e.finallyLoc=t[2],e.afterLoc=t[3]),this.tryEntries.push(e)}function _(t){var e=t.completion||{};e.type="normal",delete e.arg,t.completion=e}function E(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(w,this),this.reset(!0)}function L(t){if(t){var e=t[o];if(e)return e.call(t);if("function"==typeof t.next)return t;if(!isNaN(t.length)){var n=-1,i=function e(){for(;++n<t.length;)if(r.call(t,n))return e.value=t[n],e.done=!1,e;return e.value=void 0,e.done=!0,e};return i.next=i}}return{next:j}}function j(){return{value:void 0,done:!0}}return h.prototype=p,u(v,"constructor",p),u(p,"constructor",h),h.displayName=u(p,a,"GeneratorFunction"),t.isGeneratorFunction=function(t){var e="function"==typeof t&&t.constructor;return!!e&&(e===h||"GeneratorFunction"===(e.displayName||e.name))},t.mark=function(t){return Object.setPrototypeOf?Object.setPrototypeOf(t,p):(t.__proto__=p,u(t,a,"GeneratorFunction")),t.prototype=Object.create(v),t},t.awrap=function(t){return{__await:t}},y(x.prototype),u(x.prototype,i,(function(){return this})),t.AsyncIterator=x,t.async=function(e,r,n,o,i){void 0===i&&(i=Promise);var a=new x(s(e,r,n,o),i);return t.isGeneratorFunction(r)?a:a.next().then((function(t){return t.done?t.value:a.next()}))},y(v),u(v,a,"Generator"),u(v,o,(function(){return this})),u(v,"toString",(function(){return"[object Generator]"})),t.keys=function(t){var e=[];for(var r in t)e.push(r);return e.reverse(),function r(){for(;e.length;){var n=e.pop();if(n in t)return r.value=n,r.done=!1,r}return r.done=!0,r}},t.values=L,E.prototype={constructor:E,reset:function(t){if(this.prev=0,this.next=0,this.sent=this._sent=void 0,this.done=!1,this.delegate=null,this.method="next",this.arg=void 0,this.tryEntries.forEach(_),!t)for(var e in this)"t"===e.charAt(0)&&r.call(this,e)&&!isNaN(+e.slice(1))&&(this[e]=void 0)},stop:function(){this.done=!0;var t=this.tryEntries[0].completion;if("throw"===t.type)throw t.arg;return this.rval},dispatchException:function(t){if(this.done)throw t;var e=this;function n(r,n){return a.type="throw",a.arg=t,e.next=r,n&&(e.method="next",e.arg=void 0),!!n}for(var o=this.tryEntries.length-1;o>=0;--o){var i=this.tryEntries[o],a=i.completion;if("root"===i.tryLoc)return n("end");if(i.tryLoc<=this.prev){var u=r.call(i,"catchLoc"),s=r.call(i,"finallyLoc");if(u&&s){if(this.prev<i.catchLoc)return n(i.catchLoc,!0);if(this.prev<i.finallyLoc)return n(i.finallyLoc)}else if(u){if(this.prev<i.catchLoc)return n(i.catchLoc,!0)}else{if(!s)throw new Error("try statement without catch or finally");if(this.prev<i.finallyLoc)return n(i.finallyLoc)}}}},abrupt:function(t,e){for(var n=this.tryEntries.length-1;n>=0;--n){var o=this.tryEntries[n];if(o.tryLoc<=this.prev&&r.call(o,"finallyLoc")&&this.prev<o.finallyLoc){var i=o;break}}i&&("break"===t||"continue"===t)&&i.tryLoc<=e&&e<=i.finallyLoc&&(i=null);var a=i?i.completion:{};return a.type=t,a.arg=e,i?(this.method="next",this.next=i.finallyLoc,c):this.complete(a)},complete:function(t,e){if("throw"===t.type)throw t.arg;return"break"===t.type||"continue"===t.type?this.next=t.arg:"return"===t.type?(this.rval=this.arg=t.arg,this.method="return",this.next="end"):"normal"===t.type&&e&&(this.next=e),c},finish:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var r=this.tryEntries[e];if(r.finallyLoc===t)return this.complete(r.completion,r.afterLoc),_(r),c}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var r=this.tryEntries[e];if(r.tryLoc===t){var n=r.completion;if("throw"===n.type){var o=n.arg;_(r)}return o}}throw new Error("illegal catch attempt")},delegateYield:function(t,e,r){return this.delegate={iterator:L(t),resultName:e,nextLoc:r},"next"===this.method&&(this.arg=void 0),c}},t}(t.exports);try{regeneratorRuntime=n}catch(t){"object"==typeof globalThis?globalThis.regeneratorRuntime=n:Function("r","regeneratorRuntime = r")(n)}},function(t,e,r){"use strict";r.r(e),r.d(e,"ImageDrop",(function(){return h}));var n=r(1),o=r.n(n),i=r(2),a=r.n(i),u=r(3),s=r.n(u),l=r(0),c=r.n(l),f=r(4).file2b64,h=function(){function t(e,r,n){var o=this;a()(this,t),this.logger=n,this.quill=e,this.onNewDataUrl=r,this.quill.root.addEventListener("drop",(function(t){return o.handleDrop(t)}),!1),this.quill.root.addEventListener("paste",(function(t){return o.handlePaste(t)}),!1)}var e;return s()(t,[{key:"handleNewImageFiles",value:(e=o()(c.a.mark((function t(e){var r,n,o;return c.a.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(Array.isArray(e)){t.next=2;break}return t.abrupt("return");case 2:if(r=e.pop()){t.next=5;break}return t.abrupt("return");case 5:return n=r.getAsFile?r.getAsFile():r,t.next=8,f(n);case 8:o=t.sent,this.logger.log("handlePaste",{base64ImageSrc:o}),this.onNewDataUrl(o);case 11:case"end":return t.stop()}}),t,this)}))),function(t){return e.apply(this,arguments)})},{key:"handleDrop",value:function(t){t.preventDefault();var e=t.dataTransfer&&t.dataTransfer.files&&t.dataTransfer.files.length;if(this.logger.log("handleDrop",{hasFiles:e}),e){if(document.caretRangeFromPoint){var r=document.getSelection(),n=document.caretRangeFromPoint(t.clientX,t.clientY);r&&n&&r.setBaseAndExtent(n.startContainer,n.startOffset,n.startContainer,n.startOffset)}var o=this.getImageFiles(t.dataTransfer.files);this.handleNewImageFiles(o)}}},{key:"handlePaste",value:function(t){var e=t.clipboardData&&t.clipboardData.items&&!!t.clipboardData.items.length;if(this.logger.log("handlePaste",{hasItems:e}),e){var r=this.getImageFiles(t.clipboardData.items);0!==r.length&&(Array.from(t.clipboardData.items).some((function(t){return"text/html"===t.type}))?this.logger.log("detected html, not handling"):(t.preventDefault(),this.handleNewImageFiles(r)))}}},{key:"getImageFiles",value:function(t){var e=Array.from(t);return this.logger.log("readFiles",{files:e}),e.filter((function(t){return!!t.type.match(/^image\/(gif|jpe?g|a?png|svg|webp|bmp|vnd\.microsoft\.icon)/i)}))||[]}}]),t}()},function(t,e,r){"use strict";r.r(e),r.d(e,"downscaleImage",(function(){return l}));var n=r(5),o=r.n(n),i=r(1),a=r.n(i),u=r(0),s=r.n(u);function l(t,e,r,n,o,i){return c.apply(this,arguments)}function c(){return(c=a()(s.a.mark((function t(e,r,n,i,a,u){var l,c,h,p,d,g,m;return s.a.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return i=i||"image/jpeg",a=a||.7,(l=new Image).src=e,t.next=6,new Promise((function(t){l.onload=function(){t()}}));case 6:return c=f(l.width,l.height,r,n),h=o()(c,2),p=h[0],d=h[1],(g=document.createElement("canvas")).width=p,g.height=d,g.getContext("2d").drawImage(l,0,0,p,d),m=g.toDataURL(i,a),u.log("downscaling image...",{args:{dataUrl:e,maxWidth:r,maxHeight:n,imageType:i,imageQuality:a},newHeight:d,newWidth:p}),t.abrupt("return",m);case 15:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function f(t,e,r,n){if(t<=r&&e<=n)return[t,e];if(t>r){var o=r,i=Math.floor(e/t*o);if(i>n){var a=n;return[Math.floor(t/e*a),a]}return[o,i]}if(e>n){var u=n;return[Math.floor(t/e*u),u]}}},function(t,e){t.exports=function(t){if(Array.isArray(t))return t},t.exports.default=t.exports,t.exports.__esModule=!0},function(t,e){t.exports=function(t,e){var r=null==t?null:"undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(null!=r){var n,o,i=[],a=!0,u=!1;try{for(r=r.call(t);!(a=(n=r.next()).done)&&(i.push(n.value),!e||i.length!==e);a=!0);}catch(t){u=!0,o=t}finally{try{a||null==r.return||r.return()}finally{if(u)throw o}}return i}},t.exports.default=t.exports,t.exports.__esModule=!0},function(t,e,r){var n=r(13);t.exports=function(t,e){if(t){if("string"==typeof t)return n(t,e);var r=Object.prototype.toString.call(t).slice(8,-1);return"Object"===r&&t.constructor&&(r=t.constructor.name),"Map"===r||"Set"===r?Array.from(t):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?n(t,e):void 0}},t.exports.default=t.exports,t.exports.__esModule=!0},function(t,e){t.exports=function(t,e){(null==e||e>t.length)&&(e=t.length);for(var r=0,n=new Array(e);r<e;r++)n[r]=t[r];return n},t.exports.default=t.exports,t.exports.__esModule=!0},function(t,e){t.exports=function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")},t.exports.default=t.exports,t.exports.__esModule=!0}])}));
|
1
|
+
!function(e,t){if("object"==typeof exports&&"object"==typeof module)module.exports=t();else if("function"==typeof define&&define.amd)define([],t);else{var r=t();for(var n in r)("object"==typeof exports?exports:e)[n]=r[n]}}(window,(function(){return function(e){var t={};function r(n){if(t[n])return t[n].exports;var o=t[n]={i:n,l:!1,exports:{}};return e[n].call(o.exports,o,o.exports,r),o.l=!0,o.exports}return r.m=e,r.c=t,r.d=function(e,t,n){r.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:n})},r.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},r.t=function(e,t){if(1&t&&(e=r(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var n=Object.create(null);if(r.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var o in e)r.d(n,o,function(t){return e[t]}.bind(null,o));return n},r.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return r.d(t,"a",t),t},r.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},r.p="/dist/",r(r.s=6)}([function(e,t,r){e.exports=r(7)},function(e,t){function r(e,t,r,n,o,i,a){try{var u=e[i](a),s=u.value}catch(e){return void r(e)}u.done?t(s):Promise.resolve(s).then(n,o)}e.exports=function(e){return function(){var t=this,n=arguments;return new Promise((function(o,i){var a=e.apply(t,n);function u(e){r(a,o,i,u,s,"next",e)}function s(e){r(a,o,i,u,s,"throw",e)}u(void 0)}))}},e.exports.__esModule=!0,e.exports.default=e.exports},function(e,t){e.exports=function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")},e.exports.__esModule=!0,e.exports.default=e.exports},function(e,t){function r(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}e.exports=function(e,t,n){return t&&r(e.prototype,t),n&&r(e,n),Object.defineProperty(e,"prototype",{writable:!1}),e},e.exports.__esModule=!0,e.exports.default=e.exports},function(e,t,r){"use strict";r.r(t),r.d(t,"file2b64",(function(){return u}));var n=r(1),o=r.n(n),i=r(0),a=r.n(i);function u(e){return s.apply(this,arguments)}function s(){return(s=o()(a.a.mark((function e(t){var r,n;return a.a.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=new FileReader,n=new Promise((function(e){r.addEventListener("load",(function(){var t=r.result;e(t)}),!1)})),r.readAsDataURL(t),e.abrupt("return",n);case 4:case"end":return e.stop()}}),e)})))).apply(this,arguments)}},function(e,t,r){var n=r(11),o=r(12),i=r(13),a=r(15);e.exports=function(e,t){return n(e)||o(e,t)||i(e,t)||a()},e.exports.__esModule=!0,e.exports.default=e.exports},function(e,t,r){"use strict";r.r(t),r.d(t,"imageCompressor",(function(){return v}));var n=r(1),o=r.n(n),i=r(2),a=r.n(i),u=r(3),s=r.n(u),l=r(0),c=r.n(l),f=!0,p=!1,h={prefixString:function(){return"</> quill-image-compress: "},get log(){return f?console.log.bind(console,this.prefixString()):function(){}},get error(){return p?function(){}:console.error.bind(console,this.prefixString())},get warn(){return p?function(){}:console.warn.bind(console,this.prefixString())}},g=r(8).ImageDrop,d=r(9).warnAboutOptions,m=r(4).file2b64,y=r(10).downscaleImage,v=function(){function e(t,r){var n=this;a()(this,e),this.quill=t,this.range=null,this.options=r||{},f=r.debug,p=r.suppressErrorLogging,d(r);var i=function(){var e=o()(c.a.mark((function e(t){var r;return c.a.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return h.log("onImageDrop",{dataUrl:t}),e.next=3,n.downscaleImageFromUrl(t);case 3:r=e.sent,n.insertToEditor(r);case 5:case"end":return e.stop()}}),e)})));return function(t){return e.apply(this,arguments)}}();this.imageDrop=new g(t,i,h),h.log("fileChanged",{options:r,quill:t,debug:f});var u=this.quill.getModule("toolbar");u?u.addHandler("image",(function(){return n.selectLocalImage()})):h.error("Quill toolbar module not found! need { toolbar: // options } in Quill.modules for image icon to sit in")}var t,r;return s()(e,[{key:"selectLocalImage",value:function(){var e=this;this.range=this.quill.getSelection(),this.fileHolder=document.createElement("input"),this.fileHolder.setAttribute("type","file"),this.fileHolder.setAttribute("accept","image/*"),this.fileHolder.setAttribute("style","visibility:hidden"),this.fileHolder.onchange=function(){return e.fileChanged()},document.body.appendChild(this.fileHolder),this.fileHolder.click(),window.requestAnimationFrame((function(){document.body.removeChild(e.fileHolder)}))}},{key:"fileChanged",value:(r=o()(c.a.mark((function e(){var t,r,n;return c.a.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(t=this.fileHolder.files[0],h.log("fileChanged",{file:t}),t){e.next=4;break}return e.abrupt("return");case 4:return e.next=6,m(t);case 6:return r=e.sent,e.next=9,this.downscaleImageFromUrl(r);case 9:n=e.sent,this.insertToEditor(n);case 11:case"end":return e.stop()}}),e,this)}))),function(){return r.apply(this,arguments)})},{key:"downscaleImageFromUrl",value:(t=o()(c.a.mark((function e(t){var r;return c.a.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,y(t,this.options.maxWidth,this.options.maxHeight,this.options.imageType,this.options.keepImageTypes,this.options.ignoreImageTypes,this.options.quality,h);case 2:return r=e.sent,h.log("downscaleImageFromUrl",{dataUrl:t,dataUrlCompressed:r}),e.abrupt("return",r);case 5:case"end":return e.stop()}}),e,this)}))),function(e){return t.apply(this,arguments)})},{key:"insertToEditor",value:function(e){h.log("insertToEditor",{url:e}),this.range=this.quill.getSelection();var t=this.range;this.logFileSize(e),this.quill.insertEmbed(t.index,"image","".concat(e),"user"),t.index++,this.quill.setSelection(t,"api")}},{key:"logFileSize",value:function(e){var t=(Math.round(3*(e.length-"data:image/png;base64,".length)/4)/1024).toFixed(0);h.log("estimated img size: "+t+" kb")}}]),e}();window.imageCompressor=v,t.default=v},function(e,t,r){var n=function(e){"use strict";var t=Object.prototype,r=t.hasOwnProperty,n="function"==typeof Symbol?Symbol:{},o=n.iterator||"@@iterator",i=n.asyncIterator||"@@asyncIterator",a=n.toStringTag||"@@toStringTag";function u(e,t,r){return Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}),e[t]}try{u({},"")}catch(e){u=function(e,t,r){return e[t]=r}}function s(e,t,r,n){var o=t&&t.prototype instanceof f?t:f,i=Object.create(o.prototype),a=new T(n||[]);return i._invoke=function(e,t,r){var n="suspendedStart";return function(o,i){if("executing"===n)throw new Error("Generator is already running");if("completed"===n){if("throw"===o)throw i;return k()}for(r.method=o,r.arg=i;;){var a=r.delegate;if(a){var u=b(a,r);if(u){if(u===c)continue;return u}}if("next"===r.method)r.sent=r._sent=r.arg;else if("throw"===r.method){if("suspendedStart"===n)throw n="completed",r.arg;r.dispatchException(r.arg)}else"return"===r.method&&r.abrupt("return",r.arg);n="executing";var s=l(e,t,r);if("normal"===s.type){if(n=r.done?"completed":"suspendedYield",s.arg===c)continue;return{value:s.arg,done:r.done}}"throw"===s.type&&(n="completed",r.method="throw",r.arg=s.arg)}}}(e,r,a),i}function l(e,t,r){try{return{type:"normal",arg:e.call(t,r)}}catch(e){return{type:"throw",arg:e}}}e.wrap=s;var c={};function f(){}function p(){}function h(){}var g={};u(g,o,(function(){return this}));var d=Object.getPrototypeOf,m=d&&d(d(L([])));m&&m!==t&&r.call(m,o)&&(g=m);var y=h.prototype=f.prototype=Object.create(g);function v(e){["next","throw","return"].forEach((function(t){u(e,t,(function(e){return this._invoke(t,e)}))}))}function x(e,t){var n;this._invoke=function(o,i){function a(){return new t((function(n,a){!function n(o,i,a,u){var s=l(e[o],e,i);if("throw"!==s.type){var c=s.arg,f=c.value;return f&&"object"==typeof f&&r.call(f,"__await")?t.resolve(f.__await).then((function(e){n("next",e,a,u)}),(function(e){n("throw",e,a,u)})):t.resolve(f).then((function(e){c.value=e,a(c)}),(function(e){return n("throw",e,a,u)}))}u(s.arg)}(o,i,n,a)}))}return n=n?n.then(a,a):a()}}function b(e,t){var r=e.iterator[t.method];if(void 0===r){if(t.delegate=null,"throw"===t.method){if(e.iterator.return&&(t.method="return",t.arg=void 0,b(e,t),"throw"===t.method))return c;t.method="throw",t.arg=new TypeError("The iterator does not provide a 'throw' method")}return c}var n=l(r,e.iterator,t.arg);if("throw"===n.type)return t.method="throw",t.arg=n.arg,t.delegate=null,c;var o=n.arg;return o?o.done?(t[e.resultName]=o.value,t.next=e.nextLoc,"return"!==t.method&&(t.method="next",t.arg=void 0),t.delegate=null,c):o:(t.method="throw",t.arg=new TypeError("iterator result is not an object"),t.delegate=null,c)}function w(e){var t={tryLoc:e[0]};1 in e&&(t.catchLoc=e[1]),2 in e&&(t.finallyLoc=e[2],t.afterLoc=e[3]),this.tryEntries.push(t)}function I(e){var t=e.completion||{};t.type="normal",delete t.arg,e.completion=t}function T(e){this.tryEntries=[{tryLoc:"root"}],e.forEach(w,this),this.reset(!0)}function L(e){if(e){var t=e[o];if(t)return t.call(e);if("function"==typeof e.next)return e;if(!isNaN(e.length)){var n=-1,i=function t(){for(;++n<e.length;)if(r.call(e,n))return t.value=e[n],t.done=!1,t;return t.value=void 0,t.done=!0,t};return i.next=i}}return{next:k}}function k(){return{value:void 0,done:!0}}return p.prototype=h,u(y,"constructor",h),u(h,"constructor",p),p.displayName=u(h,a,"GeneratorFunction"),e.isGeneratorFunction=function(e){var t="function"==typeof e&&e.constructor;return!!t&&(t===p||"GeneratorFunction"===(t.displayName||t.name))},e.mark=function(e){return Object.setPrototypeOf?Object.setPrototypeOf(e,h):(e.__proto__=h,u(e,a,"GeneratorFunction")),e.prototype=Object.create(y),e},e.awrap=function(e){return{__await:e}},v(x.prototype),u(x.prototype,i,(function(){return this})),e.AsyncIterator=x,e.async=function(t,r,n,o,i){void 0===i&&(i=Promise);var a=new x(s(t,r,n,o),i);return e.isGeneratorFunction(r)?a:a.next().then((function(e){return e.done?e.value:a.next()}))},v(y),u(y,a,"Generator"),u(y,o,(function(){return this})),u(y,"toString",(function(){return"[object Generator]"})),e.keys=function(e){var t=[];for(var r in e)t.push(r);return t.reverse(),function r(){for(;t.length;){var n=t.pop();if(n in e)return r.value=n,r.done=!1,r}return r.done=!0,r}},e.values=L,T.prototype={constructor:T,reset:function(e){if(this.prev=0,this.next=0,this.sent=this._sent=void 0,this.done=!1,this.delegate=null,this.method="next",this.arg=void 0,this.tryEntries.forEach(I),!e)for(var t in this)"t"===t.charAt(0)&&r.call(this,t)&&!isNaN(+t.slice(1))&&(this[t]=void 0)},stop:function(){this.done=!0;var e=this.tryEntries[0].completion;if("throw"===e.type)throw e.arg;return this.rval},dispatchException:function(e){if(this.done)throw e;var t=this;function n(r,n){return a.type="throw",a.arg=e,t.next=r,n&&(t.method="next",t.arg=void 0),!!n}for(var o=this.tryEntries.length-1;o>=0;--o){var i=this.tryEntries[o],a=i.completion;if("root"===i.tryLoc)return n("end");if(i.tryLoc<=this.prev){var u=r.call(i,"catchLoc"),s=r.call(i,"finallyLoc");if(u&&s){if(this.prev<i.catchLoc)return n(i.catchLoc,!0);if(this.prev<i.finallyLoc)return n(i.finallyLoc)}else if(u){if(this.prev<i.catchLoc)return n(i.catchLoc,!0)}else{if(!s)throw new Error("try statement without catch or finally");if(this.prev<i.finallyLoc)return n(i.finallyLoc)}}}},abrupt:function(e,t){for(var n=this.tryEntries.length-1;n>=0;--n){var o=this.tryEntries[n];if(o.tryLoc<=this.prev&&r.call(o,"finallyLoc")&&this.prev<o.finallyLoc){var i=o;break}}i&&("break"===e||"continue"===e)&&i.tryLoc<=t&&t<=i.finallyLoc&&(i=null);var a=i?i.completion:{};return a.type=e,a.arg=t,i?(this.method="next",this.next=i.finallyLoc,c):this.complete(a)},complete:function(e,t){if("throw"===e.type)throw e.arg;return"break"===e.type||"continue"===e.type?this.next=e.arg:"return"===e.type?(this.rval=this.arg=e.arg,this.method="return",this.next="end"):"normal"===e.type&&t&&(this.next=t),c},finish:function(e){for(var t=this.tryEntries.length-1;t>=0;--t){var r=this.tryEntries[t];if(r.finallyLoc===e)return this.complete(r.completion,r.afterLoc),I(r),c}},catch:function(e){for(var t=this.tryEntries.length-1;t>=0;--t){var r=this.tryEntries[t];if(r.tryLoc===e){var n=r.completion;if("throw"===n.type){var o=n.arg;I(r)}return o}}throw new Error("illegal catch attempt")},delegateYield:function(e,t,r){return this.delegate={iterator:L(e),resultName:t,nextLoc:r},"next"===this.method&&(this.arg=void 0),c}},e}(e.exports);try{regeneratorRuntime=n}catch(e){"object"==typeof globalThis?globalThis.regeneratorRuntime=n:Function("r","regeneratorRuntime = r")(n)}},function(e,t,r){"use strict";r.r(t),r.d(t,"ImageDrop",(function(){return p}));var n=r(1),o=r.n(n),i=r(2),a=r.n(i),u=r(3),s=r.n(u),l=r(0),c=r.n(l),f=r(4).file2b64,p=function(){function e(t,r,n){var o=this;a()(this,e),this.logger=n,this.quill=t,this.onNewDataUrl=r,this.quill.root.addEventListener("drop",(function(e){return o.handleDrop(e)}),!1),this.quill.root.addEventListener("paste",(function(e){return o.handlePaste(e)}),!1)}var t;return s()(e,[{key:"handleNewImageFiles",value:(t=o()(c.a.mark((function e(t){var r,n,o;return c.a.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(Array.isArray(t)){e.next=2;break}return e.abrupt("return");case 2:if(r=t.pop()){e.next=5;break}return e.abrupt("return");case 5:return n=r.getAsFile?r.getAsFile():r,e.next=8,f(n);case 8:o=e.sent,this.logger.log("handleNewImageFiles",{base64ImageSrc:o}),this.onNewDataUrl(o);case 11:case"end":return e.stop()}}),e,this)}))),function(e){return t.apply(this,arguments)})},{key:"handleDrop",value:function(e){e.preventDefault();var t=e.dataTransfer&&e.dataTransfer.files&&e.dataTransfer.files.length;if(this.logger.log("handleDrop",{hasFiles:t}),t){if(document.caretRangeFromPoint){var r=document.getSelection(),n=document.caretRangeFromPoint(e.clientX,e.clientY);r&&n&&r.setBaseAndExtent(n.startContainer,n.startOffset,n.startContainer,n.startOffset)}var o=this.getImageFiles(e.dataTransfer.files);this.handleNewImageFiles(o)}}},{key:"handlePaste",value:function(e){var t=e.clipboardData&&e.clipboardData.items&&!!e.clipboardData.items.length;if(this.logger.log("handlePaste",{hasItems:t}),t){var r=this.getImageFiles(e.clipboardData.items),n=r.length>0;if(this.logger.log("handlePaste",{hasImages:n,imageCount:r.length}),n)Array.from(e.clipboardData.items).some((function(e){return"text/html"===e.type}))?this.logger.log("detected html, not handling"):(e.preventDefault(),this.handleNewImageFiles(r))}}},{key:"getImageFiles",value:function(e){var t=Array.from(e);return this.logger.log("getImageFiles",{types:t.map((function(e){return e.type}))}),t.filter((function(e){return!!e.type.match(/^image\/(gif|jpe?g|a?png|svg|webp|bmp|vnd\.microsoft\.icon)/i)}))||[]}}]),e}()},function(e,t,r){"use strict";function n(e){e.maxWidth=e.maxWidth||1e3,e.maxHeight=e.maxHeight||1e3,e.maxWidth&&"number"!=typeof e.maxWidth&&(Logger.warn("[config error] 'maxWidth' is required to be a \"number\" (in pixels), \nreceived: ".concat(e.maxWidth,"\n-> using default 1000")),e.maxWidth=1e3),e.maxHeight&&"number"!=typeof e.maxHeight&&(Logger.warn("[config error] 'maxHeight' is required to be a \"number\" (in pixels), \nreceived: ".concat(e.maxHeight,"\n-> using default 1000")),e.maxHeight=1e3),e.quality&&"number"!=typeof e.quality&&(Logger.warn("quill.imageCompressor: [config error] 'quality' is required to be a \"number\", \nreceived: ".concat(e.quality,"\n-> using default 0.7")),e.quality=.7),!e.imageType||"string"==typeof e.imageType&&e.imageType.startsWith("image/")||(Logger.warn('quill.imageCompressor: [config error] \'imageType\' is required be in the form of "image/png" or "image/jpeg" etc ..., \nreceived: '.concat(e.imageType,"\n-> using default image/jpeg")),e.imageType="image/jpeg"),e.keepImageTypes||(e.keepImageTypes=[]),e.keepImageTypes&&!Array.isArray(e.keepImageTypes)&&(Logger.warn("quill.imageCompressor: [config error] 'keepImageTypes' is required to be a \"array\", received: ".concat(e.keepImageTypes," -> using default []")),e.keepImageTypes=[]),e.ignoreImageTypes||(e.ignoreImageTypes=[]),e.ignoreImageTypes&&!Array.isArray(e.ignoreImageTypes)&&(Logger.warn("quill.imageCompressor: [config error] 'ignoreImageTypes' is required to be a \"array\", received: ".concat(e.ignoreImageTypes," -> using default []")),e.ignoreImageTypes=[])}r.r(t),r.d(t,"warnAboutOptions",(function(){return n}))},function(e,t,r){"use strict";r.r(t),r.d(t,"downscaleImage",(function(){return l}));var n=r(5),o=r.n(n),i=r(1),a=r.n(i),u=r(0),s=r.n(u);function l(e,t,r,n,o,i,a,u){return c.apply(this,arguments)}function c(){return(c=a()(s.a.mark((function e(t,r,n,i,a,u,l,c){var p,h,g,d,m,y,v,x,b;return s.a.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return p=t.split(";")[0].split(":")[1],i=i||"image/jpeg",l=l||.7,(h=new Image).src=t,e.next=7,new Promise((function(e){h.onload=function(){e()}}));case 7:if(g=f(h.width,h.height,r,n),d=o()(g,2),m=d[0],y=d[1],(v=document.createElement("canvas")).width=m,v.height=y,x=v.getContext("2d"),"image/jpeg"===i&&(x.fillStyle="#FFFFFF",x.fillRect(0,0,h.width,h.height)),!u.includes(p)){e.next=15;break}return e.abrupt("return",t);case 15:return a.includes(p)&&(i=p),x.drawImage(h,0,0,m,y),b=v.toDataURL(i,l),c.log("downscaling image...",{args:{dataUrl:t,maxWidth:r,maxHeight:n,imageType:i,ignoreImageTypes:u,keepImageTypes:a,imageQuality:l},newHeight:y,newWidth:m}),e.abrupt("return",b);case 20:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function f(e,t,r,n){if(e<=r&&t<=n)return[e,t];if(e>r){var o=r,i=Math.floor(t/e*o);if(i>n){var a=n;return[Math.floor(e/t*a),a]}return[o,i]}if(t>n){var u=n;return[Math.floor(e/t*u),u]}}},function(e,t){e.exports=function(e){if(Array.isArray(e))return e},e.exports.__esModule=!0,e.exports.default=e.exports},function(e,t){e.exports=function(e,t){var r=null==e?null:"undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(null!=r){var n,o,i=[],a=!0,u=!1;try{for(r=r.call(e);!(a=(n=r.next()).done)&&(i.push(n.value),!t||i.length!==t);a=!0);}catch(e){u=!0,o=e}finally{try{a||null==r.return||r.return()}finally{if(u)throw o}}return i}},e.exports.__esModule=!0,e.exports.default=e.exports},function(e,t,r){var n=r(14);e.exports=function(e,t){if(e){if("string"==typeof e)return n(e,t);var r=Object.prototype.toString.call(e).slice(8,-1);return"Object"===r&&e.constructor&&(r=e.constructor.name),"Map"===r||"Set"===r?Array.from(e):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?n(e,t):void 0}},e.exports.__esModule=!0,e.exports.default=e.exports},function(e,t){e.exports=function(e,t){(null==t||t>e.length)&&(t=e.length);for(var r=0,n=new Array(t);r<t;r++)n[r]=e[r];return n},e.exports.__esModule=!0,e.exports.default=e.exports},function(e,t){e.exports=function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")},e.exports.__esModule=!0,e.exports.default=e.exports}])}));
|
package/package.json
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
{
|
2
2
|
"name": "quill-image-compress",
|
3
3
|
"description": "A Quill rich text editor Module which compresses images uploaded to the editor",
|
4
|
-
"version": "1.2.
|
4
|
+
"version": "1.2.20",
|
5
5
|
"main": "dist/quill.imageCompressor.min.js",
|
6
6
|
"homepage": "https://benwinding.github.io/quill-image-compress/src/demo.html",
|
7
7
|
"repository": "https://github.com/benwinding/quill-image-compress",
|
package/src/downscaleImage.js
CHANGED
@@ -4,10 +4,15 @@ export async function downscaleImage(
|
|
4
4
|
maxWidth,
|
5
5
|
maxHeight,
|
6
6
|
imageType,
|
7
|
+
keepImageTypes,
|
8
|
+
ignoreImageTypes,
|
7
9
|
imageQuality,
|
8
10
|
logger,
|
9
11
|
) {
|
10
12
|
"use strict";
|
13
|
+
// Input image values
|
14
|
+
const inputImageType = dataUrl.split(';')[0].split(':')[1];
|
15
|
+
|
11
16
|
// Provide default values
|
12
17
|
imageType = imageType || "image/jpeg";
|
13
18
|
imageQuality = imageQuality || 0.7;
|
@@ -32,8 +37,25 @@ export async function downscaleImage(
|
|
32
37
|
canvas.width = newWidth;
|
33
38
|
canvas.height = newHeight;
|
34
39
|
|
35
|
-
// Draw the downscaled image on the canvas and return the new data URL.
|
36
40
|
const ctx = canvas.getContext("2d");
|
41
|
+
|
42
|
+
// If the type is an jpeg, draw a white background first.
|
43
|
+
if (imageType === "image/jpeg") {
|
44
|
+
ctx.fillStyle = "#FFFFFF";
|
45
|
+
ctx.fillRect(0, 0, image.width, image.height);
|
46
|
+
}
|
47
|
+
|
48
|
+
// If the type is included in the ignore list, return the original
|
49
|
+
if (ignoreImageTypes.includes(inputImageType)) {
|
50
|
+
return dataUrl;
|
51
|
+
}
|
52
|
+
|
53
|
+
// If the type is included in keep type list, fix the image type
|
54
|
+
if (keepImageTypes.includes(inputImageType)) {
|
55
|
+
imageType = inputImageType;
|
56
|
+
}
|
57
|
+
|
58
|
+
// Draw the downscaled image on the canvas and return the new data URL.
|
37
59
|
ctx.drawImage(image, 0, 0, newWidth, newHeight);
|
38
60
|
const newDataUrl = canvas.toDataURL(imageType, imageQuality);
|
39
61
|
logger.log("downscaling image...", {
|
@@ -42,6 +64,8 @@ export async function downscaleImage(
|
|
42
64
|
maxWidth,
|
43
65
|
maxHeight,
|
44
66
|
imageType,
|
67
|
+
ignoreImageTypes,
|
68
|
+
keepImageTypes,
|
45
69
|
imageQuality,
|
46
70
|
},
|
47
71
|
newHeight,
|
@@ -0,0 +1,66 @@
|
|
1
|
+
export function warnAboutOptions(options) {
|
2
|
+
// Safe-ify Options
|
3
|
+
options.maxWidth = options.maxWidth || 1000;
|
4
|
+
options.maxHeight = options.maxHeight || 1000;
|
5
|
+
|
6
|
+
if (options.maxWidth && typeof options.maxWidth !== "number") {
|
7
|
+
Logger.warn(
|
8
|
+
`[config error] 'maxWidth' is required to be a "number" (in pixels),
|
9
|
+
received: ${options.maxWidth}
|
10
|
+
-> using default 1000`
|
11
|
+
);
|
12
|
+
options.maxWidth = 1000;
|
13
|
+
}
|
14
|
+
if (options.maxHeight && typeof options.maxHeight !== "number") {
|
15
|
+
Logger.warn(
|
16
|
+
`[config error] 'maxHeight' is required to be a "number" (in pixels),
|
17
|
+
received: ${options.maxHeight}
|
18
|
+
-> using default 1000`
|
19
|
+
);
|
20
|
+
options.maxHeight = 1000;
|
21
|
+
}
|
22
|
+
if (options.quality && typeof options.quality !== "number") {
|
23
|
+
Logger.warn(
|
24
|
+
`quill.imageCompressor: [config error] 'quality' is required to be a "number",
|
25
|
+
received: ${options.quality}
|
26
|
+
-> using default 0.7`
|
27
|
+
);
|
28
|
+
options.quality = 0.7;
|
29
|
+
}
|
30
|
+
if (
|
31
|
+
options.imageType &&
|
32
|
+
(typeof options.imageType !== "string" ||
|
33
|
+
!options.imageType.startsWith("image/"))
|
34
|
+
) {
|
35
|
+
Logger.warn(
|
36
|
+
`quill.imageCompressor: [config error] 'imageType' is required be in the form of "image/png" or "image/jpeg" etc ...,
|
37
|
+
received: ${options.imageType}
|
38
|
+
-> using default image/jpeg`
|
39
|
+
);
|
40
|
+
options.imageType = "image/jpeg";
|
41
|
+
}
|
42
|
+
if (!options.keepImageTypes) {
|
43
|
+
options.keepImageTypes = []
|
44
|
+
}
|
45
|
+
if (
|
46
|
+
options.keepImageTypes &&
|
47
|
+
(!Array.isArray(options.keepImageTypes))
|
48
|
+
) {
|
49
|
+
Logger.warn(
|
50
|
+
`quill.imageCompressor: [config error] 'keepImageTypes' is required to be a "array", received: ${options.keepImageTypes} -> using default []`
|
51
|
+
)
|
52
|
+
options.keepImageTypes = [];
|
53
|
+
}
|
54
|
+
if (!options.ignoreImageTypes) {
|
55
|
+
options.ignoreImageTypes = []
|
56
|
+
}
|
57
|
+
if (
|
58
|
+
options.ignoreImageTypes &&
|
59
|
+
(!Array.isArray(options.ignoreImageTypes))
|
60
|
+
) {
|
61
|
+
Logger.warn(
|
62
|
+
`quill.imageCompressor: [config error] 'ignoreImageTypes' is required to be a "array", received: ${options.ignoreImageTypes} -> using default []`
|
63
|
+
)
|
64
|
+
options.ignoreImageTypes = [];
|
65
|
+
}
|
66
|
+
}
|
@@ -1,46 +1,3 @@
|
|
1
|
-
function warnAboutOptions(options) {
|
2
|
-
// Safe-ify Options
|
3
|
-
options.maxWidth = options.maxWidth || 1000;
|
4
|
-
options.maxHeight = options.maxHeight || 1000;
|
5
|
-
|
6
|
-
if (options.maxWidth && typeof options.maxWidth !== "number") {
|
7
|
-
Logger.warn(
|
8
|
-
`[config error] 'maxWidth' is required to be a "number" (in pixels),
|
9
|
-
recieved: ${options.maxWidth}
|
10
|
-
-> using default 1000`
|
11
|
-
);
|
12
|
-
options.maxWidth = 1000;
|
13
|
-
}
|
14
|
-
if (options.maxHeight && typeof options.maxHeight !== "number") {
|
15
|
-
Logger.warn(
|
16
|
-
`[config error] 'maxHeight' is required to be a "number" (in pixels),
|
17
|
-
recieved: ${options.maxHeight}
|
18
|
-
-> using default 1000`
|
19
|
-
);
|
20
|
-
options.maxHeight = 1000;
|
21
|
-
}
|
22
|
-
if (options.quality && typeof options.quality !== "number") {
|
23
|
-
Logger.warn(
|
24
|
-
`quill.imageCompressor: [config error] 'quality' is required to be a "number",
|
25
|
-
recieved: ${options.quality}
|
26
|
-
-> using default 0.7`
|
27
|
-
);
|
28
|
-
options.quality = 0.7;
|
29
|
-
}
|
30
|
-
if (
|
31
|
-
options.imageType &&
|
32
|
-
(typeof options.imageType !== "string" ||
|
33
|
-
!options.imageType.startsWith("image/"))
|
34
|
-
) {
|
35
|
-
Logger.warn(
|
36
|
-
`quill.imageCompressor: [config error] 'imageType' is required be in the form of "image/png" or "image/jpeg" etc ...,
|
37
|
-
recieved: ${options.imageType}
|
38
|
-
-> using default image/jpeg`
|
39
|
-
);
|
40
|
-
options.imageType = "image/jpeg";
|
41
|
-
}
|
42
|
-
}
|
43
|
-
|
44
1
|
let debug = true;
|
45
2
|
let suppressErrorLogging = false;
|
46
3
|
const Logger = {
|
@@ -71,6 +28,7 @@ const Logger = {
|
|
71
28
|
};
|
72
29
|
|
73
30
|
const { ImageDrop } = require("./quill.imageDrop");
|
31
|
+
const { warnAboutOptions } = require("./options.validation");
|
74
32
|
const { file2b64 } = require("./file2b64");
|
75
33
|
const { downscaleImage } = require("./downscaleImage");
|
76
34
|
|
@@ -78,17 +36,17 @@ class imageCompressor {
|
|
78
36
|
constructor(quill, options) {
|
79
37
|
this.quill = quill;
|
80
38
|
this.range = null;
|
81
|
-
this.options = options;
|
82
|
-
debug = options
|
83
|
-
suppressErrorLogging = options
|
39
|
+
this.options = options || {};
|
40
|
+
debug = options.debug;
|
41
|
+
suppressErrorLogging = options.suppressErrorLogging;
|
84
42
|
|
43
|
+
warnAboutOptions(options);
|
85
44
|
const onImageDrop = async (dataUrl) => {
|
86
45
|
Logger.log("onImageDrop", { dataUrl });
|
87
46
|
const dataUrlCompressed = await this.downscaleImageFromUrl(dataUrl);
|
88
47
|
this.insertToEditor(dataUrlCompressed);
|
89
48
|
};
|
90
49
|
this.imageDrop = new ImageDrop(quill, onImageDrop, Logger);
|
91
|
-
warnAboutOptions(options);
|
92
50
|
|
93
51
|
Logger.log("fileChanged", { options, quill, debug });
|
94
52
|
|
@@ -132,14 +90,15 @@ class imageCompressor {
|
|
132
90
|
}
|
133
91
|
|
134
92
|
async downscaleImageFromUrl(dataUrl) {
|
135
|
-
const logger = Logger;
|
136
93
|
const dataUrlCompressed = await downscaleImage(
|
137
94
|
dataUrl,
|
138
95
|
this.options.maxWidth,
|
139
96
|
this.options.maxHeight,
|
140
97
|
this.options.imageType,
|
98
|
+
this.options.keepImageTypes,
|
99
|
+
this.options.ignoreImageTypes,
|
141
100
|
this.options.quality,
|
142
|
-
|
101
|
+
Logger,
|
143
102
|
);
|
144
103
|
Logger.log("downscaleImageFromUrl", { dataUrl, dataUrlCompressed });
|
145
104
|
return dataUrlCompressed;
|
package/src/quill.imageDrop.js
CHANGED
@@ -28,7 +28,7 @@ export class ImageDrop {
|
|
28
28
|
}
|
29
29
|
const blob = firstImage.getAsFile ? firstImage.getAsFile() : firstImage;
|
30
30
|
const base64ImageSrc = await file2b64(blob);
|
31
|
-
this.logger.log("
|
31
|
+
this.logger.log("handleNewImageFiles", { base64ImageSrc });
|
32
32
|
this.onNewDataUrl(base64ImageSrc);
|
33
33
|
}
|
34
34
|
|
@@ -68,15 +68,15 @@ export class ImageDrop {
|
|
68
68
|
return;
|
69
69
|
}
|
70
70
|
const images = this.getImageFiles(evt.clipboardData.items);
|
71
|
-
|
72
|
-
|
71
|
+
const hasImages = images.length > 0;
|
72
|
+
this.logger.log("handlePaste", { hasImages, imageCount: images.length });
|
73
|
+
if (!hasImages) {
|
73
74
|
return;
|
74
75
|
}
|
75
76
|
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
if (Array.from(evt.clipboardData.items).some(f => f.type === 'text/html')) {
|
77
|
+
// Text pasted from word will contain both text/html and image/png.
|
78
|
+
const hasHtmlMixed = Array.from(evt.clipboardData.items).some(f => f.type === 'text/html');
|
79
|
+
if (hasHtmlMixed) {
|
80
80
|
this.logger.log("detected html, not handling");
|
81
81
|
return;
|
82
82
|
}
|
@@ -87,7 +87,7 @@ export class ImageDrop {
|
|
87
87
|
|
88
88
|
getImageFiles(filesList) {
|
89
89
|
const files = Array.from(filesList);
|
90
|
-
this.logger.log("
|
90
|
+
this.logger.log("getImageFiles", { types: files.map(f => f.type) });
|
91
91
|
// check each file for an image
|
92
92
|
function isFileImage(file) {
|
93
93
|
const isImage = !!file.type.match(
|