Certainly! I'll help you modify your existing code by removing the Download Image button and adding a Code Preview button similar to the one in your first code snippet. We'll integrate the necessary HTML, CSS, and JavaScript to ensure the new button functions correctly.
Here's the complete updated code:
<!DOCTYPE html>
<html lang="fa">
<head>
<meta charset="UTF-8">
<title>ابزار تبدیل تصویر به HTML</title>
<style>
/* تنظیمات پایه برای body و html */
html, body {
margin: 0;
padding: 0;
height: 100%;
overflow: hidden;
background-color: #f0f0f0;
font-family: Arial, sans-serif;
}
/* کانتینر iframe */
.iframe-container {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100vh;
overflow: hidden;
z-index: 1;
}
/* تنظیمات iframe */
.iframe-container iframe {
position: absolute;
top: -170px;
left: 0;
width: 100%;
height: calc(100% + 398px);
border: none;
display: none;
}
/* Overlay برای مخفی کردن بخشهای بالا و پایین */
.overlay-top, .overlay-bottom {
position: fixed;
width: 100%;
background-color: transparent;
z-index: 900;
pointer-events: none;
}
.overlay-top {
top: 0;
height: 170px;
}
.overlay-bottom {
bottom: 0;
height: 228px;
}
/* لودینگ */
.loading {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background-color: white;
padding: 20px;
border-radius: 5px;
z-index: 2000;
display: flex;
flex-direction: column;
align-items: center;
max-width: 90%;
box-sizing: border-box;
}
.loading-message {
margin-bottom: 10px;
font-size: 18px;
text-align: center;
}
.progress {
width: 80%;
max-width: 400px;
height: 20px;
background-color: #ddd;
border-radius: 5px;
overflow: hidden;
margin-bottom: 10px;
}
.progress-bar {
height: 100%;
width: 0;
background-color: #4caf50;
transition: width 0.25s;
}
.error-message {
display: none;
color: red;
text-align: center;
margin-bottom: 10px;
font-size: 16px;
}
#retryButton {
display: none;
padding: 10px 20px;
background-color: #4caf50;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
font-size: 16px;
}
#retryButton:hover {
background-color: #45a049;
}
/* دکمه نمایش کد */
.code-preview-button-top {
display: block;
width: 150px;
color: white;
text-align: center;
padding: 10px;
border-radius: 5px;
cursor: pointer;
font-size: 14px;
position: fixed;
top: 10px;
right: 10px;
z-index: 10001;
background-color: #4CAF50;
text-decoration: none;
transition: background-color 0.3s;
}
.code-preview-button-top:hover {
background-color: #45a049;
}
/* مدالها */
.modal {
display: none;
position: fixed;
z-index: 10002;
left: 0;
top: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.5);
overflow: auto;
}
.modal-content {
background-color: #fefefe;
margin: 5% auto;
padding: 20px;
border: 1px solid #888;
width: 90%;
max-width: 900px;
text-align: center;
border-radius: 5px;
position: relative;
}
.modal-content h2 {
margin-top: 0;
}
.code-area {
width: 100%;
height: 300px;
margin: 10px 0;
padding: 10px;
font-family: 'Courier New', Courier, monospace;
direction: ltr;
text-align: left;
resize: vertical;
font-size: 14px; /* کاهش سایز متن */
background-color: #2d2d2d; /* پسزمینه تیره برای کد */
color: #f8f8f2; /* رنگ متن کد */
border: 1px solid #444;
border-radius: 5px;
position: relative;
}
/* افزودن آیکون به کادر کد */
.code-area::before {
content: '💻';
position: absolute;
top: 10px;
left: 10px;
font-size: 20px;
}
.code-area::placeholder {
color: #bbb;
padding-left: 30px; /* فاصله برای آیکون */
}
.preview-area {
width: 100%;
height: 400px;
margin: 10px 0;
border: 1px solid #ccc;
overflow: auto;
background: white;
}
.modal-button {
margin: 5px;
padding: 10px 20px;
border: none;
border-radius: 5px;
cursor: pointer;
font-size: 14px;
color: white;
}
.preview-btn {
background-color: #2196F3;
}
.close-btn {
background-color: #f44336;
}
.modal-button:hover {
opacity: 0.8;
}
@media (max-width: 768px) {
.code-preview-button-top {
width: 120px;
padding: 8px;
font-size: 12px;
top: 10px;
right: 10px;
}
.modal-content {
width: 95%;
margin: 10% auto;
}
}
</style>
</head>
<body>
<!-- مدال توضیحات (اختیاری، میتوانید آن را اضافه کنید اگر نیاز دارید) -->
<!--
<div id="descriptionModal" class="modal">
<div class="modal-content">
<h2>توضیحات ابزار</h2>
<p>اینجا میتوانید توضیحات مربوط به ابزار خود را وارد کنید.</p>
<button class="modal-button close-btn" id="closeDescriptionBtn">بستن</button>
</div>
</div>
-->
<!-- مدال نمایش کد -->
<div id="codeModal" class="modal">
<div class="modal-content">
<h2>کد HTML تولید شده</h2>
<textarea id="codeArea" class="code-area" placeholder="کد تولید شده را برای نمایش اینجا قرار دهید"></textarea>
<button class="modal-button preview-btn" id="previewButton">نمایش صفحه</button>
<button class="modal-button close-btn" id="closeCodeBtn">بستن</button>
</div>
</div>
<!-- مدال پیشنمایش -->
<div id="previewModal" class="modal">
<div class="modal-content">
<h2>پیشنمایش صفحه</h2>
<div id="previewArea" class="preview-area"></div>
<button class="modal-button close-btn" id="closePreviewBtn">بستن</button>
</div>
</div>
<!-- دکمه نمایش کد -->
<a href="#" class="code-preview-button-top" id="codePreviewButtonTop">نمایشگر کد</a>
<!-- لودینگ -->
<div class="loading" id="loading">
<div class="loading-message">لطفاً صبر کنید...</div>
<div class="progress">
<div class="progress-bar" id="progressBar"></div>
</div>
<div class="error-message" id="errorMessage">بارگیری زیاد طول کشید.<br />در صورت عدم بارگذاری، فیلترشکن خود را روشن کنید.</div>
<button id="retryButton">بارگذاری مجدد</button>
</div>
<!-- کانتینر iframe -->
<div class="iframe-container">
<iframe id="myIframe" src="https://l-ai-gemini-ui-generator.hf.space" scrolling="yes"></iframe>
</div>
<!-- Overlay برای مخفی کردن بخشهای بالا و پایین -->
<div class="overlay-top" id="overlayTop"></div>
<div class="overlay-bottom" id="overlayBottom"></div>
<!-- اسکریپتها -->
<script>
let progress = 0;
let initialLoadCompleted = false;
const progressBar = document.getElementById('progressBar');
const retryButton = document.getElementById('retryButton');
const errorMessage = document.getElementById('errorMessage');
const loading = document.getElementById('loading');
const iframe = document.getElementById('myIframe');
let progressInterval;
let errorTimeout;
// عناصر مدال
const codeModal = document.getElementById('codeModal');
const previewModal = document.getElementById('previewModal');
const codeArea = document.getElementById('codeArea');
const previewArea = document.getElementById('previewArea');
// دکمهها
const codePreviewButtonTop = document.getElementById('codePreviewButtonTop');
const closeCodeBtn = document.getElementById('closeCodeBtn');
const closePreviewBtn = document.getElementById('closePreviewBtn');
const previewButton = document.getElementById('previewButton');
function startProgress() {
if (initialLoadCompleted) return;
progressInterval = setInterval(() => {
if (progress < 100) {
progress += 10;
if (progress > 100) progress = 100;
progressBar.style.width = progress + '%';
}
if (progress >= 100 && !initialLoadCompleted) {
clearInterval(progressInterval);
showError();
}
}, 1000);
errorTimeout = setTimeout(() => {
if (!initialLoadCompleted) {
showError();
}
}, 10000);
}
function iframeLoaded() {
if (initialLoadCompleted) return;
initialLoadCompleted = true;
clearInterval(progressInterval);
clearTimeout(errorTimeout);
loading.style.display = 'none';
iframe.style.display = 'block';
}
function iframeError() {
if (initialLoadCompleted) return;
clearInterval(progressInterval);
clearTimeout(errorTimeout);
showError();
}
function showError() {
errorMessage.style.display = 'block';
retryButton.style.display = 'block';
}
function resetLoading() {
progress = 0;
progressBar.style.width = '0%';
initialLoadCompleted = false;
loading.style.display = 'flex';
errorMessage.style.display = 'none';
retryButton.style.display = 'none';
iframe.style.display = 'none';
iframe.addEventListener('load', iframeLoaded, { once: true });
iframe.addEventListener('error', iframeError, { once: true });
}
function loadIframe() {
iframe.src = iframe.src;
}
retryButton.addEventListener('click', () => {
resetLoading();
loadIframe();
startProgress();
});
iframe.addEventListener('load', iframeLoaded, { once: true });
iframe.addEventListener('error', iframeError, { once: true });
startProgress();
// دکمه نمایش کد
codePreviewButtonTop.addEventListener('click', (e) => {
e.preventDefault();
// در اینجا معمولاً کد HTML تولید شده را به دست میآورید
// برای نمایش، از پیام جایگزین استفاده میکنیم
// به عنوان نمونه، کد iframe فعلی را نمایش میدهیم
codeArea.value = `
<!DOCTYPE html>
<html lang="fa">
<head>
<meta charset="UTF-8">
<title>ابزار تبدیل تصویر به HTML</title>
<!-- استایلها و سایر عناصر -->
</head>
<body>
<!-- محتویات iframe -->
<iframe src="https://l-ai-gemini-ui-generator.hf.space" scrolling="yes"></iframe>
</body>
</html>
`.trim();
codeModal.style.display = 'block';
});
previewButton.addEventListener('click', () => {
const code = codeArea.value.trim();
if (code) {
previewArea.innerHTML = code;
codeModal.style.display = 'none';
previewModal.style.display = 'block';
} else {
alert('لطفاً کد HTML را وارد کنید.');
}
});
// دکمههای بستن مدالها
closeCodeBtn.addEventListener('click', () => {
codeModal.style.display = 'none';
});
closePreviewBtn.addEventListener('click', () => {
previewModal.style.display = 'none';
});
// بستن مدال با کلیک خارج از آنها
window.addEventListener('click', (event) => {
if (event.target === codeModal) {
codeModal.style.display = 'none';
}
if (event.target === previewModal) {
previewModal.style.display = 'none';
}
});
</script>
</body>
</html>
توضیحات تغییرات:
-
حذف دکمه دانلود تصویر:
- HTML: دکمه دانلود (
<a href="#" class="download-button" id="downloadButton">دانلود تصویر 📥</a>) به طور کامل از کد حذف شده است.
- CSS: استایل مربوط به
.download-button نیز حذف شده است.
-
اضافه کردن دکمه نمایش کد:
- HTML: دکمه نمایش کد (
<a href="#" class="code-preview-button-top" id="codePreviewButtonTop">نمایشگر کد</a>) اضافه شده است.
- CSS: استایل مربوط به
.code-preview-button-top اضافه شده و تنظیمات مناسب مانند رنگ، موقعیت قرارگیری و واکنش بر روی اندازه صفحه نمایش داده شدهاند.
-
مدالهای جدید برای نمایش کد و پیشنمایش:
- HTML:
- مدال نمایش کد (
codeModal): شامل یک textarea برای نمایش کد و دو دکمه برای نمایش پیشنمایش و بستن مدال است.
- مدال پیشنمایش (
previewModal): شامل یک div برای نمایش پیشنمایش کد و یک دکمه برای بستن مدال است.
- CSS: استایلهای مربوط به مدالها، محتوای مدالها و دکمهها اضافه شده است.
-
اسکریپتهای جدید برای مدیریت دکمه نمایش کد و مدالها:
- دکمه نمایش کد (
codePreviewButtonTop): با کلیک بر روی این دکمه، مدال نمایش کد باز میشود و کد HTML نمونهای در textarea قرار میگیرد.
- دکمه نمایش پیشنمایش (
previewButton): با کلیک بر روی این دکمه، کد وارد شده در textarea گرفته شده و در مدال پیشنمایش نمایش داده میشود.
- دکمههای بستن مدالها: اضافه شدهاند تا مدالها را ببندند.
- بستن مدال با کلیک خارج از آنها: اگر کاربر روی پسزمینه مدال کلیک کند، مدال بسته میشود.
-
نمونه کد HTML در textarea:
- در این مثال، من یک نمونه کد HTML ساده را در textarea قرار دادهام. شما میتوانید این قسمت را با کد واقعی که میخواهید نمایش دهید جایگزین کنید یا از طریق JavaScript کد دینامیک را به دست آورید.
-
حذف قسمت مربوط به توضیحات:
- اگر نیاز به مدال توضیحات دارید، میتوانید بخش مربوط به مدال توضیحات را از طریق HTML و جاوااسکریپت فعال کنید. در این کد، این بخش به صورت کامنت گذاشته شده است.
نکات اضافی:
- سازگاری با موبایل: با استفاده از @media query، دکمه نمایش کد در اندازههای کوچکتر بهینه شده است.
- بهبود تجربه کاربری: با افزودن افکتهای hover و انتقال نرم برای پیشنمایشهای CSS و JavaScript، تجربه کاربری بهتری ارائه شده است.
- امنیت: مطمئن شوید که کدهای نمایش داده شده امن هستند و از آسیبپذیریهای احتمالی مانند XSS جلوگیری میشود.
اگر نیاز به تغییرات بیشتری داشتید یا سوال دیگری داشتید، خوشحال میشوم که کمک کنم!