feat: 保留后台图片预览工具条

This commit is contained in:
Nixevol
2026-07-05 05:12:43 +08:00
parent 3619929195
commit fee893a716
2 changed files with 226 additions and 0 deletions
+1
View File
@@ -159,6 +159,7 @@
- 图片预览滚轮缩放兜底(2026-07-05):后台和客户端共用的 `openMediaViewer` 在 PhotoSwipe `wheel` 事件中补充自定义缩放处理,拦截默认滚轮行为并按鼠标位置缩放当前图片;缩放上限至少放宽到初始比例 6 倍或 4 倍,避免认证文件、较小证书图等因 PhotoSwipe 默认上限判断导致鼠标滚轮看似无法放大。视频项仍通过 `isZoomable()` 自动跳过。
- 后台排序媒体预览分流(2026-07-05):`MediaSorter` 用于作品主图和溯源节点媒体排序上传;点击图片时现在只把图片项传入 PhotoSwipe 预览队列,打开样式与认证文件等图片预览保持一致,避免混入视频项影响图片查看体验。点击视频仍沿用原有媒体预览逻辑。
- 后台详情页图片预览统一(2026-07-05):艺术品详情页中溯源时间轴图片和认证文件封面不再使用 Naive UI `n-image` 预览层,统一改为普通缩略图点击后调用后台 `openMediaViewer`。这样打开样式与后台其他图片预览一致,并能使用 PhotoSwipe 的鼠标滚轮缩放兜底;视频仍保持原页面内原生 video 控件。
- 后台图片预览工具条(2026-07-05):后台 `openMediaViewer` 在 PhotoSwipe 中注册底部工具条,保留 Naive Image 预览常用的向左旋转、向右旋转、还原、缩小、放大、下载、关闭按钮;默认 PhotoSwipe 顶栏关闭/缩放/计数被关闭,避免重复。图片按钮在视频项自动置灰,视频仍使用现有 video 控件。
## 当前代码基准与注意事项
@@ -9,6 +9,8 @@ export interface MediaViewerItem {
}
const sizeCache = new Map<string, { width: number; height: number }>();
const rotationCache = new WeakMap<object, number[]>();
let toolbarStyleInjected = false;
function isVideo(item: MediaViewerItem): boolean {
return item.type === 'video' || /\.(mp4|webm|mov|m4v|ogg)$/i.test(item.url || '');
@@ -32,6 +34,59 @@ function getImageSize(url: string): Promise<{ width: number; height: number }> {
});
}
function injectToolbarStyle(): void {
if (toolbarStyleInjected) return;
toolbarStyleInjected = true;
const style = document.createElement('style');
style.textContent = `
.artveris-pswp-toolbar {
position: absolute;
left: 50%;
top: auto;
right: auto;
bottom: 18px;
z-index: 1600;
width: auto;
height: auto;
display: inline-flex;
align-items: center;
gap: 2px;
padding: 8px 10px;
border-radius: 999px;
background: rgba(31, 41, 55, 0.72);
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.18);
transform: translateX(-50%);
backdrop-filter: blur(8px);
}
.artveris-pswp-toolbar .pswp__button {
position: static;
width: 30px;
height: 30px;
opacity: 1;
color: #fff;
background: transparent;
}
.artveris-pswp-toolbar .pswp__button svg {
width: 22px;
height: 22px;
fill: none;
stroke: currentColor;
stroke-width: 1.8;
stroke-linecap: round;
stroke-linejoin: round;
pointer-events: none;
}
.artveris-pswp-toolbar .pswp__button:hover {
background: rgba(255, 255, 255, 0.14);
}
.artveris-pswp-toolbar .pswp__button:disabled {
cursor: not-allowed;
opacity: 0.35;
}
`;
document.head.appendChild(style);
}
function bindWheelZoom(lightbox: PhotoSwipeLightbox): void {
lightbox.on('wheel', (event: any) => {
const originalEvent = event?.originalEvent as WheelEvent | undefined;
@@ -69,6 +124,172 @@ function bindWheelZoom(lightbox: PhotoSwipeLightbox): void {
});
}
function getCurrentSlide(pswp: any) {
return pswp?.currSlide;
}
function getCurrentData(pswp: any) {
return pswp?.currSlide?.data || pswp?.options?.dataSource?.[pswp.currIndex];
}
function isCurrentImage(pswp: any): boolean {
return getCurrentData(pswp)?.type !== 'video';
}
function getRotation(pswp: any): number {
const rotations = rotationCache.get(pswp);
return rotations?.[pswp.currIndex] || 0;
}
function setRotation(pswp: any, value: number): void {
const slide = getCurrentSlide(pswp);
const element = slide?.content?.element as HTMLElement | undefined;
if (!element || !isCurrentImage(pswp)) return;
const rotations = rotationCache.get(pswp) || [];
rotations[pswp.currIndex] = value;
rotationCache.set(pswp, rotations);
element.style.transform = value ? `rotate(${value}deg)` : '';
element.style.transformOrigin = 'center center';
}
function applyCurrentRotation(pswp: any): void {
setRotation(pswp, getRotation(pswp));
}
function zoomCurrent(pswp: any, factor: number): void {
const slide = getCurrentSlide(pswp);
if (!slide?.isZoomable?.()) return;
const zoomLevels = slide.zoomLevels || {};
const current = slide.currZoomLevel || zoomLevels.initial || 1;
const initial = zoomLevels.initial || current || 1;
const min = zoomLevels.min || initial;
const max = Math.max(zoomLevels.max || 0, initial * 6, 4);
const nextZoom = Math.min(max, Math.max(min, current * factor));
slide.zoomTo(nextZoom, pswp.getViewportCenterPoint?.(), 0, true);
}
function resetCurrent(pswp: any): void {
const slide = getCurrentSlide(pswp);
setRotation(pswp, 0);
if (slide?.isZoomable?.()) {
slide.zoomTo(slide.zoomLevels?.initial || 1, pswp.getViewportCenterPoint?.(), 0, true);
}
}
function downloadCurrent(pswp: any): void {
const data = getCurrentData(pswp);
const url = data?.src;
if (!url || data?.type === 'video') return;
const link = document.createElement('a');
link.href = url;
link.download = data?.title || data?.alt || url.split('/').pop() || 'image';
link.rel = 'noopener';
document.body.appendChild(link);
link.click();
link.remove();
}
function icon(paths: string): string {
return `<svg viewBox="0 0 24 24" aria-hidden="true">${paths}</svg>`;
}
function registerToolbarButton(
lightbox: PhotoSwipeLightbox,
options: {
name: string;
title: string;
order: number;
html: string;
imageOnly?: boolean;
onClick: (pswp: any) => void;
},
): void {
lightbox.pswp?.ui?.registerElement({
name: options.name,
className: `artveris-pswp-${options.name}`,
title: options.title,
ariaLabel: options.title,
order: options.order,
isButton: true,
appendTo: 'bar',
html: options.html,
onInit: (element: HTMLElement, pswp: any) => {
element.parentElement?.classList.add('artveris-pswp-toolbar');
if (!options.imageOnly) return;
const update = () => {
(element as HTMLButtonElement).disabled = !isCurrentImage(pswp);
};
pswp.on('change', update);
pswp.on('afterSetContent', update);
update();
},
onClick: (_event: MouseEvent, _element: HTMLElement, pswp: any) => options.onClick(pswp),
});
}
function registerToolbar(lightbox: PhotoSwipeLightbox): void {
injectToolbarStyle();
lightbox.on('uiRegister', () => {
registerToolbarButton(lightbox, {
name: 'rotate-left',
title: '向左旋转',
order: 41,
html: icon('<path d="M4 7v5h5"/><path d="M5.5 12a7 7 0 1 0 2-5"/><path d="M4 7l3.5-.2"/>'),
imageOnly: true,
onClick: (instance) => setRotation(instance, getRotation(instance) - 90),
});
registerToolbarButton(lightbox, {
name: 'rotate-right',
title: '向右旋转',
order: 42,
html: icon('<path d="M20 7v5h-5"/><path d="M18.5 12a7 7 0 1 1-2-5"/><path d="M20 7l-3.5-.2"/>'),
imageOnly: true,
onClick: (instance) => setRotation(instance, getRotation(instance) + 90),
});
registerToolbarButton(lightbox, {
name: 'reset',
title: '还原',
order: 43,
html: icon('<path d="M8 3h8v4"/><path d="M16 3l-9 9 5 5 9-9"/><path d="M5 19h14"/>'),
imageOnly: true,
onClick: resetCurrent,
});
registerToolbarButton(lightbox, {
name: 'zoom-out',
title: '缩小',
order: 44,
html: icon('<circle cx="11" cy="11" r="6"/><path d="M8.5 11h5"/><path d="M16 16l4 4"/>'),
imageOnly: true,
onClick: (instance) => zoomCurrent(instance, 1 / 1.25),
});
registerToolbarButton(lightbox, {
name: 'zoom-in',
title: '放大',
order: 45,
html: icon('<circle cx="11" cy="11" r="6"/><path d="M8.5 11h5"/><path d="M11 8.5v5"/><path d="M16 16l4 4"/>'),
imageOnly: true,
onClick: (instance) => zoomCurrent(instance, 1.25),
});
registerToolbarButton(lightbox, {
name: 'download',
title: '下载',
order: 46,
html: icon('<path d="M12 4v10"/><path d="M8 10l4 4 4-4"/><path d="M5 20h14"/>'),
imageOnly: true,
onClick: downloadCurrent,
});
registerToolbarButton(lightbox, {
name: 'bottom-close',
title: '关闭',
order: 47,
html: icon('<path d="M6 6l12 12"/><path d="M18 6L6 18"/>'),
onClick: (instance) => instance?.close?.(),
});
});
lightbox.on('change', () => applyCurrentRotation(lightbox.pswp));
lightbox.on('afterSetContent', () => applyCurrentRotation(lightbox.pswp));
}
export async function openMediaViewer(items: MediaViewerItem[], index = 0): Promise<void> {
const list = (items || []).filter((item) => item.url);
if (!list.length) return;
@@ -96,8 +317,12 @@ export async function openMediaViewer(items: MediaViewerItem[], index = 0): Prom
loop: true,
bgOpacity: 0.96,
wheelToZoom: true,
close: false,
zoom: false,
counter: false,
});
registerToolbar(lightbox);
bindWheelZoom(lightbox);
lightbox.on('contentLoad', (event: any) => {