@substrate-system/blur-hash 0.0.2 → 0.0.4

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/LICENSE CHANGED
@@ -1,20 +1,99 @@
1
- THE MIT LICENSE (MIT)
2
-
3
- Copyright © 2024 Nick Thomas <nichoth@nichoth.com>
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy of
6
- this software and associated documentation files (the “Software”), to deal in
7
- the Software without restriction, including without limitation the rights to
8
- use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
- the Software, and to permit persons to whom the Software is furnished to do so,
10
- subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17
- FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
- COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
- IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
- CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
1
+ # Big Time Public License
2
+
3
+ Version 2.0.2
4
+
5
+ <https://bigtimelicense.com/versions/2.0.2>
6
+
7
+ ## Purpose
8
+
9
+ These terms let you use and share this software for noncommercial purposes and in small business for free, while also guaranteeing that paid licenses for big businesses will be available on fair, reasonable, and nondiscriminatory terms.
10
+
11
+ ## Acceptance
12
+
13
+ In order to get any license under these terms, you must agree to them as both strict obligations and conditions to all your licenses.
14
+
15
+ ## Noncommercial Purposes
16
+
17
+ You may use the software for any noncommercial purpose.
18
+
19
+ ## Personal Uses
20
+
21
+ Personal use for research, experiment, and testing for the benefit of public knowledge, personal study, private entertainment, hobby projects, amateur pursuits, or religious observance, without any anticipated commercial application, count as use for noncommercial purposes.
22
+
23
+ ## Noncommercial Organizations
24
+
25
+ Use by any charitable organization, educational institution, public research organization, public safety or health organization, environmental protection organization, or government institution counts as use for noncommercial purposes, regardless of the source of funding or obligations resulting from the funding.
26
+
27
+ ## Small Business
28
+
29
+ You may use the software for the benefit of your company if it meets all these criteria:
30
+
31
+ 1. had fewer than 20 total individuals working as employees and independent contractors at all times during the last tax year
32
+
33
+ 2. earned less than $1,000,000 total revenue in the last tax year
34
+
35
+ 3. received less than $1,000,000 total debt, equity, and other investment in the last five tax years, counting investment in predecessor companies that reorganized into, merged with, or spun out your company
36
+
37
+ All dollar figures are United States dollars as of 2019. Adjust for them inflation according to the United States Bureau of Labor Statistics' consumer price index for all urban consumers, United States city average, for all items, not seasonally adjusted, with 1982–1984=100 reference base.
38
+
39
+ ## Big Business
40
+
41
+ You may use the software for the benefit of your company:
42
+
43
+ 1. for 128 days after your company stops qualifying under [Small Business](#small-business)
44
+
45
+ 2. indefinitely, if the licensor or their legal successor does not offer fair, reasonable, and nondiscriminatory terms for a commercial license for the software within 32 days of [written request](#how-to-request) and negotiate in good faith to conclude a deal
46
+
47
+ ## How to Request
48
+
49
+ If this software includes an address for the licensor or an agent of the licensor in a standard place, such as in documentation, software package metadata, or an "about" page or screen, try to request a fair commercial license at that address. If this package includes both online and offline addresses, try online before offline. If you can't deliver a request that way, or this software doesn't include any addressees, spend one hour online researching an address, recording all your searches and inquiries as you go, and try any addresses that you find. If you can't find any addresses, or if those addresses also fail, that counts as failure to offer a fair commercial license by the licensor under [Big Business](#big-business).
50
+
51
+ ## Fair, Reasonable, and Nondiscriminatory Terms
52
+
53
+ Fair, reasonable, and nondiscriminatory terms may license the software perpetually or for a term, and may or may not cover new versions of the software. If the licensor advertises license terms and a pricing structure for generally available commercial licenses, the licensor proposes license terms and a price as advertised, and a customer not affiliated with the licensor has bought a commercial license for the software on substantially equivalent terms in the past year, the proposal is fair, reasonable, and nondiscriminatory.
54
+
55
+ ## Copyright License
56
+
57
+ The licensor grants you a copyright license to do everything with the software that would otherwise infringe the licensor's copyright in it for any purpose allowed by these terms.
58
+
59
+ ## Notices
60
+
61
+ You must ensure that anyone who gets a copy of any part of the software from you also gets a copy of these terms or the URL for them above, as well as copies of any plain-text lines beginning with `Required Notice:` that the licensor provided with the software. For example:
62
+
63
+ > Required Notice: Copyright Nick Thomas (https://nichoth.com)
64
+
65
+ ## Patent License
66
+
67
+ The licensor grants you a patent license for the software that covers patent claims the licensor can license, or becomes able to license, that you would infringe by using the software.
68
+
69
+ ## Fair Use
70
+
71
+ You may have "fair use" rights for the software under the law. These terms do not limit them.
72
+
73
+ ## No Other Rights
74
+
75
+ These terms do not allow you to sublicense or transfer any of your licenses to anyone else, or prevent the licensor from granting licenses to anyone else. These terms do not imply any other licenses.
76
+
77
+ ## Patent Defense
78
+
79
+ If you make any written claim that the software infringes or contributes to infringement of any patent, your patent license for the software granted under these terms ends immediately. If your company makes such a claim, your patent license ends immediately for work on behalf of your company.
80
+
81
+ ## Violations
82
+
83
+ The first time you are notified in writing that you have violated any of these terms, or done anything with the software not covered by your licenses, your licenses can nonetheless continue if you come into full compliance with these terms, and take practical steps to correct past violations, within 32 days of receiving notice. Otherwise, all your licenses end immediately.
84
+
85
+ ## No Liability
86
+
87
+ ***As far as the law allows, the software comes as is, without any warranty or condition, and the licensor will not be liable to you for any damages arising out of these terms or the use or nature of the software, under any kind of legal claim.***
88
+
89
+ ## Definitions
90
+
91
+ The **licensor** is the individual or entity offering these terms, and the **software** is the software the licensor makes available under these terms.
92
+
93
+ **You** refers to the individual or entity agreeing to these terms.
94
+
95
+ **Your company** is any legal entity, sole proprietorship, or other kind of organization that you work for, plus all organizations that have control over, are under the control of, or are under common control with that organization. **Control** means ownership of substantially all the assets of an entity, or the power to direct its management and policies by vote, contract, or otherwise. Control can be direct or indirect.
96
+
97
+ **Your licenses** are all the licenses granted to you for the software under these terms.
98
+
99
+ **Use** means anything you do with the software requiring one of your licenses.
package/README.md CHANGED
@@ -1,11 +1,16 @@
1
1
  # blur hash
2
- ![tests](https://github.com/substrate-system/icons/actions/workflows/nodejs.yml/badge.svg)
3
- [![types](https://img.shields.io/npm/types/@substrate-system/icons?style=flat-square)](README.md)
2
+ [![tests](https://img.shields.io/github/actions/workflow/status/substrate-system/blur-hash/nodejs.yml?style=flat-square)](https://github.com/substrate-system/blur-hash/actions/workflows/nodejs.yml)
3
+ [![types](https://img.shields.io/npm/types/@substrate-system/blur-hash?style=flat-square)](README.md)
4
4
  [![module](https://img.shields.io/badge/module-ESM%2FCJS-blue?style=flat-square)](README.md)
5
+ [![Common Changelog](https://nichoth.github.io/badge/common-changelog.svg)](https://common-changelog.org)
5
6
  [![semantic versioning](https://img.shields.io/badge/semver-2.0.0-blue?logo=semver&style=flat-square)](https://semver.org/)
6
- [![license](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE)
7
+ [![install size](https://flat.badgen.net/packagephobia/install/@substrate-system/blur-hash?cache-control=no-cache)](https://packagephobia.com/result?p=@substrate-system/blur-hash)
8
+ [![GZip size](https://flat.badgen.net/bundlephobia/minzip/@substrate-system/blur-hash?color=green)](https://bundlephobia.com/package/@substrate-system/blur-hash)
9
+ [![license](https://img.shields.io/badge/license-Big_Time-blue?style=flat-square)](LICENSE)
7
10
 
8
- This is the ["blur-up" image loading technique](https://css-tricks.com/the-blur-up-technique-for-loading-background-images/), with the [blur-hash algorithm](https://blurha.sh/), as a [web component](https://developer.mozilla.org/en-US/docs/Web/API/Web_components).
11
+
12
+ This is the ["blur-up" image loading technique](https://css-tricks.com/the-blur-up-technique-for-loading-background-images/),
13
+ with the [blur-hash algorithm](https://blurha.sh/), as a [web component](https://developer.mozilla.org/en-US/docs/Web/API/Web_components).
9
14
 
10
15
  [See a live demonstration](https://substrate-system.github.io/blur-hash/)
11
16
 
@@ -15,10 +20,12 @@ This is the ["blur-up" image loading technique](https://css-tricks.com/the-blur-
15
20
  <!-- toc -->
16
21
 
17
22
  - [install](#install)
18
- - [API](#api)
23
+ - [Modules](#modules)
19
24
  * [ESM](#esm)
20
25
  * [Common JS](#common-js)
26
+ - [API](#api)
21
27
  * [attributes](#attributes)
28
+ * [`.reset`](#reset)
22
29
  - [CSS](#css)
23
30
  * [Import CSS](#import-css)
24
31
  * [variables](#variables)
@@ -36,7 +43,7 @@ This is the ["blur-up" image loading technique](https://css-tricks.com/the-blur-
36
43
  npm i -S @substrate-system/blur-hash
37
44
  ```
38
45
 
39
- ## API
46
+ ## Modules
40
47
 
41
48
  This exposes ESM and common JS via [package.json `exports` field](https://nodejs.org/api/packages.html#exports).
42
49
 
@@ -50,6 +57,8 @@ import '@substrate-system/blur-hash'
50
57
  require('@substrate-system/blur-hash')
51
58
  ```
52
59
 
60
+ ## API
61
+
53
62
  ### attributes
54
63
  Takes the following attributes
55
64
 
@@ -62,6 +71,39 @@ The string created by the blurhash algorithm. See [node example](#create-the-str
62
71
  #### width & height
63
72
  The dimensions for the image
64
73
 
74
+ ### `.reset`
75
+
76
+ Change the image, and do the blur-up thing again.
77
+
78
+ ```ts
79
+ reset (
80
+ newSrc:string,
81
+ alt:string,
82
+ placeholder:string,
83
+ newSrcset?:string|null,
84
+ newSizes?:string|null,
85
+ attrs:Partial<{
86
+ srcset:string|null;
87
+ width:string|null;
88
+ height:string|null;
89
+ time:number|null;
90
+ }> = {}
91
+ ):void
92
+ ```
93
+
94
+ #### example
95
+
96
+ ```js
97
+ document.querySelector('blur-hash')?.reset(
98
+ 'llamas.jpg',
99
+ 'some llamas',
100
+ 'UgI}q#%O%eNa?^I?awaf?aIVs*WBxZxaRjR*'
101
+ )
102
+ ```
103
+
104
+ -------------------------------------------------
105
+
106
+
65
107
  ## CSS
66
108
 
67
109
  ### Import CSS
@@ -82,7 +124,7 @@ __CSS variables__
82
124
  * `--blur-hash-time` -- the transition time for animating blurry -> sharp,
83
125
  default is `0.8s`
84
126
  * `--blur-hash-opactiy` -- the opacity to use for the placeholder image,
85
- default is `0.6`
127
+ default is `0.2`
86
128
 
87
129
  ## use
88
130
  This calls the global function `customElements.define`. Just import, then use
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+ import yargs from "yargs";
3
+ import inkjet from "inkjet";
4
+ import { hideBin } from "yargs/helpers";
5
+ import { encode } from "blurhash";
6
+ import fs from "node:fs/promises";
7
+ import { resolve } from "node:path";
8
+ import { fileURLToPath } from "node:url";
9
+ import { imageSizeFromFile } from "image-size/fromFile";
10
+ export const encodeImageToBlurhash = async (filepath, width, height) => {
11
+ const data = await fs.readFile(filepath);
12
+ return new Promise((resolve2, reject) => {
13
+ inkjet.decode(data, function(err, decoded) {
14
+ if (err) return reject(err);
15
+ resolve2(encode(decoded.data, width, height, 4, 4));
16
+ });
17
+ });
18
+ };
19
+ const pathToThisFile = resolve(fileURLToPath(import.meta.url));
20
+ const pathPassedToNode = resolve(process.argv[1]);
21
+ const isThisFileBeingRunViaCLI = pathToThisFile.includes(pathPassedToNode);
22
+ if (isThisFileBeingRunViaCLI) {
23
+ const args = yargs(hideBin(process.argv)).demandCommand(1).command("filename", "the local filename to read").example(
24
+ "`npx blur my-fiile.jpg`",
25
+ "Create a small placeholder string from a local file"
26
+ ).usage("Usage: blur <filename>").argv;
27
+ const filename = args._[0];
28
+ const { width, height } = await imageSizeFromFile(filename);
29
+ const hash = await encodeImageToBlurhash(filename, width, height);
30
+ process.stdout.write(hash + "\n");
31
+ }
package/dist/index.cjs CHANGED
@@ -17,44 +17,62 @@ var __copyProps = (to, from, except, desc) => {
17
17
  return to;
18
18
  };
19
19
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
20
- var src_exports = {};
21
- __export(src_exports, {
20
+ var index_exports = {};
21
+ __export(index_exports, {
22
22
  BlurHash: () => BlurHash
23
23
  });
24
- module.exports = __toCommonJS(src_exports);
24
+ module.exports = __toCommonJS(index_exports);
25
+ var import_web_component = require("@substrate-system/web-component");
25
26
  var import_blurhash = require("blurhash");
26
- var import_debug = require("@bicycle-codes/debug");
27
- const debug = (0, import_debug.createDebug)();
28
- class BlurHash extends HTMLElement {
27
+ class BlurHash extends import_web_component.WebComponent.create("blur-hash") {
29
28
  static {
30
29
  __name(this, "BlurHash");
31
30
  }
31
+ time;
32
32
  constructor() {
33
33
  super();
34
- const srcset = this.getAttribute("srcset");
35
34
  const w = this.getAttribute("width");
36
35
  const h = this.getAttribute("height");
36
+ const time = this.getAttribute("time");
37
+ this.time = time ? parseInt(time) : 800;
37
38
  this.style.width = "" + w;
38
39
  this.style.height = "" + h;
39
40
  document.body.style.setProperty(
40
41
  "--blur-hash-time",
41
- this.getAttribute("time") || "0.8s"
42
+ time ? "." + (parseInt(time) / 1e3 + "s") : "0.8s"
42
43
  );
43
- this.innerHTML = `<canvas
44
- class="blurry"
45
- width=${w}
46
- height=${h}
47
- ></canvas>
48
- <img class="blurry"
49
- ${srcset ? `srcset="${srcset}"` : ""}
50
- alt="${this.getAttribute("alt")}"
51
- content-visibility="${this.getAttribute("content-visibility") || "auto"}"
52
- decoding="${this.getAttribute("decoding") || "async"}"
53
- loading="${this.getAttribute("loading") || "lazy"}"
54
- class="image-element blurry"
55
- src="${this.getAttribute("src")}"
56
- />
57
- `;
44
+ }
45
+ /**
46
+ * Change the image, and do the blur-up thing again.
47
+ */
48
+ reset(newSrc, alt, placeholder, newSrcset, newSizes, attrs = {}) {
49
+ if (attrs.width) this.style.width = attrs.width;
50
+ if (attrs.height) this.style.height = attrs.height;
51
+ const width = attrs.width ? parseInt(attrs.width) : parseInt(this.style.width);
52
+ const height = attrs.height ? parseInt(attrs.height) : parseInt(this.style.height);
53
+ this.innerHTML = BlurHash.html({
54
+ srcset: attrs.srcset,
55
+ w: "" + width,
56
+ h: "" + height,
57
+ src: newSrc,
58
+ alt
59
+ });
60
+ const pixels = (0, import_blurhash.decode)(placeholder, width, height);
61
+ const canvas = this.querySelector("canvas");
62
+ const ctx = canvas.getContext("2d");
63
+ const imageData = ctx.createImageData(width, height);
64
+ imageData.data.set(pixels);
65
+ ctx.putImageData(imageData, 0, 0);
66
+ this.setAttribute("src", newSrc);
67
+ this.setAttribute("placeholder", placeholder);
68
+ const img = this.querySelector("img");
69
+ if (newSrcset) img.setAttribute("srcset", newSrcset);
70
+ if (newSizes) img.setAttribute("sizes", newSizes);
71
+ img.addEventListener("load", () => {
72
+ canvas.style.display = "none";
73
+ img.classList.remove("blurry");
74
+ img.classList.add("sharp");
75
+ });
58
76
  }
59
77
  connectedCallback() {
60
78
  const width = parseInt(this.getAttribute("width") ?? "");
@@ -63,6 +81,7 @@ class BlurHash extends HTMLElement {
63
81
  if (!placeholder || !width || !height) {
64
82
  throw new Error("Missing attributes");
65
83
  }
84
+ this.innerHTML = this.render();
66
85
  const pixels = (0, import_blurhash.decode)(placeholder, width, height);
67
86
  const canvas = this.querySelector("canvas");
68
87
  const ctx = canvas.getContext("2d");
@@ -71,12 +90,49 @@ class BlurHash extends HTMLElement {
71
90
  ctx.putImageData(imageData, 0, 0);
72
91
  const img = this.querySelector("img");
73
92
  img.addEventListener("load", () => {
74
- debug("loaded");
75
93
  canvas.style.display = "none";
76
94
  img.classList.remove("blurry");
77
95
  img.classList.add("sharp");
78
96
  });
79
- debug("the image", img);
97
+ }
98
+ static html(attrs) {
99
+ const {
100
+ srcset,
101
+ w,
102
+ h,
103
+ alt,
104
+ contentVisibility,
105
+ decoding,
106
+ loading,
107
+ src
108
+ } = attrs;
109
+ return `<canvas
110
+ class="blurry"
111
+ width=${w}
112
+ height=${h}
113
+ ></canvas>
114
+
115
+ <img class="blurry"
116
+ ${srcset ? `srcset="${srcset}"` : ""}
117
+ alt="${alt}"
118
+ content-visibility="${contentVisibility || "auto"}"
119
+ decoding="${decoding || "async"}"
120
+ loading="${loading || "lazy"}"
121
+ class="image-element blurry"
122
+ src="${src}"
123
+ />`;
124
+ }
125
+ render() {
126
+ const srcset = this.getAttribute("srcset");
127
+ const w = this.getAttribute("width");
128
+ const h = this.getAttribute("height");
129
+ const time = this.getAttribute("time");
130
+ this.time = time ? parseInt(time) : 800;
131
+ const src = this.getAttribute("src");
132
+ const alt = this.getAttribute("alt");
133
+ if (!src) throw new Error("Not src");
134
+ if (!alt) throw new Error("Not alt");
135
+ return BlurHash.html({ srcset, w, h, src, alt });
80
136
  }
81
137
  }
82
138
  customElements.define("blur-hash", BlurHash);
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../src/index.ts"],
4
- "sourcesContent": ["import { decode } from 'blurhash'\nimport { createDebug } from '@bicycle-codes/debug'\nconst debug = createDebug()\n\n// for docuement.querySelector\ndeclare global {\n interface HTMLElementTagNameMap {\n 'blur-hash': BlurHash\n }\n}\n\nexport class BlurHash extends HTMLElement {\n constructor () {\n super()\n const srcset = this.getAttribute('srcset')\n const w = this.getAttribute('width')\n const h = this.getAttribute('height')\n\n this.style.width = '' + w\n this.style.height = '' + h\n\n document.body.style.setProperty('--blur-hash-time',\n this.getAttribute('time') || '0.8s')\n\n this.innerHTML = `<canvas\n class=\"blurry\"\n width=${w}\n height=${h}\n ></canvas>\n <img class=\"blurry\"\n ${srcset ? `srcset=\"${srcset}\"` : ''}\n alt=\"${this.getAttribute('alt')}\"\n content-visibility=\"${this.getAttribute('content-visibility') || 'auto'}\"\n decoding=\"${this.getAttribute('decoding') || 'async'}\"\n loading=\"${this.getAttribute('loading') || 'lazy'}\"\n class=\"image-element blurry\"\n src=\"${this.getAttribute('src')}\"\n />\n `\n }\n\n connectedCallback () {\n const width = parseInt(this.getAttribute('width') ?? '')\n const height = parseInt(this.getAttribute('height') ?? '')\n const placeholder = this.getAttribute('placeholder')\n if (!placeholder || !width || !height) {\n throw new Error('Missing attributes')\n }\n\n const pixels = decode(placeholder, width, height)\n const canvas = this.querySelector('canvas') as HTMLCanvasElement\n const ctx = canvas.getContext('2d')!\n const imageData = ctx.createImageData(width, height)\n imageData.data.set(pixels)\n ctx.putImageData(imageData, 0, 0)\n\n const img = this.querySelector('img')!\n\n img.addEventListener('load', () => {\n debug('loaded')\n canvas.style.display = 'none'\n img.classList.remove('blurry')\n img.classList.add('sharp')\n })\n\n debug('the image', img)\n }\n}\n\ncustomElements.define('blur-hash', BlurHash)\n"],
5
- "mappings": ";;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,sBAAuB;AACvB,mBAA4B;AAC5B,MAAM,YAAQ,0BAAY;AASnB,MAAM,iBAAiB,YAAY;AAAA,EAX1C,OAW0C;AAAA;AAAA;AAAA,EACtC,cAAe;AACX,UAAM;AACN,UAAM,SAAS,KAAK,aAAa,QAAQ;AACzC,UAAM,IAAI,KAAK,aAAa,OAAO;AACnC,UAAM,IAAI,KAAK,aAAa,QAAQ;AAEpC,SAAK,MAAM,QAAQ,KAAK;AACxB,SAAK,MAAM,SAAS,KAAK;AAEzB,aAAS,KAAK,MAAM;AAAA,MAAY;AAAA,MAC5B,KAAK,aAAa,MAAM,KAAK;AAAA,IAAM;AAEvC,SAAK,YAAY;AAAA;AAAA,oBAEL,CAAC;AAAA,qBACA,CAAC;AAAA;AAAA;AAAA,cAGR,SAAS,WAAW,MAAM,MAAM,EAAE;AAAA,mBAC7B,KAAK,aAAa,KAAK,CAAC;AAAA,kCACT,KAAK,aAAa,oBAAoB,KAAK,MAAM;AAAA,wBAC3D,KAAK,aAAa,UAAU,KAAK,OAAO;AAAA,uBACzC,KAAK,aAAa,SAAS,KAAK,MAAM;AAAA;AAAA,mBAE1C,KAAK,aAAa,KAAK,CAAC;AAAA;AAAA;AAAA,EAGvC;AAAA,EAEA,oBAAqB;AACjB,UAAM,QAAQ,SAAS,KAAK,aAAa,OAAO,KAAK,EAAE;AACvD,UAAM,SAAS,SAAS,KAAK,aAAa,QAAQ,KAAK,EAAE;AACzD,UAAM,cAAc,KAAK,aAAa,aAAa;AACnD,QAAI,CAAC,eAAe,CAAC,SAAS,CAAC,QAAQ;AACnC,YAAM,IAAI,MAAM,oBAAoB;AAAA,IACxC;AAEA,UAAM,aAAS,wBAAO,aAAa,OAAO,MAAM;AAChD,UAAM,SAAS,KAAK,cAAc,QAAQ;AAC1C,UAAM,MAAM,OAAO,WAAW,IAAI;AAClC,UAAM,YAAY,IAAI,gBAAgB,OAAO,MAAM;AACnD,cAAU,KAAK,IAAI,MAAM;AACzB,QAAI,aAAa,WAAW,GAAG,CAAC;AAEhC,UAAM,MAAM,KAAK,cAAc,KAAK;AAEpC,QAAI,iBAAiB,QAAQ,MAAM;AAC/B,YAAM,QAAQ;AACd,aAAO,MAAM,UAAU;AACvB,UAAI,UAAU,OAAO,QAAQ;AAC7B,UAAI,UAAU,IAAI,OAAO;AAAA,IAC7B,CAAC;AAED,UAAM,aAAa,GAAG;AAAA,EAC1B;AACJ;AAEA,eAAe,OAAO,aAAa,QAAQ;",
4
+ "sourcesContent": ["import { WebComponent } from '@substrate-system/web-component'\nimport { decode } from 'blurhash'\n\n// for docuement.querySelector\ndeclare global {\n interface HTMLElementTagNameMap {\n 'blur-hash': BlurHash\n }\n}\n\nexport class BlurHash extends WebComponent.create('blur-hash') {\n time:number\n\n constructor () {\n super()\n // const srcset = this.getAttribute('srcset')\n const w = this.getAttribute('width')\n const h = this.getAttribute('height')\n const time = this.getAttribute('time')\n this.time = time ? parseInt(time) : 800\n\n this.style.width = '' + w\n this.style.height = '' + h\n\n document.body.style.setProperty('--blur-hash-time',\n time ? '.' + (parseInt(time) / 1000 + 's') : '0.8s')\n }\n\n /**\n * Change the image, and do the blur-up thing again.\n */\n reset (\n newSrc:string,\n alt:string,\n placeholder:string,\n newSrcset?:string|null,\n newSizes?:string|null,\n attrs:Partial<{\n srcset:string|null;\n width:string|null;\n height:string|null;\n time:number|null;\n }> = {}\n ):void {\n if (attrs.width) this.style.width = attrs.width\n if (attrs.height) this.style.height = attrs.height\n\n const width = (attrs.width ?\n parseInt(attrs.width) :\n parseInt(this.style.width))\n const height = (attrs.height ?\n parseInt(attrs.height) :\n parseInt(this.style.height))\n\n this.innerHTML = BlurHash.html({\n srcset: attrs.srcset,\n w: '' + width,\n h: '' + height,\n src: newSrc,\n alt\n })\n\n const pixels = decode(placeholder, width, height)\n const canvas = this.querySelector('canvas') as HTMLCanvasElement\n const ctx = canvas.getContext('2d')!\n const imageData = ctx.createImageData(width, height)\n imageData.data.set(pixels)\n ctx.putImageData(imageData, 0, 0)\n\n this.setAttribute('src', newSrc)\n this.setAttribute('placeholder', placeholder)\n\n const img = this.querySelector('img')!\n if (newSrcset) img.setAttribute('srcset', newSrcset)\n if (newSizes) img.setAttribute('sizes', newSizes)\n\n img.addEventListener('load', () => {\n canvas.style.display = 'none'\n img.classList.remove('blurry')\n img.classList.add('sharp')\n })\n }\n\n connectedCallback () {\n const width = parseInt(this.getAttribute('width') ?? '')\n const height = parseInt(this.getAttribute('height') ?? '')\n const placeholder = this.getAttribute('placeholder')\n if (!placeholder || !width || !height) {\n throw new Error('Missing attributes')\n }\n\n this.innerHTML = this.render()\n const pixels = decode(placeholder, width, height)\n const canvas = this.querySelector('canvas') as HTMLCanvasElement\n const ctx = canvas.getContext('2d')!\n const imageData = ctx.createImageData(width, height)\n imageData.data.set(pixels)\n ctx.putImageData(imageData, 0, 0)\n\n const img = this.querySelector('img')!\n\n img.addEventListener('load', () => {\n canvas.style.display = 'none'\n img.classList.remove('blurry')\n img.classList.add('sharp')\n })\n }\n\n static html (attrs:{\n alt:string;\n srcset?:string|null;\n w?:string|null;\n h?:string|null;\n time?:number;\n contentVisibility?:'visible'|'auto'|'hidden'|null;\n decoding?:'sync'|'async'|'auto'|null;\n loading?:'lazy'|'eager'|'auto'|null;\n src:string;\n }) {\n const {\n srcset,\n w,\n h,\n alt,\n contentVisibility,\n decoding,\n loading,\n src\n } = attrs\n\n return `<canvas\n class=\"blurry\"\n width=${w}\n height=${h}\n ></canvas>\n\n <img class=\"blurry\"\n ${srcset ? `srcset=\"${srcset}\"` : ''}\n alt=\"${alt}\"\n content-visibility=\"${contentVisibility || 'auto'}\"\n decoding=\"${decoding || 'async'}\"\n loading=\"${loading || 'lazy'}\"\n class=\"image-element blurry\"\n src=\"${src}\"\n />`\n }\n\n render ():string {\n const srcset = this.getAttribute('srcset')\n const w = this.getAttribute('width')\n const h = this.getAttribute('height')\n const time = this.getAttribute('time')\n this.time = time ? parseInt(time) : 800\n const src = this.getAttribute('src')\n const alt = this.getAttribute('alt')\n if (!src) throw new Error('Not src')\n if (!alt) throw new Error('Not alt')\n\n return BlurHash.html({ srcset, w, h, src, alt })\n }\n}\n\ncustomElements.define('blur-hash', BlurHash)\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,2BAA6B;AAC7B,sBAAuB;AAShB,MAAM,iBAAiB,kCAAa,OAAO,WAAW,EAAE;AAAA,EAV/D,OAU+D;AAAA;AAAA;AAAA,EAC3D;AAAA,EAEA,cAAe;AACX,UAAM;AAEN,UAAM,IAAI,KAAK,aAAa,OAAO;AACnC,UAAM,IAAI,KAAK,aAAa,QAAQ;AACpC,UAAM,OAAO,KAAK,aAAa,MAAM;AACrC,SAAK,OAAO,OAAO,SAAS,IAAI,IAAI;AAEpC,SAAK,MAAM,QAAQ,KAAK;AACxB,SAAK,MAAM,SAAS,KAAK;AAEzB,aAAS,KAAK,MAAM;AAAA,MAAY;AAAA,MAC5B,OAAO,OAAO,SAAS,IAAI,IAAI,MAAO,OAAO;AAAA,IAAM;AAAA,EAC3D;AAAA;AAAA;AAAA;AAAA,EAKA,MACI,QACA,KACA,aACA,WACA,UACA,QAKK,CAAC,GACH;AACH,QAAI,MAAM,MAAO,MAAK,MAAM,QAAQ,MAAM;AAC1C,QAAI,MAAM,OAAQ,MAAK,MAAM,SAAS,MAAM;AAE5C,UAAM,QAAS,MAAM,QACjB,SAAS,MAAM,KAAK,IACpB,SAAS,KAAK,MAAM,KAAK;AAC7B,UAAM,SAAU,MAAM,SAClB,SAAS,MAAM,MAAM,IACrB,SAAS,KAAK,MAAM,MAAM;AAE9B,SAAK,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