From 3b44f99fb8cd2f4c39267b638782c880731892d5 Mon Sep 17 00:00:00 2001 From: kicer Date: Thu, 3 Sep 2026 19:10:24 +0800 Subject: [PATCH] =?UTF-8?q?feat(ui):=20=E4=BB=BB=E5=8A=A1=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E9=A2=84=E8=A7=88=E8=B0=83=E6=95=B4=E2=80=94=E2=80=94?= =?UTF-8?q?=E5=9B=BE=E7=89=87=E8=B5=B0=20fs-viewer=EF=BC=8Cpdf=20=E4=B8=8D?= =?UTF-8?q?=E9=A2=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - md/txt/html + png/jpg/gif/webp/svg 全部进 fs-viewer 预览 html 用 sandbox iframe,md/txt 走 bubble,图片 fetch+blob URL 用 居中 - pdf 退回普通文件图标(行不可点),仅下载/删除 - closeFullscreen 派发 fs:close 事件,图片 blob URL 随关闭释放 - 移除 openInTab 及其所有调用 --- app/server/public/css/style.css | 2 ++ app/server/public/index.html | 53 ++++++++++++++++----------------- 2 files changed, 28 insertions(+), 27 deletions(-) diff --git a/app/server/public/css/style.css b/app/server/public/css/style.css index 773e0a0..220b7be 100644 --- a/app/server/public/css/style.css +++ b/app/server/public/css/style.css @@ -558,6 +558,8 @@ footer { flex: none; padding: 8px 12px calc(10px + env(safe-area-inset-bottom, 0 /* HTML 预览:沙箱 iframe 铺满全屏查看器 */ .fs-html-frame { width: 100%; height: 100%; min-height: 70vh; border: 0; background: #fff; border-radius: 12px; } +/* 图片预览:fs-viewer 居中缩放显示原图,大图可滚动 */ +.fs-img-frame { display: block; max-width: 100%; max-height: 80vh; margin: 0 auto; border-radius: 8px; object-fit: contain; background: #fff; } /* ================= 设置区(覆盖层页面;卡片/表单样式搬自 style.css 适配 v2 变量) ================= */ #settings-page { diff --git a/app/server/public/index.html b/app/server/public/index.html index fec9dd0..6852d00 100644 --- a/app/server/public/index.html +++ b/app/server/public/index.html @@ -1036,7 +1036,7 @@ body.appendChild(clone); $('fs-viewer').classList.remove('hidden'); } - function closeFullscreen() { $('fs-viewer').classList.add('hidden'); } + function closeFullscreen() { $('fs-viewer').classList.add('hidden'); $('fs-viewer').dispatchEvent(new Event('fs:close')); } // 双击消息气泡/工具卡 → 全屏查看(操作钮/图片不误触全屏) $('chat-view').addEventListener('dblclick', function (e) { if (e.target.closest('.act') || e.target.closest('img')) return; @@ -1178,15 +1178,13 @@ return; } el.innerHTML = files.map(f => { - // 预览策略:md/txt/html 进 fs-viewer 预览;pdf/图片浏览器原生渲染更稳,转 blob URL 新标签页打开 + // 预览策略:md/txt/html + 图片在 fs-viewer 预览(pdf 不预) const low = f.filename.toLowerCase(); - const fsPv = /\.(md|txt|html?)$/.test(low); - const tabOpen = /\.(pdf|png|jpe?g|gif|webp|svg)$/.test(low); - let ico; - if (fsPv) ico = ''; - else if (tabOpen) ico = ''; - else ico = ''; - return '
' + + const fsPv = /\.(md|txt|html?|png|jpe?g|gif|webp|svg)$/.test(low); + const ico = fsPv + ? '' + : ''; + return '
' + ico + '' + esc(String(f.filename).split('/').pop() || f.filename) + '' + '' + fmtSize(f.size) + '' + @@ -1196,7 +1194,7 @@ }).join(''); } catch (e) { toast('读取文件失败:' + e.message); } } - // 单击可预览文件 → 全屏预览(#fs-viewer):md 按 markdown 渲染、txt 原文排版、html 用沙箱 iframe + // 单击可预览文件 → 全屏预览(#fs-viewer):md 按 markdown 渲染、txt 原文排版、html 用沙箱 iframe、图片用 async function previewInFs(name) { try { const r = await fetch('/api/sessions/' + state.sessionId + '/files/download?path=' + encodeURIComponent(name)); @@ -1212,7 +1210,23 @@ iframe.srcdoc = html; 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); } else { + // md/txt:复用双击气泡的全屏查看器 const text = await r.text(); let cls = 'bubble-ai', html; if (/\.md$/i.test(name)) { @@ -1225,22 +1239,10 @@ const wrap = document.createElement('div'); wrap.className = 'msg other'; wrap.innerHTML = '
' + html + '
'; - openFullscreen(wrap.firstChild); // 复用双击气泡的全屏查看器 + openFullscreen(wrap.firstChild); } } catch (e) { toast('预览失败:' + e.message); } } - // 单击 pdf/图片 → 浏览器原生渲染最稳;拉流走同源 cookie 鉴权,转 blob URL 后新标签页打开 - async function openInTab(name) { - try { - const r = await fetch('/api/sessions/' + state.sessionId + '/files/download?path=' + encodeURIComponent(name)); - if (r.status === 401) { location.href = '/login'; return; } - if (!r.ok) { let msg = 'HTTP ' + r.status; try { msg = (await r.json()).error || msg; } catch (_) {} throw new Error(msg); } - const blob = await r.blob(); - const url = URL.createObjectURL(blob); - window.open(url, '_blank', 'noopener'); - setTimeout(function () { URL.revokeObjectURL(url); }, 60 * 1000); - } catch (e) { toast('打开失败:' + e.message); } - } async function downloadFile(name) { if (!state.sessionId) return; try { @@ -1261,10 +1263,7 @@ const path = row.dataset.path; if (!path) return; if (e.target.closest('.del')) deleteFile(path); - else if (e.target.closest('.file-row.clickable') && !e.target.closest('.file-act')) { - if (/\.(pdf|png|jpe?g|gif|webp|svg)$/i.test(path)) openInTab(path); - else previewInFs(path); - } + else if (e.target.closest('.file-row.clickable') && !e.target.closest('.file-act')) previewInFs(path); else if (e.target.closest('.file-act')) downloadFile(path); }); // ---------- 大图上传前压缩(同步 v1:阈值/参数由 /api/system/status 透出) ----------