feat: 相册点击直接进大图查看器,移除「选择图片」列表

- AlbumBubble: 点 +N 直接打开全屏查看器(从该图开始),删除中间的选择列表弹窗
- ImageLightbox: 底部加可滑动缩略图条(当前高亮+自动滚动定位),顶栏加下载按钮
- 下载按钮保证此前藏在 +N 列表里的图片仍可下载

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
TerryM
2026-05-29 17:50:53 +08:00
parent b22ecc22ad
commit 1f8772f645
2 changed files with 188 additions and 203 deletions

View File

@@ -1,17 +1,10 @@
import { LoaderCircle, X } from "lucide-react";
import { DownloadCloudIcon } from "../../icons/DownloadCloudIcon";
import { useEffect, useState } from "react";
import { createPortal } from "react-dom";
import { useI18n } from "../../../i18n";
import type { Attachment, Post } from "../../../types/post";
import type { Post } from "../../../types/post";
import { AttachmentDownloadPill } from "../AttachmentDownloadPill";
import { BubbleImage } from "../BubbleImage";
import { useLightbox } from "../overlays/ImageLightbox";
import { autolink } from "../utils/autolink";
import { downloadAttachment } from "../utils/downloadFile";
import { formatBytes } from "../utils/formatBytes";
import { postDisplayText } from "../utils/postText";
import { useToast } from "../../Toast";
const MAX_VISIBLE = 4;
@@ -26,135 +19,9 @@ function albumItemClass(index: number, count: number) {
return "";
}
function ImageListDownloadButton({
postId,
attachment,
}: {
postId: string;
attachment: Attachment;
}) {
const { t } = useI18n();
const { showToast } = useToast();
const [isDownloading, setIsDownloading] = useState(false);
const handleDownload = async () => {
if (isDownloading) return;
setIsDownloading(true);
try {
await downloadAttachment(postId, attachment.id, attachment.filename);
showToast(t("downloadOk"));
} catch {
showToast(t("downloadFail"), "error");
} finally {
setIsDownloading(false);
}
};
return (
<button
type="button"
onClick={(e) => {
e.stopPropagation();
handleDownload();
}}
disabled={isDownloading}
className="flex h-9 w-9 shrink-0 items-center justify-center rounded-full bg-white/10 text-white transition hover:bg-white/20 disabled:cursor-wait"
aria-label={
isDownloading ? t("downloading") : `Download ${attachment.filename}`
}
aria-busy={isDownloading}
>
{isDownloading ? (
<LoaderCircle className="h-4 w-4 animate-spin" strokeWidth={2.3} />
) : (
<DownloadCloudIcon className="h-4 w-4" />
)}
</button>
);
}
function ImageListDialog({
postId,
images,
onClose,
onPick,
}: {
postId: string;
images: Attachment[];
onClose: () => void;
onPick: (index: number) => void;
}) {
useEffect(() => {
const onKey = (event: KeyboardEvent) => {
if (event.key === "Escape") onClose();
};
window.addEventListener("keydown", onKey);
return () => window.removeEventListener("keydown", onKey);
}, [onClose]);
return createPortal(
<div
className="fixed inset-0 z-[100] flex items-center justify-center bg-black/75 px-4 backdrop-blur-sm"
onClick={onClose}
role="dialog"
aria-modal="true"
aria-label="Image list"
>
<div
className="w-full max-w-md overflow-hidden rounded-2xl border border-ark-line bg-ark-panel shadow-2xl"
onClick={(e) => e.stopPropagation()}
>
<div className="flex items-center justify-between border-b border-ark-line px-4 py-3">
<div className="text-sm font-semibold text-neutral-100"></div>
<button
type="button"
onClick={onClose}
className="flex h-8 w-8 items-center justify-center rounded-full bg-white/10 text-white transition hover:bg-white/20"
aria-label="Close"
>
<X className="h-4 w-4" />
</button>
</div>
<div className="max-h-[70vh] overflow-y-auto p-2 [-ms-overflow-style:none] [scrollbar-width:none] [&::-webkit-scrollbar]:hidden">
{images.map((image, index) => (
<div
key={image.id}
className="flex w-full items-center gap-3 rounded-xl p-2 text-left transition hover:bg-white/5"
>
<button
type="button"
onClick={() => onPick(index)}
className="flex min-w-0 flex-1 items-center gap-3 text-left"
>
<div className="h-16 w-16 shrink-0 overflow-hidden rounded-lg bg-black">
<BubbleImage
src={image.thumbnailUrl ?? image.url}
className="h-full w-full object-cover"
/>
</div>
<div className="min-w-0 flex-1">
<div className="truncate text-sm font-medium text-neutral-100">
{index + 1}
</div>
<div className="mt-1 truncate text-xs text-neutral-400">
{formatBytes(image.sizeBytes)}
</div>
</div>
</button>
<ImageListDownloadButton postId={postId} attachment={image} />
</div>
))}
</div>
</div>
</div>,
document.body,
);
}
export function AlbumBubble({ post }: { post: Post }) {
const { openLightbox } = useLightbox();
const { lang } = useI18n();
const [listOpen, setListOpen] = useState(false);
const images = post.attachments;
const text = postDisplayText(post, lang);
const visible = images.slice(0, MAX_VISIBLE);
@@ -176,21 +43,22 @@ export function AlbumBubble({ post }: { post: Post }) {
>
<button
type="button"
onClick={() => {
if (isLastSlot) setListOpen(true);
else openLightbox(images, i, text, post.id);
}}
className="block h-full w-full"
aria-label={isLastSlot ? "Open image list" : "View image"}
onClick={() => openLightbox(images, i, text, post.id)}
className="group block h-full w-full"
aria-label={
isLastSlot ? `View all ${images.length} images` : "View image"
}
>
<BubbleImage
src={att.thumbnailUrl ?? att.url}
loading="lazy"
className="h-full w-full object-cover"
className="h-full w-full object-cover transition duration-300 group-hover:scale-[1.03]"
/>
{isLastSlot ? (
<div className="absolute inset-0 flex items-center justify-center bg-black/55 text-4xl font-bold text-white">
<div className="absolute inset-0 flex flex-col items-center justify-center gap-0.5 bg-black/60 text-white backdrop-blur-[1px] transition group-hover:bg-black/50">
<span className="text-3xl font-bold leading-none md:text-4xl">
+{extra}
</span>
</div>
) : null}
</button>
@@ -206,17 +74,6 @@ export function AlbumBubble({ post }: { post: Post }) {
{autolink(text)}
</div>
) : null}
{listOpen ? (
<ImageListDialog
postId={post.id}
images={images}
onClose={() => setListOpen(false)}
onPick={(index) => {
setListOpen(false);
openLightbox(images, index, text, post.id);
}}
/>
) : null}
</div>
);
}

