feat: add admin dashboard, pipeline service, lightbox, and update dependencies
This commit is contained in:
@@ -0,0 +1,156 @@
|
||||
/* 管理后台公共样式 — 全局链接,可被浏览器缓存 */
|
||||
/* 原 admin_styles.html 内容,改为独立 CSS 文件 */
|
||||
|
||||
/* ── Admin Shared ─────────────────────────────────────────────── */
|
||||
.admin-page { max-width:100%; }
|
||||
|
||||
/* subnav */
|
||||
.admin-subnav { display:flex; align-items:center; border-bottom:2px solid var(--border); margin-bottom:24px; }
|
||||
.admin-subnav-link { padding:10px 20px; font-size:.9rem; font-weight:500; color:var(--ink-light); border:none; border-bottom:2px solid transparent; margin-bottom:-2px; background:none; cursor:pointer; font-family:var(--font-sans); text-decoration:none; transition:color .2s,border-color .2s; }
|
||||
.admin-subnav-link:hover { color:var(--accent); text-decoration:none; }
|
||||
.admin-subnav-link.active { color:var(--accent); border-bottom-color:var(--accent); }
|
||||
.admin-subnav-spacer { flex:1; }
|
||||
.admin-subnav-form { margin:0; }
|
||||
.admin-subnav-logout { color:var(--ink-muted); font-weight:400; }
|
||||
.admin-subnav-logout:hover { color:#8c2828; }
|
||||
|
||||
/* tabs */
|
||||
.admin-tabs { display:flex; border-bottom:2px solid var(--border); margin-bottom:20px; }
|
||||
.admin-tab { padding:10px 24px; border:none; background:none; font-size:.9rem; font-weight:500; color:var(--ink-light); cursor:pointer; border-bottom:2px solid transparent; margin-bottom:-2px; transition:color .2s,border-color .2s; font-family:var(--font-sans); }
|
||||
.admin-tab:hover { color:var(--accent); }
|
||||
.admin-tab.active { color:var(--accent); border-bottom-color:var(--accent); }
|
||||
.admin-tab-content { display:none; }
|
||||
.admin-tab-content.active { display:block; }
|
||||
|
||||
/* table */
|
||||
.admin-table-wrap { overflow-x:auto; border:1px solid var(--border); border-radius:var(--radius); }
|
||||
.admin-table { width:100%; border-collapse:collapse; font-size:.85rem; background:var(--surface); }
|
||||
.admin-table th { padding:10px 12px; text-align:left; font-weight:600; color:var(--ink-light); background:var(--bg); border-bottom:1px solid var(--border); white-space:nowrap; }
|
||||
.admin-table td { padding:8px 12px; border-bottom:1px solid var(--border); color:var(--ink); vertical-align:middle; }
|
||||
.admin-table tbody tr:hover { background:var(--bg); }
|
||||
.admin-table tbody tr:last-child td { border-bottom:none; }
|
||||
.admin-table-compact { font-size:.8rem; }
|
||||
.admin-table-compact th, .admin-table-compact td { padding:6px 8px; }
|
||||
|
||||
/* badges */
|
||||
.task-badge, .status-badge { display:inline-block; padding:2px 8px; border-radius:3px; font-size:.75rem; font-weight:500; white-space:nowrap; }
|
||||
.task-crawl { background:#e3f2fd; color:#1565c0; }
|
||||
.task-summarize { background:#f3e5f5; color:#7b1fa2; }
|
||||
.task-cleanup { background:#e8f5e9; color:#2e7d32; }
|
||||
.task-delete { background:#fce4ec; color:#c62828; }
|
||||
.task-scheduler { background:#fff3e0; color:#e65100; }
|
||||
.status-success { background:#e8f5e9; color:#388e3c; }
|
||||
.status-running { background:#e3f2fd; color:#1976d2; }
|
||||
.status-failed { background:#fce4ec; color:#c62828; }
|
||||
.time-cell { white-space:nowrap; color:var(--ink-light); }
|
||||
.error-cell { max-width:200px; overflow:hidden; text-overflow:ellipsis; white-space:nowrap; color:#c62828; font-size:.8rem; }
|
||||
|
||||
/* action button */
|
||||
.admin-action-btn { display:inline-flex; align-items:center; gap:6px; padding:8px 18px; background:var(--surface); border:1px solid var(--border); border-radius:var(--radius); font-size:.85rem; font-weight:500; color:var(--ink); cursor:pointer; transition:all .2s; font-family:var(--font-sans); line-height:1.4; }
|
||||
.admin-action-btn:hover { border-color:var(--accent); color:var(--accent); box-shadow:0 2px 8px var(--shadow); }
|
||||
.admin-action-btn:active { transform:translateY(1px); box-shadow:none; }
|
||||
.admin-action-btn-sm { padding:5px 12px; font-size:.8rem; }
|
||||
.admin-action-btn-danger:hover { border-color:#8c2828; color:#8c2828; }
|
||||
|
||||
/* checkbox */
|
||||
.admin-check { appearance:none; -webkit-appearance:none; width:18px; height:18px; border:1.5px solid var(--border); border-radius:3px; background:var(--surface); cursor:pointer; vertical-align:middle; position:relative; transition:all .15s; }
|
||||
.admin-check:hover { border-color:var(--accent); }
|
||||
.admin-check:checked { background:var(--accent); border-color:var(--accent); }
|
||||
.admin-check:checked::after { content:''; position:absolute; top:2px; left:5px; width:5px; height:9px; border:solid #fff; border-width:0 2px 2px 0; transform:rotate(45deg); }
|
||||
|
||||
/* toast */
|
||||
.admin-toast { position:fixed; bottom:24px; left:50%; transform:translateX(-50%) translateY(20px); background:var(--ink); color:var(--surface); padding:12px 24px; border-radius:var(--radius); font-size:.88rem; z-index:9999; opacity:0; transition:opacity .3s,transform .3s; max-width:400px; text-align:center; pointer-events:none; }
|
||||
.admin-toast.show { opacity:1; transform:translateX(-50%) translateY(0); }
|
||||
|
||||
/* confirm dialog */
|
||||
.confirm-overlay { position:fixed; inset:0; background:rgba(0,0,0,.4); display:flex; align-items:center; justify-content:center; z-index:9999; }
|
||||
.confirm-dialog { background:var(--surface); border:1px solid var(--border); border-radius:var(--radius); padding:24px; max-width:400px; width:90%; box-shadow:0 8px 32px rgba(0,0,0,.15); }
|
||||
.confirm-msg { font-size:.95rem; color:var(--ink); margin-bottom:20px; line-height:1.6; }
|
||||
.confirm-actions { display:flex; justify-content:flex-end; gap:10px; }
|
||||
.confirm-btn { padding:8px 18px; border-radius:var(--radius); font-size:.85rem; font-weight:500; cursor:pointer; border:1px solid var(--border); font-family:var(--font-sans); transition:all .15s; }
|
||||
.confirm-btn-cancel { background:var(--surface); color:var(--ink-light); }
|
||||
.confirm-btn-cancel:hover { border-color:var(--ink-light); }
|
||||
.confirm-btn-ok { background:#8c2828; color:#fff; border-color:#8c2828; }
|
||||
.confirm-btn-ok:hover { background:#a13030; }
|
||||
|
||||
/* ── Dashboard ────────────────────────────────────────────────── */
|
||||
.stats-grid { display:grid; grid-template-columns:repeat(4,1fr); gap:16px; margin-bottom:24px; }
|
||||
.stat-card { background:var(--surface); border:1px solid var(--border); border-radius:var(--radius); padding:20px; text-align:center; }
|
||||
.stat-value { font-family:var(--font-body); font-size:2rem; font-weight:500; color:var(--accent); line-height:1.2; }
|
||||
.stat-warn { color:#7a6430; }
|
||||
.stat-danger { color:#8c2828; }
|
||||
.stat-label { font-size:.82rem; color:var(--ink-light); margin-top:4px; }
|
||||
.admin-quick-actions { display:flex; gap:10px; flex-wrap:wrap; margin-bottom:24px; }
|
||||
.admin-info-grid { display:grid; grid-template-columns:1fr 1fr; gap:20px; margin-bottom:24px; }
|
||||
.admin-info-card { background:var(--surface); border:1px solid var(--border); border-radius:var(--radius); padding:20px; }
|
||||
.admin-info-title { font-family:var(--font-body); font-size:1.05rem; font-weight:500; margin-bottom:16px; color:var(--ink); }
|
||||
.admin-info-body { display:flex; flex-direction:column; gap:10px; }
|
||||
.info-row { display:flex; align-items:center; gap:12px; }
|
||||
.info-label { font-size:.85rem; color:var(--ink-light); min-width:72px; flex-shrink:0; }
|
||||
.info-value { font-size:.88rem; color:var(--ink); display:flex; align-items:center; gap:6px; }
|
||||
.status-dot { display:inline-block; width:8px; height:8px; border-radius:50%; }
|
||||
.status-dot-on { background:#3d6e3d; }
|
||||
.status-dot-off { background:var(--ink-muted); }
|
||||
.scheduler-history { margin-top:20px; padding-top:16px; border-top:1px solid var(--border); }
|
||||
.section-subtitle { font-size:.9rem; font-weight:500; color:var(--ink-light); margin-bottom:10px; }
|
||||
.summary-dist { margin-top:20px; padding-top:16px; border-top:1px solid var(--border); }
|
||||
.summary-dist-bars { display:flex; flex-direction:column; gap:8px; }
|
||||
.dist-row { display:flex; align-items:center; gap:8px; }
|
||||
.dist-label { font-size:.8rem; color:var(--ink-light); min-width:60px; text-align:right; }
|
||||
.dist-bar-wrap { flex:1; height:16px; background:var(--bg); border-radius:4px; overflow:hidden; }
|
||||
.dist-bar { height:100%; border-radius:4px; min-width:2px; transition:width .3s; }
|
||||
.dist-bar-done { background:#3d6e3d; }
|
||||
.dist-bar-pending { background:#7a6430; }
|
||||
.dist-bar-running,.dist-bar-processing { background:var(--accent); }
|
||||
.dist-bar-failed,.dist-bar-permanent_failure { background:#8c2828; }
|
||||
.dist-bar-none { background:var(--ink-muted); }
|
||||
.dist-count { font-size:.8rem; color:var(--ink); font-variant-numeric:tabular-nums; min-width:28px; }
|
||||
.admin-section { margin-top:24px; }
|
||||
.admin-section-title { font-family:var(--font-body); font-size:1.1rem; font-weight:500; margin-bottom:12px; color:var(--ink); }
|
||||
|
||||
/* ── Logs: Summary ────────────────────────────────────────────── */
|
||||
.summary-filters { display:flex; align-items:center; gap:6px; flex-wrap:wrap; margin-bottom:12px; }
|
||||
.summary-filter-label { font-size:.85rem; color:var(--ink-light); }
|
||||
.summary-filters .filter-chip { padding:4px 10px; font-size:.8rem; background:var(--surface); border:1px solid var(--border); border-radius:4px; color:var(--ink-light); cursor:pointer; transition:all .2s; font-family:var(--font-sans); }
|
||||
.summary-filters .filter-chip:hover { border-color:var(--accent); color:var(--accent); }
|
||||
.summary-filters .filter-chip.active { background:var(--accent); color:#fff; border-color:var(--accent); }
|
||||
.summary-stats-row { display:flex; gap:16px; margin-bottom:16px; flex-wrap:wrap; }
|
||||
.summary-stat { font-size:.85rem; color:var(--ink-light); }
|
||||
.summary-stat strong { font-variant-numeric:tabular-nums; }
|
||||
.summary-stat-pending strong { color:#7a6430; }
|
||||
.summary-stat-failed strong { color:#8c2828; }
|
||||
.summary-stat-done strong { color:#3d6e3d; }
|
||||
.summary-table td.title-cell { max-width:300px; overflow:hidden; text-overflow:ellipsis; white-space:nowrap; }
|
||||
.retry-btn { padding:3px 10px; font-size:.75rem; background:var(--surface); border:1px solid var(--border); border-radius:4px; color:var(--accent); cursor:pointer; transition:all .2s; font-family:var(--font-sans); }
|
||||
.retry-btn:hover { border-color:var(--accent); background:var(--accent); color:#fff; }
|
||||
.retry-btn:disabled { opacity:.5; cursor:not-allowed; }
|
||||
.summary-batch-actions { margin-top:16px; padding-top:16px; border-top:1px solid var(--border); }
|
||||
.admin-actions { margin-top:32px; padding-top:20px; border-top:1px solid var(--border); }
|
||||
.admin-actions-title { font-family:var(--font-body); font-size:1.1rem; font-weight:600; margin-bottom:12px; color:var(--ink); }
|
||||
.admin-action-buttons { display:flex; gap:10px; flex-wrap:wrap; }
|
||||
|
||||
/* ── Papers ────────────────────────────────────────────────────── */
|
||||
.paper-search-form { margin-bottom:16px; }
|
||||
.paper-search-row { display:flex; gap:8px; flex-wrap:wrap; align-items:center; }
|
||||
.paper-search-input { flex:1; min-width:200px; padding:8px 14px; border:1px solid var(--border); border-radius:var(--radius); font-size:.85rem; font-family:var(--font-sans); background:var(--surface); color:var(--ink); }
|
||||
.paper-search-input:focus { outline:none; border-color:var(--accent); }
|
||||
.paper-filter-input { padding:8px 10px; border:1px solid var(--border); border-radius:var(--radius); font-size:.82rem; font-family:var(--font-sans); background:var(--surface); color:var(--ink); }
|
||||
.paper-filter-input:focus { outline:none; border-color:var(--accent); }
|
||||
.paper-search-btn { padding:8px 18px; background:var(--accent); color:#fff; border:none; border-radius:var(--radius); font-size:.85rem; font-weight:500; cursor:pointer; font-family:var(--font-sans); transition:background .2s; }
|
||||
.paper-search-btn:hover { background:var(--accent-hover); }
|
||||
.paper-batch-bar { display:flex; align-items:center; gap:12px; padding:10px 0; margin-bottom:8px; border-bottom:1px solid var(--border); }
|
||||
.paper-batch-label { font-size:.85rem; color:var(--ink-light); }
|
||||
.paper-selected-count { font-size:.82rem; color:var(--ink-muted); }
|
||||
.th-check { width:40px; text-align:center; }
|
||||
.title-cell { max-width:400px; overflow:hidden; text-overflow:ellipsis; white-space:nowrap; }
|
||||
.title-cell a { color:var(--ink); }
|
||||
.title-cell a:hover { color:var(--accent); }
|
||||
.action-cell { white-space:nowrap; }
|
||||
.action-btn-sm { display:inline-flex; align-items:center; justify-content:center; width:28px; height:28px; background:var(--surface); border:1px solid var(--border); border-radius:4px; font-size:.85rem; color:var(--ink-light); cursor:pointer; transition:all .15s; padding:0; vertical-align:middle; }
|
||||
.action-btn-sm:hover { border-color:var(--accent); color:var(--accent); }
|
||||
.action-btn-danger:hover { border-color:#8c2828; color:#8c2828; }
|
||||
|
||||
/* ── Responsive ────────────────────────────────────────────────── */
|
||||
@media (max-width:880px) { .stats-grid{grid-template-columns:repeat(2,1fr);} .admin-info-grid{grid-template-columns:1fr;} }
|
||||
@media (max-width:640px) { .admin-table{font-size:.8rem;} .admin-table th,.admin-table td{padding:6px 8px;} .admin-action-buttons{flex-direction:column;} .admin-action-btn{width:100%;justify-content:center;} .paper-search-row{flex-direction:column;} .paper-search-input,.paper-filter-input,.paper-search-btn{width:100%;} .paper-batch-bar{flex-wrap:wrap;gap:8px;} }
|
||||
@media (max-width:480px) { .stats-grid{grid-template-columns:1fr 1fr;} .stat-value{font-size:1.5rem;} .admin-quick-actions{flex-direction:column;} }
|
||||
@@ -1073,3 +1073,110 @@ mark {
|
||||
.motivation-block p {
|
||||
margin-bottom: 0.8rem;
|
||||
}
|
||||
|
||||
/* ── Login ──────────────────────────────────────────────────────── */
|
||||
|
||||
.login-page {
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
min-height: 60vh;
|
||||
padding: 40px 16px;
|
||||
}
|
||||
|
||||
.login-card {
|
||||
width: 100%;
|
||||
max-width: 400px;
|
||||
background: var(--surface);
|
||||
border: 1px solid var(--border);
|
||||
border-radius: var(--radius-lg);
|
||||
padding: 36px 32px;
|
||||
box-shadow: 0 4px 24px var(--shadow);
|
||||
}
|
||||
|
||||
.login-header {
|
||||
text-align: center;
|
||||
margin-bottom: 28px;
|
||||
}
|
||||
|
||||
.login-title {
|
||||
font-family: var(--font-body);
|
||||
font-size: 1.4rem;
|
||||
font-weight: 700;
|
||||
color: var(--ink);
|
||||
margin: 0 0 8px;
|
||||
}
|
||||
|
||||
.login-subtitle {
|
||||
font-size: 0.9rem;
|
||||
color: var(--ink-light);
|
||||
margin: 0;
|
||||
}
|
||||
|
||||
.login-error {
|
||||
background: #fce4ec;
|
||||
color: #c62828;
|
||||
padding: 10px 14px;
|
||||
border-radius: var(--radius);
|
||||
font-size: 0.85rem;
|
||||
margin-bottom: 20px;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.login-form {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 18px;
|
||||
}
|
||||
|
||||
.login-field label {
|
||||
display: block;
|
||||
font-size: 0.85rem;
|
||||
font-weight: 600;
|
||||
color: var(--ink);
|
||||
margin-bottom: 6px;
|
||||
}
|
||||
|
||||
.login-field input {
|
||||
width: 100%;
|
||||
padding: 10px 14px;
|
||||
border: 1px solid var(--border);
|
||||
border-radius: var(--radius);
|
||||
font-size: 0.9rem;
|
||||
font-family: var(--font-sans);
|
||||
background: var(--bg);
|
||||
color: var(--ink);
|
||||
transition: border-color 0.2s;
|
||||
box-sizing: border-box;
|
||||
}
|
||||
|
||||
.login-field input:focus {
|
||||
outline: none;
|
||||
border-color: var(--accent);
|
||||
box-shadow: 0 0 0 3px rgba(27, 54, 93, 0.1);
|
||||
}
|
||||
|
||||
.login-btn {
|
||||
width: 100%;
|
||||
padding: 12px;
|
||||
background: var(--accent);
|
||||
color: #fff;
|
||||
border: none;
|
||||
border-radius: var(--radius);
|
||||
font-size: 0.95rem;
|
||||
font-weight: 600;
|
||||
cursor: pointer;
|
||||
transition: background 0.2s;
|
||||
font-family: var(--font-sans);
|
||||
margin-top: 4px;
|
||||
}
|
||||
|
||||
.login-btn:hover {
|
||||
background: var(--accent-hover);
|
||||
}
|
||||
|
||||
@media (max-width: 480px) {
|
||||
.login-card {
|
||||
padding: 28px 20px;
|
||||
}
|
||||
}
|
||||
|
||||
+47
-2
@@ -1,11 +1,10 @@
|
||||
/* app.js — 基础前端交互 */
|
||||
/* app.js — 基础前端交互 + 管理后台共享工具 */
|
||||
|
||||
// Ctrl+K 或 / 聚焦搜索框
|
||||
document.addEventListener("keydown", function (e) {
|
||||
var input = document.querySelector(".nav-search-input");
|
||||
if (!input) return;
|
||||
|
||||
// 忽略在输入框内的按键
|
||||
if (e.target.tagName === "INPUT" || e.target.tagName === "TEXTAREA") return;
|
||||
|
||||
if ((e.ctrlKey || e.metaKey) && e.key === "k") {
|
||||
@@ -16,3 +15,49 @@ document.addEventListener("keydown", function (e) {
|
||||
input.focus();
|
||||
}
|
||||
});
|
||||
|
||||
// ── Toast 通知(管理后台共享)──────────────────────────────────────────
|
||||
|
||||
function showToast(msg, opts) {
|
||||
opts = opts || {};
|
||||
var duration = opts.duration || 2500;
|
||||
var callback = opts.callback || null;
|
||||
|
||||
var t = document.createElement("div");
|
||||
t.className = "admin-toast";
|
||||
t.textContent = String(msg).substring(0, 200);
|
||||
document.body.appendChild(t);
|
||||
requestAnimationFrame(function () { t.classList.add("show"); });
|
||||
setTimeout(function () {
|
||||
t.classList.remove("show");
|
||||
setTimeout(function () {
|
||||
t.remove();
|
||||
if (typeof callback === "function") callback();
|
||||
}, 300);
|
||||
}, duration);
|
||||
}
|
||||
|
||||
// ── Admin 通用操作(管理后台共享)───────────────────────────────────────
|
||||
|
||||
function adminAction(action, callback) {
|
||||
fetch("/admin/" + action, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
})
|
||||
.then(function (r) {
|
||||
if (r.status === 303 || r.status === 401) {
|
||||
window.location.href = "/admin/login";
|
||||
return;
|
||||
}
|
||||
return r.json();
|
||||
})
|
||||
.then(function (data) {
|
||||
if (data) {
|
||||
showToast(data.error ? "❌ " + data.error.substring(0, 200) : "✅ 操作成功");
|
||||
if (typeof callback === "function") callback(data);
|
||||
}
|
||||
})
|
||||
.catch(function (err) {
|
||||
showToast("❌ 请求失败");
|
||||
});
|
||||
}
|
||||
|
||||
@@ -0,0 +1,159 @@
|
||||
/* lightbox.js — 图片查看器:缩放、拖拽、键盘操作 */
|
||||
|
||||
(function() {
|
||||
function openLightbox(src, alt) {
|
||||
var existing = document.querySelector('.lightbox-overlay');
|
||||
if (existing) existing.remove();
|
||||
|
||||
var overlay = document.createElement('div');
|
||||
overlay.className = 'lightbox-overlay';
|
||||
|
||||
var img = document.createElement('img');
|
||||
img.src = src;
|
||||
img.alt = alt || '';
|
||||
img.draggable = false;
|
||||
|
||||
// 工具栏
|
||||
var toolbar = document.createElement('div');
|
||||
toolbar.className = 'lightbox-toolbar';
|
||||
toolbar.innerHTML =
|
||||
'<button title="缩小">−</button>' +
|
||||
'<button title="放大">+</button>' +
|
||||
'<button title="适合窗口">⊡</button>' +
|
||||
'<button title="原始大小">1:1</button>' +
|
||||
'<button title="关闭">✕</button>';
|
||||
|
||||
overlay.appendChild(img);
|
||||
overlay.appendChild(toolbar);
|
||||
document.body.appendChild(overlay);
|
||||
|
||||
// 视图状态
|
||||
var scale = 1, tx = 0, ty = 0;
|
||||
var baseW = 0, baseH = 0;
|
||||
var dragging = false, dragStartX = 0, dragStartY = 0, startTx = 0, startTy = 0;
|
||||
|
||||
function apply() {
|
||||
img.style.transform = 'translate(' + tx + 'px,' + ty + 'px) scale(' + scale + ')';
|
||||
}
|
||||
|
||||
function fitToScreen() {
|
||||
if (!baseW) return;
|
||||
var sw = window.innerWidth, sh = window.innerHeight;
|
||||
scale = Math.min(sw * 0.9 / baseW, sh * 0.9 / baseH, 1);
|
||||
tx = (sw - baseW * scale) / 2;
|
||||
ty = (sh - baseH * scale) / 2;
|
||||
apply();
|
||||
}
|
||||
|
||||
function resetOrigin() {
|
||||
scale = 1;
|
||||
tx = (window.innerWidth - baseW) / 2;
|
||||
ty = (window.innerHeight - baseH) / 2;
|
||||
apply();
|
||||
}
|
||||
|
||||
function zoomAt(factor, cx, cy) {
|
||||
var newScale = Math.max(0.1, Math.min(scale * factor, 20));
|
||||
tx = cx - (cx - tx) * (newScale / scale);
|
||||
ty = cy - (cy - ty) * (newScale / scale);
|
||||
scale = newScale;
|
||||
apply();
|
||||
}
|
||||
|
||||
function zoomCenter(factor) {
|
||||
var cx = window.innerWidth / 2;
|
||||
var cy = window.innerHeight / 2;
|
||||
var newScale = Math.max(0.1, Math.min(scale * factor, 20));
|
||||
tx = cx - (cx - tx) * (newScale / scale);
|
||||
ty = cy - (cy - ty) * (newScale / scale);
|
||||
scale = newScale;
|
||||
apply();
|
||||
}
|
||||
|
||||
// 图片加载后初始化
|
||||
img.onload = function() {
|
||||
baseW = img.naturalWidth;
|
||||
baseH = img.naturalHeight;
|
||||
fitToScreen();
|
||||
};
|
||||
// 如果已缓存
|
||||
if (img.complete && img.naturalWidth) {
|
||||
baseW = img.naturalWidth;
|
||||
baseH = img.naturalHeight;
|
||||
fitToScreen();
|
||||
}
|
||||
|
||||
// 工具栏按钮(缩小 / 放大 / 适合 / 原始 / 关闭)
|
||||
var btns = toolbar.querySelectorAll('button');
|
||||
btns[0].onclick = function(e) { e.stopPropagation(); zoomCenter(0.7); };
|
||||
btns[1].onclick = function(e) { e.stopPropagation(); zoomCenter(1.4); };
|
||||
btns[2].onclick = function(e) { e.stopPropagation(); fitToScreen(); };
|
||||
btns[3].onclick = function(e) { e.stopPropagation(); resetOrigin(); };
|
||||
btns[4].onclick = function(e) { e.stopPropagation(); close(); };
|
||||
|
||||
// 滚轮缩放(以鼠标为中心)
|
||||
overlay.addEventListener('wheel', function(e) {
|
||||
e.preventDefault();
|
||||
var factor = e.deltaY < 0 ? 1.15 : 0.87;
|
||||
var rect = overlay.getBoundingClientRect();
|
||||
var cx = e.clientX - rect.left;
|
||||
var cy = e.clientY - rect.top;
|
||||
var newScale = Math.max(0.1, Math.min(scale * factor, 20));
|
||||
tx = cx - (cx - tx) * (newScale / scale);
|
||||
ty = cy - (cy - ty) * (newScale / scale);
|
||||
scale = newScale;
|
||||
apply();
|
||||
}, { passive: false });
|
||||
|
||||
// 拖拽平移
|
||||
overlay.addEventListener('pointerdown', function(e) {
|
||||
if (e.target.closest('.lightbox-toolbar')) return;
|
||||
dragging = true;
|
||||
dragStartX = e.clientX;
|
||||
dragStartY = e.clientY;
|
||||
startTx = tx;
|
||||
startTy = ty;
|
||||
img.classList.add('dragging');
|
||||
overlay.setPointerCapture(e.pointerId);
|
||||
});
|
||||
overlay.addEventListener('pointermove', function(e) {
|
||||
if (!dragging) return;
|
||||
tx = startTx + (e.clientX - dragStartX);
|
||||
ty = startTy + (e.clientY - dragStartY);
|
||||
apply();
|
||||
});
|
||||
overlay.addEventListener('pointerup', function() {
|
||||
dragging = false;
|
||||
img.classList.remove('dragging');
|
||||
});
|
||||
|
||||
// ESC 关闭
|
||||
function onKey(e) {
|
||||
if (e.key === 'Escape') { close(); }
|
||||
else if (e.key === '+' || e.key === '=') { zoomCenter(1.4); }
|
||||
else if (e.key === '-') { zoomCenter(0.7); }
|
||||
else if (e.key === '0') { fitToScreen(); }
|
||||
}
|
||||
|
||||
function close() {
|
||||
overlay.remove();
|
||||
document.removeEventListener('keydown', onKey);
|
||||
}
|
||||
|
||||
document.addEventListener('keydown', onKey);
|
||||
|
||||
// 激活动画
|
||||
requestAnimationFrame(function() {
|
||||
overlay.classList.add('active');
|
||||
});
|
||||
}
|
||||
|
||||
document.addEventListener('click', function(e) {
|
||||
var img = e.target;
|
||||
if (img.tagName !== 'IMG') return;
|
||||
if (!img.closest('.inline-figure') && !img.closest('.gallery-item')) return;
|
||||
if (img.closest('.lightbox-overlay')) return;
|
||||
e.preventDefault();
|
||||
openLightbox(img.src, img.alt);
|
||||
});
|
||||
})();
|
||||
Reference in New Issue
Block a user