@sitelintcode/optimize-image-on-the-client-side 0.0.8 → 0.0.10

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.
package/README.md CHANGED
@@ -33,7 +33,7 @@ jsdelivr.com comment:
33
33
  ## TypeScript
34
34
 
35
35
  ```TypeScript
36
- import OptimizeImage from '@sitelint/optimize-image-on-the-client-side';
36
+ import { OptimizeImage } from '@sitelintcode/optimize-image-on-the-client-side';
37
37
 
38
38
  const optimizeImage: OptimizeImage = new OptimizeImage();
39
39
 
@@ -8,7 +8,7 @@ declare class OptimizeImage {
8
8
  private handleChangeEvent;
9
9
  addImageOptimization(cssQuerySelector: string): void;
10
10
  uninstall(): void;
11
- install(cssQuerySelector: string | undefined, onCompressionDoneCallback?: Function): void;
11
+ install(cssQuerySelector?: string | undefined, onCompressionDoneCallback?: Function): void;
12
12
  }
13
13
 
14
14
  export { OptimizeImage };
@@ -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 a=await new Promise((e=>s.toBlob(e,n,t)));return null===a?Promise.resolve(null):new File([a],e.name,{type:a.type})}async processImages(e){const t=e.target;if(null===t)return;const n=t.files;if(null===n||0===n.length)return;const i=["image/jpeg","image/png","image/webp"],s=t=>t===e.target;let o;if(Array.isArray(this.customInputElements)&&(o=this.customInputElements.find(s),void 0===o))return;const a=new DataTransfer,l={quality:.5,type:"image/jpeg"};for(const e of n){if(!1===i.includes(e.type))continue;if(!1===e.type.startsWith("image")){a.items.add(e);continue}const t=Object.assign(l,{type:e.type}),n=await this.compressImage(e,t);n instanceof File&&(n.size<e.size?a.items.add(n):a.items.add(e))}e.target.files=a.files,"function"==typeof this.onCompressionDoneCallback&&this.onCompressionDoneCallback(n,a.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
+ !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 a=s.getContext("2d");if(null===a)return Promise.resolve(null);a.drawImage(i,0,0);const o=await new Promise((e=>s.toBlob(e,n,t)));return null===o?Promise.resolve(null):new File([o],e.name,{type:o.type})}async processImages(e){const t=e.target;if(null===t)return;const n=t.files;if(null===n||0===n.length)return;const i=["image/jpeg","image/png","image/webp"],s=t=>t===e.target;let a;if(Array.isArray(this.customInputElements)&&(a=this.customInputElements.find(s),void 0===a))return;const o=new DataTransfer,l={quality:.5,type:"image/jpeg"};for(const e of n){if(!1===i.includes(e.type))continue;if(!1===e.type.startsWith("image")){o.items.add(e);continue}const t=Object.assign(l,{type:e.type}),n=await this.compressImage(e,t);n instanceof File&&(n.size<e.size?o.items.add(n):o.items.add(e))}e.target.files=o.files,"function"==typeof this.onCompressionDoneCallback&&this.onCompressionDoneCallback(n,o.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};
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.8",
5
+ "version": "0.0.10",
6
6
  "license": "MOZILLA PUBLIC LICENSE, VERSION 2.0",
7
7
  "node": ">=16",
8
8
  "main": "dist/optimize-image-on-the-client-side.js",