-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
108 lines (93 loc) · 2.95 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
const getSearches = () =>
JSON.parse(window.localStorage.getItem("searches")) || [];
const storeSearch = (search) => {
const searches = getSearches();
if (!searches.includes(search)) {
searches.push(search);
window.localStorage.setItem("searches", JSON.stringify(searches));
}
};
const renderSearches = () => {
const $previousPrompts = document.getElementById("previous-prompts");
$previousPrompts.innerHTML = "";
const searches = getSearches();
searches.forEach((search) => {
const $li = document.createElement("li");
$li.className = "previous-prompt";
$li.textContent = search;
$li.onclick = () => {
document.getElementById("text-input").value = search;
displayImage();
};
$previousPrompts.appendChild($li);
});
};
renderSearches();
function displayImage() {
var text = document.getElementById("text-input").value.trim();
var width = document.getElementById("width-input").value.trim();
var height = document.getElementById("height-input").value.trim();
var seed = document.getElementById("seed-input").value.trim();
var spinner = document.getElementById("loading-spinner");
var imageContainer = document.getElementById("image-container");
if (text !== "") {
spinner.style.display = "block";
imageContainer.style.display = "block";
const params = new Map();
if (width) params.set("width", width);
if (height) params.set("height", height);
if (seed) params.set("seed", seed);
var existingImage = document.getElementById("displayed-image");
if (existingImage) {
existingImage.remove();
}
var image = document.createElement("img");
image.id = "displayed-image";
image.onload = function () {
spinner.style.display = "none";
};
image.onerror = function () {
spinner.style.display = "none";
alert("Failed to load image. Please try again.");
};
image.src = `https://image.pollinations.ai/prompt/${encodeURIComponent(
text
)}?${new URLSearchParams(params).toString()}`;
document.getElementById("image-container").appendChild(image);
storeSearch(text);
renderSearches();
const imageRecord = {
prompt: text,
imageUrl: image.src,
timestamp: new Date().toISOString(),
width: width || null,
height: height || null,
seed: seed || null,
};
const client = algoliasearch(
config.ALGOLIA_APP_ID,
config.ALGOLIA_ADMIN_KEY
);
const index = client.initIndex("SPEC_IMAGES");
index
.saveObject({
...imageRecord,
objectID: imageRecord.prompt,
})
.catch((err) => {
console.error(err);
});
} else {
alert("Please, insert some text.");
}
}
function handleKeyDown(event) {
if (event.key === "Enter") {
displayImage();
}
}
function toggleCustom() {
var customOptions = document.getElementById("custom-options");
customOptions.style.display =
customOptions.style.display === "block" ? "none" : "block";
}