fix(web): navigate markdown images as galleries (#10625)
This commit is contained in:
1 parent
5a18fb95e4
commit
6df0add6e6
5 files changed
+107
-40
No files matched your search
@@ -83,9 +83,14 @@ export const ExpandedImageDialog = memo(function ExpandedImageDialog({
|
||||
}
|
||||
: source;
|
||||
|
||||
const navigateImage = useCallback((direction: -1 | 1) => {
|
||||
setImageOffset((current) => current + direction);
|
||||
}, []);
|
||||
const navigateImage = useCallback(
|
||||
(direction: -1 | 1) => {
|
||||
setImageOffset(
|
||||
(current) => (current + direction + preview.images.length) % preview.images.length,
|
||||
);
|
||||
},
|
||||
[preview.images.length],
|
||||
);
|
||||
|
||||
// The element that opened the preview gets focus back on close. Without
|
||||
// this a close button click leaves focus on the unmounted dialog, and the
|
||||
@@ -161,13 +166,13 @@ export const ExpandedImageDialog = memo(function ExpandedImageDialog({
|
||||
{preview.images.length > 1 && (
|
||||
<Button
|
||||
type="button"
|
||||
size="icon"
|
||||
variant="ghost"
|
||||
className="absolute left-2 top-1/2 z-20 -translate-y-1/2 text-white/90 hover:bg-white/10 hover:text-white sm:left-6"
|
||||
size="icon-xl"
|
||||
variant="overlay"
|
||||
className="absolute left-2 top-1/2 z-20 -translate-y-1/2 sm:left-6"
|
||||
aria-label="Previous image"
|
||||
onClick={() => navigateImage(-1)}
|
||||
>
|
||||
<ChevronLeftIcon className="size-5" />
|
||||
<ChevronLeftIcon className="size-7" />
|
||||
</Button>
|
||||
)}
|
||||
<MediaActions source={actionsSource}>
|
||||
@@ -209,7 +214,7 @@ export const ExpandedImageDialog = memo(function ExpandedImageDialog({
|
||||
onError={() => setFailedImageSrc(item.src)}
|
||||
/>
|
||||
)}
|
||||
<div className="mt-2 flex max-w-[92vw] items-center justify-center gap-1.5 text-xs text-muted-foreground/80">
|
||||
<div className="mt-2 flex max-w-[92vw] items-center justify-center gap-1.5 text-xs text-white/80">
|
||||
<span className="truncate">
|
||||
{item.name}
|
||||
{preview.images.length > 1 ? ` (${index + 1}/${preview.images.length})` : ""}
|
||||
@@ -247,13 +252,13 @@ export const ExpandedImageDialog = memo(function ExpandedImageDialog({
|
||||
{preview.images.length > 1 && (
|
||||
<Button
|
||||
type="button"
|
||||
size="icon"
|
||||
variant="ghost"
|
||||
className="absolute right-2 top-1/2 z-20 -translate-y-1/2 text-white/90 hover:bg-white/10 hover:text-white sm:right-6"
|
||||
size="icon-xl"
|
||||
variant="overlay"
|
||||
className="absolute right-2 top-1/2 z-20 -translate-y-1/2 sm:right-6"
|
||||
aria-label="Next image"
|
||||
onClick={() => navigateImage(1)}
|
||||
>
|
||||
<ChevronRightIcon className="size-5" />
|
||||
<ChevronRightIcon className="size-7" />
|
||||
</Button>
|
||||
)}
|
||||
</div>,
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
import { mediaKindFromPath } from "@t3tools/shared/filePreview";
|
||||
import { mediaUrlReference } from "@t3tools/client-runtime/media-reference";
|
||||
import type { ExpandedImageItem, ExpandedImagePreview } from "./ExpandedImagePreview";
|
||||
import { resolveExternalWebLinkHost } from "./externalLinkContextMenu";
|
||||
import { resolveProtocolRelativeMediaUrl } from "../media/mediaContent";
|
||||
|
||||
// Weak keys retain resolved media actions only while the rendered image is reachable.
|
||||
export const markdownImageItems = new WeakMap<Element, ExpandedImageItem>();
|
||||
|
||||
/** Collect in document order only when opened, including PR sections separated by videos. */
|
||||
export function markdownImageGallery(
|
||||
element: Element,
|
||||
selected: ExpandedImageItem,
|
||||
): ExpandedImagePreview {
|
||||
const scope = element.closest("[data-image-gallery]") ?? element.closest(".chat-markdown");
|
||||
const images: ExpandedImageItem[] = [];
|
||||
let index = -1;
|
||||
for (const image of scope?.querySelectorAll("img") ?? []) {
|
||||
const registered = markdownImageItems.get(image);
|
||||
if (!registered) continue;
|
||||
const link = image.closest("a");
|
||||
const href = link?.getAttribute("href") ?? "";
|
||||
if (link && mediaKindFromPath(href) !== "image") continue;
|
||||
const linkedSource =
|
||||
resolveExternalWebLinkHost(href) !== null ? resolveProtocolRelativeMediaUrl(href) : null;
|
||||
const reference = mediaUrlReference(href);
|
||||
const item = linkedSource
|
||||
? {
|
||||
...registered,
|
||||
src: linkedSource,
|
||||
originalUrl: href,
|
||||
actionsSource: {
|
||||
kind: "image" as const,
|
||||
name: registered.name,
|
||||
src: linkedSource,
|
||||
...(reference ? { reference } : {}),
|
||||
},
|
||||
}
|
||||
: registered;
|
||||
if (
|
||||
image === element ||
|
||||
(!markdownImageItems.has(element) && index < 0 && item.src === selected.src)
|
||||
) {
|
||||
index = images.length;
|
||||
images.push(selected);
|
||||
} else {
|
||||
images.push(item);
|
||||
}
|
||||
}
|
||||
return index < 0 ? { images: [selected], index: 0 } : { images, index };
|
||||
}
|
||||
Reference in new issue
Block a user