第二批小功能: 引用回复/复制消息/时间戳/主题切换/账号全量备份/置顶排序/归档/重命名/发送防连点/空态文案/音色试听 等

This commit is contained in:
zhiqui
2026-11-15 01:01:57 +08:00
parent 778854796a
commit 18ae7e1f9d
5 changed files with 165 additions and 12 deletions
+117 -10
View File
@@ -71,10 +71,24 @@ document.querySelectorAll(".nav-btn[data-page]").forEach(b =>
/* ---------- 书架 / 会话 ---------- */
async function loadShelf() {
const d = await api("/sessions");
$("#shelf-list").innerHTML = d.sessions.filter(s => s.mode === "roleplay").map(s =>
`<div class="mini-card" onclick="openChat(${s.id},'roleplay')">
<h4>${esc(s.title)}</h4><p><span class="tag">角色扮演</span></p></div>`).join("")
|| '<p class="hint">还没有会话,点右上角开一个。</p>';
const list = sortSessions(d.sessions.filter(s => !s.archived));
$("#shelf-list").innerHTML = list.map(s => {
const tag = { roleplay: "角色扮演", novel: "动态小说 · AI自主扩列", group: "群聊", call: "通话" }[s.mode] || s.mode;
return `<div class="mini-card" onclick="openChat(${s.id},'${s.mode}')">
<h4>${s.pinned ? "📌 " : ""}${esc(s.title)}</h4>
<p><span class="tag">${tag}</span></p>
<div class="row">
<button class="btn small" onclick="event.stopPropagation();renameSession(${s.id},'${esc(s.title)}')">✏️</button>
<button class="btn small" onclick="event.stopPropagation();archiveSession(${s.id},false)">📦</button>
</div></div>`;}).join("")
|| `<p class="hint">${emptyHint()}</p>`;
const archived = d.sessions.filter(s => s.archived);
if (archived.length)
$("#shelf-list").insertAdjacentHTML("beforeend",
`<h3 style="margin-top:16px">📦 归档 (${archived.length})</h3>` +
archived.map(s => `<div class="mini-card" onclick="openChat(${s.id},'${s.mode}')">
<h4>${esc(s.title)}</h4>
<div class="row"><button class="btn small" onclick="event.stopPropagation();archiveSession(${s.id},true)">取消归档</button></div></div>`).join(""));
}
async function newRoleplaySession() {
const chars = (await api("/characters")).characters;
@@ -174,11 +188,14 @@ function renderBubble(m) {
const cls = m.role === "user" ? "user" : "ai";
const sw = m.swipe_count || 0;
return `<div class="bubble ${cls}" data-mid="${m.id}">
${m.speaker ? `<div class="speaker">${esc(m.speaker)}</div>` : ""}${esc(m.content)}
${m.quote ? `<div class="quote">↩ ${esc(m.quote)}</div>` : ""}
${m.speaker ? `<div class="speaker">${esc(m.speaker)}</div>` : ""}${esc(m.content)}${ts(m)}
${sw > 1 ? `<div class="swipe-bar"><button onclick="swipeMsg(${m.id},-1)">‹</button>
<span>${(m.active_swipe ?? 0) + 1}/${sw}</span>
<button onclick="swipeMsg(${m.id},1)">›</button></div>` : ""}
<div class="acts">
<button onclick="copyMsg(${m.id})">📋</button>
<button onclick="quoteMsg(${m.id})">↩</button>
<button onclick="editMsg(${m.id})">✏️</button>
<button onclick="delMsg(${m.id})">🗑</button>
<button onclick="starMsg(${m.id})">${m.starred ? "⭐" : "☆"}</button>
@@ -201,7 +218,10 @@ function refreshChat() { openChat(CUR.sessionId, CUR.mode, CUR.characterId); }
async function sendMsg() {
const input = $("#chat-input");
const text = input.value.trim();
if (!text) return;
if (!text || SENDING) return;
SENDING = true;
const replyTo = REPLY_TO; REPLY_TO = null;
input.placeholder = "说点什么…";
input.value = "";
$("#chat-log").insertAdjacentHTML("beforeend",
`<div class="bubble user">${esc(text)}</div>`);
@@ -211,13 +231,14 @@ async function sendMsg() {
$("#chat-log").scrollTop = 1e9;
try {
const url = CUR.mode === "group" ? `/sessions/${CUR.sessionId}/send_group` : `/sessions/${CUR.sessionId}/send`;
const d = await api(url, { method: "POST", body: { content: text } });
thinking.innerHTML = renderBubble({ id: d.message_id, role: "assistant", content: d.reply });
const d = await api(url, { method: "POST", body: { content: text, reply_to_id: replyTo } });
thinking.innerHTML = renderBubble({ id: d.message_id, role: "assistant", content: d.reply, created_at: new Date().toISOString() });
d.new_characters.forEach(c => {
$("#chat-log").insertAdjacentHTML("beforeend",
`<div class="new-char-banner">✨ 新角色入场:${esc(c.name)}(已自动建档${c.avatar_url ? " + 生成立绘" : ""})</div>`);
});
} catch (e) { thinking.textContent = "⚠️ " + e.message; }
finally { SENDING = false; }
$("#chat-log").scrollTop = 1e9;
}
async function editMsg(mid) {
@@ -477,7 +498,93 @@ async function revokeGrant(cid, uid) {
adminGrantChannel();
}
/* ================= 扩展功能 ================= */
/* ================= 第二批小功能 ================= */
/* 复制消息 */
function copyMsg(mid) {
const el = document.querySelector(`[data-mid="${mid}"]`);
navigator.clipboard.writeText(el.textContent.trim());
toast("已复制");
}
/* 时间戳渲染 */
function ts(m) {
const t = m.created_at ? new Date(m.created_at + "Z") : new Date();
return `<span class="ts">${t.getHours()}:${String(t.getMinutes()).padStart(2, "0")}</span>`;
}
/* 主题切换 */
async function toggleTheme() {
const light = document.body.classList.toggle("theme-light");
await api("/prefs", { method: "POST", body: { theme: light ? "light" : "dark" } });
}
async function applySavedPrefs() {
try {
const d = await api("/prefs");
const p = d.prefs || {};
applyPrefs(p);
if (p.theme === "light") document.body.classList.add("theme-light");
} catch (e) {}
}
/* 全量数据导出(账号级备份) */
async function exportAll() {
const blob = await fetch(`${API}/me/export`, {
headers: { Authorization: "Bearer " + TOKEN } }).then(r => r.json());
const a = document.createElement("a");
a.href = URL.createObjectURL(new Blob([JSON.stringify(blob, null, 2)],
{ type: "application/json" }));
a.download = "maopaoya-backup.json"; a.click();
toast("备份已下载");
}
/* 置顶排序 + 归档过滤 */
function sortSessions(list) {
return [...list].sort((a, b) => (b.pinned - a.pinned) || (b.id - a.id));
}
/* 会话重命名 */
async function renameSession(sid, old) {
const nv = prompt("新标题", old || "");
if (!nv) return;
await api(`/sessions/${sid}/settings`, { method: "PATCH", body: { title: nv } });
toast("已改名"); backToShelf();
}
/* 归档会话 */
async function archiveSession(sid, cur) {
await api(`/sessions/${sid}/settings`, { method: "PATCH", body: { archived: !cur } });
toast(cur ? "已取消归档" : "已归档"); backToShelf();
}
/* 引用回复 */
let REPLY_TO = null;
function quoteMsg(mid) {
REPLY_TO = mid;
const el = document.querySelector(`[data-mid="${mid}"] .speaker`);
$("#chat-input").placeholder = "回复 " + (el ? el.textContent : "消息") + "…";
$("#chat-input").focus();
}
/* 发送防连点 */
let SENDING = false;
/* 音色试听 */
async function testVoice(voice) {
const chars = (await api("/characters")).characters;
const sid = CUR.sessionId;
if (!sid) { toast("先进入一个会话"); return; }
toast("试听中…");
try {
const r = await fetch(`${API}/sessions/${sid}/tts/${(await api(`/sessions/${sid}/messages`)).messages.at(-1).id}`,
{ headers: { Authorization: "Bearer " + TOKEN } });
new Audio(URL.createObjectURL(await r.blob())).play();
} catch (e) { toast("试听失败:" + e.message); }
}
/* 空态随机文案 */
const EMPTY_LINES = ["这里空空如也,像冒泡鸭的池塘冬天。", "先去建个角色?", "万物起始于一根手指的点击。"];
function emptyHint() { return EMPTY_LINES[Math.floor(Math.random() * EMPTY_LINES.length)]; }
/* ---------- 群聊 ---------- */
async function newGroup() {
@@ -671,6 +778,6 @@ function esc(s) { return String(s || "").replace(/[&<>"']/g, c =>
(async () => {
if (!TOKEN) return;
try { ME = await api("/auth/me"); enterApp(); }
try { ME = await api("/auth/me"); await enterApp(); await applySavedPrefs(); }
catch (e) { localStorage.removeItem("token"); }
})();