Spaces:
Running
Running
Update index.html
Browse files- index.html +90 -144
index.html
CHANGED
|
@@ -48,14 +48,13 @@
|
|
| 48 |
}
|
| 49 |
.container { max-width: 820px; width: 100%; }
|
| 50 |
|
| 51 |
-
/* START: Changes for Header Animation */
|
| 52 |
header {
|
| 53 |
-
position: relative;
|
| 54 |
text-align: center;
|
| 55 |
margin-bottom: 2.5rem;
|
| 56 |
-
padding: 2rem 0;
|
| 57 |
animation: fadeIn 0.8s 0.1s ease-out backwards;
|
| 58 |
-
overflow: hidden;
|
| 59 |
}
|
| 60 |
#neural-network-canvas {
|
| 61 |
position: absolute;
|
|
@@ -63,13 +62,12 @@
|
|
| 63 |
left: 0;
|
| 64 |
width: 100%;
|
| 65 |
height: 100%;
|
| 66 |
-
z-index: 1;
|
| 67 |
}
|
| 68 |
h1, .subtitle {
|
| 69 |
-
position: relative;
|
| 70 |
z-index: 2;
|
| 71 |
}
|
| 72 |
-
/* END: Changes for Header Animation */
|
| 73 |
|
| 74 |
h1 {
|
| 75 |
font-size: 2.8rem;
|
|
@@ -236,7 +234,7 @@
|
|
| 236 |
</header>
|
| 237 |
|
| 238 |
<main>
|
| 239 |
-
<!-- ...
|
| 240 |
<div class="form-group">
|
| 241 |
<div class="form-label">
|
| 242 |
<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>
|
|
@@ -252,7 +250,6 @@
|
|
| 252 |
</label>
|
| 253 |
<input type="file" id="file-input" accept="image/*" hidden>
|
| 254 |
</div>
|
| 255 |
-
|
| 256 |
<div class="form-group">
|
| 257 |
<label for="prompt-input" class="form-label">
|
| 258 |
<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="M17 3a2.85 2.83 0 1 1 4 4L7.5 20.5 2 22l1.5-5.5Z"/><path d="m15 5 4 4"/></svg>
|
|
@@ -266,7 +263,6 @@
|
|
| 266 |
</button>
|
| 267 |
<p id="error-message"></p>
|
| 268 |
</div>
|
| 269 |
-
|
| 270 |
<div class="form-group">
|
| 271 |
<div class="form-label">
|
| 272 |
<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="M10 3L12 8L17 10L12 12L10 17L8 12L3 10L8 8L10 3z"/><path d="M21 14l-1.5 3-3-1.5 3-3 1.5 3z"/><path d="M19.5 2.5l-3 1.5 1.5 3 3-1.5-1.5-3z"/></svg>
|
|
@@ -284,7 +280,6 @@
|
|
| 284 |
</div>
|
| 285 |
</div>
|
| 286 |
</main>
|
| 287 |
-
|
| 288 |
<section class="gallery-section">
|
| 289 |
<div class="gallery-header">
|
| 290 |
<div class="form-label">
|
|
@@ -299,7 +294,6 @@
|
|
| 299 |
<div id="history-grid"></div>
|
| 300 |
</section>
|
| 301 |
</div>
|
| 302 |
-
|
| 303 |
<div id="lightbox">
|
| 304 |
<div id="lightbox-content">
|
| 305 |
<img id="lightbox-img" src="">
|
|
@@ -313,7 +307,6 @@
|
|
| 313 |
</button>
|
| 314 |
</div>
|
| 315 |
</div>
|
| 316 |
-
|
| 317 |
<div id="confirmation-modal">
|
| 318 |
<div class="modal-dialog">
|
| 319 |
<div class="modal-icon">
|
|
@@ -331,151 +324,104 @@
|
|
| 331 |
</div>
|
| 332 |
|
| 333 |
<script>
|
| 334 |
-
// ---
|
|
|
|
|
|
|
| 335 |
const API_URL = 'https://alfa-editor-worker.onrender.com/api/edit';
|
| 336 |
-
const uploadArea = document.getElementById('upload-area');
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
const
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
|
| 356 |
-
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
|
| 362 |
-
|
| 363 |
-
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
|
| 367 |
-
|
| 368 |
-
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
const
|
| 373 |
-
|
| 374 |
-
|
| 375 |
-
|
| 376 |
-
|
| 377 |
-
|
| 378 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 379 |
document.addEventListener('DOMContentLoaded', () => { renderHistory(); checkFormState(); });
|
| 380 |
-
removeFileBtn.addEventListener('click', (e) => { e.preventDefault(); e.stopPropagation(); resetUploader(); });
|
| 381 |
-
fileInput.addEventListener('change', (e) => handleFile(e.target.files[0]));
|
| 382 |
-
['dragenter', 'dragover', 'dragleave', 'drop'].forEach(e => { uploadArea.addEventListener(e, p => { p.preventDefault(); p.stopPropagation(); }); });
|
| 383 |
-
['dragenter', 'dragover'].forEach(e => { uploadArea.addEventListener(e, () => { if (!uploadArea.classList.contains('has-file')) uploadArea.classList.add('drag-over'); }); });
|
| 384 |
-
['dragleave', 'drop'].forEach(e => { uploadArea.addEventListener(e, () => uploadArea.classList.remove('drag-over')); });
|
| 385 |
-
uploadArea.addEventListener('drop', e => { if (!uploadArea.classList.contains('has-file')) handleFile(e.dataTransfer.files[0]); });
|
| 386 |
-
promptInput.addEventListener('input', checkFormState);
|
| 387 |
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); } });
|
| 388 |
-
lightboxClose.addEventListener('click', closeLightbox);
|
| 389 |
-
|
| 390 |
-
lightboxDownload.addEventListener('click', handleDownload);
|
| 391 |
-
lightboxNext.addEventListener('click', () => { currentLightboxIndex = (currentLightboxIndex + 1) % currentLightboxGroup.length; updateLightboxImage(); });
|
| 392 |
-
clearHistoryBtn.addEventListener('click', handleClearHistory);
|
| 393 |
</script>
|
| 394 |
|
| 395 |
<script>
|
| 396 |
-
// --- Header Animation Script (
|
| 397 |
document.addEventListener('DOMContentLoaded', () => {
|
| 398 |
const canvas = document.getElementById('neural-network-canvas');
|
| 399 |
const header = canvas.parentElement;
|
| 400 |
const ctx = canvas.getContext('2d');
|
| 401 |
-
|
| 402 |
let particles = [];
|
| 403 |
-
const particleCount =
|
| 404 |
-
const maxDistance = 150; // Increased connection distance
|
| 405 |
-
|
| 406 |
const computedStyles = getComputedStyle(document.documentElement);
|
| 407 |
const particleColor = computedStyles.getPropertyValue('--accent-primary').trim();
|
| 408 |
const lineColor = computedStyles.getPropertyValue('--text-tertiary').trim();
|
| 409 |
-
|
| 410 |
-
|
| 411 |
-
|
| 412 |
-
|
| 413 |
-
|
| 414 |
-
|
| 415 |
-
|
| 416 |
-
class Particle {
|
| 417 |
-
constructor() {
|
| 418 |
-
this.x = Math.random() * canvas.width;
|
| 419 |
-
this.y = Math.random() * canvas.height;
|
| 420 |
-
this.vx = (Math.random() - 0.5) * 0.3; // Slower speed
|
| 421 |
-
this.vy = (Math.random() - 0.5) * 0.3; // Slower speed
|
| 422 |
-
this.radius = 1.5;
|
| 423 |
-
}
|
| 424 |
-
update() {
|
| 425 |
-
this.x += this.vx;
|
| 426 |
-
this.y += this.vy;
|
| 427 |
-
if (this.x < 0 || this.x > canvas.width) this.vx *= -1;
|
| 428 |
-
if (this.y < 0 || this.y > canvas.height) this.vy *= -1;
|
| 429 |
-
}
|
| 430 |
-
draw() {
|
| 431 |
-
ctx.beginPath();
|
| 432 |
-
ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2);
|
| 433 |
-
ctx.fillStyle = particleColor;
|
| 434 |
-
ctx.fill();
|
| 435 |
-
}
|
| 436 |
-
}
|
| 437 |
-
|
| 438 |
-
function init() {
|
| 439 |
-
particles = [];
|
| 440 |
-
for (let i = 0; i < particleCount; i++) {
|
| 441 |
-
particles.push(new Particle());
|
| 442 |
-
}
|
| 443 |
-
}
|
| 444 |
-
|
| 445 |
-
function connectParticles() {
|
| 446 |
-
for (let i = 0; i < particles.length; i++) {
|
| 447 |
-
for (let j = i + 1; j < particles.length; j++) {
|
| 448 |
-
const dx = particles[i].x - particles[j].x;
|
| 449 |
-
const dy = particles[i].y - particles[j].y;
|
| 450 |
-
const distance = Math.sqrt(dx * dx + dy * dy);
|
| 451 |
-
|
| 452 |
-
if (distance < maxDistance) {
|
| 453 |
-
ctx.beginPath();
|
| 454 |
-
ctx.moveTo(particles[i].x, particles[i].y);
|
| 455 |
-
ctx.lineTo(particles[j].x, particles[j].y);
|
| 456 |
-
ctx.strokeStyle = lineColor;
|
| 457 |
-
ctx.lineWidth = 0.3; // Thinner lines
|
| 458 |
-
ctx.globalAlpha = 1 - distance / maxDistance;
|
| 459 |
-
ctx.stroke();
|
| 460 |
-
}
|
| 461 |
-
}
|
| 462 |
-
}
|
| 463 |
-
ctx.globalAlpha = 1;
|
| 464 |
-
}
|
| 465 |
-
|
| 466 |
-
function animate() {
|
| 467 |
-
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
| 468 |
-
particles.forEach(particle => {
|
| 469 |
-
particle.update();
|
| 470 |
-
particle.draw();
|
| 471 |
-
});
|
| 472 |
-
connectParticles();
|
| 473 |
-
requestAnimationFrame(animate);
|
| 474 |
-
}
|
| 475 |
-
|
| 476 |
-
window.addEventListener('resize', resizeCanvas);
|
| 477 |
-
resizeCanvas();
|
| 478 |
-
animate();
|
| 479 |
});
|
| 480 |
</script>
|
| 481 |
</body>
|
|
|
|
| 48 |
}
|
| 49 |
.container { max-width: 820px; width: 100%; }
|
| 50 |
|
|
|
|
| 51 |
header {
|
| 52 |
+
position: relative;
|
| 53 |
text-align: center;
|
| 54 |
margin-bottom: 2.5rem;
|
| 55 |
+
padding: 2rem 0;
|
| 56 |
animation: fadeIn 0.8s 0.1s ease-out backwards;
|
| 57 |
+
overflow: hidden;
|
| 58 |
}
|
| 59 |
#neural-network-canvas {
|
| 60 |
position: absolute;
|
|
|
|
| 62 |
left: 0;
|
| 63 |
width: 100%;
|
| 64 |
height: 100%;
|
| 65 |
+
z-index: 1;
|
| 66 |
}
|
| 67 |
h1, .subtitle {
|
| 68 |
+
position: relative;
|
| 69 |
z-index: 2;
|
| 70 |
}
|
|
|
|
| 71 |
|
| 72 |
h1 {
|
| 73 |
font-size: 2.8rem;
|
|
|
|
| 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>
|
|
|
|
| 250 |
</label>
|
| 251 |
<input type="file" id="file-input" accept="image/*" hidden>
|
| 252 |
</div>
|
|
|
|
| 253 |
<div class="form-group">
|
| 254 |
<label for="prompt-input" class="form-label">
|
| 255 |
<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="M17 3a2.85 2.83 0 1 1 4 4L7.5 20.5 2 22l1.5-5.5Z"/><path d="m15 5 4 4"/></svg>
|
|
|
|
| 263 |
</button>
|
| 264 |
<p id="error-message"></p>
|
| 265 |
</div>
|
|
|
|
| 266 |
<div class="form-group">
|
| 267 |
<div class="form-label">
|
| 268 |
<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="M10 3L12 8L17 10L12 12L10 17L8 12L3 10L8 8L10 3z"/><path d="M21 14l-1.5 3-3-1.5 3-3 1.5 3z"/><path d="M19.5 2.5l-3 1.5 1.5 3 3-1.5-1.5-3z"/></svg>
|
|
|
|
| 280 |
</div>
|
| 281 |
</div>
|
| 282 |
</main>
|
|
|
|
| 283 |
<section class="gallery-section">
|
| 284 |
<div class="gallery-header">
|
| 285 |
<div class="form-label">
|
|
|
|
| 294 |
<div id="history-grid"></div>
|
| 295 |
</section>
|
| 296 |
</div>
|
|
|
|
| 297 |
<div id="lightbox">
|
| 298 |
<div id="lightbox-content">
|
| 299 |
<img id="lightbox-img" src="">
|
|
|
|
| 307 |
</button>
|
| 308 |
</div>
|
| 309 |
</div>
|
|
|
|
| 310 |
<div id="confirmation-modal">
|
| 311 |
<div class="modal-dialog">
|
| 312 |
<div class="modal-icon">
|
|
|
|
| 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 ناموفق بود.'));
|
| 350 |
+
return;
|
| 351 |
+
}
|
| 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>
|
| 409 |
+
// --- Header Animation Script (Unchanged) ---
|
| 410 |
document.addEventListener('DOMContentLoaded', () => {
|
| 411 |
const canvas = document.getElementById('neural-network-canvas');
|
| 412 |
const header = canvas.parentElement;
|
| 413 |
const ctx = canvas.getContext('2d');
|
|
|
|
| 414 |
let particles = [];
|
| 415 |
+
const particleCount = 20; const maxDistance = 100;
|
|
|
|
|
|
|
| 416 |
const computedStyles = getComputedStyle(document.documentElement);
|
| 417 |
const particleColor = computedStyles.getPropertyValue('--accent-primary').trim();
|
| 418 |
const lineColor = computedStyles.getPropertyValue('--text-tertiary').trim();
|
| 419 |
+
function resizeCanvas() { canvas.width = header.clientWidth; canvas.height = header.clientHeight; init(); }
|
| 420 |
+
class Particle { constructor() { this.x = Math.random() * canvas.width; this.y = Math.random() * canvas.height; this.vx = (Math.random() - 0.5) * 0.3; this.vy = (Math.random() - 0.5) * 0.3; this.radius = 1.2; } update() { this.x += this.vx; this.y += this.vy; if (this.x < 0 || this.x > canvas.width) this.vx *= -1; if (this.y < 0 || this.y > canvas.height) this.vy *= -1; } draw() { ctx.beginPath(); ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2); ctx.fillStyle = particleColor; ctx.fill(); } }
|
| 421 |
+
function init() { particles = []; for (let i = 0; i < particleCount; i++) { particles.push(new Particle()); } }
|
| 422 |
+
function connectParticles() { for (let i = 0; i < particles.length; i++) { for (let j = i + 1; j < particles.length; j++) { const dx = particles[i].x - particles[j].x; const dy = particles[i].y - particles[j].y; const distance = Math.sqrt(dx * dx + dy * dy); if (distance < maxDistance) { ctx.beginPath(); ctx.moveTo(particles[i].x, particles[i].y); ctx.lineTo(particles[j].x, particles[j].y); ctx.strokeStyle = lineColor; ctx.lineWidth = 0.2; ctx.globalAlpha = 1 - distance / maxDistance; ctx.stroke(); } } } ctx.globalAlpha = 1; }
|
| 423 |
+
function animate() { ctx.clearRect(0, 0, canvas.width, canvas.height); particles.forEach(particle => { particle.update(); particle.draw(); }); connectParticles(); requestAnimationFrame(animate); }
|
| 424 |
+
window.addEventListener('resize', resizeCanvas); resizeCanvas(); animate();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 425 |
});
|
| 426 |
</script>
|
| 427 |
</body>
|