next-blurhash-previews 0.0.3-beta34 → 0.0.3-beta37

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.
@@ -8,6 +8,7 @@ class ImageWithPreview extends HTMLElement {
8
8
 
9
9
  static observedAttributes = ["preview"];
10
10
 
11
+ #previewSet = false;
11
12
  get #imgEl(): any {
12
13
  return this.querySelector("img");
13
14
  }
@@ -49,7 +50,7 @@ class ImageWithPreview extends HTMLElement {
49
50
  }
50
51
 
51
52
  #imgLoad = () => {
52
- this.sd.innerHTML = `<slot name="image"></slot>`;
53
+ //this.sd.innerHTML = `<slot name="image"></slot>`;
53
54
  };
54
55
 
55
56
  attributeChangedCallback(name) {
@@ -59,6 +60,10 @@ class ImageWithPreview extends HTMLElement {
59
60
  }
60
61
 
61
62
  #updatePreview() {
63
+ if (this.#previewSet) {
64
+ return;
65
+ }
66
+ this.#previewSet = true;
62
67
  const previewObj = JSON.parse(this.getAttribute("preview")!);
63
68
  updateBlurHashPreview(this.#canvasEl, previewObj);
64
69
  }
@@ -70,6 +75,8 @@ if (!customElements.get("blurhash-image")) {
70
75
 
71
76
  function updateBlurHashPreview(canvasEl: HTMLCanvasElement, preview: blurhash) {
72
77
  const { w: width, h: height, blurhash } = preview;
78
+ canvasEl.width = width;
79
+ canvasEl.height = height;
73
80
 
74
81
  const worker = new Worker("/canvas-worker.js");
75
82
 
@@ -77,9 +84,8 @@ function updateBlurHashPreview(canvasEl: HTMLCanvasElement, preview: blurhash) {
77
84
  worker.postMessage({ canvas: offscreen, width, height, blurhash }, [
78
85
  offscreen,
79
86
  ]);
80
- canvasEl.width = width;
81
- canvasEl.height = height;
82
87
 
88
+ return;
83
89
  const pixels = decode(blurhash, width, height);
84
90
  const ctx = canvasEl.getContext("2d")!;
85
91
  const imageData = ctx.createImageData(width, height);
@@ -1,5 +1,82 @@
1
1
  import { createElement } from "react";
2
2
 
3
3
  export const imagePreviewBootstrap = createElement("script", {
4
- dangerouslySetInnerHTML: { __html: `(() => { "use strict";const T=["0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","#","$","%","*","+",",","-",".",":",";","=","?","@","[","]","^","_","{","|","}","~"],d=t=>{let e=0;for(let s=0;s<t.length;s++){const n=t[s],i=T.indexOf(n);e=e*83+i}return e},b=t=>{let e=t/255;return e<=.04045?e/12.92:Math.pow((e+.055)/1.055,2.4)},p=t=>{let e=Math.max(0,Math.min(1,t));return e<=.0031308?Math.round(e*12.92*255+.5):Math.round((1.055*Math.pow(e,.4166666666666667)-.055)*255+.5)},k=t=>t<0?-1:1,v=(t,e)=>k(t)*Math.pow(Math.abs(t),e);class B extends Error{constructor(e){super(e),this.name="ValidationError",this.message=e}}const E=t=>{if(!t||t.length<6)throw new B("The blurhash string must be at least 6 characters");const e=d(t[0]),s=Math.floor(e/9)+1,n=e%9+1;if(t.length!==4+2*n*s)throw new B(\`blurhash length mismatch: length is \${t.length} but it should be \${4+2*n*s}\`)},A=t=>{const e=t>>16,s=t>>8&255,n=t&255;return[b(e),b(s),b(n)]},G=(t,e)=>{const s=Math.floor(t/361),n=Math.floor(t/19)%19,i=t%19;return[v((s-9)/9,2)*e,v((n-9)/9,2)*e,v((i-9)/9,2)*e]},I=(t,e,s,n)=>{E(t),n=n|1;const i=d(t[0]),l=Math.floor(i/9)+1,c=i%9+1,g=(d(t[1])+1)/166,a=new Array(c*l);for(let o=0;o<a.length;o++)if(o===0){const r=d(t.substring(2,6));a[o]=A(r)}else{const r=d(t.substring(4+o*2,6+o*2));a[o]=G(r,g*n)}const h=e*4,u=new Uint8ClampedArray(h*s);for(let o=0;o<s;o++)for(let r=0;r<e;r++){let C=0,q=0,y=0;for(let m=0;m<l;m++)for(let f=0;f<c;f++){const w=Math.cos(Math.PI*r*f/e)*Math.cos(Math.PI*o*m/s);let M=a[f+m*c];C+=M[0]*w,q+=M[1]*w,y+=M[2]*w}let L=p(C),P=p(q),R=p(y);u[4*r+0+o*h]=L,u[4*r+1+o*h]=P,u[4*r+2+o*h]=R,u[4*r+3+o*h]=255}return u};class O extends HTMLElement{sd;mo;static observedAttributes=["preview"];get#t(){return this.querySelector("img")}get#e(){return this.querySelector("canvas")}constructor(){super(),this.sd=this.attachShadow({mode:"open"}),this.sd.innerHTML='<slot name="preview"></slot>'}#s=()=>{if(this.#t&&this.#e)return this.mo?.disconnect(),this.#t.complete?this.#n():(this.#o(),this.#t.addEventListener("load",this.#n)),!0};connectedCallback(){this.#s()||(this.mo=new MutationObserver(this.#s),this.mo.observe(this,{subtree:!0,childList:!0,attributes:!1}))}#n=()=>{this.sd.innerHTML='<slot name="image"></slot>'};attributeChangedCallback(e){this.#e&&e==="preview"&&this.#o()}#o(){const e=JSON.parse(this.getAttribute("preview"));D(this.#e,e)}}customElements.get("blurhash-image")||customElements.define("blurhash-image",O);function D(t,e){const{w:s,h:n,blurhash:i}=e,l=new Worker("/canvas-worker.js"),c=t.transferControlToOffscreen();l.postMessage({canvas:c,width:s,height:n,blurhash:i},[c]),t.width=s,t.height=n;const x=I(i,s,n),g=t.getContext("2d"),a=g.createImageData(s,n);a.data.set(x),g.putImageData(a,0,0)} })();` },
4
+ dangerouslySetInnerHTML: { __html: `(() => { "use strict";
5
+ class ImageWithPreview extends HTMLElement {
6
+ sd;
7
+ mo;
8
+ static observedAttributes = ["preview"];
9
+ #previewSet = false;
10
+ get #imgEl() {
11
+ return this.querySelector("img");
12
+ }
13
+ get #canvasEl() {
14
+ return this.querySelector("canvas");
15
+ }
16
+ constructor() {
17
+ super();
18
+ this.sd = this.attachShadow({
19
+ mode: "open"
20
+ });
21
+ this.sd.innerHTML = \`<slot name="preview"></slot>\`;
22
+ }
23
+ #checkReady = () => {
24
+ if (this.#imgEl && this.#canvasEl) {
25
+ this.mo?.disconnect();
26
+ if (this.#imgEl.complete) {
27
+ this.#imgLoad();
28
+ } else {
29
+ this.#updatePreview();
30
+ this.#imgEl.addEventListener("load", this.#imgLoad);
31
+ }
32
+ return true;
33
+ }
34
+ };
35
+ connectedCallback() {
36
+ if (!this.#checkReady()) {
37
+ this.mo = new MutationObserver(this.#checkReady);
38
+ this.mo.observe(this, {
39
+ subtree: true,
40
+ childList: true,
41
+ attributes: false
42
+ });
43
+ }
44
+ }
45
+ #imgLoad = () => {
46
+ };
47
+ attributeChangedCallback(name) {
48
+ if (this.#canvasEl && name === "preview") {
49
+ this.#updatePreview();
50
+ }
51
+ }
52
+ #updatePreview() {
53
+ if (this.#previewSet) {
54
+ return;
55
+ }
56
+ this.#previewSet = true;
57
+ const previewObj = JSON.parse(this.getAttribute("preview"));
58
+ updateBlurHashPreview(this.#canvasEl, previewObj);
59
+ }
60
+ }
61
+ if (!customElements.get("blurhash-image")) {
62
+ customElements.define("blurhash-image", ImageWithPreview);
63
+ }
64
+ function updateBlurHashPreview(canvasEl, preview) {
65
+ const {
66
+ w: width,
67
+ h: height,
68
+ blurhash
69
+ } = preview;
70
+ canvasEl.width = width;
71
+ canvasEl.height = height;
72
+ const worker = new Worker("/canvas-worker.js");
73
+ const offscreen = canvasEl.transferControlToOffscreen();
74
+ worker.postMessage({
75
+ canvas: offscreen,
76
+ width,
77
+ height,
78
+ blurhash
79
+ }, [offscreen]);
80
+ return;
81
+ } })();` },
5
82
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "next-blurhash-previews",
3
- "version": "0.0.3-beta34",
3
+ "version": "0.0.3-beta37",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "module": "index.js",
package/vite.config.ts CHANGED
@@ -12,7 +12,7 @@ export default defineConfig({
12
12
  fileName: () => "imageWithPreview.js",
13
13
  name: "imageWithPreview",
14
14
  },
15
- //minify: false,
15
+ minify: false,
16
16
  rollupOptions: {
17
17
  external: ["react", "react-dom", "next", "next/script"],
18
18
  },