Commit
·
5fcf2eb
1
Parent(s):
be32788
Create script.js
Browse files
script.js
ADDED
@@ -0,0 +1,217 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import emojiToText from './emojiMapping.js';
|
2 |
+
|
3 |
+
function mapEmojisToText(emojiString) {
|
4 |
+
return [...emojiString].map(emoji => emojiToText[emoji] || emoji).join(' ');
|
5 |
+
}
|
6 |
+
|
7 |
+
function handleEmojiClick(thumbnail, index) {
|
8 |
+
thumbnail.addEventListener('click', () => handleEmojiSelection(index));
|
9 |
+
thumbnail.addEventListener('keypress', event => {
|
10 |
+
if (event.key === 'Enter' || event.key === ' ') {
|
11 |
+
handleEmojiSelection(index);
|
12 |
+
}
|
13 |
+
});
|
14 |
+
}
|
15 |
+
|
16 |
+
function handleEmojiSelection(index) {
|
17 |
+
showCategory(index);
|
18 |
+
}
|
19 |
+
|
20 |
+
function hideAllCategories() {
|
21 |
+
document.querySelectorAll('.emoji-category').forEach(cat => cat.classList.add('hidden'));
|
22 |
+
}
|
23 |
+
|
24 |
+
function showCategory(index) {
|
25 |
+
const category = document.getElementById(`emoji-category-${index + 1}`);
|
26 |
+
category.classList.remove('hidden');
|
27 |
+
}
|
28 |
+
|
29 |
+
function handleCategoryClick(category) {
|
30 |
+
category.addEventListener('click', event => {
|
31 |
+
if (isEmojiClicked(event)) {
|
32 |
+
addEmojiToInput(event.target);
|
33 |
+
}
|
34 |
+
event.stopPropagation();
|
35 |
+
});
|
36 |
+
}
|
37 |
+
|
38 |
+
function isEmojiClicked(event) {
|
39 |
+
return event.target !== event.currentTarget && event.target.classList.contains('emoji');
|
40 |
+
}
|
41 |
+
|
42 |
+
function addEmojiToInput(emojiElement) {
|
43 |
+
const selectedEmojisInput = document.getElementById('selected-emojis');
|
44 |
+
selectedEmojisInput.value += emojiElement.textContent.trim();
|
45 |
+
|
46 |
+
// Adicione a classe de animação ao emojiElement
|
47 |
+
emojiElement.classList.add('emoji-clicked');
|
48 |
+
|
49 |
+
// Remova a classe de animação após a animação ser concluída
|
50 |
+
setTimeout(() => {
|
51 |
+
emojiElement.classList.remove('emoji-clicked');
|
52 |
+
}, 100); // A duração da animação em milissegundos
|
53 |
+
}
|
54 |
+
|
55 |
+
function adjustEmojiGrid() {
|
56 |
+
const grids = document.querySelectorAll('.emoji-category div');
|
57 |
+
grids.forEach(grid => {
|
58 |
+
grid.className = window.innerWidth < 768 ? 'grid grid-cols-3 gap-4' : 'grid grid-cols-6 gap-4';
|
59 |
+
});
|
60 |
+
}
|
61 |
+
|
62 |
+
async function query(data) {
|
63 |
+
console.log("Sending request to worker with data:", data);
|
64 |
+
const requestData = {
|
65 |
+
prompt: data.inputs,
|
66 |
+
negative_prompt: "bad art, ugly, watermark, deformed", // adicione um valor aqui se necessário
|
67 |
+
sync_mode: 1 // ativa o modo síncrono
|
68 |
+
};
|
69 |
+
|
70 |
+
const response = await fetch(
|
71 |
+
"https://requesteracessibili.joaovitorkas13.workers.dev", // URL do seu Worker
|
72 |
+
{
|
73 |
+
method: "POST",
|
74 |
+
headers: {
|
75 |
+
'Content-Type': 'application/json'
|
76 |
+
},
|
77 |
+
body: JSON.stringify(requestData)
|
78 |
+
}
|
79 |
+
);
|
80 |
+
console.log("Received response from worker");
|
81 |
+
if (!response.ok) {
|
82 |
+
throw new Error(`Worker call failed: ${response.status}`);
|
83 |
+
}
|
84 |
+
const result = await response.json(); // Alterado para json
|
85 |
+
console.log("Received JSON from worker");
|
86 |
+
return result;
|
87 |
+
}
|
88 |
+
|
89 |
+
|
90 |
+
function toggleButtonState(button, isLoading) {
|
91 |
+
button.style.backgroundColor = isLoading ? '#FFA500' : '#007BFF';
|
92 |
+
button.disabled = isLoading;
|
93 |
+
button.ariaBusy = isLoading;
|
94 |
+
document.querySelector('.loading-container').classList.toggle('hidden', !isLoading);
|
95 |
+
const imageElement = document.querySelector('.generated-image');
|
96 |
+
if (imageElement) {
|
97 |
+
imageElement.style.display = isLoading ? 'none' : 'block';
|
98 |
+
}
|
99 |
+
}
|
100 |
+
|
101 |
+
document.addEventListener('DOMContentLoaded', function () {
|
102 |
+
document.querySelectorAll('.emoji-thumbnail').forEach((thumbnail, index) => {
|
103 |
+
thumbnail.addEventListener('click', () => {
|
104 |
+
const currentCategory = document.getElementById(`emoji-category-${index + 1}`);
|
105 |
+
|
106 |
+
// Toggle the visibility of the current category
|
107 |
+
if (currentCategory.style.display === 'none' || currentCategory.style.display === '') {
|
108 |
+
// Hide all other categories
|
109 |
+
document.querySelectorAll('.emoji-category').forEach(cat => {
|
110 |
+
cat.style.display = 'none';
|
111 |
+
});
|
112 |
+
// Show the clicked category
|
113 |
+
currentCategory.style.display = 'block';
|
114 |
+
} else {
|
115 |
+
// If the category is already visible, hide it
|
116 |
+
currentCategory.style.display = 'none';
|
117 |
+
}
|
118 |
+
});
|
119 |
+
});
|
120 |
+
});
|
121 |
+
|
122 |
+
function handleGenerateClick() {
|
123 |
+
const button = document.getElementById('generate-btn');
|
124 |
+
button.addEventListener('click', async function() {
|
125 |
+
toggleButtonState(this, true);
|
126 |
+
|
127 |
+
// Obter emojis selecionados
|
128 |
+
const selectedEmojis = document.getElementById('selected-emojis').value;
|
129 |
+
|
130 |
+
// Mapear emojis para texto correspondente
|
131 |
+
const mappedText = mapEmojisToText(selectedEmojis);
|
132 |
+
|
133 |
+
if (!mappedText.trim()) {
|
134 |
+
alert("Please select some emojis first!");
|
135 |
+
toggleButtonState(this, false);
|
136 |
+
return;
|
137 |
+
}
|
138 |
+
|
139 |
+
try {
|
140 |
+
// Enviar texto mapeado para a API
|
141 |
+
const response = await query({ "inputs": ` ${mappedText}` });
|
142 |
+
displayGeneratedImage(response);
|
143 |
+
} catch (error) {
|
144 |
+
console.error("Error calling the API: ", error);
|
145 |
+
alert("Failed to generate image. Please try again.");
|
146 |
+
} finally {
|
147 |
+
toggleButtonState(this, false);
|
148 |
+
}
|
149 |
+
});
|
150 |
+
}
|
151 |
+
|
152 |
+
|
153 |
+
function displayGeneratedImage(responseData) {
|
154 |
+
if (responseData.images && responseData.images.length > 0) {
|
155 |
+
const imageUrl = responseData.images[0].url;
|
156 |
+
const imageElement = document.querySelector('.generated-image');
|
157 |
+
if (imageElement) {
|
158 |
+
imageElement.src = imageUrl;
|
159 |
+
imageElement.style.display = 'block';
|
160 |
+
} else {
|
161 |
+
console.error('Elemento de imagem não encontrado.');
|
162 |
+
}
|
163 |
+
} else {
|
164 |
+
console.error('Nenhuma imagem encontrada na resposta.');
|
165 |
+
}
|
166 |
+
}
|
167 |
+
|
168 |
+
|
169 |
+
function setupSpeechRecognition() {
|
170 |
+
const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
|
171 |
+
|
172 |
+
if (SpeechRecognition) {
|
173 |
+
const recognition = new SpeechRecognition();
|
174 |
+
recognition.lang = navigator.language || 'en-US';
|
175 |
+
recognition.interimResults = false;
|
176 |
+
|
177 |
+
const startSpeechBtn = document.getElementById('start-speech-recognition');
|
178 |
+
const selectedEmojisInput = document.getElementById('selected-emojis');
|
179 |
+
|
180 |
+
startSpeechBtn.addEventListener('click', () => {
|
181 |
+
if (startSpeechBtn.classList.contains('listening')) {
|
182 |
+
recognition.stop();
|
183 |
+
startSpeechBtn.classList.remove('listening');
|
184 |
+
} else {
|
185 |
+
recognition.start();
|
186 |
+
startSpeechBtn.classList.add('listening');
|
187 |
+
}
|
188 |
+
});
|
189 |
+
|
190 |
+
recognition.addEventListener('result', (event) => {
|
191 |
+
const transcript = event.results[0][0].transcript;
|
192 |
+
selectedEmojisInput.value = transcript;
|
193 |
+
});
|
194 |
+
|
195 |
+
recognition.addEventListener('end', () => {
|
196 |
+
startSpeechBtn.classList.remove('listening');
|
197 |
+
});
|
198 |
+
} else {
|
199 |
+
console.error("Your browser does not support speech recognition.");
|
200 |
+
}
|
201 |
+
}
|
202 |
+
|
203 |
+
|
204 |
+
document.addEventListener('DOMContentLoaded', function () {
|
205 |
+
// Hide the generated image initially
|
206 |
+
const imageElement = document.querySelector('.generated-image');
|
207 |
+
if (imageElement) {
|
208 |
+
imageElement.style.display = 'none';
|
209 |
+
}
|
210 |
+
|
211 |
+
document.querySelectorAll('.emoji-thumbnail').forEach(handleEmojiClick);
|
212 |
+
document.querySelectorAll('.emoji-category').forEach(handleCategoryClick);
|
213 |
+
window.addEventListener('load', adjustEmojiGrid);
|
214 |
+
window.addEventListener('resize', adjustEmojiGrid);
|
215 |
+
handleGenerateClick();
|
216 |
+
setupSpeechRecognition();
|
217 |
+
});
|