next-blurhash-previews 0.0.3-beta6 → 0.0.3-beta62

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.
Files changed (59) hide show
  1. package/.vscode/settings.json +10 -0
  2. package/bin/generateBlurhash.js +15 -1
  3. package/bin/remark-plugin.js +5 -3
  4. package/components/ImageWithPreview.tsx +23 -10
  5. package/components/imagePreviewBootstrap.tsx +13 -6
  6. package/components/workerScript.ts +17 -0
  7. package/imagePreviewBootstrap.js +305 -0
  8. package/index.js +1 -3
  9. package/package.json +3 -3
  10. package/util/setBootstrap.js +19 -20
  11. package/{vite.config.ts → vite.wc.config.ts} +4 -7
  12. package/vite.worker.config.ts +18 -0
  13. package/.prettierrc +0 -3
  14. package/img1.png +0 -0
  15. package/img5.png +0 -0
  16. package/junk.js +0 -1
  17. package/next-runner/.eslintrc.json +0 -3
  18. package/next-runner/README.md +0 -34
  19. package/next-runner/blog-posts/post1.md +0 -303
  20. package/next-runner/blog-posts/post2.md +0 -10
  21. package/next-runner/blog-posts/post3.md +0 -10
  22. package/next-runner/blog-posts/post4.md +0 -10
  23. package/next-runner/next.config.js +0 -6
  24. package/next-runner/package-lock.json +0 -5080
  25. package/next-runner/package.json +0 -23
  26. package/next-runner/pages/_app.js +0 -11
  27. package/next-runner/pages/_document.js +0 -17
  28. package/next-runner/pages/index.js +0 -47
  29. package/next-runner/public/css-modules/loadingError.png +0 -0
  30. package/next-runner/public/css-modules/styledComponent.png +0 -0
  31. package/next-runner/public/css-modules/styledSass.png +0 -0
  32. package/next-runner/public/css-modules/unstyledComp.png +0 -0
  33. package/next-runner/public/dynamo-introduction/img1.png +0 -0
  34. package/next-runner/public/dynamo-introduction/img10.png +0 -0
  35. package/next-runner/public/dynamo-introduction/img11.png +0 -0
  36. package/next-runner/public/dynamo-introduction/img12.png +0 -0
  37. package/next-runner/public/dynamo-introduction/img13.png +0 -0
  38. package/next-runner/public/dynamo-introduction/img14.png +0 -0
  39. package/next-runner/public/dynamo-introduction/img15.png +0 -0
  40. package/next-runner/public/dynamo-introduction/img2.png +0 -0
  41. package/next-runner/public/dynamo-introduction/img3.png +0 -0
  42. package/next-runner/public/dynamo-introduction/img4.png +0 -0
  43. package/next-runner/public/dynamo-introduction/img5.png +0 -0
  44. package/next-runner/public/dynamo-introduction/img5a.png +0 -0
  45. package/next-runner/public/dynamo-introduction/img6.png +0 -0
  46. package/next-runner/public/dynamo-introduction/img7.png +0 -0
  47. package/next-runner/public/dynamo-introduction/img8.png +0 -0
  48. package/next-runner/public/dynamo-introduction/img9.png +0 -0
  49. package/next-runner/public/favicon.ico +0 -0
  50. package/next-runner/public/img1.png +0 -0
  51. package/next-runner/public/img5.png +0 -0
  52. package/next-runner/public/vercel.svg +0 -4
  53. package/next-runner/styles/Home.module.css +0 -3
  54. package/next-runner/styles/globals.css +0 -22
  55. package/next-runner/yalc.lock +0 -9
  56. package/next-runner/yarn.lock +0 -1650
  57. package/tsconfig.json +0 -21
  58. package/tsconfig.node.json +0 -8
  59. package/yalc.lock +0 -9
@@ -0,0 +1,10 @@
1
+ {
2
+ "vsicons.associations.files": [
3
+ {
4
+ "icon": "vite",
5
+ "extensions": ["vite.worker.config.ts"],
6
+ "filename": true
7
+ },
8
+ { "icon": "vite", "extensions": ["vite.wc.config.ts"], "filename": true }
9
+ ]
10
+ }
@@ -1,8 +1,10 @@
1
+ import fs from "fs";
2
+ import path from "path";
3
+
1
4
  import sharp from "sharp";
2
5
  import fetch from "node-fetch";
3
6
  import { encode, isBlurhashValid } from "blurhash";
4
7
 
5
- import path from "path";
6
8
  const __dirname = process.cwd();
7
9
 
