/* 白板管理页:拉取 /api/admin/whiteboards、渲染表格、删除、新建并跳转。 */ (function () { "use strict"; const { el, toast, fmtTime, api } = window.ZK; const listEl = document.getElementById("list"); const countEl = document.getElementById("count"); const refreshBtn = document.getElementById("refresh"); const newBtn = document.getElementById("newBtn"); refreshBtn.addEventListener("click", load); newBtn.addEventListener("click", () => { // 生成一个随机 board_id 并打开(访问即创建) const id = "b_" + Math.random().toString(36).slice(2, 10); window.open(`/whiteboard/${id}`, "_blank"); }); async function load() { listEl.innerHTML = '
加载中…
'; countEl.textContent = ""; try { const res = await api("/api/admin/whiteboards?limit=500&offset=0"); if (!res.ok) throw new Error("HTTP " + res.status); const body = await res.json(); render(body.items || []); countEl.textContent = `共 ${body.total} 个白板`; } catch (e) { listEl.innerHTML = '
加载失败:' + (e.message || e) + "
"; } } function render(items) { if (!items.length) { listEl.innerHTML = '
还没有白板。点「新建并打开」创建一个。
'; return; } const table = el("table", { class: "wb-admin-table" }); const thead = el("thead", null, el("tr", null, el("th", { class: "col-id" }, "白板 ID"), el("th", { class: "col-mods" }, "修改次数"), el("th", { class: "col-created" }, "创建时间"), el("th", { class: "col-updated" }, "上次修改"), el("th", { class: "col-act" }, "操作") ) ); const tbody = el("tbody", null); for (const b of items) { const row = el("tr", null, el("td", { class: "col-id" }, el("a", { class: "bid link", href: `/whiteboard/${b.board_id}`, target: "_blank" }, b.board_id) ), el("td", { class: "col-mods mono" }, String(b.stroke_count ?? 0)), el("td", { class: "col-created muted" }, fmtTime(b.created_at)), el("td", { class: "col-updated muted" }, fmtTime(b.updated_at)), el("td", { class: "col-act" }, el("button", { class: "btn danger", onclick: () => remove(b, row) }, "删除") ) ); tbody.appendChild(row); } table.appendChild(thead); table.appendChild(tbody); listEl.innerHTML = ""; listEl.appendChild(table); } async function remove(b, row) { if (!confirm(`确定删除白板「${b.board_id}」?\n所有在线协作者会被断开,内容不可恢复。`)) return; try { const res = await api(`/api/admin/whiteboards/${encodeURIComponent(b.board_id)}`, { method: "DELETE" }); if (res.status === 404) { toast("白板已不存在"); } else if (!res.ok) throw new Error("HTTP " + res.status); row.classList.add("row-removed"); setTimeout(() => row.remove(), 250); toast("已删除"); const m = (countEl.textContent || "").match(/(\d+)/); if (m) countEl.textContent = `共 ${Math.max(0, Number(m[1]) - 1)} 个白板`; } catch (e) { toast("删除失败:" + (e.message || e), "err"); } } load(); })();