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

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
@@ -17,11 +17,12 @@ npm install @sitelintcode/optimize-image-on-the-client-side --save
17
17
  ### Browser environment
18
18
 
19
19
  ```Html
20
- <script src="https://cdn.jsdelivr.net/npm/@sitelintcode/optimize-image-on-the-client-side@0.0.3/dist/optimize-image-on-the-client-side.js"></script>
20
+ <script src="https://cdn.jsdelivr.net/npm/@sitelintcode/optimize-image-on-the-client-side@0.0.10/dist/optimize-image-on-the-client-side.js"></script>
21
21
  ```
22
22
 
23
23
  #### Notes
24
- See **0.0.3** version. Don't forget to set desired version. You may check releases: https://github.com/sitelint/optimize-image-on-the-client-side/releases
24
+
25
+ See **0.0.10** version. Don't forget to set desired version. You may check releases: https://github.com/sitelint/optimize-image-on-the-client-side/releases
25
26
 
26
27
  jsdelivr.com comment:
27
28
 
@@ -42,10 +43,14 @@ jsdelivr.com comment:
42
43
 
43
44
  ## Browser environment
44
45
 
45
- ```JavaScript
46
- const optimizeImage = new globalThis.sitelint.OptimizeImage();
47
-
46
+ ```HTML
47
+ <script src="https://cdn.jsdelivr.net/npm/@sitelintcode/optimize-image-on-the-client-side@0.0.10/dist/optimize-image-on-the-client-side.js"></script>
48
+ <script>
49
+ (function() {
50
+ const optimizeImage = new window.sitelint.OptimizeImage();
48
51
  optimizeImage.install();
52
+ }())
53
+ </script>
49
54
  ```
50
55
 
51
56
  ## Technical
@@ -2,7 +2,11 @@ declare class OptimizeImage {
2
2
  private inputTypeFileHandlerReference;
3
3
  private customInputElements;
4
4
  private onCompressionDoneCallback;
5
+ private originalCursor;
5
6
  constructor();
7
+ private createCSS;
8
+ private enableBusyIndicator;
9
+ private disableBusyIndicator;
6
10
  private compressImage;
7
11
  private processImages;
8
12
  private handleChangeEvent;
@@ -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";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
+ !function(A,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((A="undefined"!=typeof globalThis?globalThis:A||self).sitelint={})}(this,(function(A){"use strict";class e{static isHtmlElement(A){if(null===A)return!1;try{return A instanceof Element||A instanceof Document}catch(e){return"object"==typeof A&&A.nodeType===Node.ELEMENT_NODE&&"object"==typeof A.style&&"object"==typeof A.ownerDocument}}static getComputedStyle(A,t){return e.isHtmlElement(A)?document&&document.defaultView&&document.defaultView.getComputedStyle(A,t||null):null}static createCSS(A,e,t){if(null===A)throw new Error("[CommonUtilities.createCSS] passed content is not a string. Is type "+typeof A);const i=document.head,n=document.createElement("style");"string"==typeof e&&(n.id=e),"string"==typeof t&&t.length>0&&n.setAttribute("media",t),"object"==typeof n.styleSheet?n.styleSheet.cssText=A:n.appendChild(document.createTextNode(A)),i.appendChild(n)}}A.OptimizeImage=class{constructor(){this.inputTypeFileHandlerReference=null,this.customInputElements=null,this.onCompressionDoneCallback=void 0,this.originalCursor=void 0}createCSS(){e.createCSS('.sl-busy-indicator {\n background-image: url("data:image/gif;base64,R0lGODlhHwAfAPUAAP///wAAAOjo6NLS0ry8vK6urqKiotzc3Li4uJqamuTk5NjY2KqqqqCgoLCwsMzMzPb29qioqNTU1Obm5jY2NiYmJlBQUMTExHBwcJKSklZWVvr6+mhoaEZGRsbGxvj4+EhISDIyMgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH/C05FVFNDQVBFMi4wAwEAAAAh/hpDcmVhdGVkIHdp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6TGCmNJY4zGQhBjStPTFBXb21DY1VsGFtzbF9gAzlsFGOQVGefIW2LtGhvYwVgDD+0V17+6Y6BwaNfBwy9YY2YBcMAPnStTY1B9YMdNiyZOngCFGuIBxDZAiRY1eoTvE6UoDEIAGrNSUoNBUuzAaYlljxo2M+HIeXiJpRsRNMaq+JSFCpsRJEqYOPH2JQgAIfkECQoAAAAsAAAAAB8AHwAABv9AgHBIFAwIBQPAUCAMBMSodHhAJK5XAPaKOEynCsIWqx0nCIrvcMEwZ90JxkINaMATZXfjywjlzX9jdXNEHiAVFX8ODwoQABAKDw5bZQxpQh8YiIhaERJqEhF4WwRDDpubAJdqaWZeAByoFR0edEMTolsIAA+yFUq2QxJmAgmyGhvBRJNbA5qoGcpED2MEFrIX0kMKYwUUslDaj2PA4soGY47iEOQFY6vS3FtNYw/m1KQDYw7mzFhPZj5JGzYGipUtESYowzVmF4ADgOCBCZTgFQAxZBJ4AiXqT6ltbUZhWdToUSR/Ii1FWbDnDkUyDQhJsQPn5ZU9atjUhCPHVhgTNy/RSKsiqKFFbUaQKGHiJNyXIAAh+QQJCgAAACwAAAAAHwAfAAAG/0CAcEh8JDAWCsBQIAwExKhU+HFwKlgsIMHlIg7TqQeTLW+7XYIiPGSAymY0mrFgA0LwuLzbCC/6eVlnewkADXVECgxcAGUaGRdQEAoPDmhnDGtDBJcVHQYbYRIRhWgEQwd7AB52AGt7YAAIchETrUITpGgIAAJ7ErdDEnsCA3IOwUSWaAOcaA/JQ0amBXKa0QpyBQZyENFCEHIG39HcaN7f4WhM1uTZaE1y0N/TacZoyN/LXU+/0cNyoMxCUytYLjm8AKSS46rVKzmxADhjlCACMFGkBiU4NUQRxS4OHijwNqnSJS6ZovzRyJAQo0NhGrgs5bIPmwWLCLHsQsfhxBWTe9QkOzCwC8sv5Ho127akyRM7QQAAOwAAAAAAAAAAAA==");\n background-repeat: no-repeat;\n background-position: 100% 50%;\n background-size: 1.5rem 1.5rem;\n }')}enableBusyIndicator(A){var t;this.originalCursor=null===(t=e.getComputedStyle(document.body))||void 0===t?void 0:t.getPropertyValue("cursor"),document.body.style.cursor="progress",A.classList.add("sl-busy-indicator")}disableBusyIndicator(A){"string"==typeof this.originalCursor&&(document.body.style.cursor=this.originalCursor),A.classList.remove("sl-busy-indicator")}async compressImage(A,{quality:e=1,type:t=A.type}){const i=await createImageBitmap(A),n=document.createElement("canvas");n.width=i.width,n.height=i.height;const s=n.getContext("2d");if(null===s)return Promise.resolve(null);s.drawImage(i,0,0);const o=await new Promise((A=>n.toBlob(A,t,e)));return null===o?Promise.resolve(null):new File([o],A.name,{type:o.type})}async processImages(A){const e=A.target;if(null===e)return;const t=e.files;if(null===t||0===t.length)return;this.enableBusyIndicator(e);const i=["image/jpeg","image/png","image/webp"],n=e=>e===A.target;let s;if(Array.isArray(this.customInputElements)&&(s=this.customInputElements.find(n),void 0===s))return;const o=new DataTransfer,a={quality:.5,type:"image/jpeg"};for(const A of t){if(!1===i.includes(A.type))continue;if(!1===A.type.startsWith("image")){o.items.add(A);continue}const e=Object.assign(a,{type:A.type}),t=await this.compressImage(A,e);t instanceof File&&(t.size<A.size?o.items.add(t):o.items.add(A))}A.target.files=o.files,this.disableBusyIndicator(e),"function"==typeof this.onCompressionDoneCallback&&this.onCompressionDoneCallback(t,o.files)}handleChangeEvent(A){const e=A.target;"input"!==e.nodeName.toLowerCase()&&"file"===e.type&&"change"!==A.type||this.processImages(A)}addImageOptimization(A){"string"==typeof A&&(this.customInputElements=Array.from(document.querySelectorAll(A)))}uninstall(){null!==this.inputTypeFileHandlerReference&&document.removeEventListener("change",this.inputTypeFileHandlerReference)}install(A,e){this.inputTypeFileHandlerReference=this.handleChangeEvent.bind(this),this.onCompressionDoneCallback=e;document.addEventListener("change",this.inputTypeFileHandlerReference,{capture:!0}),this.createCSS(),"string"==typeof A&&this.addImageOptimization(A)}}}));
@@ -1 +1 @@
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};
1
+ class CommonUtilities{static isHtmlElement(A){if(null===A)return!1;try{return A instanceof Element||A instanceof Document}catch(e){return"object"==typeof A&&A.nodeType===Node.ELEMENT_NODE&&"object"==typeof A.style&&"object"==typeof A.ownerDocument}}static getComputedStyle(A,e){return CommonUtilities.isHtmlElement(A)?document&&document.defaultView&&document.defaultView.getComputedStyle(A,e||null):null}static createCSS(A,e,t){if(null===A)throw new Error("[CommonUtilities.createCSS] passed content is not a string. Is type "+typeof A);const i=document.head,n=document.createElement("style");"string"==typeof e&&(n.id=e),"string"==typeof t&&t.length>0&&n.setAttribute("media",t),"object"==typeof n.styleSheet?n.styleSheet.cssText=A:n.appendChild(document.createTextNode(A)),i.appendChild(n)}}class OptimizeImage{constructor(){this.inputTypeFileHandlerReference=null,this.customInputElements=null,this.onCompressionDoneCallback=void 0,this.originalCursor=void 0}createCSS(){CommonUtilities.createCSS('.sl-busy-indicator {\n background-image: url("data:image/gif;base64,R0lGODlhHwAfAPUAAP///wAAAOjo6NLS0ry8vK6urqKiotzc3Li4uJqamuTk5NjY2KqqqqCgoLCwsMzMzPb29qioqNTU1Obm5jY2NiYmJlBQUMTExHBwcJKSklZWVvr6+mhoaEZGRsbGxvj4+EhISDIyMgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH/C05FVFNDQVBFMi4wAwEAAAAh/hpDcmVhdGVkIHdpdGggYWpheGxvYWQuaW5mbwAh+QQJCgAAACwAAAAAHwAfAAAG/0CAcEgUDAgFA4BiwSQexKh0eEAkrldAZbvlOD5TqYKALWu5XIwnPFwwymY0GsRgAxrwuJwbCi8aAHlYZ3sVdwtRCm8JgVgODwoQAAIXGRpojQwKRGSDCRESYRsGHYZlBFR5AJt2a3kHQlZlERN2QxMRcAiTeaG2QxJ5RnAO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ABv9AgHBIFAwIBQPAUCAMBMSodHhAJK5XAPaKOEynCsIWqx0nCIrvcMEwZ90JxkINaMATZXfjywjlzX9jdXNEHiAVFX8ODwoQABAKDw5bZQxpQh8YiIhaERJqEhF4WwRDDpubAJdqaWZeAByoFR0edEMTolsIAA+yFUq2QxJmAgmyGhvBRJNbA5qoGcpED2MEFrIX0kMKYwUUslDaj2PA4soGY47iEOQFY6vS3FtNYw/m1KQDYw7mzFhPZj5JGzYGipUtESYowzVmF4ADgOCBCZTgFQAxZBJ4AiXqT6ltbUZhWdToUSR/Ii1FWbDnDkUyDQhJsQPn5ZU9atjUhCPHVhgTNy/RSKsiqKFFbUaQKGHiJNyXIAAh+QQJCgAAACwAAAAAHwAfAAAG/0CAcEh8JDAWCsBQIAwExKhU+HFwKlgsIMHlIg7TqQeTLW+7XYIiPGSAymY0mrFgA0LwuLzbCC/6eVlnewkADXVECgxcAGUaGRdQEAoPDmhnDGtDBJcVHQYbYRIRhWgEQwd7AB52AGt7YAAIchETrUITpGgIAAJ7ErdDEnsCA3IOwUSWaAOcaA/JQ0amBXKa0QpyBQZyENFCEHIG39HcaN7f4WhM1uTZaE1y0N/TacZoyN/LXU+/0cNyoMxCUytYLjm8AKSS46rVKzmxADhjlCACMFGkBiU4NUQRxS4OHijwNqnSJS6ZovzRyJAQo0NhGrgs5bIPmwWLCLHsQsfhxBWTe9QkOzCwC8sv5Ho127akyRM7QQAAOwAAAAAAAAAAAA==");\n background-repeat: no-repeat;\n background-position: 100% 50%;\n background-size: 1.5rem 1.5rem;\n }')}enableBusyIndicator(A){var e;this.originalCursor=null===(e=CommonUtilities.getComputedStyle(document.body))||void 0===e?void 0:e.getPropertyValue("cursor"),document.body.style.cursor="progress",A.classList.add("sl-busy-indicator")}disableBusyIndicator(A){"string"==typeof this.originalCursor&&(document.body.style.cursor=this.originalCursor),A.classList.remove("sl-busy-indicator")}async compressImage(A,{quality:e=1,type:t=A.type}){const i=await createImageBitmap(A),n=document.createElement("canvas");n.width=i.width,n.height=i.height;const o=n.getContext("2d");if(null===o)return Promise.resolve(null);o.drawImage(i,0,0);const s=await new Promise((A=>n.toBlob(A,t,e)));return null===s?Promise.resolve(null):new File([s],A.name,{type:s.type})}async processImages(A){const e=A.target;if(null===e)return;const t=e.files;if(null===t||0===t.length)return;this.enableBusyIndicator(e);const i=["image/jpeg","image/png","image/webp"],n=e=>e===A.target;let o;if(Array.isArray(this.customInputElements)&&(o=this.customInputElements.find(n),void 0===o))return;const s=new DataTransfer,a={quality:.5,type:"image/jpeg"};for(const A of t){if(!1===i.includes(A.type))continue;if(!1===A.type.startsWith("image")){s.items.add(A);continue}const e=Object.assign(a,{type:A.type}),t=await this.compressImage(A,e);t instanceof File&&(t.size<A.size?s.items.add(t):s.items.add(A))}A.target.files=s.files,this.disableBusyIndicator(e),"function"==typeof this.onCompressionDoneCallback&&this.onCompressionDoneCallback(t,s.files)}handleChangeEvent(A){const e=A.target;"input"!==e.nodeName.toLowerCase()&&"file"===e.type&&"change"!==A.type||this.processImages(A)}addImageOptimization(A){"string"==typeof A&&(this.customInputElements=Array.from(document.querySelectorAll(A)))}uninstall(){null!==this.inputTypeFileHandlerReference&&document.removeEventListener("change",this.inputTypeFileHandlerReference)}install(A,e){this.inputTypeFileHandlerReference=this.handleChangeEvent.bind(this),this.onCompressionDoneCallback=e;document.addEventListener("change",this.inputTypeFileHandlerReference,{capture:!0}),this.createCSS(),"string"==typeof A&&this.addImageOptimization(A)}}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.10",
5
+ "version": "0.0.12",
6
6
  "license": "MOZILLA PUBLIC LICENSE, VERSION 2.0",
7
7
  "node": ">=16",
8
8
  "main": "dist/optimize-image-on-the-client-side.js",