8
10
  export async function getSharpImage(imgPath) {
@@ -13,6 +15,18 @@ export async function getSharpImage(imgPath) {
13
15
 
14
16
  return sharp(buffer);
15
17
  } else {
18
+ const ext = path.extname(imgPath);
19
+ const dir = path.dirname(imgPath);
20
+ const basename = path.basename(imgPath, ext);
21
+
22
+ const previewOption = path.join(dir, basename + "-preview" + ext);
23
+ console.log("Trying preview", previewOption);
24
+
25
+ if (fs.existsSync(previewOption)) {
26
+ console.log("Preview found");
27
+ return sharp(previewOption);
28
+ }
29
+
16
30
  return sharp(imgPath);
17
31
  }
18
32
  }
@@ -10,7 +10,7 @@ export const blurhashPlugin = publicPath => () => {
10
10
  let outstanding = 0;
11
11
 
12
12
  visitParents(tree, "image", async (node, ancestors) => {
13
- let { url: imagePath, alt } = node;
13
+ let { url: imagePath, alt = "" } = node;
14
14
 
15
15
  const originalImg = imagePath;
16
16
  if (!/http/.test(imagePath)) {
@@ -28,6 +28,8 @@ export const blurhashPlugin = publicPath => () => {
28
28
 
29
29
  const blurHash = await getBlurhash(imagePath);
30
30
 
31
+ const { w, h } = blurHash;
32
+
31
33
  console.log(
32
34
  colors.green(`Finished processing ${imagePath}\n\t`, blurHash)
33
35
  );
@@ -37,8 +39,8 @@ export const blurhashPlugin = publicPath => () => {
37
39
 
38
40
  const newNode = `
39
41
  <blurhash-image url="${originalImg}" preview='${JSON.stringify(blurHash)}'>
40
- <img alt="${alt || ""}" src="${originalImg}" slot="image" />
41
- <canvas width="${blurHash.w}" height="${blurHash.h}" slot="preview"></canvas>
42
+ <img alt="${alt}" width="${w}" height="${h}" src="${originalImg}" slot="image" />
43
+ <canvas width="${w}" height="${h}" slot="preview"></canvas>
42
44
  </blurhash-image>`.trim();
43
45
 
44
46
  parent.children[index] = {
@@ -1,5 +1,3 @@
1
- import { decode } from "../node_modules/blurhash/dist/esm/index";
2
-
3
1
  type blurhash = { w: number; h: number; blurhash: string };
4
2
 
5
3
  class ImageWithPreview extends HTMLElement {
@@ -8,6 +6,7 @@ class ImageWithPreview extends HTMLElement {
8
6
 
9
7
  static observedAttributes = ["preview"];
10
8
 
9
+ #connected = false;
11
10
  get #imgEl(): any {
12
11
  return this.querySelector("img");
13
12
  }
@@ -33,11 +32,12 @@ class ImageWithPreview extends HTMLElement {
33
32
  this.#imgEl.addEventListener("load", this.#imgLoad);
34
33
  }
35
34
 
36
- return true;
35
+ return 1;
37
36
  }
38
37
  };
39
38
 
40
39
  connectedCallback() {
40
+ this.#connected = true;
41
41
  if (!this.#checkReady()) {
42
42
  this.mo = new MutationObserver(this.#checkReady);
43
43
  this.mo.observe(this, {
@@ -59,6 +59,10 @@ class ImageWithPreview extends HTMLElement {
59
59
  }
60
60
 
61
61
  #updatePreview() {
62
+ if (!this.#connected || !this.getAttribute("preview")) {
63
+ return;
64
+ }
65
+
62
66
  const previewObj = JSON.parse(this.getAttribute("preview")!);
63
67
  updateBlurHashPreview(this.#canvasEl, previewObj);
64
68
  }
@@ -68,15 +72,24 @@ if (!customElements.get("blurhash-image")) {
68
72
  customElements.define("blurhash-image", ImageWithPreview);
69
73
  }
70
74
 
71
- function updateBlurHashPreview(canvasEl: HTMLCanvasElement, preview: blurhash) {
72
- const { w: width, h: height } = preview;
75
+ var worker;
73
76
 
77
+ function updateBlurHashPreview(canvasEl: HTMLCanvasElement, preview: blurhash) {
78
+ const { w: width, h: height, blurhash } = preview;
74
79
  canvasEl.width = width;
75
80
  canvasEl.height = height;
76
81
 
77
- const pixels = decode(preview.blurhash, width, height);
78
- const ctx = canvasEl.getContext("2d")!;
79
- const imageData = ctx.createImageData(width, height);
80
- imageData.data.set(pixels);
81
- ctx.putImageData(imageData, 0, 0);
82
+ if (true) {
83
+ if (!worker) {
84
+ const workerBlob = new Blob(
85
+ [document.querySelector("#next-blurhash-worker-script")!.textContent!],
86
+ { type: "text/javascript" }
87
+ );
88
+ worker = new Worker(window.URL.createObjectURL(workerBlob));
89
+ }
90
+ const offscreen = (canvasEl as any).transferControlToOffscreen();
91
+ worker.postMessage({ canvas: offscreen, width, height, blurhash }, [
92
+ offscreen,
93
+ ]);
94
+ }
82
95
  }
@@ -1,7 +1,14 @@
1
- import { createElement } from "react";
2
- import Script from "next/script";
1
+ import { createElement, Fragment } from "react";
3
2
 
4
- export const imagePreviewBootstrap = createElement(Script, {
5
- dangerouslySetInnerHTML: { __html: `(() => { /*HERE*/ })();` },
6
- strategy: "beforeInteractive",
7
- });
3
+ export const imagePreviewBootstrap = createElement(
4
+ Fragment,
5
+ {},
6
+ createElement("script", {
7
+ dangerouslySetInnerHTML: { __html: `(() => { /*WC*/ })();` },
8
+ }),
9
+ createElement("script", {
10
+ id: "next-blurhash-worker-script",
11
+ type: "javascript/worker",
12
+ dangerouslySetInnerHTML: { __html: `(() => { /*WORKER*/ })();` },
13
+ })
14
+ );
@@ -0,0 +1,17 @@
1
+ import { decode } from "blurhash/dist/esm";
2
+
3
+ addEventListener("message", evt => {
4
+ console.log(evt);
5
+
6
+ const { canvas, width, height, blurhash } = evt.data;
7
+
8
+ console.log("Encoding", blurhash);
9
+ const start = +new Date();
10
+ const pixels = decode(blurhash, width, height);
11
+ const ctx = canvas.getContext("2d");
12
+ const imageData = ctx.createImageData(width, height);
13
+ imageData.data.set(pixels);
14
+ ctx.putImageData(imageData, 0, 0);
15
+ const end = +new Date();
16
+ console.log("Done Encoding", blurhash, end - start);
17
+ });
@@ -0,0 +1,305 @@
1
+ import { createElement, Fragment } from "react";
2
+
3
+ export const imagePreviewBootstrap = createElement(
4
+ Fragment,
5
+ {},
6
+ createElement("script", {
7
+ dangerouslySetInnerHTML: { __html: `(() => { "use strict";
8
+ class ImageWithPreview extends HTMLElement {
9
+ sd;
10
+ mo;
11
+ static observedAttributes = ["preview"];
12
+ #connected = false;
13
+ get #imgEl() {
14
+ return this.querySelector("img");
15
+ }
16
+ get #canvasEl() {
17
+ return this.querySelector("canvas");
18
+ }
19
+ constructor() {
20
+ super();
21
+ this.sd = this.attachShadow({
22
+ mode: "open"
23
+ });
24
+ this.sd.innerHTML = \`<slot name="preview"></slot>\`;
25
+ }
26
+ #checkReady = () => {
27
+ if (this.#imgEl && this.#canvasEl) {
28
+ this.mo?.disconnect();
29
+ if (this.#imgEl.complete) {
30
+ this.#imgLoad();
31
+ } else {
32
+ this.#updatePreview();
33
+ this.#imgEl.addEventListener("load", this.#imgLoad);
34
+ }
35
+ return 1;
36
+ }
37
+ };
38
+ connectedCallback() {
39
+ this.#connected = true;
40
+ if (!this.#checkReady()) {
41
+ this.mo = new MutationObserver(this.#checkReady);
42
+ this.mo.observe(this, {
43
+ subtree: true,
44
+ childList: true,
45
+ attributes: false
46
+ });
47
+ }
48
+ }
49
+ #imgLoad = () => {
50
+ this.sd.innerHTML = \`<slot name="image"></slot>\`;
51
+ };
52
+ attributeChangedCallback(name) {
53
+ if (this.#canvasEl && name === "preview") {
54
+ this.#updatePreview();
55
+ }
56
+ }
57
+ #updatePreview() {
58
+ if (!this.#connected || !this.getAttribute("preview")) {
59
+ return;
60
+ }
61
+ const previewObj = JSON.parse(this.getAttribute("preview"));
62
+ updateBlurHashPreview(this.#canvasEl, previewObj);
63
+ }
64
+ }
65
+ if (!customElements.get("blurhash-image")) {
66
+ customElements.define("blurhash-image", ImageWithPreview);
67
+ }
68
+ var worker;
69
+ function updateBlurHashPreview(canvasEl, preview) {
70
+ const {
71
+ w: width,
72
+ h: height,
73
+ blurhash
74
+ } = preview;
75
+ canvasEl.width = width;
76
+ canvasEl.height = height;
77
+ {
78
+ if (!worker) {
79
+ const workerBlob = new Blob([document.querySelector("#next-blurhash-worker-script").textContent], {
80
+ type: "text/javascript"
81
+ });
82
+ worker = new Worker(window.URL.createObjectURL(workerBlob));
83
+ }
84
+ const offscreen = canvasEl.transferControlToOffscreen();
85
+ worker.postMessage({
86
+ canvas: offscreen,
87
+ width,
88
+ height,
89
+ blurhash
90
+ }, [offscreen]);
91
+ }
92
+ } })();` },
93
+ }),
94
+ createElement("script", {
95
+ id: "next-blurhash-worker-script",
96
+ type: "javascript/worker",
97
+ dangerouslySetInnerHTML: { __html: `(() => { "use strict";
98
+ const digitCharacters = [
99
+ "0",
100
+ "1",
101
+ "2",
102
+ "3",
103
+ "4",
104
+ "5",
105
+ "6",
106
+ "7",
107
+ "8",
108
+ "9",
109
+ "A",
110
+ "B",
111
+ "C",
112
+ "D",
113
+ "E",
114
+ "F",
115
+ "G",
116
+ "H",
117
+ "I",
118
+ "J",
119
+ "K",
120
+ "L",
121
+ "M",
122
+ "N",
123
+ "O",
124
+ "P",
125
+ "Q",
126
+ "R",
127
+ "S",
128
+ "T",
129
+ "U",
130
+ "V",
131
+ "W",
132
+ "X",
133
+ "Y",
134
+ "Z",
135
+ "a",
136
+ "b",
137
+ "c",
138
+ "d",
139
+ "e",
140
+ "f",
141
+ "g",
142
+ "h",
143
+ "i",
144
+ "j",
145
+ "k",
146
+ "l",
147
+ "m",
148
+ "n",
149
+ "o",
150
+ "p",
151
+ "q",
152
+ "r",
153
+ "s",
154
+ "t",
155
+ "u",
156
+ "v",
157
+ "w",
158
+ "x",
159
+ "y",
160
+ "z",
161
+ "#",
162
+ "$",
163
+ "%",
164
+ "*",
165
+ "+",
166
+ ",",
167
+ "-",
168
+ ".",
169
+ ":",
170
+ ";",
171
+ "=",
172
+ "?",
173
+ "@",
174
+ "[",
175
+ "]",
176
+ "^",
177
+ "_",
178
+ "{",
179
+ "|",
180
+ "}",
181
+ "~"
182
+ ];
183
+ const decode83 = (str) => {
184
+ let value = 0;
185
+ for (let i = 0; i < str.length; i++) {
186
+ const c = str[i];
187
+ const digit = digitCharacters.indexOf(c);
188
+ value = value * 83 + digit;
189
+ }
190
+ return value;
191
+ };
192
+ const sRGBToLinear = (value) => {
193
+ let v = value / 255;
194
+ if (v <= 0.04045) {
195
+ return v / 12.92;
196
+ } else {
197
+ return Math.pow((v + 0.055) / 1.055, 2.4);
198
+ }
199
+ };
200
+ const linearTosRGB = (value) => {
201
+ let v = Math.max(0, Math.min(1, value));
202
+ if (v <= 31308e-7) {
203
+ return Math.round(v * 12.92 * 255 + 0.5);
204
+ } else {
205
+ return Math.round((1.055 * Math.pow(v, 1 / 2.4) - 0.055) * 255 + 0.5);
206
+ }
207
+ };
208
+ const sign = (n) => n < 0 ? -1 : 1;
209
+ const signPow = (val, exp) => sign(val) * Math.pow(Math.abs(val), exp);
210
+ class ValidationError extends Error {
211
+ constructor(message) {
212
+ super(message);
213
+ this.name = "ValidationError";
214
+ this.message = message;
215
+ }
216
+ }
217
+ const validateBlurhash = (blurhash) => {
218
+ if (!blurhash || blurhash.length < 6) {
219
+ throw new ValidationError("The blurhash string must be at least 6 characters");
220
+ }
221
+ const sizeFlag = decode83(blurhash[0]);
222
+ const numY = Math.floor(sizeFlag / 9) + 1;
223
+ const numX = sizeFlag % 9 + 1;
224
+ if (blurhash.length !== 4 + 2 * numX * numY) {
225
+ throw new ValidationError(\`blurhash length mismatch: length is \${blurhash.length} but it should be \${4 + 2 * numX * numY}\`);
226
+ }
227
+ };
228
+ const decodeDC = (value) => {
229
+ const intR = value >> 16;
230
+ const intG = value >> 8 & 255;
231
+ const intB = value & 255;
232
+ return [sRGBToLinear(intR), sRGBToLinear(intG), sRGBToLinear(intB)];
233
+ };
234
+ const decodeAC = (value, maximumValue) => {
235
+ const quantR = Math.floor(value / (19 * 19));
236
+ const quantG = Math.floor(value / 19) % 19;
237
+ const quantB = value % 19;
238
+ const rgb = [
239
+ signPow((quantR - 9) / 9, 2) * maximumValue,
240
+ signPow((quantG - 9) / 9, 2) * maximumValue,
241
+ signPow((quantB - 9) / 9, 2) * maximumValue
242
+ ];
243
+ return rgb;
244
+ };
245
+ const decode = (blurhash, width, height, punch) => {
246
+ validateBlurhash(blurhash);
247
+ punch = punch | 1;
248
+ const sizeFlag = decode83(blurhash[0]);
249
+ const numY = Math.floor(sizeFlag / 9) + 1;
250
+ const numX = sizeFlag % 9 + 1;
251
+ const quantisedMaximumValue = decode83(blurhash[1]);
252
+ const maximumValue = (quantisedMaximumValue + 1) / 166;
253
+ const colors = new Array(numX * numY);
254
+ for (let i = 0; i < colors.length; i++) {
255
+ if (i === 0) {
256
+ const value = decode83(blurhash.substring(2, 6));
257
+ colors[i] = decodeDC(value);
258
+ } else {
259
+ const value = decode83(blurhash.substring(4 + i * 2, 6 + i * 2));
260
+ colors[i] = decodeAC(value, maximumValue * punch);
261
+ }
262
+ }
263
+ const bytesPerRow = width * 4;
264
+ const pixels = new Uint8ClampedArray(bytesPerRow * height);
265
+ for (let y = 0; y < height; y++) {
266
+ for (let x = 0; x < width; x++) {
267
+ let r = 0;
268
+ let g = 0;
269
+ let b = 0;
270
+ for (let j = 0; j < numY; j++) {
271
+ for (let i = 0; i < numX; i++) {
272
+ const basis = Math.cos(Math.PI * x * i / width) * Math.cos(Math.PI * y * j / height);
273
+ let color = colors[i + j * numX];
274
+ r += color[0] * basis;
275
+ g += color[1] * basis;
276
+ b += color[2] * basis;
277
+ }
278
+ }
279
+ let intR = linearTosRGB(r);
280
+ let intG = linearTosRGB(g);
281
+ let intB = linearTosRGB(b);
282
+ pixels[4 * x + 0 + y * bytesPerRow] = intR;
283
+ pixels[4 * x + 1 + y * bytesPerRow] = intG;
284
+ pixels[4 * x + 2 + y * bytesPerRow] = intB;
285
+ pixels[4 * x + 3 + y * bytesPerRow] = 255;
286
+ }
287
+ }
288
+ return pixels;
289
+ };
290
+ var decode$1 = decode;
291
+ addEventListener("message", (evt) => {
292
+ console.log(evt);
293
+ const { canvas, width, height, blurhash } = evt.data;
294
+ console.log("Encoding", blurhash);
295
+ const start = +new Date();
296
+ const pixels = decode$1(blurhash, width, height);
297
+ const ctx = canvas.getContext("2d");
298
+ const imageData = ctx.createImageData(width, height);
299
+ imageData.data.set(pixels);
300
+ ctx.putImageData(imageData, 0, 0);
301
+ const end = +new Date();
302
+ console.log("Done Encoding", blurhash, end - start);
303
+ }); })();` },
304
+ })
305
+ );
package/index.js CHANGED
@@ -1,5 +1,3 @@
1
- import { imagePreviewBootstrap } from "./imagePreviewBootstrap";
1
+ import { imagePreviewBootstrap } from "./imagePreviewBootstrap.js";
2
2
 
3
3
  export { imagePreviewBootstrap };
4
-
5
- export const src = `(() => { "use strict";var A=Object.defineProperty;var R=(t,e,s)=>e in t?A(t,e,{enumerable:!0,configurable:!0,writable:!0,value:s}):t[e]=s;var u=(t,e,s)=>(R(t,typeof e!="symbol"?e+"":e,s),s);const G=["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","#","$","%","*","+",",","-",".",":",";","=","?","@","[","]","^","_","{","|","}","~"],h=t=>{let e=0;for(let s=0;s<t.length;s++){const n=t[s],o=G.indexOf(n);e=e*83+o}return e},p=t=>{let e=t/255;return e<=.04045?e/12.92:Math.pow((e+.055)/1.055,2.4)},b=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)},P=t=>t<0?-1:1,E=(t,e)=>P(t)*Math.pow(Math.abs(t),e);class v extends Error{constructor(e){super(e),this.name="ValidationError",this.message=e}}const k=t=>{if(!t||t.length<6)throw new v("The blurhash string must be at least 6 characters");const e=h(t[0]),s=Math.floor(e/9)+1,n=e%9+1;if(t.length!==4+2*n*s)throw new v(\`blurhash length mismatch: length is \${t.length} but it should be \${4+2*n*s}\`)},D=t=>{const e=t>>16,s=t>>8&255,n=t&255;return[p(e),p(s),p(n)]},H=(t,e)=>{const s=Math.floor(t/361),n=Math.floor(t/19)%19,o=t%19;return[E((s-9)/9,2)*e,E((n-9)/9,2)*e,E((o-9)/9,2)*e]},S=(t,e,s,n)=>{k(t),n=n|1;const o=h(t[0]),a=Math.floor(o/9)+1,c=o%9+1,q=(h(t[1])+1)/166,g=new Array(c*a);for(let r=0;r<g.length;r++)if(r===0){const i=h(t.substring(2,6));g[r]=D(i)}else{const i=h(t.substring(4+r*2,6+r*2));g[r]=H(i,q*n)}const l=e*4,d=new Uint8ClampedArray(l*s);for(let r=0;r<s;r++)for(let i=0;i<e;i++){let I=0,x=0,C=0;for(let m=0;m<a;m++)for(let f=0;f<c;f++){const M=Math.cos(Math.PI*i*f/e)*Math.cos(Math.PI*r*m/s);let w=g[f+m*c];I+=w[0]*M,x+=w[1]*M,C+=w[2]*M}let B=b(I),T=b(x),y=b(C);d[4*i+0+r*l]=B,d[4*i+1+r*l]=T,d[4*i+2+r*l]=y,d[4*i+3+r*l]=255}return d};class L extends HTMLElement{constructor(){super();u(this,"loaded",!1);u(this,"sd");u(this,"onImageLoad",()=>{this.getAttribute("url")!==this.currentImageEl.src&&setTimeout(()=>{this.loaded=!0,this.render()},1500)});this.sd=this.attachShadow({mode:"open"}),this.sd.innerHTML='<slot name="preview"></slot>'}get currentImageEl(){return this.querySelector("img")}get currentCanvasEl(){return this.querySelector("canvas")}connectedCallback(){this.currentImageEl.complete&&this.onImageLoad(),this.currentImageEl.addEventListener("load",this.onImageLoad)}attributeChangedCallback(s,n,o){if(s==="preview"){const a=JSON.parse(o);O(this.currentCanvasEl,a)}else s==="url"&&o!==this.currentImageEl.getAttribute("src")&&(this.loaded=!1,this.currentImageEl.src=o||"");this.render()}render(){this.sd.innerHTML=\`<slot name="\${this.loaded?"image":"preview"}"></slot>\`}}u(L,"observedAttributes",["preview","url"]);customElements.get("uikit-image")||customElements.define("uikit-image",L);function O(t,e){const{w:s,h:n}=e;t.width=s,t.height=n;const o=S(e.blurhash,s,n),a=t.getContext("2d"),c=a.createImageData(s,n);c.data.set(o),a.putImageData(c,0,0)} })();`;
package/package.json CHANGED
@@ -1,14 +1,15 @@
1
1
  {
2
2
  "name": "next-blurhash-previews",
3
- "version": "0.0.3-beta6",
3
+ "version": "0.0.3-beta62",
4
4
  "description": "",
5
5
  "main": "index.js",
6
+ "module": "index.js",
6
7
  "bin": {
7
8
  "blurhash-markdown": "./bin/markdown-sync.js"
8
9
  },
9
10
  "scripts": {
10
11
  "test": "echo \"Error: no test specified\" && exit 1",
11
- "build": "vite build",
12
+ "build": "rm -rf build && vite build --config vite.wc.config.ts && vite build --config vite.worker.config.ts && node util/setBootstrap.js",
12
13
  "build-watch": "vite build -w",
13
14
  "prepare": "npm run build"
14
15
  },
@@ -31,7 +32,6 @@
31
32
  "colors": "^1.4.0",
32
33
  "glob": "^8.0.3",
33
34
  "install": "^0.13.0",
34
- "next": "^12.2.0",
35
35
  "node-fetch": "^3.2.6",
36
36
  "npm": "^8.15.1",
37
37
  "remark": "^14.0.2",
@@ -1,22 +1,21 @@
1
1
  import fs from "fs";
2
2
 
3
- export default function writeBootstrapScript() {
4
- return {
5
- name: "write-bootstrap-script",
6
- closeBundle() {
7
- let BootstrapModule = fs.readFileSync(
8
- "./components/imagePreviewBootstrap.tsx",
9
- "utf8"
10
- );
11
- const bootstrapScript = fs.readFileSync(
12
- "./build/imageWithPreview.js",
13
- "utf8"
14
- );
15
- BootstrapModule = BootstrapModule.replace(
16
- "/*HERE*/",
17
- bootstrapScript.replace(/[\r\n]\s*$/, "").replace(/`/g, "\\`")
18
- ).replace(/\${/g, "\\${");
19
- fs.writeFileSync("./imagePreviewBootstrap.js", BootstrapModule);
20
- },
21
- };
22
- }
3
+ let BootstrapModule = fs.readFileSync(
4
+ "./components/imagePreviewBootstrap.tsx",
5
+ "utf8"
6
+ );
7
+
8
+ const wcScript = fs.readFileSync("./build/imageWithPreview.js", "utf8");
9
+ const workerScript = fs.readFileSync("./build/workerScript.js", "utf8");
10
+
11
+ BootstrapModule = BootstrapModule.replace(
12
+ "/*WC*/",
13
+ wcScript.replace(/[\r\n]\s*$/, "").replace(/`/g, "\\`")
14
+ )
15
+ .replace(
16
+ "/*WORKER*/",
17
+ workerScript.replace(/[\r\n]\s*$/, "").replace(/`/g, "\\`")
18
+ )
19
+ .replace(/\${/g, "\\${");
20
+
21
+ fs.writeFileSync("./imagePreviewBootstrap.js", BootstrapModule);
@@ -1,21 +1,18 @@
1
1
  import { defineConfig } from "vite";
2
2
  import react from "@vitejs/plugin-react";
3
- import writeBootstrapPlugin from "./util/setBootstrap";
4
3
 
5
4
  export default defineConfig({
6
5
  build: {
7
6
  target: "es2022",
8
7
  outDir: "./build",
8
+ emptyOutDir: false,
9
9
  lib: {
10
- entry: "components/imageWithPreview.tsx",
10
+ entry: "components/ImageWithPreview.tsx",
11
11
  formats: ["cjs"],
12
12
  fileName: () => "imageWithPreview.js",
13
13
  name: "imageWithPreview",
14
14
  },
15
- //minify: false,
16
- rollupOptions: {
17
- external: ["react", "react-dom", "next", "next/script"],
18
- },
15
+ minify: false,
19
16
  },
20
- plugins: [react(), writeBootstrapPlugin()],
17
+ plugins: [react()],
21
18
  });
@@ -0,0 +1,18 @@
1
+ import { defineConfig } from "vite";
2
+ import react from "@vitejs/plugin-react";
3
+
4
+ export default defineConfig({
5
+ build: {
6
+ target: "es2022",
7
+ outDir: "./build",
8
+ emptyOutDir: false,
9
+ lib: {
10
+ entry: "components/workerScript.ts",
11
+ formats: ["cjs"],
12
+ fileName: () => "workerScript.js",
13
+ name: "workerScript",
14
+ },
15
+ minify: false,
16
+ },
17
+ plugins: [react()],
18
+ });
package/.prettierrc DELETED
@@ -1,3 +0,0 @@
1
- {
2
- "arrowParens": "avoid"
3
- }
package/img1.png DELETED
Binary file
package/img5.png DELETED
Binary file
package/junk.js DELETED
@@ -1 +0,0 @@
1
- "use strict";var B=Object.defineProperty;var T=(t,e,s)=>e in t?B(t,e,{enumerable:!0,configurable:!0,writable:!0,value:s}):t[e]=s;var a=(t,e,s)=>(T(t,typeof e!="symbol"?e+"":e,s),s);const k=["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","#","$","%","*","+",",","-",".",":",";","=","?","@","[","]","^","_","{","|","}","~"],h=t=>{let e=0;for(let s=0;s<t.length;s++){const n=t[s],o=k.indexOf(n);e=e*83+o}return e},p=t=>{let e=t/255;return e<=.04045?e/12.92:Math.pow((e+.055)/1.055,2.4)},b=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)},A=t=>t<0?-1:1,v=(t,e)=>A(t)*Math.pow(Math.abs(t),e);class C extends Error{constructor(e){super(e),this.name="ValidationError",this.message=e}}const G=t=>{if(!t||t.length<6)throw new C("The blurhash string must be at least 6 characters");const e=h(t[0]),s=Math.floor(e/9)+1,n=e%9+1;if(t.length!==4+2*n*s)throw new C(\`blurhash length mismatch: length is \${t.length} but it should be \${4+2*n*s}\`)},V=t=>{const e=t>>16,s=t>>8&255,n=t&255;return[p(e),p(s),p(n)]},D=(t,e)=>{const s=Math.floor(t/361),n=Math.floor(t/19)%19,o=t%19;return[v((s-9)/9,2)*e,v((n-9)/9,2)*e,v((o-9)/9,2)*e]},H=(t,e,s,n)=>{G(t),n=n|1;const o=h(t[0]),c=Math.floor(o/9)+1,l=o%9+1,x=(h(t[1])+1)/166,g=new Array(l*c);for(let r=0;r<g.length;r++)if(r===0){const i=h(t.substring(2,6));g[r]=V(i)}else{const i=h(t.substring(4+r*2,6+r*2));g[r]=D(i,x*n)}const d=e*4,u=new Uint8ClampedArray(d*s);for(let r=0;r<s;r++)for(let i=0;i<e;i++){let y=0,E=0,I=0;for(let m=0;m<c;m++)for(let f=0;f<l;f++){const w=Math.cos(Math.PI*i*f/e)*Math.cos(Math.PI*r*m/s);let M=g[f+m*l];y+=M[0]*w,E+=M[1]*w,I+=M[2]*w}let L=b(y),P=b(E),q=b(I);u[4*i+0+r*d]=L,u[4*i+1+r*d]=P,u[4*i+2+r*d]=q,u[4*i+3+r*d]=255}return u};class R extends HTMLElement{constructor(){super();a(this,"isReady",!1);a(this,"loaded",!1);a(this,"sd");a(this,"mo");a(this,"checkReady",()=>{this.currentImageEl&&this.currentCanvasEl&&(this.ready(),this.mo.disconnect())});a(this,"onImageLoad",()=>{this.getAttribute("url")!==this.currentImageEl.src&&setTimeout(()=>{this.loaded=!0,this.render()},1500)});this.sd=this.attachShadow({mode:"open"}),this.sd.innerHTML='<slot name="preview"></slot>'}get currentImageEl(){return this.querySelector("img")}get currentCanvasEl(){return this.querySelector("canvas")}connectedCallback(){this.mo=new MutationObserver(this.checkReady),this.mo.observe(this,{subtree:!0,childList:!0,attributes:!1})}ready(){this.isReady=!0,this.currentImageEl.complete&&this.onImageLoad(),this.currentImageEl.addEventListener("load",this.onImageLoad),this.loaded||this.updatePreview()}attributeChangedCallback(s,n,o){!this.isReady||(s==="preview"?this.updatePreview():s==="url"&&(this.loaded=!1),this.render())}updatePreview(){const s=JSON.parse(this.getAttribute("preview"));O(this.currentCanvasEl,s)}render(){this.sd.innerHTML=\`<slot name="\${this.loaded?"image":"preview"}"></slot>\`}}a(R,"observedAttributes",["preview","url"]);customElements.get("uikit-image")||customElements.define("uikit-image",R);function O(t,e){const{w:s,h:n}=e;t.width=s,t.height=n;const o=H(e.blurhash,s,n),c=t.getContext("2d"),l=c.createImageData(s,n);l.data.set(o),c.putImageData(l,0,0)}
@@ -1,3 +0,0 @@
1
- {
2
- "extends": "next/core-web-vitals"
3
- }
@@ -1,34 +0,0 @@
1
- This is a [Next.js](https://nextjs.org/) project bootstrapped with [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app).
2
-
3
- ## Getting Started
4
-
5
- First, run the development server:
6
-
7
- ```bash
8
- npm run dev
9
- # or
10
- yarn dev
11
- ```
12
-
13
- Open [http://localhost:3000](http://localhost:3000) with your browser to see the result.
14
-
15
- You can start editing the page by modifying `pages/index.js`. The page auto-updates as you edit the file.
16
-
17
- [API routes](https://nextjs.org/docs/api-routes/introduction) can be accessed on [http://localhost:3000/api/hello](http://localhost:3000/api/hello). This endpoint can be edited in `pages/api/hello.js`.
18
-
19
- The `pages/api` directory is mapped to `/api/*`. Files in this directory are treated as [API routes](https://nextjs.org/docs/api-routes/introduction) instead of React pages.
20
-
21
- ## Learn More
22
-
23
- To learn more about Next.js, take a look at the following resources:
24
-
25
- - [Next.js Documentation](https://nextjs.org/docs) - learn about Next.js features and API.
26
- - [Learn Next.js](https://nextjs.org/learn) - an interactive Next.js tutorial.
27
-
28
- You can check out [the Next.js GitHub repository](https://github.com/vercel/next.js/) - your feedback and contributions are welcome!
29
-
30
- ## Deploy on Vercel
31
-
32
- The easiest way to deploy your Next.js app is to use the [Vercel Platform](https://vercel.com/new?utm_medium=default-template&filter=next.js&utm_source=create-next-app&utm_campaign=create-next-app-readme) from the creators of Next.js.
33
-
34
- Check out our [Next.js deployment documentation](https://nextjs.org/docs/deployment) for more details.