View File

@@ -8,9 +8,14 @@ import {
type PropsWithChildren,
} from "react";
import { createPortal } from "react-dom";
import { ChevronLeft, ChevronRight, X } from "lucide-react";
import { ChevronLeft, ChevronRight, LoaderCircle, X } from "lucide-react";
import type { Attachment } from "../../../types/post";
import { DownloadCloudIcon } from "../../icons/DownloadCloudIcon";
import { useI18n } from "../../../i18n";
import { useToast } from "../../Toast";
import { BubbleImage } from "../BubbleImage";
import { autolink } from "../utils/autolink";
import { downloadAttachment } from "../utils/downloadFile";
type LightboxState = {
images: Attachment[];
@@ -65,6 +70,7 @@ export function ImageLightboxProvider({ children }: PropsWithChildren) {
images={state.images}
startIndex={state.index}
caption={state.caption}
postId={state.postId}
onClose={closeLightbox}
/>
) : null}
@@ -72,15 +78,118 @@ export function ImageLightboxProvider({ children }: PropsWithChildren) {
);
}
function LightboxDownloadButton({
postId,
attachment,
}: {
postId: string;
attachment: Attachment;
}) {
const { t } = useI18n();
const { showToast } = useToast();
const [isDownloading, setIsDownloading] = useState(false);
const handleDownload = async () => {
if (isDownloading) return;
setIsDownloading(true);
try {
await downloadAttachment(postId, attachment.id, attachment.filename);
showToast(t("downloadOk"));
} catch {
showToast(t("downloadFail"), "error");
} finally {
setIsDownloading(false);
}
};
return (
<button
type="button"
onClick={(e) => {
e.stopPropagation();
handleDownload();
}}
disabled={isDownloading}
className="flex h-10 w-10 items-center justify-center rounded-full bg-white/10 text-white transition hover:bg-white/20 disabled:cursor-wait"
aria-label={isDownloading ? t("downloading") : t("download")}
aria-busy={isDownloading}
>
{isDownloading ? (
<LoaderCircle className="h-5 w-5 animate-spin" strokeWidth={2.3} />
) : (
<DownloadCloudIcon className="h-5 w-5" />
)}
</button>
);
}
function Filmstrip({
images,
index,
onSelect,
}: {
images: Attachment[];
index: number;
onSelect: (i: number) => void;
}) {
const activeRef = useRef<HTMLButtonElement | null>(null);
useEffect(() => {
activeRef.current?.scrollIntoView({
behavior: "smooth",
block: "nearest",
inline: "center",
});
}, [index]);
return (
<div
className="shrink-0 bg-gradient-to-t from-black/90 to-transparent pb-[max(env(safe-area-inset-bottom),0.75rem)] pt-3"
onClick={(e) => e.stopPropagation()}
>
<div className="mx-auto flex max-w-[920px] gap-2 overflow-x-auto px-4 [-ms-overflow-style:none] [scrollbar-width:none] [&::-webkit-scrollbar]:hidden">
{images.map((image, i) => {
const active = i === index;
return (
<button
key={image.id}
ref={active ? activeRef : null}
type="button"
onClick={(e) => {
e.stopPropagation();
onSelect(i);
}}
className={`relative h-14 w-14 shrink-0 overflow-hidden rounded-lg bg-black transition duration-200 md:h-16 md:w-16 ${
active
? "opacity-100 ring-2 ring-white"
: "opacity-45 hover:opacity-80"
}`}
aria-label={`Image ${i + 1}`}
aria-current={active}
>
<BubbleImage
src={image.thumbnailUrl ?? image.thumbUrl ?? image.url}
className="h-full w-full object-cover"
/>
</button>
);
})}
</div>
</div>
);
}
function LightboxView({
images,
startIndex,
caption: captionText,
postId,
onClose,
}: {
images: Attachment[];
startIndex: number;
caption?: string;
postId?: string;
onClose: () => void;
}) {
const [index, setIndex] = useState(startIndex);
@@ -118,6 +227,7 @@ function LightboxView({
const current = images[index];
const caption = captionText?.trim();
const showCaption = !!caption && isCaptionVisible;
const hasMany = images.length > 1;
if (!current) return null;
return createPortal(
@@ -127,19 +237,36 @@ function LightboxView({
role="dialog"
aria-modal="true"
>
{/* Top bar: counter + actions */}
<div
className="relative z-20 flex shrink-0 items-center justify-between px-4 pb-2 pt-[max(env(safe-area-inset-top),1rem)]"
onClick={(e) => e.stopPropagation()}
>
<div className="flex h-10 items-center">
{hasMany ? (
<span className="rounded-full bg-white/10 px-3 py-1 text-xs font-medium text-white">
{index + 1} / {images.length}
</span>
) : null}
</div>
<div className="flex items-center gap-2">
{postId ? (
<LightboxDownloadButton postId={postId} attachment={current} />
) : null}
<button
type="button"
onClick={(e) => {
e.stopPropagation();
onClose();
}}
className="absolute right-4 top-4 z-10 flex h-10 w-10 items-center justify-center rounded-full bg-white/10 text-white transition hover:bg-white/20"
onClick={onClose}
className="flex h-10 w-10 items-center justify-center rounded-full bg-white/10 text-white transition hover:bg-white/20"
aria-label="Close"
>
<X className="h-5 w-5" />
</button>
</div>
</div>
{images.length > 1 ? (
{/* Image stage */}
<div className="relative flex min-h-0 w-full flex-1 items-center justify-center">
{hasMany ? (
<>
<button
type="button"
@@ -163,15 +290,11 @@ function LightboxView({
>
<ChevronRight className="h-6 w-6" />
</button>
<div className="absolute left-1/2 top-4 z-10 -translate-x-1/2 rounded-full bg-white/10 px-3 py-1 text-xs text-white">
{index + 1} / {images.length}
</div>
</>
) : null}
<div className="flex min-h-0 w-full flex-1 items-center justify-center px-4 py-16">
<div
className="flex h-full w-full items-center justify-center"
className="flex h-full w-full items-center justify-center px-4 py-2"
onClick={(e) => {
e.stopPropagation();
if (caption) setIsCaptionVisible((visible) => !visible);
@@ -192,11 +315,10 @@ function LightboxView({
<img
src={current.url}
alt={current.filename}
className="max-h-full max-w-full object-contain select-none"
className="max-h-full max-w-full select-none object-contain"
draggable={false}
/>
</div>
</div>
{showCaption ? (
<div
@@ -208,6 +330,12 @@ function LightboxView({
</div>
</div>
) : null}
</div>
{/* Bottom thumbnail filmstrip */}
{hasMany ? (
<Filmstrip images={images} index={index} onSelect={setIndex} />
) : null}
</div>,
document.body,
);