document.addEventListener('DOMContentLoaded', () => {
// State
let features = [
{ icon: 'fa-bolt', title: '极速构建', desc: '无需编写代码,所见即所得,快速生成高品质页面。' },
{ icon: 'fa-mobile-screen', title: '响应式设计', desc: '完美适配手机、平板和桌面端,随时随地展示。' },
{ icon: 'fa-code', title: '代码导出', desc: '支持一键导出标准 HTML/Tailwind 代码,方便二次开发。' }
];
// DOM Elements
const inputs = {
brand: document.getElementById('input-brand'),
headline: document.getElementById('input-headline'),
subhead: document.getElementById('input-subhead'),
ctaText: document.getElementById('input-cta-text'),
ctaLink: document.getElementById('input-cta-link'),
copyright: document.getElementById('input-copyright')
};
const featuresContainer = document.getElementById('features-container');
const addFeatureBtn = document.getElementById('add-feature-btn');
const previewFrame = document.getElementById('preview-frame');
const themeRadios = document.querySelectorAll('input[name="theme"]');
const downloadBtn = document.getElementById('downloadBtn');
// Tab Switching
document.querySelectorAll('.tab-btn').forEach(btn => {
btn.addEventListener('click', (e) => {
document.querySelectorAll('.tab-btn').forEach(b => {
b.classList.remove('active', 'bg-indigo-100', 'text-indigo-700');
b.classList.add('text-gray-500', 'hover:bg-gray-100');
});
e.target.classList.add('active', 'bg-indigo-100', 'text-indigo-700');
e.target.classList.remove('text-gray-500', 'hover:bg-gray-100');
document.querySelectorAll('.tab-pane').forEach(p => p.classList.add('hidden'));
document.getElementById(`tab-${e.target.dataset.tab}`).classList.remove('hidden');
});
});
// Feature Management
function renderFeaturesList() {
featuresContainer.innerHTML = '';
features.forEach((feature, index) => {
const div = document.createElement('div');
div.className = 'bg-white border border-gray-200 rounded-lg p-3 relative group';
div.innerHTML = `
`;
featuresContainer.appendChild(div);
});
// Add event listeners for dynamic inputs
document.querySelectorAll('.feature-icon').forEach(el => el.addEventListener('input', (e) => {
features[e.target.dataset.index].icon = e.target.value;
updatePreview();
}));
document.querySelectorAll('.feature-title').forEach(el => el.addEventListener('input', (e) => {
features[e.target.dataset.index].title = e.target.value;
updatePreview();
}));
document.querySelectorAll('.feature-desc').forEach(el => el.addEventListener('input', (e) => {
features[e.target.dataset.index].desc = e.target.value;
updatePreview();
}));
document.querySelectorAll('.delete-feature').forEach(el => el.addEventListener('click', (e) => {
features.splice(e.currentTarget.dataset.index, 1);
renderFeaturesList();
updatePreview();
}));
}
addFeatureBtn.addEventListener('click', () => {
features.push({ icon: 'fa-star', title: '新特性', desc: '描述这个特性的优势...' });
renderFeaturesList();
updatePreview();
});
// Core Generation Logic
function getThemeColors(theme) {
switch(theme) {
case 'dark':
return {
bg: 'bg-gray-900',
text: 'text-white',
primary: 'bg-indigo-600 hover:bg-indigo-700',
cardBg: 'bg-gray-800',
cardText: 'text-gray-300',
navBg: 'bg-gray-900/90',
footerBg: 'bg-gray-950'
};
case 'minimal':
return {
bg: 'bg-white',
text: 'text-gray-900',
primary: 'bg-black hover:bg-gray-800',
cardBg: 'bg-gray-50',
cardText: 'text-gray-600',
navBg: 'bg-white/90',
footerBg: 'bg-gray-100'
};
case 'startup':
return {
bg: 'bg-white',
text: 'text-gray-800',
primary: 'bg-emerald-500 hover:bg-emerald-600',
cardBg: 'bg-white shadow-lg border border-gray-100',
cardText: 'text-gray-600',
navBg: 'bg-white/90',
footerBg: 'bg-gray-50'
};
case 'modern':
default:
return {
bg: 'bg-white',
text: 'text-gray-900',
primary: 'bg-blue-600 hover:bg-blue-700',
cardBg: 'bg-white shadow-md border border-gray-100',
cardText: 'text-gray-500',
navBg: 'bg-white/90',
footerBg: 'bg-gray-50'
};
}
}
function generateHTML() {
const selectedTheme = document.querySelector('input[name="theme"]:checked').value;
const colors = getThemeColors(selectedTheme);
const data = {
brand: inputs.brand.value,
headline: inputs.headline.value,
subhead: inputs.subhead.value,
ctaText: inputs.ctaText.value,
ctaLink: inputs.ctaLink.value,
copyright: inputs.copyright.value
};
const featuresHTML = features.map(f => `
`).join('');
return `
${data.brand}
`;
}
function updatePreview() {
const html = generateHTML();
const iframe = document.getElementById('preview-frame');
iframe.srcdoc = html;
}
// Initialize
renderFeaturesList();
updatePreview();
// Event Listeners
Object.values(inputs).forEach(input => {
input.addEventListener('input', updatePreview);
});
themeRadios.forEach(radio => {
radio.addEventListener('change', updatePreview);
});
// Download Logic
downloadBtn.addEventListener('click', () => {
const zip = new JSZip();
const html = generateHTML();
zip.file("index.html", html);
zip.file("README.txt", "Generated by One-Page Builder.\nDeploy this index.html anywhere (GitHub Pages, Vercel, Netlify).");
zip.generateAsync({type:"blob"})
.then(function(content) {
saveAs(content, "website-export.zip");
});
});
});