refactor: 重命名客户端工程
This commit is contained in:
@@ -0,0 +1,99 @@
|
||||
import PhotoSwipeLightbox from 'photoswipe/lightbox';
|
||||
import PhotoSwipe from 'photoswipe';
|
||||
import 'photoswipe/style.css';
|
||||
|
||||
export interface MediaItem {
|
||||
url?: string;
|
||||
name?: string;
|
||||
type?: string;
|
||||
}
|
||||
|
||||
function isVideo(m: MediaItem) {
|
||||
return !!m && (m.type === 'video' || /\.(mp4|webm|mov|m4v|ogg)$/i.test(m.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);
|
||||
return new Promise((resolve) => {
|
||||
const img = new Image();
|
||||
img.onload = () => {
|
||||
const dim = { w: img.naturalWidth || 1600, h: img.naturalHeight || 1200 };
|
||||
sizeCache.set(url, dim);
|
||||
resolve(dim);
|
||||
};
|
||||
img.onerror = () => resolve({ w: 1600, h: 1200 });
|
||||
img.src = url;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 打开图片 / 视频详情查看器(基于 PhotoSwipe v5)。
|
||||
* - 图片:支持双指/滚轮/双击缩放、拖动、左右滑动切换、下滑关闭。
|
||||
* - 视频:渲染为可控制的 <video> 播放,切到其它张或关闭时自动暂停。
|
||||
* - 循环:到最后一张的下一张回到第一张。
|
||||
*/
|
||||
export async function openMediaViewer(items: MediaItem[], index = 0) {
|
||||
const list = (items || []).filter((m) => m && m.url);
|
||||
if (!list.length) return;
|
||||
const start = Math.min(Math.max(0, index), 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 };
|
||||
}
|
||||
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 lightbox = new PhotoSwipeLightbox({
|
||||
dataSource,
|
||||
index: start,
|
||||
pswpModule: PhotoSwipe as any,
|
||||
loop: true,
|
||||
bgOpacity: 0.96,
|
||||
wheelToZoom: true,
|
||||
// 图片支持双击/双指/滚轮缩放;视频项无宽高,PhotoSwipe 会自动隐藏其缩放按钮
|
||||
});
|
||||
|
||||
// 视频项:用自定义 DOM 替换默认内容
|
||||
lightbox.on('contentLoad', (e: any) => {
|
||||
const { content } = e;
|
||||
if (content?.data?.type !== 'video') return;
|
||||
e.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;';
|
||||
const video = document.createElement('video');
|
||||
video.src = content.data.src;
|
||||
video.controls = true;
|
||||
video.playsInline = true;
|
||||
video.preload = 'metadata';
|
||||
video.style.cssText = 'max-width:100%;max-height:100%;background:#000;';
|
||||
wrap.appendChild(video);
|
||||
content.element = wrap;
|
||||
});
|
||||
|
||||
// 离开当前张时暂停其视频
|
||||
const pauseVideo = (e: any) => {
|
||||
const v = e?.content?.element?.querySelector?.('video') as HTMLVideoElement | null;
|
||||
if (v) v.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 自行清理 */
|
||||
});
|
||||
|
||||
lightbox.init();
|
||||
lightbox.loadAndOpen(start);
|
||||
}
|
||||
Reference in New Issue
Block a user