diff --git a/js/admin.js b/js/admin.js
index 12ea827..0946946 100644
--- a/js/admin.js
+++ b/js/admin.js
@@ -214,18 +214,45 @@ function renderAdminSettings() {
const container = document.getElementById('adminContent');
container.innerHTML = `
+
+
+
+
+
+
+
+
🎨 自定义 CSS
+
上传 style.css 覆盖默认样式,自定义论坛外观。
+
+
+
+
+
+ 点击或拖拽上传 style.css
+
+
只能上传 style.css 文件
+
+
+
+
+
+
+
+
+
`;
+ // 加载当前论坛名称
API.getSettings().then(data => {
document.getElementById('settingsForumName').value = data.forum_name || 'Forumlify';
}).catch(() => {});
+ // 保存论坛名称
document.getElementById('settingsForumSave').addEventListener('click', async () => {
const name = document.getElementById('settingsForumName').value.trim();
if (!name) { alert('请输入论坛名称'); return; }
@@ -242,166 +269,167 @@ function renderAdminSettings() {
document.getElementById('settingsResult').style.color = '#ef4444';
}
});
-}
-// ============================================================
-// 📄 自定义页面管理
-// ============================================================
+ // 自定义 CSS 上传
+ const dropZone = document.getElementById('customCssDropZone');
+ const fileInput = document.getElementById('customCssInput');
+ const dropText = document.getElementById('customCssDropText');
+ const statusEl = document.getElementById('customCssStatus');
-let editingPageId = null;
+ let selectedFile = null;
-function renderAdminCustomPages() {
- const container = document.getElementById('adminContent');
- container.innerHTML = `
-
-
📄 自定义页面
-
-
-
- `;
+ if (dropZone && fileInput) {
+ dropZone.addEventListener('click', function() {
+ fileInput.click();
+ });
- loadCustomPageList();
+ fileInput.addEventListener('change', function() {
+ if (this.files.length > 0) {
+ handleCssFile(this.files[0]);
+ }
+ });
- document.getElementById('addCustomPageBtn').addEventListener('click', function() {
- editingPageId = null;
- openCustomPageEditor();
- });
-}
+ dropZone.addEventListener('dragover', function(e) {
+ e.preventDefault();
+ this.style.borderColor = 'var(--primary)';
+ this.style.background = 'var(--primary-bg)';
+ dropText.textContent = '松开上传';
+ });
-function loadCustomPageList() {
- const container = document.getElementById('customPageList');
- container.innerHTML = '
加载中...
';
- API.getAdminCustomPages().then(pages => {
- if (pages.length === 0) {
- container.innerHTML = '
暂无自定义页面
';
+ dropZone.addEventListener('dragleave', function(e) {
+ e.preventDefault();
+ this.style.borderColor = 'var(--border)';
+ this.style.background = 'var(--bg)';
+ dropText.textContent = '点击或拖拽上传 style.css';
+ });
+
+ dropZone.addEventListener('drop', function(e) {
+ e.preventDefault();
+ this.style.borderColor = 'var(--border)';
+ this.style.background = 'var(--bg)';
+ dropText.textContent = '点击或拖拽上传 style.css';
+ if (e.dataTransfer.files.length > 0) {
+ handleCssFile(e.dataTransfer.files[0]);
+ }
+ });
+ }
+
+ function handleCssFile(file) {
+ if (file.name !== 'style.css') {
+ statusEl.textContent = '❌ 文件名必须是 style.css';
+ statusEl.style.color = '#ef4444';
+ selectedFile = null;
+ fileInput.value = '';
return;
}
- let html = `
-
-
-
- | 名称 |
- 标题 |
- URL |
- 状态 |
- 操作 |
-
-
-
- `;
- pages.forEach(p => {
- html += `
-
- ${p.name} |
- ${p.title} |
- ?custom=${p.name} |
- ${p.enabled ? '✅ 启用' : '❌ 禁用'} |
-
-
-
- |
-
- `;
- });
- html += '
';
- container.innerHTML = html;
+ if (!file.type.includes('text/css') && !file.name.endsWith('.css')) {
+ statusEl.textContent = '❌ 请上传 CSS 文件';
+ statusEl.style.color = '#ef4444';
+ selectedFile = null;
+ fileInput.value = '';
+ return;
+ }
+ selectedFile = file;
+ statusEl.textContent = '✅ 已选择: ' + file.name + ' (' + (file.size / 1024).toFixed(1) + ' KB)';
+ statusEl.style.color = '#22c55e';
+ }
- container.querySelectorAll('[data-action="edit"]').forEach(btn => {
- btn.addEventListener('click', function() {
- const page = pages.find(p => p.id === this.dataset.id);
- if (page) {
- editingPageId = page.id;
- openCustomPageEditor(page);
- }
- });
- });
+ // 保存 CSS
+ document.getElementById('customCssSaveBtn').addEventListener('click', function() {
+ if (!selectedFile) {
+ statusEl.textContent = '⚠️ 请先选择 style.css 文件';
+ statusEl.style.color = '#f59e0b';
+ return;
+ }
- container.querySelectorAll('[data-action="delete"]').forEach(btn => {
- btn.addEventListener('click', function() {
- if (!confirm('确定要删除这个页面吗?')) return;
- API.deleteCustomPage(this.dataset.id).then(() => {
- loadCustomPageList();
- loadCustomPagesNav();
- }).catch(err => alert('删除失败:' + err.message));
- });
+ showCustomCssWarningModal(async function() {
+ try {
+ await API.uploadCustomCSS(selectedFile);
+ statusEl.textContent = '✅ CSS 上传成功!刷新页面查看效果';
+ statusEl.style.color = '#22c55e';
+ selectedFile = null;
+ fileInput.value = '';
+ dropText.textContent = '点击或拖拽上传 style.css';
+ loadCustomCSS();
+ } catch (err) {
+ statusEl.textContent = '❌ 上传失败:' + err.message;
+ statusEl.style.color = '#ef4444';
+ }
});
- }).catch(err => {
- container.innerHTML = '
加载失败
';
+ });
+
+ // 删除 CSS
+ document.getElementById('customCssDeleteBtn').addEventListener('click', async function() {
+ if (!confirm('确定要删除自定义 CSS 吗?将恢复默认样式。')) return;
+ try {
+ await API.deleteCustomCSS();
+ statusEl.textContent = '✅ 已删除自定义 CSS';
+ statusEl.style.color = '#22c55e';
+ const link = document.getElementById('customCssLink');
+ if (link) link.remove();
+ } catch (err) {
+ statusEl.textContent = '❌ 删除失败:' + err.message;
+ statusEl.style.color = '#ef4444';
+ }
});
}
-function openCustomPageEditor(page) {
- const isEdit = !!page;
+// ============================================================
+// 自定义 CSS 警告模态框
+// ============================================================
+
+function showCustomCssWarningModal(onConfirm) {
const modal = document.createElement('div');
modal.className = 'modal active';
modal.style.display = 'flex';
modal.innerHTML = `
-
-
×
-
${isEdit ? '✏️ 编辑页面' : '📄 添加页面'}
-
-
-
- ${isEdit ? '
⚠️ 名称不可修改
' : '
只允许字母、数字、短横线和下划线
'}
+
+
⚠️ 警告
+
+ 若上传的 style.css 存在问题,将导致整个论坛界面样式错乱,甚至无法正常使用。
+
+
+ 请确保 CSS 文件是完整的、经过测试的版本。如出现问题,可点击「删除自定义 CSS」恢复默认样式。
+
+
+
+
-
-
-
-
-
-
-
-
-
-
-
-
支持 HTML、CSS(<style>)、JS(<script>),内容会在独立的沙盒中渲染
-
-
`;
document.body.appendChild(modal);
- modal.querySelector('.close').addEventListener('click', function() {
+ modal.querySelector('#cssWarningConfirm').addEventListener('click', function() {
+ modal.remove();
+ if (onConfirm) onConfirm();
+ });
+
+ modal.querySelector('#cssWarningCancel').addEventListener('click', function() {
modal.remove();
});
+
modal.addEventListener('click', function(e) {
if (e.target === this) modal.remove();
});
+}
- modal.querySelector('#editorSaveBtn').addEventListener('click', async function() {
- const name = document.getElementById('editorPageName').value.trim();
- const title = document.getElementById('editorPageTitle').value.trim();
- const content = document.getElementById('editorContent').value.trim();
- const enabled = document.getElementById('editorEnabled').value === 'true';
+// ============================================================
+// 加载自定义 CSS
+// ============================================================
- if (!name) { alert('请输入页面名称'); return; }
- if (!title) { alert('请输入导航栏显示名称'); return; }
- if (!content) { alert('请输入页面内容'); return; }
- if (!/^[a-zA-Z0-9\-_]+$/.test(name)) {
- alert('页面名称只允许字母、数字、短横线和下划线');
- return;
- }
+function loadCustomCSS() {
+ const existing = document.getElementById('customCssLink');
+ if (existing) existing.remove();
- try {
- if (isEdit) {
- await API.updateCustomPage(page.id, title, content, enabled);
- } else {
- await API.createCustomPage(name, title, content);
- }
- modal.remove();
- loadCustomPageList();
- loadCustomPagesNav();
- } catch (err) {
- alert('保存失败:' + err.message);
- }
- });
+ const link = document.createElement('link');
+ link.id = 'customCssLink';
+ link.rel = 'stylesheet';
+ link.href = CONFIG.API_BASE_URL + '/custom-css?v=' + Date.now();
+ link.onerror = function() {
+ // 如果自定义 CSS 不存在,静默失败
+ };
+ document.head.appendChild(link);
}
// ============================================================
@@ -423,3 +451,8 @@ document.querySelectorAll('.admin-tab').forEach(tab => {
if (tabMap[this.dataset.tab]) tabMap[this.dataset.tab]();
});
});
+
+// 页面加载时尝试加载自定义 CSS
+document.addEventListener('DOMContentLoaded', function() {
+ setTimeout(loadCustomCSS, 500);
+});