Elias207 commited on
Commit
0f01a39
·
verified ·
1 Parent(s): dce075b

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +128 -43
index.html CHANGED
@@ -142,22 +142,122 @@
142
  #loading-placeholder { display: none; }
143
  #result-container.loading #loading-placeholder { display: flex; animation: fadeIn 0.5s; justify-content: center; align-items: center; width: 100%; }
144
 
145
- .generator-container { position: relative; width: 400px; max-width: 100%; height: 300px; border: 2px solid #38bdf8; border-radius: 20px; overflow: hidden; box-shadow: 0 0 40px rgba(56, 189, 248, 0.3); animation: pulse 5s infinite cubic-bezier(0.4, 0, 0.6, 1); background-color: #161b22; color: #f0f6fc; }
146
- @keyframes pulse { 0% { box-shadow: 0 0 40px rgba(56, 189, 248, 0.3); } 50% { box-shadow: 0 0 60px rgba(56, 189, 248, 0.7); } 100% { box-shadow: 0 0 40px rgba(56, 189, 248, 0.3); } }
147
- .noise-layer, .sketch-layer, .building-layer { position: absolute; top: 0; left: 0; width: 100%; height: 100%; background: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100"><rect width="100" height="100" fill="none"/><filter id="noise"><feTurbulence type="fractalNoise" baseFrequency="0.5" numOctaves="4" stitchTiles="stitch"/></filter><rect width="100%" height="100%" filter="url(%23noise)" opacity="0.6"/></svg>') repeat; }
148
- .noise-layer { opacity: 1; animation: fade-noise 7s infinite ease-in-out; }
149
- @keyframes fade-noise { 0% { opacity: 1; filter: blur(5px); } 30% { opacity: 0.8; filter: blur(2px); } 100% { opacity: 0; filter: blur(0px); } }
150
- .sketch-layer { filter: grayscale(1) contrast(1.5) blur(3px); opacity: 0; animation: reveal-sketch 7s infinite ease-in-out; }
151
- @keyframes reveal-sketch { 0% { opacity: 0; filter: grayscale(1) contrast(1.5) blur(3px); } 20% { opacity: 1; filter: grayscale(1) contrast(1.2) blur(1px); } 60% { opacity: 0.5; filter: grayscale(0.5) contrast(1) blur(0px); } 100% { opacity: 0; } }
152
- .building-layer { filter: blur(15px) saturate(0.5) brightness(0.7); opacity: 0; animation: denoise-color 7s infinite ease-in-out; }
153
- @keyframes denoise-color { 0% { opacity: 0; filter: blur(15px) saturate(0.5) brightness(0.7); } 40% { opacity: 0.6; filter: blur(5px) saturate(1) brightness(1); } 100% { opacity: 1; filter: blur(0px) saturate(1.5) brightness(1.1); } }
154
- .pixel-grid { position: absolute; top: 0; left: 0; width: 100%; height: 100%; background: repeating-linear-gradient(0deg, transparent 0 1px, rgba(255,255,255,0.1) 1px 2px), repeating-linear-gradient(90deg, transparent 0 1px, rgba(255,255,255,0.1) 1px 2px); opacity: 1; animation: dissolve-grid 7s infinite ease-in-out; }
155
- @keyframes dissolve-grid { 0% { opacity: 1; } 70% { opacity: 0.5; } 100% { opacity: 0; } }
156
- .particles { position: absolute; top: 0; left: 0; width: 100%; height: 100%; background: radial-gradient(circle, rgba(56, 189, 248, 0.2) 0%, transparent 50%), radial-gradient(circle at 30% 70%, rgba(187, 134, 252, 0.2) 0%, transparent 50%), radial-gradient(circle at 70% 30%, rgba(250, 204, 21, 0.2) 0%, transparent 50%); animation: flow-particles 7s infinite cubic-bezier(0.4, 0, 0.6, 1); }
157
- @keyframes flow-particles { 0% { transform: translate(0, 0) scale(1); opacity: 0.5; } 50% { transform: translate(10px, -15px) scale(1.05); opacity: 0.8; } 100% { transform: translate(0, 0) scale(1); opacity: 0.5; } }
158
- .text-overlay { position: absolute; top: 45%; left: 50%; transform: translate(-50%, -50%); font-size: 24px; font-weight: 700; text-shadow: 0 0 20px rgba(56, 189, 248, 0.8), 0 0 30px rgba(187, 134, 252, 0.5); animation: glow-text 7s infinite ease-in-out; }
159
- @keyframes glow-text { 0% { opacity: 0.7; text-shadow: 0 0 20px rgba(56, 189, 248, 0.8); } 50% { opacity: 1; text-shadow: 0 0 30px rgba(56, 189, 248, 1), 0 0 40px rgba(187, 134, 252, 0.7); } 100% { opacity: 0.7; text-shadow: 0 0 20px rgba(56, 189, 248, 0.8); } }
160
- .progress-bar { position: absolute; bottom: 0; left: 0; width: 0%; height: 6px; background: linear-gradient(to right, #38bdf8, #bb86fc, #facc15); animation: progress 7s infinite linear; }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
161
 
162
  #result-grid { display: none; grid-template-columns: repeat(2, 1fr); gap: 1.5rem; width: 100%; }
163
  #result-container.has-content #result-grid { display: grid; }
@@ -234,7 +334,6 @@
234
  </header>
235
 
236
  <main>
237
- <!-- ... Main content ... -->
238
  <div class="form-group">
239
  <div class="form-label">
240
  <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M21 12v7a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h7"/><path d="M18.5 2.5a2.121 2.121 0 0 1 3 3L12 15l-4 1 1-4Z"/></svg>
@@ -269,13 +368,19 @@
269
  ۳. نتیجه را ببینید
270
  </div>
271
  <div id="result-container">
 
272
  <div id="loading-placeholder">
273
  <div class="generator-container">
274
- <div class="noise-layer"></div><div class="sketch-layer"></div><div class="building-layer"></div>
275
- <div class="pixel-grid"></div><div class="particles"></div>
276
- <div class="text-overlay">در حال ساخت تصویر...</div><div class="progress-bar"></div>
 
 
 
 
277
  </div>
278
  </div>
 
279
  <div id="result-grid"></div>
280
  </div>
281
  </div>
@@ -324,26 +429,20 @@
324
  </div>
325
 
326
  <script>
327
- // --- START OF APPLICATION LOGIC SCRIPT ---
328
-
329
- // ... (all selectors are the same) ...
330
  const API_URL = 'https://alfa-editor-worker.onrender.com/api/edit';
331
  const uploadArea = document.getElementById('upload-area'); const fileInput = document.getElementById('file-input'); const previewImage = document.getElementById('preview-image-main'); const removeFileBtn = document.getElementById('remove-file-btn-main'); const promptInput = document.getElementById('prompt-input'); const submitBtn = document.getElementById('submit-btn'); const resultContainer = document.getElementById('result-container'); const resultGrid = document.getElementById('result-grid'); const errorMessage = document.getElementById('error-message'); const lightbox = document.getElementById('lightbox'); const lightboxImg = document.getElementById('lightbox-img'); const lightboxClose = document.getElementById('lightbox-close'); const lightboxDownload = document.getElementById('lightbox-download'); const lightboxNext = document.getElementById('lightbox-next'); const historyGrid = document.getElementById('history-grid'); const clearHistoryBtn = document.getElementById('clear-history-btn'); const confirmationModal = document.getElementById('confirmation-modal'); const modalMessageText = document.getElementById('modal-message-text'); const modalConfirmBtn = document.getElementById('modal-confirm-btn'); const modalCancelBtn = document.getElementById('modal-cancel-btn');
332
  let uploadedFile = null; let currentLightboxUrl = null; let currentLightboxGroup = []; let currentLightboxIndex = 0;
333
-
334
- // --- NEW: Function to convert any image file to a PNG File object ---
335
  const convertToPNG = (file) => {
336
  return new Promise((resolve, reject) => {
337
  const image = new Image();
338
  const reader = new FileReader();
339
-
340
  image.onload = () => {
341
  const canvas = document.createElement('canvas');
342
  canvas.width = image.width;
343
  canvas.height = image.height;
344
  const ctx = canvas.getContext('2d');
345
  ctx.drawImage(image, 0, 0);
346
-
347
  canvas.toBlob((blob) => {
348
  if (!blob) {
349
  reject(new Error('تبدیل به PNG ناموفق بود.'));
@@ -352,57 +451,43 @@
352
  const originalName = file.name.substring(0, file.name.lastIndexOf('.')) || file.name;
353
  const pngFile = new File([blob], `${originalName}.png`, { type: 'image/png' });
354
  resolve(pngFile);
355
- }, 'image/png', 1.0); // 1.0 for max quality
356
  };
357
-
358
  image.onerror = () => reject(new Error('فایل تصویر قابل خواندن نیست.'));
359
  reader.onload = e => { image.src = e.target.result; };
360
  reader.onerror = () => reject(new Error('خطا در خواندن فایل.'));
361
-
362
  reader.readAsDataURL(file);
363
  });
364
  };
365
-
366
- // --- MODIFIED: The handleFile function is now async to handle conversion ---
367
  const handleFile = async (file) => {
368
  if (!file || !file.type.startsWith('image/')) {
369
  displayError('لطفا یک فایل تصویری معتبر انتخاب کنید.');
370
  return;
371
  }
372
-
373
  let fileToProcess = file;
374
- // Optimize: If the file is not PNG or JPEG, convert it.
375
  if (file.type !== 'image/png' && file.type !== 'image/jpeg') {
376
  try {
377
- // console.log(`Converting ${file.type} to PNG...`);
378
  fileToProcess = await convertToPNG(file);
379
  } catch (error) {
380
  console.error('Image conversion failed:', error);
381
  displayError('خطا در تبدیل فرمت تصویر. لطفا فایل دیگری را امتحان کنید.');
382
- resetUploader(); // Clear the uploader on failure
383
  return;
384
  }
385
  }
386
-
387
  uploadedFile = fileToProcess;
388
-
389
- // Use the original file for preview to avoid delay
390
  const reader = new FileReader();
391
  reader.onload = (e) => { previewImage.src = e.target.result; };
392
  reader.readAsDataURL(file);
393
-
394
  uploadArea.classList.add('has-file');
395
  checkFormState();
396
  clearResult();
397
  };
398
-
399
- // ... (The rest of the JS functions are unchanged) ...
400
  const showConfirmationModal = (message, onConfirm) => { modalMessageText.textContent = message; confirmationModal.classList.add('visible'); modalConfirmBtn.onclick = () => { onConfirm(); hideConfirmationModal(); }; modalCancelBtn.onclick = () => { hideConfirmationModal(); }; }; const hideConfirmationModal = () => { confirmationModal.classList.remove('visible'); modalConfirmBtn.onclick = null; modalCancelBtn.onclick = null; }; const resetUploader = () => { uploadedFile = null; fileInput.value = ''; previewImage.src = ''; uploadArea.classList.remove('has-file'); checkFormState(); }; const checkFormState = () => { submitBtn.disabled = !uploadedFile || promptInput.value.trim() === ''; }; const setLoading = (isLoading) => { const btnSpinner = submitBtn.querySelector('.spinner'); const btnText = document.getElementById('btn-text'); const btnIcon = submitBtn.querySelector('svg'); if (isLoading) { resultContainer.classList.remove('has-content'); resultContainer.classList.add('loading'); btnSpinner.style.display = 'inline-block'; btnIcon.style.display = 'none'; btnText.textContent = 'در حال پردازش...'; submitBtn.disabled = true; resultGrid.innerHTML = ''; errorMessage.style.display = 'none'; } else { resultContainer.classList.remove('loading'); btnSpinner.style.display = 'none'; btnIcon.style.display = 'inline-block'; btnText.textContent = 'خلق کن'; checkFormState(); } }; const displayResult = (imageUrls) => { resultGrid.innerHTML = ''; imageUrls.forEach((url) => { const img = document.createElement('img'); img.src = url; img.alt = 'تصویر ویرایش شده'; img.addEventListener('click', () => openLightbox(url, imageUrls)); resultGrid.appendChild(img); }); resultContainer.classList.add('has-content'); }; const clearResult = () => { resultGrid.innerHTML = ''; errorMessage.style.display = 'none'; resultContainer.classList.remove('has-content'); }; const displayError = (message) => { errorMessage.textContent = message; errorMessage.style.display = 'block'; }; const updateLightboxImage = () => { const newUrl = currentLightboxGroup[currentLightboxIndex]; lightboxImg.src = newUrl; currentLightboxUrl = newUrl; }; const openLightbox = (clickedUrl, urlGroup) => { currentLightboxGroup = urlGroup; currentLightboxIndex = urlGroup.indexOf(clickedUrl); updateLightboxImage(); lightboxNext.style.display = urlGroup.length > 1 ? 'flex' : 'none'; lightbox.classList.add('visible'); }; const closeLightbox = () => { lightbox.classList.remove('visible'); currentLightboxGroup = []; }; const handleDownload = async () => { if (!currentLightboxUrl) return; const spinner = lightboxDownload.querySelector('.spinner'); const icon = lightboxDownload.querySelector('svg'); spinner.style.display = 'block'; icon.style.display = 'none'; lightboxDownload.disabled = true; try { const response = await fetch(currentLightboxUrl); if (!response.ok) throw new Error('Network response was not ok.'); const blob = await response.blob(); const objectUrl = URL.createObjectURL(blob); const a = document.createElement('a'); a.href = objectUrl; a.download = `ai-edited-image-${Date.now()}.png`; document.body.appendChild(a); a.click(); document.body.removeChild(a); URL.revokeObjectURL(objectUrl); } catch (e) { console.error('Download failed:', e); alert('خطا در دانلود تصویر. لطفا دوباره تلاش کنید.'); } finally { spinner.style.display = 'none'; icon.style.display = 'block'; lightboxDownload.disabled = false; } }; const getHistory = () => JSON.parse(localStorage.getItem('aiPhotoshopHistory') || '[]'); const saveHistory = (history) => { localStorage.setItem('aiPhotoshopHistory', JSON.stringify(history)); renderHistory(); }; const addToHistory = (item) => { let history = getHistory(); history.unshift(item); if (history.length > 15) history.pop(); saveHistory(history); }; const handleClearHistory = () => { showConfirmationModal('آیا از پاک کردن تمام تاریخچه مطمئن هستید؟ این عمل غیرقابل بازگشت است.', () => { saveHistory([]); }); }; const handleDeleteItem = (index) => { showConfirmationModal('آیا از حذف این مورد از تاریخچه مطمئن هستید؟', () => { let history = getHistory(); history.splice(index, 1); saveHistory(history); }); }; const renderHistory = () => { const history = getHistory(); historyGrid.innerHTML = ''; clearHistoryBtn.style.display = history.length > 0 ? 'flex' : 'none'; history.forEach((item, index) => { const card = document.createElement('div'); card.className = 'history-item'; const deleteBtn = document.createElement('button'); deleteBtn.className = 'history-delete-btn'; deleteBtn.title = 'حذف این مورد'; deleteBtn.innerHTML = `<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M3 6h18"/><path d="M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6"/><path d="M8 6V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2"/></svg>`; deleteBtn.onclick = () => handleDeleteItem(index); card.appendChild(deleteBtn); const imageGrid = document.createElement('div'); imageGrid.className = 'history-item-grid'; item.urls.forEach(url => { const img = document.createElement('img'); img.src = url; img.alt = 'تصویر از تاریخچه'; img.addEventListener('click', () => openLightbox(url, item.urls)); imageGrid.appendChild(img); }); card.appendChild(imageGrid); historyGrid.appendChild(card); }); };
401
  document.addEventListener('DOMContentLoaded', () => { renderHistory(); checkFormState(); });
402
  removeFileBtn.addEventListener('click', (e) => { e.preventDefault(); e.stopPropagation(); resetUploader(); }); fileInput.addEventListener('change', (e) => handleFile(e.target.files[0])); ['dragenter', 'dragover', 'dragleave', 'drop'].forEach(e => { uploadArea.addEventListener(e, p => { p.preventDefault(); p.stopPropagation(); }); }); ['dragenter', 'dragover'].forEach(e => { uploadArea.addEventListener(e, () => { if (!uploadArea.classList.contains('has-file')) uploadArea.classList.add('drag-over'); }); }); ['dragleave', 'drop'].forEach(e => { uploadArea.addEventListener(e, () => uploadArea.classList.remove('drag-over')); }); uploadArea.addEventListener('drop', e => { if (!uploadArea.classList.contains('has-file')) handleFile(e.dataTransfer.files[0]); }); promptInput.addEventListener('input', checkFormState);
403
  submitBtn.addEventListener('click', async () => { if (submitBtn.disabled) return; setLoading(true); const formData = new FormData(); formData.append('image', uploadedFile); formData.append('prompt', promptInput.value.trim()); try { const response = await fetch(API_URL, { method: 'POST', body: formData }); if (!response.ok) { const errorData = await response.json().catch(() => ({ error: `خطای سرور: ${response.statusText}` })); throw new Error(errorData.error || `یک خطای ناشناخته رخ داد.`); } const result = await response.json(); if (result.image_urls && Array.isArray(result.image_urls) && result.image_urls.length > 0) { displayResult(result.image_urls); addToHistory({ prompt: promptInput.value.trim(), urls: result.image_urls }); } else { throw new Error('پاسخ معتبری از سرور دریافت نشد.'); } } catch (error) { displayError(error.message); } finally { setLoading(false); } });
404
  lightboxClose.addEventListener('click', closeLightbox); lightbox.addEventListener('click', (e) => { if (e.target === lightbox) closeLightbox(); }); lightboxDownload.addEventListener('click', handleDownload); lightboxNext.addEventListener('click', () => { currentLightboxIndex = (currentLightboxIndex + 1) % currentLightboxGroup.length; updateLightboxImage(); }); clearHistoryBtn.addEventListener('click', handleClearHistory);
405
-
406
  </script>
407
 
408
  <script>
 
142
  #loading-placeholder { display: none; }
143
  #result-container.loading #loading-placeholder { display: flex; animation: fadeIn 0.5s; justify-content: center; align-items: center; width: 100%; }
144
 
145
+ /* --- START: NEW LOADING ANIMATION STYLES --- */
146
+ .generator-container {
147
+ position: relative;
148
+ width: 400px;
149
+ max-width: 100%;
150
+ height: 300px;
151
+ border: 2px solid #38bdf8;
152
+ border-radius: 20px;
153
+ overflow: hidden;
154
+ box-shadow: 0 0 40px rgba(56, 189, 248, 0.3);
155
+ animation: pulse 5s infinite cubic-bezier(0.4, 0, 0.6, 1);
156
+ background-color: #161b22;
157
+ color: #f0f6fc;
158
+ }
159
+ @keyframes pulse {
160
+ 0% { box-shadow: 0 0 40px rgba(56, 189, 248, 0.3); }
161
+ 50% { box-shadow: 0 0 60px rgba(56, 189, 248, 0.7); }
162
+ 100% { box-shadow: 0 0 40px rgba(56, 189, 248, 0.3); }
163
+ }
164
+ .noise-layer, .sketch-layer, .building-layer {
165
+ position: absolute;
166
+ top: 0;
167
+ left: 0;
168
+ width: 100%;
169
+ height: 100%;
170
+ }
171
+ .noise-layer {
172
+ background: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100"><rect width="100" height="100" fill="none"/><filter id="noise"><feTurbulence type="fractalNoise" baseFrequency="0.5" numOctaves="4" stitchTiles="stitch"/></filter><rect width="100%" height="100%" filter="url(%23noise)" opacity="0.6"/></svg>') repeat;
173
+ opacity: 1;
174
+ animation: fade-noise 7s infinite ease-in-out;
175
+ }
176
+ @keyframes fade-noise {
177
+ 0% { opacity: 1; filter: blur(5px); }
178
+ 30% { opacity: 0.8; filter: blur(2px); }
179
+ 100% { opacity: 0; filter: blur(0px); }
180
+ }
181
+ .sketch-layer {
182
+ filter: grayscale(1) contrast(1.5) blur(3px);
183
+ opacity: 0;
184
+ animation: reveal-sketch 7s infinite ease-in-out;
185
+ }
186
+ @keyframes reveal-sketch {
187
+ 0% { opacity: 0; filter: grayscale(1) contrast(1.5) blur(3px); }
188
+ 20% { opacity: 1; filter: grayscale(1) contrast(1.2) blur(1px); }
189
+ 60% { opacity: 0.5; filter: grayscale(0.5) contrast(1) blur(0px); }
190
+ 100% { opacity: 0; }
191
+ }
192
+ .building-layer {
193
+ filter: blur(15px) saturate(0.5) brightness(0.7);
194
+ opacity: 0;
195
+ animation: denoise-color 7s infinite ease-in-out;
196
+ }
197
+ @keyframes denoise-color {
198
+ 0% { opacity: 0; filter: blur(15px) saturate(0.5) brightness(0.7); }
199
+ 40% { opacity: 0.6; filter: blur(5px) saturate(1) brightness(1); }
200
+ 100% { opacity: 1; filter: blur(0px) saturate(1.5) brightness(1.1); }
201
+ }
202
+ .pixel-grid {
203
+ position: absolute;
204
+ top: 0;
205
+ left: 0;
206
+ width: 100%;
207
+ height: 100%;
208
+ background: repeating-linear-gradient(0deg, transparent 0 1px, rgba(255,255,255,0.1) 1px 2px), repeating-linear-gradient(90deg, transparent 0 1px, rgba(255,255,255,0.1) 1px 2px);
209
+ opacity: 1;
210
+ animation: dissolve-grid 7s infinite ease-in-out;
211
+ }
212
+ @keyframes dissolve-grid {
213
+ 0% { opacity: 1; }
214
+ 70% { opacity: 0.5; }
215
+ 100% { opacity: 0; }
216
+ }
217
+ .particles {
218
+ position: absolute;
219
+ top: 0;
220
+ left: 0;
221
+ width: 100%;
222
+ height: 100%;
223
+ background: radial-gradient(circle, rgba(56, 189, 248, 0.2) 0%, transparent 50%), radial-gradient(circle at 30% 70%, rgba(187, 134, 252, 0.2) 0%, transparent 50%), radial-gradient(circle at 70% 30%, rgba(250, 204, 21, 0.2) 0%, transparent 50%);
224
+ animation: flow-particles 7s infinite cubic-bezier(0.4, 0, 0.6, 1);
225
+ }
226
+ @keyframes flow-particles {
227
+ 0% { transform: translate(0, 0) scale(1); opacity: 0.5; }
228
+ 50% { transform: translate(10px, -15px) scale(1.05); opacity: 0.8; }
229
+ 100% { transform: translate(0, 0) scale(1); opacity: 0.5; }
230
+ }
231
+ .text-overlay {
232
+ position: absolute;
233
+ top: 45%;
234
+ left: 50%;
235
+ transform: translate(-50%, -50%);
236
+ font-size: 24px;
237
+ font-weight: 700;
238
+ text-shadow: 0 0 20px rgba(56, 189, 248, 0.8), 0 0 30px rgba(187, 134, 252, 0.5);
239
+ animation: glow-text 7s infinite ease-in-out;
240
+ font-family: var(--app-font);
241
+ }
242
+ @keyframes glow-text {
243
+ 0% { opacity: 0.7; text-shadow: 0 0 20px rgba(56, 189, 248, 0.8); }
244
+ 50% { opacity: 1; text-shadow: 0 0 30px rgba(56, 189, 248, 1), 0 0 40px rgba(187, 134, 252, 0.7); }
245
+ 100% { opacity: 0.7; text-shadow: 0 0 20px rgba(56, 189, 248, 0.8); }
246
+ }
247
+ .progress-bar {
248
+ position: absolute;
249
+ bottom: 0;
250
+ left: 0;
251
+ width: 0%;
252
+ height: 6px;
253
+ background: linear-gradient(to right, #38bdf8, #bb86fc, #facc15);
254
+ animation: progress 7s infinite linear;
255
+ }
256
+ @keyframes progress {
257
+ 0% { width: 0%; }
258
+ 100% { width: 100%; }
259
+ }
260
+ /* --- END: NEW LOADING ANIMATION STYLES --- */
261
 
262
  #result-grid { display: none; grid-template-columns: repeat(2, 1fr); gap: 1.5rem; width: 100%; }
263
  #result-container.has-content #result-grid { display: grid; }
 
334
  </header>
335
 
336
  <main>
 
337
  <div class="form-group">
338
  <div class="form-label">
339
  <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M21 12v7a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h7"/><path d="M18.5 2.5a2.121 2.121 0 0 1 3 3L12 15l-4 1 1-4Z"/></svg>
 
368
  ۳. نتیجه را ببینید
369
  </div>
370
  <div id="result-container">
371
+ <!-- START: MODIFIED LOADING PLACEHOLDER -->
372
  <div id="loading-placeholder">
373
  <div class="generator-container">
374
+ <div class="noise-layer"></div>
375
+ <div class="sketch-layer"></div>
376
+ <div class="building-layer"></div>
377
+ <div class="pixel-grid"></div>
378
+ <div class="particles"></div>
379
+ <div class="text-overlay">در حال ساخت تصویر...</div>
380
+ <div class="progress-bar"></div>
381
  </div>
382
  </div>
383
+ <!-- END: MODIFIED LOADING PLACEHOLDER -->
384
  <div id="result-grid"></div>
385
  </div>
386
  </div>
 
429
  </div>
430
 
431
  <script>
432
+ // --- START OF APPLICATION LOGIC SCRIPT (Unchanged) ---
 
 
433
  const API_URL = 'https://alfa-editor-worker.onrender.com/api/edit';
434
  const uploadArea = document.getElementById('upload-area'); const fileInput = document.getElementById('file-input'); const previewImage = document.getElementById('preview-image-main'); const removeFileBtn = document.getElementById('remove-file-btn-main'); const promptInput = document.getElementById('prompt-input'); const submitBtn = document.getElementById('submit-btn'); const resultContainer = document.getElementById('result-container'); const resultGrid = document.getElementById('result-grid'); const errorMessage = document.getElementById('error-message'); const lightbox = document.getElementById('lightbox'); const lightboxImg = document.getElementById('lightbox-img'); const lightboxClose = document.getElementById('lightbox-close'); const lightboxDownload = document.getElementById('lightbox-download'); const lightboxNext = document.getElementById('lightbox-next'); const historyGrid = document.getElementById('history-grid'); const clearHistoryBtn = document.getElementById('clear-history-btn'); const confirmationModal = document.getElementById('confirmation-modal'); const modalMessageText = document.getElementById('modal-message-text'); const modalConfirmBtn = document.getElementById('modal-confirm-btn'); const modalCancelBtn = document.getElementById('modal-cancel-btn');
435
  let uploadedFile = null; let currentLightboxUrl = null; let currentLightboxGroup = []; let currentLightboxIndex = 0;
 
 
436
  const convertToPNG = (file) => {
437
  return new Promise((resolve, reject) => {
438
  const image = new Image();
439
  const reader = new FileReader();
 
440
  image.onload = () => {
441
  const canvas = document.createElement('canvas');
442
  canvas.width = image.width;
443
  canvas.height = image.height;
444
  const ctx = canvas.getContext('2d');
445
  ctx.drawImage(image, 0, 0);
 
446
  canvas.toBlob((blob) => {
447
  if (!blob) {
448
  reject(new Error('تبدیل به PNG ناموفق بود.'));
 
451
  const originalName = file.name.substring(0, file.name.lastIndexOf('.')) || file.name;
452
  const pngFile = new File([blob], `${originalName}.png`, { type: 'image/png' });
453
  resolve(pngFile);
454
+ }, 'image/png', 1.0);
455
  };
 
456
  image.onerror = () => reject(new Error('فایل تصویر قابل خواندن نیست.'));
457
  reader.onload = e => { image.src = e.target.result; };
458
  reader.onerror = () => reject(new Error('خطا در خواندن فایل.'));
 
459
  reader.readAsDataURL(file);
460
  });
461
  };
 
 
462
  const handleFile = async (file) => {
463
  if (!file || !file.type.startsWith('image/')) {
464
  displayError('لطفا یک فایل تصویری معتبر انتخاب کنید.');
465
  return;
466
  }
 
467
  let fileToProcess = file;
 
468
  if (file.type !== 'image/png' && file.type !== 'image/jpeg') {
469
  try {
 
470
  fileToProcess = await convertToPNG(file);
471
  } catch (error) {
472
  console.error('Image conversion failed:', error);
473
  displayError('خطا در تبدیل فرمت تصویر. لطفا فایل دیگری را امتحان کنید.');
474
+ resetUploader();
475
  return;
476
  }
477
  }
 
478
  uploadedFile = fileToProcess;
 
 
479
  const reader = new FileReader();
480
  reader.onload = (e) => { previewImage.src = e.target.result; };
481
  reader.readAsDataURL(file);
 
482
  uploadArea.classList.add('has-file');
483
  checkFormState();
484
  clearResult();
485
  };
 
 
486
  const showConfirmationModal = (message, onConfirm) => { modalMessageText.textContent = message; confirmationModal.classList.add('visible'); modalConfirmBtn.onclick = () => { onConfirm(); hideConfirmationModal(); }; modalCancelBtn.onclick = () => { hideConfirmationModal(); }; }; const hideConfirmationModal = () => { confirmationModal.classList.remove('visible'); modalConfirmBtn.onclick = null; modalCancelBtn.onclick = null; }; const resetUploader = () => { uploadedFile = null; fileInput.value = ''; previewImage.src = ''; uploadArea.classList.remove('has-file'); checkFormState(); }; const checkFormState = () => { submitBtn.disabled = !uploadedFile || promptInput.value.trim() === ''; }; const setLoading = (isLoading) => { const btnSpinner = submitBtn.querySelector('.spinner'); const btnText = document.getElementById('btn-text'); const btnIcon = submitBtn.querySelector('svg'); if (isLoading) { resultContainer.classList.remove('has-content'); resultContainer.classList.add('loading'); btnSpinner.style.display = 'inline-block'; btnIcon.style.display = 'none'; btnText.textContent = 'در حال پردازش...'; submitBtn.disabled = true; resultGrid.innerHTML = ''; errorMessage.style.display = 'none'; } else { resultContainer.classList.remove('loading'); btnSpinner.style.display = 'none'; btnIcon.style.display = 'inline-block'; btnText.textContent = 'خلق کن'; checkFormState(); } }; const displayResult = (imageUrls) => { resultGrid.innerHTML = ''; imageUrls.forEach((url) => { const img = document.createElement('img'); img.src = url; img.alt = 'تصویر ویرایش شده'; img.addEventListener('click', () => openLightbox(url, imageUrls)); resultGrid.appendChild(img); }); resultContainer.classList.add('has-content'); }; const clearResult = () => { resultGrid.innerHTML = ''; errorMessage.style.display = 'none'; resultContainer.classList.remove('has-content'); }; const displayError = (message) => { errorMessage.textContent = message; errorMessage.style.display = 'block'; }; const updateLightboxImage = () => { const newUrl = currentLightboxGroup[currentLightboxIndex]; lightboxImg.src = newUrl; currentLightboxUrl = newUrl; }; const openLightbox = (clickedUrl, urlGroup) => { currentLightboxGroup = urlGroup; currentLightboxIndex = urlGroup.indexOf(clickedUrl); updateLightboxImage(); lightboxNext.style.display = urlGroup.length > 1 ? 'flex' : 'none'; lightbox.classList.add('visible'); }; const closeLightbox = () => { lightbox.classList.remove('visible'); currentLightboxGroup = []; }; const handleDownload = async () => { if (!currentLightboxUrl) return; const spinner = lightboxDownload.querySelector('.spinner'); const icon = lightboxDownload.querySelector('svg'); spinner.style.display = 'block'; icon.style.display = 'none'; lightboxDownload.disabled = true; try { const response = await fetch(currentLightboxUrl); if (!response.ok) throw new Error('Network response was not ok.'); const blob = await response.blob(); const objectUrl = URL.createObjectURL(blob); const a = document.createElement('a'); a.href = objectUrl; a.download = `ai-edited-image-${Date.now()}.png`; document.body.appendChild(a); a.click(); document.body.removeChild(a); URL.revokeObjectURL(objectUrl); } catch (e) { console.error('Download failed:', e); alert('خطا در دانلود تصویر. لطفا دوباره تلاش کنید.'); } finally { spinner.style.display = 'none'; icon.style.display = 'block'; lightboxDownload.disabled = false; } }; const getHistory = () => JSON.parse(localStorage.getItem('aiPhotoshopHistory') || '[]'); const saveHistory = (history) => { localStorage.setItem('aiPhotoshopHistory', JSON.stringify(history)); renderHistory(); }; const addToHistory = (item) => { let history = getHistory(); history.unshift(item); if (history.length > 15) history.pop(); saveHistory(history); }; const handleClearHistory = () => { showConfirmationModal('آیا از پاک کردن تمام تاریخچه مطمئن هستید؟ این عمل غیرقابل بازگشت است.', () => { saveHistory([]); }); }; const handleDeleteItem = (index) => { showConfirmationModal('آیا از حذف این مورد از تاریخچه مطمئن هستید؟', () => { let history = getHistory(); history.splice(index, 1); saveHistory(history); }); }; const renderHistory = () => { const history = getHistory(); historyGrid.innerHTML = ''; clearHistoryBtn.style.display = history.length > 0 ? 'flex' : 'none'; history.forEach((item, index) => { const card = document.createElement('div'); card.className = 'history-item'; const deleteBtn = document.createElement('button'); deleteBtn.className = 'history-delete-btn'; deleteBtn.title = 'حذف این مورد'; deleteBtn.innerHTML = `<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M3 6h18"/><path d="M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6"/><path d="M8 6V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2"/></svg>`; deleteBtn.onclick = () => handleDeleteItem(index); card.appendChild(deleteBtn); const imageGrid = document.createElement('div'); imageGrid.className = 'history-item-grid'; item.urls.forEach(url => { const img = document.createElement('img'); img.src = url; img.alt = 'تصویر از تاریخچه'; img.addEventListener('click', () => openLightbox(url, item.urls)); imageGrid.appendChild(img); }); card.appendChild(imageGrid); historyGrid.appendChild(card); }); };
487
  document.addEventListener('DOMContentLoaded', () => { renderHistory(); checkFormState(); });
488
  removeFileBtn.addEventListener('click', (e) => { e.preventDefault(); e.stopPropagation(); resetUploader(); }); fileInput.addEventListener('change', (e) => handleFile(e.target.files[0])); ['dragenter', 'dragover', 'dragleave', 'drop'].forEach(e => { uploadArea.addEventListener(e, p => { p.preventDefault(); p.stopPropagation(); }); }); ['dragenter', 'dragover'].forEach(e => { uploadArea.addEventListener(e, () => { if (!uploadArea.classList.contains('has-file')) uploadArea.classList.add('drag-over'); }); }); ['dragleave', 'drop'].forEach(e => { uploadArea.addEventListener(e, () => uploadArea.classList.remove('drag-over')); }); uploadArea.addEventListener('drop', e => { if (!uploadArea.classList.contains('has-file')) handleFile(e.dataTransfer.files[0]); }); promptInput.addEventListener('input', checkFormState);
489
  submitBtn.addEventListener('click', async () => { if (submitBtn.disabled) return; setLoading(true); const formData = new FormData(); formData.append('image', uploadedFile); formData.append('prompt', promptInput.value.trim()); try { const response = await fetch(API_URL, { method: 'POST', body: formData }); if (!response.ok) { const errorData = await response.json().catch(() => ({ error: `خطای سرور: ${response.statusText}` })); throw new Error(errorData.error || `یک خطای ناشناخته رخ داد.`); } const result = await response.json(); if (result.image_urls && Array.isArray(result.image_urls) && result.image_urls.length > 0) { displayResult(result.image_urls); addToHistory({ prompt: promptInput.value.trim(), urls: result.image_urls }); } else { throw new Error('پاسخ معتبری از سرور دریافت نشد.'); } } catch (error) { displayError(error.message); } finally { setLoading(false); } });
490
  lightboxClose.addEventListener('click', closeLightbox); lightbox.addEventListener('click', (e) => { if (e.target === lightbox) closeLightbox(); }); lightboxDownload.addEventListener('click', handleDownload); lightboxNext.addEventListener('click', () => { currentLightboxIndex = (currentLightboxIndex + 1) % currentLightboxGroup.length; updateLightboxImage(); }); clearHistoryBtn.addEventListener('click', handleClearHistory);
 
491
  </script>
492
 
493
  <script>