From fee893a71680ea9ec8c4f67b56b5c614710e3812 Mon Sep 17 00:00:00 2001 From: Nixevol Date: Sun, 5 Jul 2026 05:12:43 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BF=9D=E7=95=99=E5=90=8E=E5=8F=B0?= =?UTF-8?q?=E5=9B=BE=E7=89=87=E9=A2=84=E8=A7=88=E5=B7=A5=E5=85=B7=E6=9D=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- aidocs/project_context.md | 1 + apps/admin/src/composables/openMediaViewer.ts | 225 ++++++++++++++++++ 2 files changed, 226 insertions(+) diff --git a/aidocs/project_context.md b/aidocs/project_context.md index af70e4b..412605c 100644 --- a/aidocs/project_context.md +++ b/aidocs/project_context.md @@ -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 控件。 ## 当前代码基准与注意事项 diff --git a/apps/admin/src/composables/openMediaViewer.ts b/apps/admin/src/composables/openMediaViewer.ts index 9a89472..e030573 100644 --- a/apps/admin/src/composables/openMediaViewer.ts +++ b/apps/admin/src/composables/openMediaViewer.ts @@ -9,6 +9,8 @@ export interface MediaViewerItem { } const sizeCache = new Map(); +const rotationCache = new WeakMap(); +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 ``; +} + +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(''), + imageOnly: true, + onClick: (instance) => setRotation(instance, getRotation(instance) - 90), + }); + registerToolbarButton(lightbox, { + name: 'rotate-right', + title: '向右旋转', + order: 42, + html: icon(''), + imageOnly: true, + onClick: (instance) => setRotation(instance, getRotation(instance) + 90), + }); + registerToolbarButton(lightbox, { + name: 'reset', + title: '还原', + order: 43, + html: icon(''), + imageOnly: true, + onClick: resetCurrent, + }); + registerToolbarButton(lightbox, { + name: 'zoom-out', + title: '缩小', + order: 44, + html: icon(''), + imageOnly: true, + onClick: (instance) => zoomCurrent(instance, 1 / 1.25), + }); + registerToolbarButton(lightbox, { + name: 'zoom-in', + title: '放大', + order: 45, + html: icon(''), + imageOnly: true, + onClick: (instance) => zoomCurrent(instance, 1.25), + }); + registerToolbarButton(lightbox, { + name: 'download', + title: '下载', + order: 46, + html: icon(''), + imageOnly: true, + onClick: downloadCurrent, + }); + registerToolbarButton(lightbox, { + name: 'bottom-close', + title: '关闭', + order: 47, + html: icon(''), + 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 { 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) => {