fix: 修复认证文件滚轮缩放
This commit is contained in:
@@ -32,6 +32,43 @@ function getImageSize(url: string): Promise<{ width: number; height: number }> {
|
||||
});
|
||||
}
|
||||
|
||||
function bindWheelZoom(lightbox: PhotoSwipeLightbox): void {
|
||||
lightbox.on('wheel', (event: any) => {
|
||||
const originalEvent = event?.originalEvent as WheelEvent | undefined;
|
||||
const pswp = lightbox.pswp as any;
|
||||
const slide = pswp?.currSlide;
|
||||
if (!originalEvent || !slide?.isZoomable?.()) return;
|
||||
|
||||
event.preventDefault();
|
||||
originalEvent.preventDefault();
|
||||
originalEvent.stopPropagation();
|
||||
|
||||
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);
|
||||
|
||||
let zoomFactor = -originalEvent.deltaY;
|
||||
if (originalEvent.deltaMode === 1) {
|
||||
zoomFactor *= 0.05;
|
||||
} else {
|
||||
zoomFactor *= originalEvent.deltaMode ? 1 : 0.002;
|
||||
}
|
||||
const nextZoom = Math.min(max, Math.max(min, current * 2 ** zoomFactor));
|
||||
|
||||
slide.zoomTo(
|
||||
nextZoom,
|
||||
{
|
||||
x: originalEvent.clientX,
|
||||
y: originalEvent.clientY,
|
||||
},
|
||||
0,
|
||||
true,
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
export async function openMediaViewer(items: MediaViewerItem[], index = 0): Promise<void> {
|
||||
const list = (items || []).filter((item) => item.url);
|
||||
if (!list.length) return;
|
||||
@@ -61,6 +98,8 @@ export async function openMediaViewer(items: MediaViewerItem[], index = 0): Prom
|
||||
wheelToZoom: true,
|
||||
});
|
||||
|
||||
bindWheelZoom(lightbox);
|
||||
|
||||
lightbox.on('contentLoad', (event: any) => {
|
||||
const { content } = event;
|
||||
if (content?.data?.type !== 'video') return;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import PhotoSwipeLightbox from 'photoswipe/lightbox';
|
||||
import PhotoSwipe from 'photoswipe';
|
||||
import PhotoSwipeLightbox from 'photoswipe/lightbox';
|
||||
import 'photoswipe/style.css';
|
||||
|
||||
export interface MediaItem {
|
||||
@@ -8,12 +8,12 @@ export interface MediaItem {
|
||||
type?: string;
|
||||
}
|
||||
|
||||
function isVideo(m: MediaItem) {
|
||||
return !!m && (m.type === 'video' || /\.(mp4|webm|mov|m4v|ogg)$/i.test(m.url || ''));
|
||||
const sizeCache = new Map<string, { w: number; h: number }>();
|
||||
|
||||
function isVideo(item: MediaItem): boolean {
|
||||
return item.type === 'video' || /\.(mp4|webm|mov|m4v|ogg)$/i.test(item.url || '');
|
||||
}
|
||||
|
||||
// 缓存图片真实尺寸,避免重复加载;PhotoSwipe 需要宽高才能正确缩放/居中
|
||||
const sizeCache = new Map<string, { w: number; h: number }>();
|
||||
function getImageSize(url: string): Promise<{ w: number; h: number }> {
|
||||
const cached = sizeCache.get(url);
|
||||
if (cached) return Promise.resolve(cached);
|
||||
@@ -29,24 +29,61 @@ function getImageSize(url: string): Promise<{ w: number; h: number }> {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 打开图片 / 视频详情查看器(基于 PhotoSwipe v5)。
|
||||
* - 图片:支持双指/滚轮/双击缩放、拖动、左右滑动切换、下滑关闭。
|
||||
* - 视频:渲染为可控制的 <video> 播放,切到其它张或关闭时自动暂停。
|
||||
* - 循环:到最后一张的下一张回到第一张。
|
||||
*/
|
||||
export async function openMediaViewer(items: MediaItem[], index = 0) {
|
||||
const list = (items || []).filter((m) => m && m.url);
|
||||
function bindWheelZoom(lightbox: PhotoSwipeLightbox): void {
|
||||
lightbox.on('wheel', (event: any) => {
|
||||
const originalEvent = event?.originalEvent as WheelEvent | undefined;
|
||||
const pswp = lightbox.pswp as any;
|
||||
const slide = pswp?.currSlide;
|
||||
if (!originalEvent || !slide?.isZoomable?.()) return;
|
||||
|
||||
event.preventDefault();
|
||||
originalEvent.preventDefault();
|
||||
originalEvent.stopPropagation();
|
||||
|
||||
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);
|
||||
|
||||
let zoomFactor = -originalEvent.deltaY;
|
||||
if (originalEvent.deltaMode === 1) {
|
||||
zoomFactor *= 0.05;
|
||||
} else {
|
||||
zoomFactor *= originalEvent.deltaMode ? 1 : 0.002;
|
||||
}
|
||||
const nextZoom = Math.min(max, Math.max(min, current * 2 ** zoomFactor));
|
||||
|
||||
slide.zoomTo(
|
||||
nextZoom,
|
||||
{
|
||||
x: originalEvent.clientX,
|
||||
y: originalEvent.clientY,
|
||||
},
|
||||
0,
|
||||
true,
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
export async function openMediaViewer(items: MediaItem[], index = 0): Promise<void> {
|
||||
const list = (items || []).filter((item) => item.url);
|
||||
if (!list.length) return;
|
||||
const start = Math.min(Math.max(0, index), list.length - 1);
|
||||
const start = Math.min(Math.max(index, 0), list.length - 1);
|
||||
|
||||
const dataSource = await Promise.all(
|
||||
list.map(async (m) => {
|
||||
if (isVideo(m)) {
|
||||
return { type: 'video', src: m.url as string, title: m.name };
|
||||
list.map(async (item) => {
|
||||
if (isVideo(item)) {
|
||||
return { type: 'video', src: item.url as string, title: item.name };
|
||||
}
|
||||
const { w, h } = await getImageSize(m.url as string);
|
||||
return { src: m.url as string, width: w, height: h, alt: m.name, title: m.name };
|
||||
const { w, h } = await getImageSize(item.url as string);
|
||||
return {
|
||||
src: item.url as string,
|
||||
width: w,
|
||||
height: h,
|
||||
alt: item.name,
|
||||
title: item.name,
|
||||
};
|
||||
}),
|
||||
);
|
||||
|
||||
@@ -57,18 +94,17 @@ export async function openMediaViewer(items: MediaItem[], index = 0) {
|
||||
loop: true,
|
||||
bgOpacity: 0.96,
|
||||
wheelToZoom: true,
|
||||
// 图片支持双击/双指/滚轮缩放;视频项无宽高,PhotoSwipe 会自动隐藏其缩放按钮
|
||||
});
|
||||
|
||||
// 视频项:用自定义 DOM 替换默认内容
|
||||
lightbox.on('contentLoad', (e: any) => {
|
||||
const { content } = e;
|
||||
bindWheelZoom(lightbox);
|
||||
|
||||
lightbox.on('contentLoad', (event: any) => {
|
||||
const { content } = event;
|
||||
if (content?.data?.type !== 'video') return;
|
||||
e.preventDefault();
|
||||
event.preventDefault();
|
||||
const wrap = document.createElement('div');
|
||||
wrap.className = 'pswp-video';
|
||||
wrap.style.cssText =
|
||||
'width:100%;height:100%;display:flex;align-items:center;justify-content:center;';
|
||||
wrap.style.cssText = 'width:100%;height:100%;display:flex;align-items:center;justify-content:center;';
|
||||
const video = document.createElement('video');
|
||||
video.src = content.data.src;
|
||||
video.controls = true;
|
||||
@@ -79,19 +115,14 @@ export async function openMediaViewer(items: MediaItem[], index = 0) {
|
||||
content.element = wrap;
|
||||
});
|
||||
|
||||
// 离开当前张时暂停其视频
|
||||
const pauseVideo = (e: any) => {
|
||||
const v = e?.content?.element?.querySelector?.('video') as HTMLVideoElement | null;
|
||||
if (v) v.pause();
|
||||
const pauseVideo = (event: any) => {
|
||||
const video = event?.content?.element?.querySelector?.('video') as HTMLVideoElement | null;
|
||||
video?.pause();
|
||||
};
|
||||
lightbox.on('contentDeactivate', pauseVideo);
|
||||
lightbox.on('close', () => {
|
||||
const v = document.querySelector('.pswp .pswp-video video') as HTMLVideoElement | null;
|
||||
if (v) v.pause();
|
||||
});
|
||||
// 关闭后销毁实例,释放键盘/尺寸监听
|
||||
lightbox.on('destroy', () => {
|
||||
/* PhotoSwipe 自行清理 */
|
||||
const video = document.querySelector('.pswp .pswp-video video') as HTMLVideoElement | null;
|
||||
video?.pause();
|
||||
});
|
||||
|
||||
lightbox.init();
|
||||
|
||||
Reference in New Issue
Block a user