原实现是 Canvas 画笔画板,与「文字白板/记事本」需求不符。重做为纯文本实时协作: - model:strokes JSON -> content TEXT + version INT(乐观锁)+ edit_count; schema/dao/service 同步重构,append_stroke/replace_strokes -> update_content。 - WS 协议:stroke -> edit(发完整文本,debounce 400ms);init 下发 content/version。 update 帧广播给他人,cleared 广播给所有人。 - 前端:canvas -> textarea;收到远端 update 用最长公共前后缀算变更区间, 仅替换该区间并保留光标(区间前不动/后平移/内移末尾);清空/复制文本按钮。 - schema.sql 更新 whiteboard 表 DDL;DB 旧表 DROP 重建(开发环境)。 - 测试脚本与 README 同步更新。
85 lines
3.2 KiB
JavaScript
85 lines
3.2 KiB
JavaScript
/* 白板管理页:拉取 /api/admin/wb、渲染表格、删除、新建并跳转。 */
|
|
(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(`/wb/${id}`, "_blank");
|
|
});
|
|
|
|
async function load() {
|
|
listEl.innerHTML = '<div class="skel">加载中…</div>';
|
|
countEl.textContent = "";
|
|
try {
|
|
const res = await api("/api/admin/wb?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 = '<div class="empty">加载失败:' + (e.message || e) + "</div>";
|
|
}
|
|
}
|
|
|
|
function render(items) {
|
|
if (!items.length) {
|
|
listEl.innerHTML = '<div class="empty">还没有白板。点「新建并打开」创建一个。</div>';
|
|
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: `/wb/${b.board_id}`, target: "_blank" }, b.board_id)
|
|
),
|
|
el("td", { class: "col-mods mono" }, String(b.edit_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/wb/${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();
|
|
})();
|