feat: batch delete

This commit is contained in:
tao.chen
2026-08-28 10:54:08 +08:00
parent a897a08534
commit 51f1fe96bf
11 changed files with 408 additions and 184 deletions
+35 -4
View File
@@ -1,9 +1,9 @@
'use strict';
// ---------------------------------------------------------------------------
// 批量选择 / 批量下载 (依赖 app.js 的 consts: checkAll, checkAllWrap,
// batchBtn, batchCount, filesList, toast)
// 用 var 顶层声明, 与 app.js 同 <script> 内共享 (loadFiles / fileRow 读写).
// 批量选择 / 批量下载 / 批量删除 (依赖 app.js 的 consts: checkAll, checkAllWrap,
// batchBtn, batchCount, batchDeleteBtn, batchDeleteCount, filesList, toast)
// 用 var 顶层声明, 跨文件共享 (classic <script> 加载顺序: app.js → helpers.js → batch.js).
// ---------------------------------------------------------------------------
var selectedFiles = new Set();
@@ -17,6 +17,8 @@ function updateBatchUI() {
const n = selectedFiles.size;
batchCount.textContent = n;
batchBtn.style.display = n > 0 ? 'inline-block' : 'none';
batchDeleteCount.textContent = n;
batchDeleteBtn.style.display = n > 0 ? 'inline-block' : 'none';
const checks = filesList.querySelectorAll('.file-check');
if (checks.length === 0) {
@@ -81,8 +83,37 @@ batchBtn.addEventListener('click', async () => {
}
});
batchDeleteBtn.addEventListener('click', async () => {
if (selectedFiles.size === 0) return;
const names = [...selectedFiles];
if (!confirm(`确定删除选中的 ${names.length} 个文件吗? 该操作不可恢复.`)) return;
batchDeleteBtn.disabled = true;
const restore = () => { batchDeleteBtn.disabled = false; batchDeleteBtn.textContent = `批量删除 (${selectedFiles.size})`; };
batchDeleteBtn.textContent = '删除中...';
try {
const r = await fetch('/files-delete', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ files: names }),
});
if (!r.ok) {
let msg = 'HTTP ' + r.status;
try { msg = (await r.json()).error || msg; } catch {}
throw new Error(msg);
}
const data = await r.json();
toast(`已删除 ${data.deleted} 个文件`, 'success');
selectedFiles.clear();
await loadFiles();
} catch (e) {
toast('批量删除失败: ' + e.message, 'error');
} finally {
restore();
}
});
// ---------------------------------------------------------------------------
// Boot (从 app.js 末尾移过来, 确保 selectedFiles 已声明再启动首次 loadFiles)
// ---------------------------------------------------------------------------
loadFiles();
setInterval(loadFiles, 30000);
setInterval(loadFiles, 30000);