-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscript.js
339 lines (293 loc) · 10.6 KB
/
script.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
let translations = {};
let currentLanguage = 'en';
let undoStack = []; // Stack for undo operations
let redoStack = []; // Stack for redo operations
// Load translations
async function loadTranslations() {
try {
const response = await fetch('translations.json');
translations = await response.json();
populateLanguageMenu();
applyLanguage();
} catch (error) {
console.error('Error loading translations:', error);
}
}
// Populate language menu
function populateLanguageMenu() {
const languageMenu = document.getElementById('language-menu');
languageMenu.innerHTML = '';
for (const lang in translations) {
const button = document.createElement('button');
button.textContent = `${translations[lang]['language_flag']} ${translations[lang]['language_name']}`;
button.onclick = () => {
changeLanguage(lang);
toggleLanguageMenu();
};
languageMenu.appendChild(button);
}
}
// Toggle language menu visibility
function toggleLanguageMenu() {
const languageMenu = document.getElementById('language-menu');
languageMenu.style.display = languageMenu.style.display === 'block' ? 'none' : 'block';
}
// Apply language
function applyLanguage() {
const savedLanguage = localStorage.getItem('language') || 'en';
changeLanguage(savedLanguage);
}
// Change language
function changeLanguage(lang) {
currentLanguage = lang;
localStorage.setItem('language', lang);
const texts = translations[lang];
// Update interface text
document.getElementById('title').textContent = texts['title'];
document.getElementById('prompt').placeholder = texts['prompt_placeholder'];
// Update action buttons
document.getElementById('copy-prompt-btn').title = texts['copy_prompt'];
document.getElementById('clear-prompt-btn').title = texts['clear_prompt'];
document.getElementById('random-prompt-btn').title = texts['generate_random_prompt'];
document.getElementById('undo-prompt-btn').title = texts['undo_prompt'];
document.getElementById('redo-prompt-btn').title = texts['redo_prompt'];
// Update current language abbreviation
document.getElementById('current-language').textContent = translations[lang]['language_abbr'];
// Update theme toggle icon
const currentTheme = document.documentElement.getAttribute('data-theme');
document.getElementById('theme-toggle').textContent =
currentTheme === 'dark' ? texts['switch_to_light'] : texts['switch_to_dark'];
// Reload keywords to update language
loadKeywords();
}
// Load keywords from language-specific JSON file
async function loadKeywords() {
try {
const response = await fetch(`keywords_${currentLanguage}.json`);
const data = await response.json();
displayDropdowns(data);
} catch (error) {
console.error('Error loading keywords:', error);
}
}
// Display dropdown menus with categories and keywords
function displayDropdowns(data) {
const container = document.getElementById('dropdown-groups');
container.innerHTML = '';
for (const category in data) {
const keywords = data[category];
const dropdownDiv = document.createElement('div');
dropdownDiv.classList.add('dropdown-group');
const label = document.createElement('label');
label.textContent = category;
dropdownDiv.appendChild(label);
const select = document.createElement('select');
select.onchange = () => addToPrompt(select.value);
const defaultOption = document.createElement('option');
defaultOption.value = '';
defaultOption.textContent = `${translations[currentLanguage]['select']} ${category}`;
select.appendChild(defaultOption);
keywords.forEach(keyword => {
const option = document.createElement('option');
option.value = keyword;
option.textContent = keyword;
select.appendChild(option);
});
dropdownDiv.appendChild(select);
container.appendChild(dropdownDiv);
}
}
// Save the current prompt to the undo stack and clear redo stack
function saveState() {
const promptTextarea = document.getElementById('prompt');
undoStack.push(promptTextarea.value);
// Limit undo stack size to 50
if (undoStack.length > 50) {
undoStack.shift();
}
redoStack = []; // Clear redo stack
}
// Add selected keyword to prompt and scroll to bottom
function addToPrompt(keyword) {
if (keyword) {
saveState();
const promptTextarea = document.getElementById('prompt');
promptTextarea.value += (promptTextarea.value ? ' ' : '') + keyword;
// Scroll to the bottom
promptTextarea.scrollTop = promptTextarea.scrollHeight;
}
}
// Copy prompt to clipboard
function copyPrompt() {
const promptTextarea = document.getElementById('prompt');
const copyButton = document.getElementById('copy-prompt-btn');
navigator.clipboard
.writeText(promptTextarea.value)
.then(() => {
// Visual feedback for success
copyButton.classList.remove('error');
copyButton.classList.add('success');
// Remove the class after the animation duration
setTimeout(() => {
copyButton.classList.remove('success');
}, 1000);
})
.catch(err => {
// Visual feedback for error
copyButton.classList.remove('success');
copyButton.classList.add('error');
setTimeout(() => {
copyButton.classList.remove('error');
}, 1000);
console.error('Failed to copy text: ', err);
});
}
// Clear the prompt
function clearPrompt() {
const promptTextarea = document.getElementById('prompt');
const clearButton = document.getElementById('clear-prompt-btn');
if (promptTextarea.value) {
saveState();
promptTextarea.value = '';
// Visual feedback for success
clearButton.classList.remove('error');
clearButton.classList.add('success');
setTimeout(() => {
clearButton.classList.remove('success');
}, 1000);
} else {
// Visual feedback for error
clearButton.classList.remove('success');
clearButton.classList.add('error');
setTimeout(() => {
clearButton.classList.remove('error');
}, 1000);
}
}
// Undo last action
function undoPrompt() {
const promptTextarea = document.getElementById('prompt');
const undoButton = document.getElementById('undo-prompt-btn');
if (undoStack.length > 0) {
redoStack.push(promptTextarea.value);
promptTextarea.value = undoStack.pop();
// Visual feedback for success
undoButton.classList.remove('error');
undoButton.classList.add('success');
setTimeout(() => {
undoButton.classList.remove('success');
}, 1000);
} else {
// Visual feedback for error
undoButton.classList.remove('success');
undoButton.classList.add('error');
setTimeout(() => {
undoButton.classList.remove('error');
}, 1000);
}
}
// Redo last undone action
function redoPrompt() {
const promptTextarea = document.getElementById('prompt');
const redoButton = document.getElementById('redo-prompt-btn');
if (redoStack.length > 0) {
undoStack.push(promptTextarea.value);
promptTextarea.value = redoStack.pop();
// Visual feedback for success
redoButton.classList.remove('error');
redoButton.classList.add('success');
setTimeout(() => {
redoButton.classList.remove('success');
}, 1000);
} else {
// Visual feedback for error
redoButton.classList.remove('success');
redoButton.classList.add('error');
setTimeout(() => {
redoButton.classList.remove('error');
}, 1000);
}
}
// Theme Toggle Functionality
function toggleTheme() {
const currentTheme = document.documentElement.getAttribute('data-theme');
const targetTheme = currentTheme === 'dark' ? 'light' : 'dark';
document.documentElement.setAttribute('data-theme', targetTheme);
localStorage.setItem('theme', targetTheme);
// Update the button icon
const texts = translations[currentLanguage];
document.getElementById('theme-toggle').textContent =
targetTheme === 'dark' ? texts['switch_to_light'] : texts['switch_to_dark'];
}
// Apply saved theme on load
function applyTheme() {
const savedTheme =
localStorage.getItem('theme') ||
(window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light');
document.documentElement.setAttribute('data-theme', savedTheme);
// Update the button icon
const texts = translations[currentLanguage];
document.getElementById('theme-toggle').textContent =
savedTheme === 'dark' ? texts['switch_to_light'] : texts['switch_to_dark'];
}
// Generate Random Prompt and scroll to bottom
function generateRandomPrompt() {
const randomButton = document.getElementById('random-prompt-btn');
fetch(`keywords_${currentLanguage}.json`)
.then(response => response.json())
.then(data => {
saveState();
const promptTextarea = document.getElementById('prompt');
const categories = Object.keys(data);
const allKeywords = [];
// Flatten all keywords into one array
categories.forEach(category => {
allKeywords.push(...data[category]);
});
// Shuffle the array
allKeywords.sort(() => 0.5 - Math.random());
// Decide on a random number of elements to add
const numberOfElements = Math.min(Math.floor(Math.random() * 5) + 3, allKeywords.length);
// Select the required number of unique keywords
const selectedKeywords = allKeywords.slice(0, numberOfElements);
// Add the selected keywords to the prompt
promptTextarea.value += (promptTextarea.value ? ' ' : '') + selectedKeywords.join(' ');
// Scroll to the bottom
promptTextarea.scrollTop = promptTextarea.scrollHeight;
// Visual feedback for success
randomButton.classList.remove('error');
randomButton.classList.add('success');
setTimeout(() => {
randomButton.classList.remove('success');
}, 1000);
// Visual feedback on prompt textarea
promptTextarea.classList.add('highlight');
setTimeout(() => {
promptTextarea.classList.remove('highlight');
}, 500);
})
.catch(error => {
console.error('Error generating random prompt:', error);
// Visual feedback for error
randomButton.classList.remove('success');
randomButton.classList.add('error');
setTimeout(() => {
randomButton.classList.remove('error');
}, 1000);
});
}
// Close language menu when clicking outside
window.onclick = function (event) {
if (!event.target.matches('#language-btn') && !event.target.matches('#current-language')) {
const languageMenu = document.getElementById('language-menu');
if (languageMenu.style.display === 'block') {
languageMenu.style.display = 'none';
}
}
};
// Initialize the application
window.onload = async function () {
await loadTranslations();
applyTheme();
await loadKeywords();
};