图片查看器支持同组图片左右滑动切换

移动端点开图片后可在同一组内横滑切换(1x 缩放时接管手势,放大后仍是拖动平移),
底部显示页码,桌面补 ←/→。组边界:聊天内同一消息/工具卡;文件面板当前目录。

- stream.js: openImageViewer 增加可选 list/at 参数与组状态,横滑+页码+键盘
- preview.js: pvImgGroup 按当前目录聚合图片(用下载直链,不逐张取 blob)
- index.html/style.css: 新增 #img-count 页码节点与样式
This commit is contained in:
2026-10-24 10:18:07 +08:00
parent 516b4a5d86
commit dfcbe6d3ea
4 changed files with 83 additions and 8 deletions
+2
View File
@@ -853,6 +853,8 @@ footer { position: relative; flex: none; padding: 8px 12px calc(10px + env(safe-
#img-viewer { position: fixed; inset: 0; z-index: 150; background: rgba(0,0,0,0.93); display: flex; align-items: center; justify-content: center; overflow: hidden; }
#img-viewer.hidden { display: none; }
#img-viewer-img { max-width: 100vw; max-height: 88vh; touch-action: none; cursor: grab; user-select: none; -webkit-user-drag: none; }
/* 组内页码:底部居中悬浮,不参与命中测试(点它等于点背景会关闭) */
#img-count { position: fixed; left: 50%; bottom: calc(18px + env(safe-area-inset-bottom, 0px)); transform: translateX(-50%); z-index: 151; padding: 3px 12px; border-radius: 999px; background: rgba(0,0,0,0.5); color: #fff; font-size: 12px; letter-spacing: .5px; pointer-events: none; }
#fs-viewer { position: fixed; inset: 0; z-index: 130; background: rgba(8,10,20,0.88); padding: 40px 14px 24px; overflow: auto; /* 缩放后支持横纵滚动 */ }
#fs-viewer.hidden { display: none; }
.fs-panel { max-width: 860px; margin: 0 auto; background: var(--page); border-radius: 14px; padding: 18px 20px; font-size: 15.5px; }
+3 -1
View File
@@ -440,10 +440,12 @@
<div id="toast" class="hidden"></div>
</div>
<!-- 图片全屏查看器(点击消息/工具卡中图片打开;双指缩放、放大后可拖动) -->
<!-- 图片全屏查看器(点击消息/工具卡中图片打开;双指缩放、放大后可拖动;同组多图可左右滑动切换) -->
<div id="img-viewer" class="hidden">
<button class="fs-close" onclick="closeImageViewer()">✕</button>
<img id="img-viewer-img" alt="">
<!-- 组内页码(如 2 / 5):仅同组多图时显示 -->
<span id="img-count" class="hidden"></span>
</div>
<!-- ================= 文档预览右栏(桌面端≥960px;office/全部文档类型统一承载;移动端走全屏 #fs-viewer,本栏隐藏) -->
+21 -2
View File
@@ -35,6 +35,23 @@ function pvKind(name) {
return 'none';
}
// 文件面板「当前目录」里的图片作为一组,供查看器左右滑动切换。
// 同组直接用下载直链(<img> 不受 Content-Disposition: attachment 影响),不必逐张取 blob。
// 依赖 files.js 的 filesNodeAt / filesCurDir / assetDownloadUrl(同为全局,加载顺序在前)。
function pvImgGroup(name) {
try {
if (typeof filesNodeAt !== 'function') return null;
const node = filesNodeAt(typeof filesCurDir === 'string' ? filesCurDir : '');
if (!node) return null;
const paths = node.files.map(f => f.filename).filter(p => PV_IMG.test(p));
if (paths.length < 2) return null;
return {
list: paths.map(p => ({ src: assetDownloadUrl(p), alt: p.split('/').pop() })),
at: paths.indexOf(name),
};
} catch (_) { return null; }
}
// 刷新/新窗口两个钮对**所有**预览类型统一开放:
// refresh → pvState.reload(各类型都注入「按同一路径重开」,与地址条回车同一条路)
// popup → pvState.url(office=预览网关;文本/HTML/图片/PDF=blob URL;不支持的类型=下载地址)
@@ -242,7 +259,8 @@ async function openFilePreview(name, sizeText) {
newBlobUrl = u;
const im = document.createElement('img');
im.className = 'pv-img'; im.src = u; im.alt = String(name);
im.addEventListener('click', () => openImageViewer(u, String(name).split('/').pop()));
const g = pvImgGroup(name);
im.addEventListener('click', () => openImageViewer(u, String(name).split('/').pop(), g && g.list, g && g.at));
node = im; tb = Object.assign({}, tbAll, { url: u });
} else { // text/md/代码/json/csv
node = await pvTextNode(name, sizeText, r);
@@ -362,7 +380,8 @@ async function pvRenderLegacy(name, r, my) {
const u = URL.createObjectURL(await r.blob());
if (state.lastImgBlobUrl) { try { URL.revokeObjectURL(state.lastImgBlobUrl); } catch (_) {} }
state.lastImgBlobUrl = u;
openImageViewer(u, name);
const g = pvImgGroup(name);
openImageViewer(u, name, g && g.list, g && g.at);
} else {
const text = await r.text();
let cls = 'bubble-ai', html;
+57 -5
View File
@@ -590,32 +590,63 @@
});
// 点击消息/工具卡里的图片 → 图片全屏查看器(#fs-viewer 里的克隆图也生效)
// 同一气泡/工具卡内的图作为一组,查看器里可左右滑动切换
function imgViewerDelegate(e) {
const im = e.target.closest('.bubble-ai img, .bubble-user img, .tool-card img');
if (im && im.src) openImageViewer(im.src, im.alt || '');
if (!im || !im.src) return;
const box = im.closest('.msg') || im.closest('.tool-card') || $('fs-body');
const list = Array.prototype.map.call(box.querySelectorAll('img'), x => ({ src: x.src, alt: x.alt || '' }))
.filter(x => x.src);
openImageViewer(im.src, im.alt || '', list);
}
$('chat-view').addEventListener('click', imgViewerDelegate);
$('fs-viewer').addEventListener('click', imgViewerDelegate);
// 图片全屏查看器:transform 缩放+平移。双指捻合缩放(1~6x)、放大后单指/鼠标拖动、双击切 2.5x。
// 第 3 参 list([{src,alt}] 或 [src]) 传入「同组图片」时开启左右滑动切换;第 4 参 at 指定起始下标。
let openImageViewer, closeImageViewer;
(function () {
const viewer = document.getElementById('img-viewer');
const img = document.getElementById('img-viewer-img');
const count = document.getElementById('img-count');
let scale = 1, tx = 0, ty = 0, mode = null;
let sx = 0, sy = 0, stx = 0, sty = 0, pDist = 0, pScale = 1;
// gallery=当前组;gi=组内下标;wx/wy/wdx=横滑起点与实时水平位移
let gallery = [], gi = 0, wx = 0, wy = 0, wdx = 0;
const tdist = t => Math.hypot(t[0].clientX - t[1].clientX, t[0].clientY - t[1].clientY);
function apply() {
img.style.transform = 'translate(' + tx + 'px,' + ty + 'px) scale(' + scale + ')';
img.style.cursor = scale > 1 ? 'grab' : 'zoom-in';
}
openImageViewer = function (src, alt) {
function drawCount() {
if (!count) return; // 旧版 HTML 缓存里没有计数节点时不报错
const on = gallery.length > 1;
count.textContent = on ? (gi + 1) + ' / ' + gallery.length : '';
count.classList.toggle('hidden', !on);
}
// 切到组内第 i 张:缩放/位移必须复位,否则会带着上一张的放大态看下一张
function showAt(i) {
if (!gallery.length) return;
gi = Math.max(0, Math.min(gallery.length - 1, i));
scale = 1; tx = ty = 0; mode = null;
img.style.transition = 'none';
apply();
img.src = src; img.alt = alt || '';
img.src = gallery[gi].src; img.alt = gallery[gi].alt || '';
drawCount();
}
openImageViewer = function (src, alt, list, at) {
const raw = Array.isArray(list) && list.length ? list : [{ src: src, alt: alt }];
gallery = raw.map(x => (typeof x === 'string' ? { src: x, alt: '' } : x)).filter(x => x && x.src);
// 起始下标优先用调用方给的(文件面板那张是 blob URL,与组里的直链对不上),否则按 src 匹配
let i = typeof at === 'number' && at >= 0 ? at : gallery.findIndex(x => x.src === src);
if (i < 0) i = 0;
showAt(i);
viewer.classList.remove('hidden');
};
closeImageViewer = function () { viewer.classList.add('hidden'); };
closeImageViewer = function () {
viewer.classList.add('hidden');
gallery = []; drawCount(); // 关窗即清组,别让任务目录的地址一直挂着
};
// 点背景(非图片)关闭
viewer.addEventListener('click', function (e) { if (e.target === viewer) closeImageViewer(); });
img.addEventListener('dblclick', function () {
@@ -624,8 +655,10 @@
apply();
});
img.addEventListener('touchstart', function (e) {
img.style.transition = 'none'; // 跟手拖动:清掉上一次回弹的过渡
if (e.touches.length === 2) { mode = 'pinch'; pDist = tdist(e.touches) || 1; pScale = scale; }
else if (e.touches.length === 1 && scale > 1) { mode = 'pan'; sx = e.touches[0].clientX; sy = e.touches[0].clientY; stx = tx; sty = ty; }
else if (e.touches.length === 1 && gallery.length > 1) { mode = 'swipe'; wx = e.touches[0].clientX; wy = e.touches[0].clientY; wdx = 0; }
else mode = null;
}, { passive: true });
img.addEventListener('touchmove', function (e) {
@@ -639,10 +672,29 @@
tx = stx + e.touches[0].clientX - sx;
ty = sty + e.touches[0].clientY - sy;
apply();
} else if (mode === 'swipe' && e.touches.length === 1) {
const dx = e.touches[0].clientX - wx, dy = e.touches[0].clientY - wy;
// 只接管水平方向;竖直位移更大时不动图,留给以后的「下滑关闭」
if (Math.abs(dx) > Math.abs(dy)) { wdx = dx; tx = dx * 0.5; apply(); }
}
}, { passive: false });
img.addEventListener('touchend', function (e) { if (e.touches.length < 2) { if (mode === 'pinch') mode = null; } if (!e.touches.length) mode = null; }, { passive: true });
img.addEventListener('touchend', function (e) {
if (mode === 'swipe') {
if (Math.abs(wdx) > 45) showAt(gi + (wdx < 0 ? 1 : -1)); // 左滑看下一张
else { img.style.transition = 'transform .16s ease-out'; tx = 0; apply(); } // 不够格:弹回原位
mode = null;
return;
}
if (e.touches.length < 2) { if (mode === 'pinch') mode = null; }
if (!e.touches.length) mode = null;
}, { passive: true });
img.addEventListener('touchcancel', function () { mode = null; }, { passive: true });
// 桌面没有滑动:组内多图时用 ←/→ 切换(单图不响应)
document.addEventListener('keydown', function (e) {
if (viewer.classList.contains('hidden') || gallery.length < 2) return;
if (e.key === 'ArrowLeft') showAt(gi - 1);
else if (e.key === 'ArrowRight') showAt(gi + 1);
});
// 桌面:放大后按住拖动平移
img.addEventListener('mousedown', function (e) {
if (scale <= 1) return;