153 lines
6.3 KiB
React
153 lines
6.3 KiB
React
/**
|
||
* 灯箱底部缩略图条 + 幻灯片播放
|
||
* 双击图片放大后,在底部列出当前文件夹(含筛选)下的全部图片,
|
||
* 点击任意缩略图直接跳转查看,与 react-photo-view 的 index 一一对应。
|
||
* 左侧提供幻灯片播放:▶ 播放 / ⏸ 暂停,间隔可选(1/2/3/5/10 秒,localStorage 记忆)。
|
||
*
|
||
* 通过 portal 挂到 body:react-photo-view 也是 portal 到 body(z-index 2000),
|
||
* 这里用 2100 保证压在其上方,且不被祖先的 transform/filter 形成的层叠上下文困住。
|
||
*/
|
||
import { useEffect, useRef, useState } from 'react';
|
||
import { createPortal } from 'react-dom';
|
||
|
||
const INTERVAL_KEY = 'flypic_slideshow_interval';
|
||
const INTERVAL_OPTIONS = [1, 2, 3, 5, 10];
|
||
|
||
export default function PhotoStrip({ images, index, onIndexChange, getThumbnailUrl }) {
|
||
const stripRef = useRef(null);
|
||
const [playing, setPlaying] = useState(false);
|
||
const [intervalSec, setIntervalSec] = useState(() => {
|
||
const v = parseFloat(localStorage.getItem(INTERVAL_KEY));
|
||
return INTERVAL_OPTIONS.includes(v) ? v : 3;
|
||
});
|
||
// 缩略图加载失败(后端可能返回 0 字节 webp)→ 退化为格式角标
|
||
const [failed, setFailed] = useState({});
|
||
|
||
const total = images ? images.length : 0;
|
||
|
||
const changeInterval = (v) => {
|
||
setIntervalSec(v);
|
||
localStorage.setItem(INTERVAL_KEY, String(v));
|
||
};
|
||
|
||
// 灯箱关闭即停播(组件在 index<0 时仍挂载,只是返回 null)
|
||
useEffect(() => {
|
||
if (index < 0 && playing) setPlaying(false);
|
||
}, [index, playing]);
|
||
|
||
// 幻灯片:每张停留 intervalSec,到末尾循环回首张;
|
||
// 手动跳转(index 变化)会重排计时器,让新图也停留满一个间隔。
|
||
useEffect(() => {
|
||
if (!playing || index < 0 || total < 2) return undefined;
|
||
const t = setTimeout(() => onIndexChange((index + 1) % total), intervalSec * 1000);
|
||
return () => clearTimeout(t);
|
||
}, [playing, index, intervalSec, total, onIndexChange]);
|
||
|
||
// 切换图片时把当前缩略图滚到条中间(只滚条自身,不动页面)
|
||
useEffect(() => {
|
||
if (index < 0 || !stripRef.current) return;
|
||
const el = stripRef.current.querySelector('[data-active="true"]');
|
||
if (!el) return;
|
||
const c = stripRef.current;
|
||
c.scrollTo({
|
||
left: el.offsetLeft - c.clientWidth / 2 + el.clientWidth / 2,
|
||
behavior: 'smooth',
|
||
});
|
||
}, [index]);
|
||
|
||
if (index < 0 || !images || !images.length) return null;
|
||
|
||
return createPortal(
|
||
<div className="fixed bottom-0 left-0 right-0 z-[2100] bg-black/85 backdrop-blur-sm border-t border-white/10">
|
||
<div className="flex items-center gap-2 px-3 py-2">
|
||
{/* 当前进度 */}
|
||
<span className="shrink-0 text-[11px] text-gray-300 tabular-nums select-none pr-1 border-r border-white/15 mr-1">
|
||
{index + 1} / {total}
|
||
</span>
|
||
|
||
{/* 幻灯片播放:▶/⏸ + 间隔选择 */}
|
||
<button
|
||
type="button"
|
||
title={playing ? '暂停幻灯片播放' : '幻灯片播放'}
|
||
aria-label={playing ? '暂停幻灯片' : '播放幻灯片'}
|
||
onClick={() => total >= 2 && setPlaying((p) => !p)}
|
||
className={`shrink-0 w-7 h-7 rounded-full flex items-center justify-center transition-colors ${
|
||
playing
|
||
? 'bg-purple-500 text-white hover:bg-purple-400'
|
||
: 'bg-white/10 text-gray-200 hover:bg-white/20'
|
||
} ${total < 2 ? 'opacity-40 cursor-not-allowed' : ''}`}
|
||
>
|
||
{playing ? (
|
||
<svg width="12" height="12" viewBox="0 0 24 24" fill="currentColor">
|
||
<rect x="6" y="4" width="4" height="16" rx="1" />
|
||
<rect x="14" y="4" width="4" height="16" rx="1" />
|
||
</svg>
|
||
) : (
|
||
<svg width="12" height="12" viewBox="0 0 24 24" fill="currentColor">
|
||
<path d="M7 4.5v15l13-7.5z" />
|
||
</svg>
|
||
)}
|
||
</button>
|
||
<select
|
||
title="幻灯片切换间隔"
|
||
aria-label="幻灯片切换间隔"
|
||
value={intervalSec}
|
||
onChange={(e) => changeInterval(parseFloat(e.target.value))}
|
||
className={`shrink-0 h-7 rounded bg-white/10 text-[11px] text-gray-200 px-1 pr-4 outline-none cursor-pointer hover:bg-white/20 border border-transparent focus:border-purple-400/60 ${
|
||
playing ? 'border-purple-400/60' : ''
|
||
}`}
|
||
>
|
||
{INTERVAL_OPTIONS.map((s) => (
|
||
<option key={s} value={s} className="bg-gray-900 text-gray-100">
|
||
{s} 秒
|
||
</option>
|
||
))}
|
||
</select>
|
||
|
||
{/* 缩略图横向滚动条 */}
|
||
<div
|
||
ref={stripRef}
|
||
className="flex-1 flex items-center gap-1.5 overflow-x-auto overflow-y-hidden scrollbar-thin"
|
||
style={{ scrollbarWidth: 'thin' }}
|
||
>
|
||
{images.map((img, i) => {
|
||
const src = getThumbnailUrl ? getThumbnailUrl(img) : '';
|
||
const active = i === index;
|
||
const bad = failed[img.id ?? i];
|
||
return (
|
||
<button
|
||
key={img.id ?? i}
|
||
type="button"
|
||
data-active={active ? 'true' : 'false'}
|
||
title={img.filename || ''}
|
||
onClick={() => onIndexChange(i)}
|
||
className={`relative shrink-0 w-12 h-12 rounded overflow-hidden border-2 transition-all duration-150 ${
|
||
active
|
||
? 'border-purple-500 ring-2 ring-purple-400/60 scale-105 z-10'
|
||
: 'border-transparent opacity-55 hover:opacity-100 hover:border-white/40'
|
||
}`}
|
||
>
|
||
{src && !bad ? (
|
||
<img
|
||
src={src}
|
||
alt={img.filename || ''}
|
||
decoding="async"
|
||
draggable={false}
|
||
onError={() => setFailed((f) => ({ ...f, [img.id ?? i]: true }))}
|
||
className="w-full h-full object-cover pointer-events-none"
|
||
/>
|
||
) : (
|
||
<span className="w-full h-full flex items-center justify-center bg-gray-800 text-[9px] text-gray-400">
|
||
{(img.format || (img.filename || '').split('.').pop() || '').slice(0, 5)}
|
||
</span>
|
||
)}
|
||
</button>
|
||
);
|
||
})}
|
||
</div>
|
||
</div>
|
||
</div>,
|
||
document.body
|
||
);
|
||
}
|