wc-img-ai 0.0.1
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 +17 -0
- package/dist/wc-image-ai.js +80 -0
- package/package.json +26 -0
- package/types/ai-image.d.ts +9 -0
- package/types/get-open-ai.d.ts +1 -0
package/README.md
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# AI based img-tags
|
|
2
|
+
|
|
3
|
+
By simply providing an src to an api that does the AI for you, and you can simply get ai-rendered images anywhere you need based on the alt-attribute.
|
|
4
|
+
|
|
5
|
+
```html
|
|
6
|
+
<script
|
|
7
|
+
type="module"
|
|
8
|
+
src="/src/ai-image.ts"
|
|
9
|
+
></script>
|
|
10
|
+
|
|
11
|
+
<img
|
|
12
|
+
is="ai-image"
|
|
13
|
+
src="/api/openai"
|
|
14
|
+
alt="a sleeping little kitten"
|
|
15
|
+
data-fallback="https://placekitten.com/12/12"
|
|
16
|
+
/>
|
|
17
|
+
```
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
const getOpenAi = async (endpoint, prompt, width, height) => {
|
|
2
|
+
let response;
|
|
3
|
+
console.log("getOpenAi", endpoint, prompt, width, height);
|
|
4
|
+
try {
|
|
5
|
+
const r = await fetch(endpoint, {
|
|
6
|
+
method: "POST",
|
|
7
|
+
headers: {
|
|
8
|
+
"Content-Type": "application/json"
|
|
9
|
+
},
|
|
10
|
+
body: JSON.stringify({
|
|
11
|
+
prompt,
|
|
12
|
+
width,
|
|
13
|
+
height
|
|
14
|
+
})
|
|
15
|
+
});
|
|
16
|
+
if (!r.ok) {
|
|
17
|
+
throw new Error("Network response was not ok");
|
|
18
|
+
}
|
|
19
|
+
response = await r.text();
|
|
20
|
+
} catch (error) {
|
|
21
|
+
console.error("There has been a problem with your fetch operation:", error);
|
|
22
|
+
}
|
|
23
|
+
return response;
|
|
24
|
+
};
|
|
25
|
+
class AIImage extends HTMLImageElement {
|
|
26
|
+
constructor() {
|
|
27
|
+
var _a;
|
|
28
|
+
super();
|
|
29
|
+
this._fallbackSrc = null;
|
|
30
|
+
this._prompt = null;
|
|
31
|
+
this._originalSrc = null;
|
|
32
|
+
this.svgPlaceholder = null;
|
|
33
|
+
this._endpoint = null;
|
|
34
|
+
this.ph_w = 256;
|
|
35
|
+
this.ph_h = 256;
|
|
36
|
+
this.getOpenAi = getOpenAi;
|
|
37
|
+
this.ph_w = Number(super.getAttribute("width")) || 256;
|
|
38
|
+
this.ph_h = Number(super.getAttribute("height")) || 256;
|
|
39
|
+
this.svgPlaceholder = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 ${this.ph_w} ${this.ph_h}"><rect width="${this.ph_w}" height="${this.ph_h}" fill="#ddd"/></svg>`;
|
|
40
|
+
this._originalSrc = this.getAttribute("currentSrc");
|
|
41
|
+
this._prompt = this.getAttribute("alt");
|
|
42
|
+
this._endpoint = ((_a = super.getAttribute("src")) == null ? void 0 : _a.toString()) || null;
|
|
43
|
+
this.addEventListener("error", (_event) => {
|
|
44
|
+
super.src = "data:image/svg+xml;utf8," + encodeURIComponent(this.svgPlaceholder);
|
|
45
|
+
});
|
|
46
|
+
super.src = "data:image/svg+xml;utf8," + encodeURIComponent(this.svgPlaceholder);
|
|
47
|
+
setTimeout(async () => {
|
|
48
|
+
if (!this._prompt) {
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
const openAiResponse = await this.getOpenAi(
|
|
52
|
+
this._endpoint,
|
|
53
|
+
this._prompt,
|
|
54
|
+
this.ph_w,
|
|
55
|
+
this.ph_h
|
|
56
|
+
);
|
|
57
|
+
super.src = openAiResponse || this._fallbackSrc || "";
|
|
58
|
+
}, 1);
|
|
59
|
+
}
|
|
60
|
+
static get observedAttributes() {
|
|
61
|
+
return ["data-fallback"];
|
|
62
|
+
}
|
|
63
|
+
get src() {
|
|
64
|
+
return this._originalSrc || "";
|
|
65
|
+
}
|
|
66
|
+
set src(value) {
|
|
67
|
+
if (this._originalSrc !== value && this.src !== void 0) {
|
|
68
|
+
super.src = value;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
set fallbackSrc(value) {
|
|
72
|
+
this._fallbackSrc = value;
|
|
73
|
+
}
|
|
74
|
+
attributeChangedCallback(name, _oldValue, newValue) {
|
|
75
|
+
if (name === "data-fallback") {
|
|
76
|
+
this.fallbackSrc = newValue;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
customElements.define("ai-image", AIImage, { extends: "img" });
|
package/package.json
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "wc-img-ai",
|
|
3
|
+
"private": false,
|
|
4
|
+
"version": "0.0.1",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"exports": "./dist/wc-image-ai.js",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"types": "./dist/wc-image-ai.d.ts",
|
|
9
|
+
"files": [
|
|
10
|
+
"dist",
|
|
11
|
+
"types"
|
|
12
|
+
],
|
|
13
|
+
"devDependencies": {
|
|
14
|
+
"@typescript-eslint/eslint-plugin": "^6.17.0",
|
|
15
|
+
"@typescript-eslint/parser": "^6.17.0",
|
|
16
|
+
"eslint": "^8.56.0",
|
|
17
|
+
"eslint-plugin-wc": "^2.0.4",
|
|
18
|
+
"typescript": "^5.2.2",
|
|
19
|
+
"vite": "^5.0.8"
|
|
20
|
+
},
|
|
21
|
+
"scripts": {
|
|
22
|
+
"dev": "vite",
|
|
23
|
+
"build": "tsc && vite build",
|
|
24
|
+
"preview": "vite preview"
|
|
25
|
+
}
|
|
26
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const getOpenAi: (endpoint: string, prompt: string, width: number, height: number) => Promise<string | undefined>;
|