@sitelintcode/optimize-image-on-the-client-side 0.0.7 → 0.0.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1 +1 @@
|
|
|
1
|
-
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).sitelint={})}(this,(function(e){"use strict";e.OptimizeImage=class{constructor(){this.inputTypeFileHandlerReference=null,this.customInputElements=null,this.onCompressionDoneCallback=void 0}async compressImage(e,{quality:t=1,type:n=e.type}){const i=await createImageBitmap(e),s=document.createElement("canvas");s.width=i.width,s.height=i.height;const o=s.getContext("2d");if(null===o)return Promise.resolve(null);o.drawImage(i,0,0);const
|
|
1
|
+
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).sitelint={})}(this,(function(e){"use strict";class t{static isHtmlElement(e){if(null===e)return!1;try{return e instanceof Element||e instanceof Document}catch(t){return"object"==typeof e&&e.nodeType===Node.ELEMENT_NODE&&"object"==typeof e.style&&"object"==typeof e.ownerDocument}}static getComputedStyle(e,n){return t.isHtmlElement(e)?document&&document.defaultView&&document.defaultView.getComputedStyle(e,n||null):null}}e.OptimizeImage=class{constructor(){this.inputTypeFileHandlerReference=null,this.customInputElements=null,this.onCompressionDoneCallback=void 0}async compressImage(e,{quality:t=1,type:n=e.type}){const i=await createImageBitmap(e),s=document.createElement("canvas");s.width=i.width,s.height=i.height;const o=s.getContext("2d");if(null===o)return Promise.resolve(null);o.drawImage(i,0,0);const l=await new Promise((e=>s.toBlob(e,n,t)));return null===l?Promise.resolve(null):new File([l],e.name,{type:l.type})}async processImages(e){var n;const i=e.target;if(null===i)return;const s=i.files;if(null===s||0===s.length)return;const o=null===(n=t.getComputedStyle(document.body))||void 0===n?void 0:n.getPropertyValue("cursor");document.body.style.cursor="progress";const l=["image/jpeg","image/png","image/webp"],a=t=>t===e.target;let r;if(Array.isArray(this.customInputElements)&&(r=this.customInputElements.find(a),void 0===r))return;const c=new DataTransfer,u={quality:.5,type:"image/jpeg"};for(const e of s){if(!1===l.includes(e.type))continue;if(!1===e.type.startsWith("image")){c.items.add(e);continue}const t=Object.assign(u,{type:e.type}),n=await this.compressImage(e,t);n instanceof File&&(n.size<e.size?c.items.add(n):c.items.add(e))}e.target.files=c.files,"string"==typeof o&&(document.body.style.cursor=o),"function"==typeof this.onCompressionDoneCallback&&this.onCompressionDoneCallback(s,c.files)}handleChangeEvent(e){const t=e.target;"input"!==t.nodeName.toLowerCase()&&"file"===t.type&&"change"!==e.type||this.processImages(e)}addImageOptimization(e){"string"==typeof e&&(this.customInputElements=Array.from(document.querySelectorAll(e)))}uninstall(){null!==this.inputTypeFileHandlerReference&&document.removeEventListener("change",this.inputTypeFileHandlerReference)}install(e,t){this.inputTypeFileHandlerReference=this.handleChangeEvent.bind(this),this.onCompressionDoneCallback=t,document.addEventListener("change",this.inputTypeFileHandlerReference),"string"==typeof e&&this.addImageOptimization(e)}}}));
|
|
@@ -1 +1 @@
|
|
|
1
|
-
class OptimizeImage{constructor(){this.inputTypeFileHandlerReference=null,this.customInputElements=null,this.onCompressionDoneCallback=void 0}async compressImage(e,{quality:t=1,type:n=e.type}){const i=await createImageBitmap(e),s=document.createElement("canvas");s.width=i.width,s.height=i.height;const
|
|
1
|
+
class CommonUtilities{static isHtmlElement(e){if(null===e)return!1;try{return e instanceof Element||e instanceof Document}catch(t){return"object"==typeof e&&e.nodeType===Node.ELEMENT_NODE&&"object"==typeof e.style&&"object"==typeof e.ownerDocument}}static getComputedStyle(e,t){return CommonUtilities.isHtmlElement(e)?document&&document.defaultView&&document.defaultView.getComputedStyle(e,t||null):null}}class OptimizeImage{constructor(){this.inputTypeFileHandlerReference=null,this.customInputElements=null,this.onCompressionDoneCallback=void 0}async compressImage(e,{quality:t=1,type:n=e.type}){const i=await createImageBitmap(e),s=document.createElement("canvas");s.width=i.width,s.height=i.height;const o=s.getContext("2d");if(null===o)return Promise.resolve(null);o.drawImage(i,0,0);const l=await new Promise((e=>s.toBlob(e,n,t)));return null===l?Promise.resolve(null):new File([l],e.name,{type:l.type})}async processImages(e){var t;const n=e.target;if(null===n)return;const i=n.files;if(null===i||0===i.length)return;const s=null===(t=CommonUtilities.getComputedStyle(document.body))||void 0===t?void 0:t.getPropertyValue("cursor");document.body.style.cursor="progress";const o=["image/jpeg","image/png","image/webp"],l=t=>t===e.target;let a;if(Array.isArray(this.customInputElements)&&(a=this.customInputElements.find(l),void 0===a))return;const r=new DataTransfer,c={quality:.5,type:"image/jpeg"};for(const e of i){if(!1===o.includes(e.type))continue;if(!1===e.type.startsWith("image")){r.items.add(e);continue}const t=Object.assign(c,{type:e.type}),n=await this.compressImage(e,t);n instanceof File&&(n.size<e.size?r.items.add(n):r.items.add(e))}e.target.files=r.files,"string"==typeof s&&(document.body.style.cursor=s),"function"==typeof this.onCompressionDoneCallback&&this.onCompressionDoneCallback(i,r.files)}handleChangeEvent(e){const t=e.target;"input"!==t.nodeName.toLowerCase()&&"file"===t.type&&"change"!==e.type||this.processImages(e)}addImageOptimization(e){"string"==typeof e&&(this.customInputElements=Array.from(document.querySelectorAll(e)))}uninstall(){null!==this.inputTypeFileHandlerReference&&document.removeEventListener("change",this.inputTypeFileHandlerReference)}install(e,t){this.inputTypeFileHandlerReference=this.handleChangeEvent.bind(this),this.onCompressionDoneCallback=t,document.addEventListener("change",this.inputTypeFileHandlerReference),"string"==typeof e&&this.addImageOptimization(e)}}export{OptimizeImage};
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@sitelintcode/optimize-image-on-the-client-side",
|
|
3
3
|
"description": "Optimize image on the client side",
|
|
4
4
|
"author": "Cezary Tomczyk <cezary.tomczyk@gmail.com> (https://www.sitelint.com/)",
|
|
5
|
-
"version": "0.0.
|
|
5
|
+
"version": "0.0.9",
|
|
6
6
|
"license": "MOZILLA PUBLIC LICENSE, VERSION 2.0",
|
|
7
7
|
"node": ">=16",
|
|
8
8
|
"main": "dist/optimize-image-on-the-client-side.js",
|