feat(ui): 任务文件上传加进度条 + 图片预览复用 img-viewer
- 面板上传改 XHR + onprogress:多文件并行,进度条显示总进度(已发字节/总字节) 1.2s 后自动收起,失败/成功分别 toast - 任务文件中的图片预览改用 openImageViewer (与消息里单击图片同一查看器) 支持双指缩放+拖动+双击放大,大图也能看完 - blob URL 通过 state.lastImgBlobUrl 缓存,新开图片时回收旧的避免泄漏 - 移除 fs-img-frame 配套样式(不再需要)
This commit is contained in:
@@ -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;
|
background: var(--active-soft); color: var(--active); cursor: pointer;
|
||||||
}
|
}
|
||||||
.files-panel-header .file-upload:active { background: var(--active); color: #fff; }
|
.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 {
|
.files-panel-header .file-upload svg {
|
||||||
width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 2;
|
width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 2;
|
||||||
stroke-linecap: round; stroke-linejoin: round;
|
stroke-linecap: round; stroke-linejoin: round;
|
||||||
|
|||||||
@@ -113,6 +113,10 @@
|
|||||||
<div class="files-panel" id="files-panel" aria-label="任务文件">
|
<div class="files-panel" id="files-panel" aria-label="任务文件">
|
||||||
<div class="files-panel-header">
|
<div class="files-panel-header">
|
||||||
<span>任务文件</span>
|
<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="上传文件">
|
<label class="file-upload" title="上传文件">
|
||||||
<input type="file" id="file-input" multiple hidden>
|
<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>
|
<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 更新)
|
status: '', // 当前会话状态(SSE 更新)
|
||||||
sessions: [], // 抽屉列表缓存
|
sessions: [], // 抽屉列表缓存
|
||||||
attachments: [], // 待发送附件 {file, name, size}
|
attachments: [], // 待发送附件 {file, name, size}
|
||||||
|
lastImgBlobUrl: null, // 任务文件图片预览的 blob URL,下次预览或开新图时回收
|
||||||
es: null, // EventSource
|
es: null, // EventSource
|
||||||
streamText: '', // 当前流式累积文本
|
streamText: '', // 当前流式累积文本
|
||||||
streamEl: null, // 当前流式 AI 气泡
|
streamEl: null, // 当前流式 AI 气泡
|
||||||
@@ -1211,20 +1216,13 @@
|
|||||||
body.appendChild(iframe);
|
body.appendChild(iframe);
|
||||||
$('fs-viewer').classList.remove('hidden');
|
$('fs-viewer').classList.remove('hidden');
|
||||||
} else if (/\.(png|jpe?g|gif|webp|svg)$/i.test(name)) {
|
} else if (/\.(png|jpe?g|gif|webp|svg)$/i.test(name)) {
|
||||||
// 图片:走 blob URL 在 fs-viewer 里居中显示;blob 需保留到查看器关闭后再释放
|
// 图片:复用会话里单击图片的全屏查看器(双指缩放+拖动+双击放大),大图也能看完
|
||||||
const blob = await r.blob();
|
const blob = await r.blob();
|
||||||
const url = URL.createObjectURL(blob);
|
const url = URL.createObjectURL(blob);
|
||||||
const body = $('fs-body');
|
// 上一次的 blob URL 顺手释放;避免反复预览图片泄漏
|
||||||
body.innerHTML = '';
|
if (state.lastImgBlobUrl) { try { URL.revokeObjectURL(state.lastImgBlobUrl); } catch (_) {} }
|
||||||
const img = document.createElement('img');
|
state.lastImgBlobUrl = url;
|
||||||
img.className = 'fs-img-frame';
|
openImageViewer(url, name);
|
||||||
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);
|
|
||||||
} else {
|
} else {
|
||||||
// md/txt:复用双击气泡的全屏查看器
|
// md/txt:复用双击气泡的全屏查看器
|
||||||
const text = await r.text();
|
const text = await r.text();
|
||||||
@@ -1330,23 +1328,56 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 面板内上传:直接写入当前会话任务目录(>阈值的图片逐个询问压缩)
|
// 面板内上传:直接写入当前会话任务目录(>阈值的图片逐个询问压缩;多文件并行上传+总进度条)
|
||||||
$('file-input').addEventListener('change', async e => {
|
$('file-input').addEventListener('change', async e => {
|
||||||
const input = e.target;
|
const input = e.target;
|
||||||
const files = Array.prototype.slice.call(input.files || []); // 先快照再清空,FileList 是实时的
|
const files = Array.prototype.slice.call(input.files || []); // 先快照再清空,FileList 是实时的
|
||||||
input.value = '';
|
input.value = '';
|
||||||
if (!files.length) return;
|
if (!files.length) return;
|
||||||
if (!state.sessionId) { toast('先发送一条消息创建会话'); return; }
|
if (!state.sessionId) { toast('先发送一条消息创建会话'); return; }
|
||||||
try {
|
|
||||||
// 大图逐个串行询问压缩(模态弹窗不能并行)
|
// 大图逐个串行询问压缩(模态弹窗不能并行)
|
||||||
const ready = [];
|
const ready = [];
|
||||||
for (const f of files) ready.push(await maybeCompressImage(f));
|
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);
|
const progWrap = $('fp-progress'), progBar = $('fp-progress-bar'), progPct = $('fp-progress-pct');
|
||||||
await req('POST', '/api/sessions/' + state.sessionId + '/files', fd, true);
|
const tot = ready.reduce((s, f) => s + f.size, 0);
|
||||||
toast('已上传 ' + ready.length + ' 个文件');
|
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();
|
loadFiles();
|
||||||
} catch (err) { toast('上传失败:' + err.message); }
|
// 1.2s 后收起进度条
|
||||||
|
setTimeout(() => { progWrap.classList.add('hidden'); progBar.style.width = '0%'; }, 1200);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user