Skip to content

Commit a526dca

Browse files
kalvinnchauam
andauthored
feat: navigate images across message threads (#6705)
## Summary - navigate across image occurrences that are currently rendered inside the nearest explicit open-thread DOM scope - preserve rendered DOM order, duplicate occurrence identity, hidden-spoiler exclusion, preview-image entry parity, and the exact thumbnail return target - keep adjacent timeline messages and unrelated surfaces as separate galleries - make Copy and Download follow the current gallery item, including when a link-preview image opens the gallery first and navigation reaches Markdown media ## Scope contract This is intentionally a **rendered-media gallery only**. Gallery membership comes from mounted image triggers under the nearest explicit thread scope. Collapsed or unloaded descendants are excluded until the thread UI renders them. There is no canonical thread fetch, descendant projection, depth override, or synchronous message reparse in this change. `ChannelPane.tsx` remains unchanged from `main`; `useSearchHighlightProps` and both timeline/thread prop spreads are preserved. ## Verification - rebased onto `origin/main` at `113a33b7e49b7173ee1767c49ef2f49c63803034` - `git diff --check` - differential file-size check - full Desktop check (Biome plus text/pubkey policy checks; only pre-existing informational diagnostics) - Desktop TypeScript typecheck - Desktop production build and rebuilt E2E build - full Desktop JS units: `5,476/5,476` passed - full Desktop Tauri workspace tests passed (one documented native performance test ignored) - rebuilt image gallery Playwright spec: `14/14` passed - search-highlight smoke cases: `5/5` passed - reduced diff: 6 files, `+417/-41`; production `+78/-41`, E2E `+339/-0` The four added E2E cases cover cross-message thread navigation and return targeting, rendered-only exclusion of collapsed descendants, preview-first current-item actions, and adjacent-message isolation. --------- Signed-off-by: Kalvin Chau <[email protected]> Signed-off-by: am <6e30cd56c30e030cd31bb0939b94a7c257c9a09d5ba2d92cf2735da45629f248@buzz.block.builderlab.xyz> Co-authored-by: am <6e30cd56c30e030cd31bb0939b94a7c257c9a09d5ba2d92cf2735da45629f248@buzz.block.builderlab.xyz>
1 parent 113a33b commit a526dca

6 files changed

Lines changed: 417 additions & 41 deletions

File tree

‎desktop/src/features/messages/ui/MessageThreadPanel.tsx‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,8 +510,12 @@ export function MessageThreadPanel({
510510
tabIndex={-1}
511511
ref={threadBodyRef}
512512
>
513+
{/* The gallery is intentionally DOM-scoped: only media currently rendered
514+
in this open thread participates. Collapsed or unloaded descendants
515+
join only after the thread UI renders them. */}
513516
<div
514517
className={cn(hasConstrainedColumn && THREAD_PANEL_COLUMN_CLASS)}
518+
data-image-gallery-scope="thread"
515519
ref={threadContentRef}
516520
style={
517521
hasConstrainedColumn ? { maxWidth: columnMaxWidthPx } : undefined

‎desktop/src/shared/ui/markdown.tsx‎

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import {
4343
markdownPropsAreEqual,
4444
} from "./markdownUtils";
4545
import { ImageMosaic } from "./markdown/ImageMosaic";
46+
import { ImageGalleryStatus } from "./markdown/ImageGalleryStatus";
4647
import { ImageLightboxZoomControls } from "./markdown/ImageLightboxZoomControls";
4748
import {
4849
CODE_BLOCK_CLASS,
@@ -145,12 +146,30 @@ type WebKitGestureLikeEvent = Event & {
145146
scale?: number;
146147
};
147148

149+
function copyImageToClipboard(src: string | undefined) {
150+
if (!src) return;
151+
invokeTauri("copy_image_to_clipboard", { url: src })
152+
.then(() => {
153+
toast.success("Copied to clipboard");
154+
})
155+
.catch((err: unknown) => {
156+
const msg = err instanceof Error ? err.message : "Copy failed";
157+
toast.error(msg);
158+
});
159+
}
160+
161+
function downloadImage(src: string | undefined) {
162+
if (!src) return;
163+
invokeTauri("download_image", { url: src }).catch((err: unknown) => {
164+
const msg = err instanceof Error ? err.message : "Download failed";
165+
toast.error(msg);
166+
});
167+
}
168+
148169
function ImageZoomOverlay({
149170
alt,
150171
galleryIndex = 0,
151172
galleryItems,
152-
onCopy,
153-
onDownload,
154173
onClose,
155174
resolvedSrc,
156175
sourceBox,
@@ -161,8 +180,6 @@ function ImageZoomOverlay({
161180
alt: string | undefined;
162181
galleryIndex?: number;
163182
galleryItems?: ImageGalleryItem[];
164-
onCopy: (src: string | undefined) => void;
165-
onDownload: (src: string | undefined) => void;
166183
onClose: () => void;
167184
resolvedSrc: string;
168185
sourceBox: ImageLightboxBox;
@@ -718,13 +735,13 @@ function ImageZoomOverlay({
718735
const handleMenuCopy = React.useCallback(() => {
719736
setMenu(null);
720737
markControlGesture();
721-
onCopy(currentItem.src);
722-
}, [currentItem.src, markControlGesture, onCopy]);
738+
copyImageToClipboard(currentItem.src);
739+
}, [currentItem.src, markControlGesture]);
723740
const handleMenuDownload = React.useCallback(() => {
724741
setMenu(null);
725742
markControlGesture();
726-
onDownload(currentItem.src);
727-
}, [currentItem.src, markControlGesture, onDownload]);
743+
downloadImage(currentItem.src);
744+
}, [currentItem.src, markControlGesture]);
728745

729746
return createPortal(
730747
<div
@@ -859,7 +876,7 @@ function ImageZoomOverlay({
859876
custom={galleryDirection}
860877
exit="exit"
861878
initial="enter"
862-
key={currentItem.resolvedSrc}
879+
key={`${currentIndex}:${currentItem.resolvedSrc}`}
863880
src={currentItem.resolvedSrc}
864881
transition={{
865882
duration: prefersReducedMotion
@@ -936,7 +953,7 @@ function ImageZoomOverlay({
936953
type="button"
937954
onClick={(event) => {
938955
event.stopPropagation();
939-
onDownload(currentItem.src);
956+
downloadImage(currentItem.src);
940957
}}
941958
>
942959
<Download className="h-4 w-4" />
@@ -952,6 +969,7 @@ function ImageZoomOverlay({
952969
updateZoom={updateZoom}
953970
zoom={zoom}
954971
/>
972+
<ImageGalleryStatus {...{ currentIndex, itemCount: items.length }} />
955973
</div>
956974
</div>
957975
{menu && canActOnCurrentImage ? (
@@ -1000,7 +1018,6 @@ function ImageBlock({ alt, dim, resolvedSrc, src, thumbSrc }: ImageBlockProps) {
10001018
const triggerRef = React.useRef<HTMLButtonElement | null>(null);
10011019
useSmoothCorners(inlineImageRef);
10021020
useSmoothCorners(thumbnailImageRef);
1003-
10041021
const [spoilerMediaSize, setSpoilerMediaSize] = React.useState<{
10051022
height: number;
10061023
src: string;
@@ -1078,7 +1095,6 @@ function ImageBlock({ alt, dim, resolvedSrc, src, thumbSrc }: ImageBlockProps) {
10781095

10791096
return () => observer.disconnect();
10801097
}, []);
1081-
10821098
const closeMenu = React.useCallback(() => setMenu(null), []);
10831099
useDismissMediaContextMenu(Boolean(menu), closeMenu);
10841100

@@ -1089,7 +1105,6 @@ function ImageBlock({ alt, dim, resolvedSrc, src, thumbSrc }: ImageBlockProps) {
10891105
e.nativeEvent.stopImmediatePropagation();
10901106
setMenu({ x: e.clientX, y: e.clientY });
10911107
};
1092-
10931108
const openLightbox = React.useCallback(
10941109
(image: HTMLImageElement) => {
10951110
if (!resolvedSrc || isInsideHiddenSpoiler(image)) {
@@ -1113,6 +1128,7 @@ function ImageBlock({ alt, dim, resolvedSrc, src, thumbSrc }: ImageBlockProps) {
11131128
{
11141129
alt,
11151130
dim,
1131+
trigger: triggerRef.current,
11161132
resolvedSrc,
11171133
src,
11181134
thumbnailBox: sourceBox,
@@ -1140,27 +1156,13 @@ function ImageBlock({ alt, dim, resolvedSrc, src, thumbSrc }: ImageBlockProps) {
11401156

11411157
const handleCopyImage = React.useCallback((copySrc: string | undefined) => {
11421158
setMenu(null);
1143-
if (!copySrc) return;
1144-
invokeTauri("copy_image_to_clipboard", { url: copySrc })
1145-
.then(() => {
1146-
toast.success("Copied to clipboard");
1147-
})
1148-
.catch((err: unknown) => {
1149-
const msg = err instanceof Error ? err.message : "Copy failed";
1150-
toast.error(msg);
1151-
});
1159+
copyImageToClipboard(copySrc);
11521160
}, []);
11531161

11541162
const handleDownload = React.useCallback(
11551163
(downloadSrc: string | undefined) => {
11561164
setMenu(null);
1157-
if (!downloadSrc) return;
1158-
invokeTauri("download_image", { url: downloadSrc }).catch(
1159-
(err: unknown) => {
1160-
const msg = err instanceof Error ? err.message : "Download failed";
1161-
toast.error(msg);
1162-
},
1163-
);
1165+
downloadImage(downloadSrc);
11641166
},
11651167
[],
11661168
);
@@ -1215,8 +1217,6 @@ function ImageBlock({ alt, dim, resolvedSrc, src, thumbSrc }: ImageBlockProps) {
12151217
alt={alt}
12161218
galleryIndex={lightboxState.galleryIndex}
12171219
galleryItems={lightboxState.galleryItems}
1218-
onCopy={handleCopyImage}
1219-
onDownload={handleDownload}
12201220
onClose={() => setLightboxState(null)}
12211221
resolvedSrc={resolvedSrc}
12221222
sourceBox={lightboxState.sourceBox}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
type ImageGalleryStatusProps = {
2+
currentIndex: number;
3+
itemCount: number;
4+
};
5+
6+
export function ImageGalleryStatus({
7+
currentIndex,
8+
itemCount,
9+
}: ImageGalleryStatusProps) {
10+
if (itemCount <= 1) {
11+
return null;
12+
}
13+
14+
const position = currentIndex + 1;
15+
return (
16+
<>
17+
<div
18+
aria-hidden="true"
19+
className="h-5 w-px shrink-0 bg-muted-foreground/15"
20+
/>
21+
<span
22+
aria-label={`Image ${position} of ${itemCount}`}
23+
aria-live="polite"
24+
className="min-w-9 text-center text-xs font-medium tabular-nums text-muted-foreground"
25+
role="status"
26+
>
27+
{position} / {itemCount}
28+
</span>
29+
</>
30+
);
31+
}

‎desktop/src/shared/ui/markdown/LinkPreviewImageLightbox.tsx‎

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
type ImageLightboxCornerRadii,
1111
imageLightboxBoxFromRect,
1212
imageLightboxCornerRadiiFromElement,
13+
imageLightboxSourceScopeForTrigger,
1314
visibleImageGalleryForTrigger,
1415
} from "./imageLightbox";
1516

@@ -18,17 +19,13 @@ type ImageZoomOverlayProps = {
1819
galleryIndex?: number;
1920
galleryItems?: ImageGalleryItem[];
2021
onClose: () => void;
21-
onCopy: (src: string | undefined) => void;
22-
onDownload: (src: string | undefined) => void;
2322
resolvedSrc: string;
2423
sourceBox: ImageLightboxBox;
2524
sourceCornerRadii: ImageLightboxCornerRadii;
2625
sourceScope?: Element | null;
2726
src: string | undefined;
2827
};
2928

30-
const ignoreUnavailableImageAction = () => undefined;
31-
3229
export function createLinkPreviewImageLightbox(
3330
ImageZoomOverlay: ComponentType<ImageZoomOverlayProps>,
3431
): ComponentType<LinkPreviewImageLightboxProps> {
@@ -52,7 +49,7 @@ export function createLinkPreviewImageLightbox(
5249

5350
const sourceBox = imageLightboxBoxFromRect(rect);
5451
const sourceCornerRadii = imageLightboxCornerRadiiFromElement(image);
55-
const sourceScope = trigger.closest("[data-link-preview-list]");
52+
const sourceScope = imageLightboxSourceScopeForTrigger(trigger);
5653
const dim =
5754
image.naturalWidth > 0 && image.naturalHeight > 0
5855
? `${image.naturalWidth}x${image.naturalHeight}`
@@ -62,6 +59,7 @@ export function createLinkPreviewImageLightbox(
6259
{
6360
alt,
6461
dim,
62+
trigger,
6563
resolvedSrc: src,
6664
src: undefined,
6765
thumbnailBox: sourceBox,
@@ -103,8 +101,6 @@ export function createLinkPreviewImageLightbox(
103101
galleryIndex={lightboxState.galleryIndex}
104102
galleryItems={lightboxState.galleryItems}
105103
onClose={() => setLightboxState(null)}
106-
onCopy={ignoreUnavailableImageAction}
107-
onDownload={ignoreUnavailableImageAction}
108104
resolvedSrc={src}
109105
sourceBox={lightboxState.sourceBox}
110106
sourceCornerRadii={lightboxState.sourceCornerRadii}

‎desktop/src/shared/ui/markdown/imageLightbox.ts‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export type ImageGalleryDirection = "forward" | "backward";
3232
export type ImageGalleryItem = {
3333
alt: string | undefined;
3434
dim?: string;
35+
trigger?: HTMLElement;
3536
resolvedSrc: string;
3637
src: string | undefined;
3738
thumbnailBox?: ImageLightboxBox;
@@ -339,12 +340,15 @@ function imageLightboxThumbnailTargetForItem(
339340
sourceScope: Element | null | undefined,
340341
): ImageLightboxThumbnailTarget | null {
341342
const root = sourceScope?.isConnected ? sourceScope : document.body;
342-
const triggers = Array.from(
343-
root.querySelectorAll<HTMLElement>("[data-image-lightbox-trigger]"),
344-
);
343+
const triggers = item.trigger
344+
? [item.trigger]
345+
: Array.from(
346+
root.querySelectorAll<HTMLElement>("[data-image-lightbox-trigger]"),
347+
);
345348

346349
for (const trigger of triggers) {
347350
const isCurrentItem =
351+
item.trigger != null ||
348352
trigger.dataset.imageLightboxResolvedSrc === item.resolvedSrc ||
349353
(item.src != null && trigger.dataset.imageLightboxSrc === item.src);
350354
if (!isCurrentItem) {
@@ -386,6 +390,7 @@ export function imageLightboxSourceScopeForTrigger(
386390
trigger: HTMLElement,
387391
): Element | null {
388392
return (
393+
trigger.closest("[data-image-gallery-scope]") ??
389394
trigger.closest(IMAGE_LIGHTBOX_MARKDOWN_SCOPE_SELECTOR) ??
390395
trigger.closest("[data-testid='message-row']")
391396
);
@@ -409,6 +414,7 @@ function imageGalleryItemFromTrigger(
409414
return {
410415
alt: trigger.dataset.imageLightboxAlt || undefined,
411416
dim: trigger.dataset.imageLightboxDim || inferredDim,
417+
trigger,
412418
resolvedSrc,
413419
src: trigger.dataset.imageLightboxSrc || undefined,
414420
thumbnailBox: thumbnail?.box,

0 commit comments

Comments
 (0)