feat: 完善后台导入日志权限与备份恢复
This commit is contained in:
@@ -0,0 +1,92 @@
|
||||
import PhotoSwipe from 'photoswipe';
|
||||
import PhotoSwipeLightbox from 'photoswipe/lightbox';
|
||||
import 'photoswipe/style.css';
|
||||
|
||||
export interface MediaViewerItem {
|
||||
url?: string;
|
||||
name?: string;
|
||||
type?: string;
|
||||
}
|
||||
|
||||
const sizeCache = new Map<string, { width: number; height: number }>();
|
||||
|
||||
function isVideo(item: MediaViewerItem): boolean {
|
||||
return item.type === 'video' || /\.(mp4|webm|mov|m4v|ogg)$/i.test(item.url || '');
|
||||
}
|
||||
|
||||
function getImageSize(url: string): Promise<{ width: number; height: number }> {
|
||||
const cached = sizeCache.get(url);
|
||||
if (cached) return Promise.resolve(cached);
|
||||
return new Promise((resolve) => {
|
||||
const img = new Image();
|
||||
img.onload = () => {
|
||||
const size = {
|
||||
width: img.naturalWidth || 1600,
|
||||
height: img.naturalHeight || 1200,
|
||||
};
|
||||
sizeCache.set(url, size);
|
||||
resolve(size);
|
||||
};
|
||||
img.onerror = () => resolve({ width: 1600, height: 1200 });
|
||||
img.src = url;
|
||||
});
|
||||
}
|
||||
|
||||
export async function openMediaViewer(items: MediaViewerItem[], index = 0): Promise<void> {
|
||||
const list = (items || []).filter((item) => item.url);
|
||||
if (!list.length) return;
|
||||
const start = Math.min(Math.max(index, 0), list.length - 1);
|
||||
const dataSource = await Promise.all(
|
||||
list.map(async (item) => {
|
||||
if (isVideo(item)) {
|
||||
return { type: 'video', src: item.url, title: item.name };
|
||||
}
|
||||
const size = await getImageSize(item.url as string);
|
||||
return {
|
||||
src: item.url,
|
||||
width: size.width,
|
||||
height: size.height,
|
||||
alt: item.name,
|
||||
title: item.name,
|
||||
};
|
||||
}),
|
||||
);
|
||||
|
||||
const lightbox = new PhotoSwipeLightbox({
|
||||
dataSource,
|
||||
index: start,
|
||||
pswpModule: PhotoSwipe as any,
|
||||
loop: true,
|
||||
bgOpacity: 0.96,
|
||||
wheelToZoom: true,
|
||||
});
|
||||
|
||||
lightbox.on('contentLoad', (event: any) => {
|
||||
const { content } = event;
|
||||
if (content?.data?.type !== 'video') return;
|
||||
event.preventDefault();
|
||||
const wrap = document.createElement('div');
|
||||
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 = (event: any) => {
|
||||
const video = event?.content?.element?.querySelector?.('video') as HTMLVideoElement | null;
|
||||
video?.pause();
|
||||
};
|
||||
lightbox.on('contentDeactivate', pauseVideo);
|
||||
lightbox.on('close', () => {
|
||||
const video = document.querySelector('.pswp video') as HTMLVideoElement | null;
|
||||
video?.pause();
|
||||
});
|
||||
|
||||
lightbox.init();
|
||||
lightbox.loadAndOpen(start);
|
||||
}
|
||||
Reference in New Issue
Block a user