wc-img-ai 0.1.0 → 0.1.2

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
@@ -1,3 +1,59 @@
1
1
  # AI based img-tags
2
2
 
3
- WIP
3
+ By simply providing a src to an api that does the AI for you, and you can simply get ai-rendered images anywhere you need based on the prompt-attribute.
4
+
5
+ ```html
6
+ <ai-img
7
+ src="http://localhost:4321/api/openai/img"
8
+ width="256"
9
+ height="256"
10
+ prompt="funny dolphin up to no good"
11
+ fallback="https://placekitten.com/200/300"
12
+ ></ai-img>
13
+ ```
14
+
15
+ ## installation
16
+
17
+ ```bash
18
+ pnpm i wc-img-ai
19
+ ```
20
+
21
+ ```html
22
+ <script type="module">
23
+ import "wc-img-ai"
24
+ </script>
25
+ ```
26
+
27
+ The api server itself needs to receive this body and return a string with the url of the image
28
+
29
+ ### Function: POST
30
+
31
+ The web-component sends a POST request to the API to generate an image and expects an image in return.
32
+
33
+ #### Request Body Format
34
+
35
+ - **prompt** (string): The description or prompt based on which the image will be generated.
36
+ - **width** (number): The width of the desired image in pixels.
37
+ - **height** (number): The height of the desired image in pixels.
38
+
39
+ #### Example Request Body
40
+
41
+ ```json
42
+ {
43
+ "prompt": "a sleeping little kitten",
44
+ "width": 256,
45
+ "height": 256
46
+ }
47
+ ```
48
+
49
+ #### Server Return
50
+
51
+ it is expected to return a simple string
52
+
53
+ ```json
54
+ "https://example.com/path/to/generated/image.jpg"
55
+ ```
56
+
57
+ #### Demo
58
+
59
+ Check it out [running](https://john.ro/lab/img-ai) inside an MDX/Astro framework
package/dist/wc-img-ai.js CHANGED
@@ -4,7 +4,7 @@
4
4
  * SPDX-License-Identifier: BSD-3-Clause
5
5
  */
6
6
  var _a;
7
- const t$3 = globalThis, e$3 = t$3.ShadowRoot && (void 0 === t$3.ShadyCSS || t$3.ShadyCSS.nativeShadow) && "adoptedStyleSheets" in Document.prototype && "replace" in CSSStyleSheet.prototype, s$3 = Symbol(), o$4 = /* @__PURE__ */ new WeakMap();
7
+ const t$4 = globalThis, e$3 = t$4.ShadowRoot && (void 0 === t$4.ShadyCSS || t$4.ShadyCSS.nativeShadow) && "adoptedStyleSheets" in Document.prototype && "replace" in CSSStyleSheet.prototype, s$3 = Symbol(), o$4 = /* @__PURE__ */ new WeakMap();
8
8
  let n$4 = class n {
9
9
  constructor(t2, e2, o2) {
10
10
  if (this._$cssResult$ = true, o2 !== s$3)
@@ -38,7 +38,7 @@ const r$5 = (t2) => new n$4("string" == typeof t2 ? t2 : t2 + "", void 0, s$3),
38
38
  s3.adoptedStyleSheets = o2.map((t2) => t2 instanceof CSSStyleSheet ? t2 : t2.styleSheet);
39
39
  else
40
40
  for (const e2 of o2) {
41
- const o3 = document.createElement("style"), n3 = t$3.litNonce;
41
+ const o3 = document.createElement("style"), n3 = t$4.litNonce;
42
42
  void 0 !== n3 && o3.setAttribute("nonce", n3), o3.textContent = e2.cssText, s3.appendChild(o3);
43
43
  }
44
44
  }, c$3 = e$3 ? (t2) => t2 : (t2) => t2 instanceof CSSStyleSheet ? ((t3) => {
@@ -299,7 +299,7 @@ b.elementStyles = [], b.shadowRootOptions = { mode: "open" }, b[d$1("elementProp
299
299
  * Copyright 2017 Google LLC
300
300
  * SPDX-License-Identifier: BSD-3-Clause
301
301
  */
302
- const t$2 = globalThis, i$1 = t$2.trustedTypes, s$2 = i$1 ? i$1.createPolicy("lit-html", { createHTML: (t2) => t2 }) : void 0, e$1 = "$lit$", h$1 = `lit$${(Math.random() + "").slice(9)}$`, o$2 = "?" + h$1, n$2 = `<${o$2}>`, r$3 = document, l = () => r$3.createComment(""), c$1 = (t2) => null === t2 || "object" != typeof t2 && "function" != typeof t2, a = Array.isArray, u = (t2) => a(t2) || "function" == typeof (t2 == null ? void 0 : t2[Symbol.iterator]), d = "[ \n\f\r]", f$2 = /<(?:(!--|\/[^a-zA-Z])|(\/?[a-zA-Z][^>\s]*)|(\/?$))/g, v = /-->/g, _ = />/g, m = RegExp(`>|${d}(?:([^\\s"'>=/]+)(${d}*=${d}*(?:[^
302
+ const t$3 = globalThis, i$1 = t$3.trustedTypes, s$2 = i$1 ? i$1.createPolicy("lit-html", { createHTML: (t2) => t2 }) : void 0, e$1 = "$lit$", h$1 = `lit$${(Math.random() + "").slice(9)}$`, o$2 = "?" + h$1, n$2 = `<${o$2}>`, r$3 = document, l = () => r$3.createComment(""), c$1 = (t2) => null === t2 || "object" != typeof t2 && "function" != typeof t2, a = Array.isArray, u = (t2) => a(t2) || "function" == typeof (t2 == null ? void 0 : t2[Symbol.iterator]), d = "[ \n\f\r]", f$2 = /<(?:(!--|\/[^a-zA-Z])|(\/?[a-zA-Z][^>\s]*)|(\/?$))/g, v = /-->/g, _ = />/g, m = RegExp(`>|${d}(?:([^\\s"'>=/]+)(${d}*=${d}*(?:[^
303
303
  \f\r"'\`<>=]|("|')|))|$)`, "g"), p = /'/g, g = /"/g, $ = /^(?:script|style|textarea|title)$/i, y = (t2) => (i2, ...s3) => ({ _$litType$: t2, strings: i2, values: s3 }), x = y(1), w = Symbol.for("lit-noChange"), T = Symbol.for("lit-nothing"), A = /* @__PURE__ */ new WeakMap(), E = r$3.createTreeWalker(r$3, 129);
304
304
  function C(t2, i2) {
305
305
  if (!Array.isArray(t2) || !t2.hasOwnProperty("raw"))
@@ -534,8 +534,8 @@ class L {
534
534
  N(this, t2);
535
535
  }
536
536
  }
537
- const Z = t$2.litHtmlPolyfillSupport;
538
- Z == null ? void 0 : Z(V, M), (t$2.litHtmlVersions ?? (t$2.litHtmlVersions = [])).push("3.1.0");
537
+ const Z = t$3.litHtmlPolyfillSupport;
538
+ Z == null ? void 0 : Z(V, M), (t$3.litHtmlVersions ?? (t$3.litHtmlVersions = [])).push("3.1.0");
539
539
  const j = (t2, i2, s3) => {
540
540
  const e2 = (s3 == null ? void 0 : s3.renderBefore) ?? i2;
541
541
  let h2 = e2._$litPart$;
@@ -584,7 +584,7 @@ r$2 == null ? void 0 : r$2({ LitElement: s$1 });
584
584
  * Copyright 2017 Google LLC
585
585
  * SPDX-License-Identifier: BSD-3-Clause
586
586
  */
587
- const t$1 = (t2) => (e2, o2) => {
587
+ const t$2 = (t2) => (e2, o2) => {
588
588
  void 0 !== o2 ? o2.addInitializer(() => {
589
589
  customElements.define(t2, e2);
590
590
  }) : customElements.define(t2, e2);
@@ -621,6 +621,17 @@ function n$1(t2) {
621
621
  return e3.constructor.createProperty(o3, r2 ? { ...t3, wrapped: true } : t3), r2 ? Object.getOwnPropertyDescriptor(e3, o3) : void 0;
622
622
  })(t2, e2, o2);
623
623
  }
624
+ /**
625
+ * @license
626
+ * Copyright 2017 Google LLC
627
+ * SPDX-License-Identifier: BSD-3-Clause
628
+ */
629
+ function t$1(t2) {
630
+ return (n3, o2) => {
631
+ const c2 = "function" == typeof n3 ? n3 : n3[o2];
632
+ Object.assign(c2, t2);
633
+ };
634
+ }
624
635
  /**
625
636
  * @license
626
637
  * Copyright 2017 Google LLC
@@ -939,6 +950,7 @@ let AiImg = class extends s$1 {
939
950
  this.prompt = "";
940
951
  this.imgAttributes = {};
941
952
  setTimeout(async () => {
953
+ var _a2;
942
954
  if (!this.prompt) {
943
955
  this.imgsrc = this.fallback;
944
956
  return;
@@ -955,7 +967,9 @@ let AiImg = class extends s$1 {
955
967
  console.error("Error fetching AI image:", error);
956
968
  this.imgsrc = this.fallback;
957
969
  }
958
- }, 1e3);
970
+ const img = (_a2 = this.shadowRoot) == null ? void 0 : _a2.querySelector("img");
971
+ img == null ? void 0 : img.addEventListener("error", this._onImgError.bind(this));
972
+ }, 1);
959
973
  }
960
974
  connectedCallback() {
961
975
  super.connectedCallback();
@@ -979,6 +993,12 @@ let AiImg = class extends s$1 {
979
993
  this.height = attr.value;
980
994
  }
981
995
  });
996
+ this.imgsrc = "data:image/svg+xml;utf8," + encodeURIComponent(
997
+ `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 ${this.width} ${this.height}"><rect width="${this.width}" height="${this.height}" fill="#ddd"/></svg>`
998
+ );
999
+ }
1000
+ _onImgError() {
1001
+ this.imgsrc = this.fallback;
982
1002
  }
983
1003
  render() {
984
1004
  const hostStyles = {
@@ -997,10 +1017,22 @@ let AiImg = class extends s$1 {
997
1017
  <img
998
1018
  src=${this.imgsrc}
999
1019
  decoding="async"
1000
- loading="lazy"
1020
+ loading="eager"
1001
1021
  ${spread(this.imgAttributes)}
1022
+ @onerror=${() => {
1023
+ console.log("onerror");
1024
+ }}
1025
+ @onabort=${() => {
1026
+ console.log("onabort");
1027
+ }}
1028
+ @onloadend=${() => {
1029
+ console.log("onloadend");
1030
+ }}
1031
+ @onprogress=${() => {
1032
+ console.log("onprogress");
1033
+ }}
1002
1034
  />
1003
- ` : ""}
1035
+ ` : x`<div>fail</div>`}
1004
1036
  `;
1005
1037
  }
1006
1038
  };
@@ -1045,8 +1077,11 @@ __decorateClass([
1045
1077
  __decorateClass([
1046
1078
  n$1({ type: String, reflect: true })
1047
1079
  ], AiImg.prototype, "prompt", 2);
1080
+ __decorateClass([
1081
+ t$1({ passive: true })
1082
+ ], AiImg.prototype, "_onImgError", 1);
1048
1083
  AiImg = __decorateClass([
1049
- t$1("ai-img")
1084
+ t$2("ai-img")
1050
1085
  ], AiImg);
1051
1086
  export {
1052
1087
  AiImg
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "wc-img-ai",
3
3
  "private": false,
4
- "version": "0.1.0",
4
+ "version": "0.1.2",
5
5
  "description": "Use AI to generate images for your img tags.",
6
6
  "author": {
7
7
  "name": "John Romani",
package/types/ai-img.d.ts CHANGED
@@ -12,6 +12,7 @@ export declare class AiImg extends LitElement {
12
12
  };
13
13
  connectedCallback(): void;
14
14
  protected constructor();
15
+ private _onImgError;
15
16
  protected render(): import("lit").TemplateResult<1>;
16
17
  static styles: import("lit").CSSResult;
17
18
  }
@@ -1,24 +0,0 @@
1
- export declare class AIImage extends HTMLImageElement {
2
- static get observedAttributes(): string[];
3
- private _fallbackSrc;
4
- private _prompt;
5
- private _originalSrc;
6
- private svgPlaceholder;
7
- private _endpoint;
8
- private ph_w;
9
- private ph_h;
10
- constructor();
11
- get src(): string;
12
- set src(value: string);
13
- set fallbackSrc(value: string | null);
14
- attributeChangedCallback(name: string, _oldValue: string, newValue: string): void;
15
- protected getGeneratedImage: (endpoint: string, prompt: string, width: number, height: number) => Promise<string | undefined>;
16
- }
17
- declare global {
18
- interface AIImage extends HTMLImageElement {
19
- "data-fallback"?: string;
20
- }
21
- interface HTMLElementTagNameMap {
22
- "ai-image": AIImage;
23
- }
24
- }