feat(ui): 任务文件上传加进度条 + 图片预览复用 img-viewer

- 面板上传改 XHR + onprogress:多文件并行,进度条显示总进度(已发字节/总字节)
  1.2s 后自动收起,失败/成功分别 toast
- 任务文件中的图片预览改用 openImageViewer (与消息里单击图片同一查看器)
  支持双指缩放+拖动+双击放大,大图也能看完
- blob URL 通过 state.lastImgBlobUrl 缓存,新开图片时回收旧的避免泄漏
- 移除 fs-img-frame 配套样式(不再需要)
This commit is contained in:
2026-09-03 19:19:05 +08:00
parent 3b44f99fb8
commit 8d1611adda
2 changed files with 70 additions and 23 deletions
+16
View File
@@ -343,6 +343,22 @@ footer { flex: none; padding: 8px 12px calc(10px + env(safe-area-inset-bottom, 0
background: var(--active-soft); color: var(--active); cursor: pointer;
}
.files-panel-header .file-upload:active { background: var(--active); color: #fff; }
/* 上传总进度条(header 中间,上传中显示) */
.files-panel-progress {
flex: 1; min-width: 0; height: 6px; margin: 0 10px;
background: var(--surface); border-radius: 3px; overflow: hidden; position: relative;
}
.files-panel-progress .bar {
height: 100%; width: 0;
background: linear-gradient(90deg, var(--active) 0%, #6b8cff 100%);
border-radius: 3px;
transition: width .12s linear;
}
.files-panel-progress .pct {
position: absolute; right: 0; top: 50%; transform: translateY(-50%);
font-size: 10px; color: var(--gray); background: var(--page); padding: 0 4px; line-height: 1;
white-space: nowrap; pointer-events: none;
}
.files-panel-header .file-upload svg {
width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 2;
stroke-linecap: round; stroke-linejoin: round;
+54 -23
View File
@@ -113,6 +113,10 @@
<div class="files-panel" id="files-panel" aria-label="任务文件">
<div class="files-panel-header">
<span>任务文件</span>
<div class="files-panel-progress hidden" id="fp-progress" aria-live="polite">
<div class="bar" id="fp-progress-bar"></div>
<span class="pct" id="fp-progress-pct">0%</span>
</div>
<label class="file-upload" title="上传文件">
<input type="file" id="file-input" multiple hidden>
<svg viewBox="0 0 24 24"><path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"></path><polyline points="17 8 12 3 7 8"></polyline><line x1="12" y1="3" x2="12" y2="15"></line></svg>
@@ -355,6 +359,7 @@
status: '', // 当前会话状态(SSE 更新)
sessions: [], // 抽屉列表缓存
attachments: [], // 待发送附件 {file, name, size}
lastImgBlobUrl: null, // 任务文件图片预览的 blob URL,下次预览或开新图时回收
es: null, // EventSource
streamText: '', // 当前流式累积文本
streamEl: null, // 当前流式 AI 气泡
@@ -1211,20 +1216,13 @@
body.appendChild(iframe);
$('fs-viewer').classList.remove('hidden');
} else if (/\.(png|jpe?g|gif|webp|svg)$/i.test(name)) {
// 图片:走 blob URL 在 fs-viewer 里居中显示;blob 需保留到查看器关闭后再释放
// 图片:复用会话里单击图片的全屏查看器(双指缩放+拖动+双击放大),大图也能看完
const blob = await r.blob();
const url = URL.createObjectURL(blob);
const body = $('fs-body');
body.innerHTML = '';
const img = document.createElement('img');
img.className = 'fs-img-frame';
img.alt = name;
img.src = url;
body.appendChild(img);
$('fs-viewer').classList.remove('hidden');
// 关闭全屏时回收 blob URL(closeFullscreen 会清空 #fs-body,顺便干掉 img.src)
const onClose = function () { try { URL.revokeObjectURL(url); } catch (_) {} $('fs-viewer').removeEventListener('fs:close', onClose); };
$('fs-viewer').addEventListener('fs:close', onClose);
// 上一次的 blob URL 顺手释放;避免反复预览图片泄漏
if (state.lastImgBlobUrl) { try { URL.revokeObjectURL(state.lastImgBlobUrl); } catch (_) {} }
state.lastImgBlobUrl = url;
openImageViewer(url, name);
} else {
// md/txt:复用双击气泡的全屏查看器
const text = await r.text();
@@ -1330,23 +1328,56 @@
}
}
// 面板内上传:直接写入当前会话任务目录(>阈值的图片逐个询问压缩)
// 面板内上传:直接写入当前会话任务目录(>阈值的图片逐个询问压缩;多文件并行上传+总进度条)
$('file-input').addEventListener('change', async e => {
const input = e.target;
const files = Array.prototype.slice.call(input.files || []); // 先快照再清空,FileList 是实时的
input.value = '';
if (!files.length) return;
if (!state.sessionId) { toast('先发送一条消息创建会话'); return; }
try {
// 大图逐个串行询问压缩(模态弹窗不能并行)
const ready = [];
for (const f of files) ready.push(await maybeCompressImage(f));
const fd = new FormData();
for (const f of ready) fd.append('files', f, f.name);
await req('POST', '/api/sessions/' + state.sessionId + '/files', fd, true);
toast('已上传 ' + ready.length + ' 个文件');
loadFiles();
} catch (err) { toast('上传失败:' + err.message); }
// 大图逐个串行询问压缩(模态弹窗不能并行)
const ready = [];
for (const f of files) ready.push(await maybeCompressImage(f));
// ---- 并行上传 + 实时进度 ----
const progWrap = $('fp-progress'), progBar = $('fp-progress-bar'), progPct = $('fp-progress-pct');
const tot = ready.reduce((s, f) => s + f.size, 0);
const loaded = { v: 0 }; // 所有文件已上传字节总和
let done = 0, errCount = 0;
function paintProg() {
const pct = tot ? Math.min(100, Math.round(loaded.v / tot * 100)) : 0;
progBar.style.width = pct + '%';
progPct.textContent = pct + '%';
}
progWrap.classList.remove('hidden');
paintProg();
const tasks = ready.map(f => new Promise(resolve => {
const fd = new FormData(); fd.append('files', f, f.name);
const xhr = new XMLHttpRequest();
xhr.open('POST', '/api/sessions/' + state.sessionId + '/files');
let lastLoaded = 0;
xhr.upload.onprogress = function (ev) {
if (!ev.lengthComputable) return;
// ev.loaded 是本文件累计已发字节,汇总时取本次增量避免多计
loaded.v += ev.loaded - lastLoaded;
lastLoaded = ev.loaded;
paintProg();
};
xhr.onload = function () {
if (xhr.status === 200) done++; else errCount++;
// 终态补齐(可能没收到最后一帧 100%)
if (lastLoaded < f.size) { loaded.v += f.size - lastLoaded; paintProg(); }
resolve();
};
xhr.onerror = function () { errCount++; resolve(); };
xhr.send(fd);
}));
await Promise.all(tasks);
paintProg();
if (errCount) toast('已上传 ' + done + ' 个,失败 ' + errCount + ' 个');
else toast('已上传 ' + done + ' 个文件');
loadFiles();
// 1.2s 后收起进度条
setTimeout(() => { progWrap.classList.add('hidden'); progBar.style.width = '0%'; }, 1200);
});