Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go

MUL-7518: fix(editor): keep an image's known kind when opening its preview (#8591)

Bohan Jiang55bc4df1734f
diff --git a/packages/views/editor/attachment-preview-modal.test.tsx b/packages/views/editor/attachment-preview-modal.test.tsx
index 51b5a8851d..6fe14fef73 100644
--- a/packages/views/editor/attachment-preview-modal.test.tsx
+++ b/packages/views/editor/attachment-preview-modal.test.tsx
@@ -536,6 +536,21 @@ describe("AttachmentPreviewModal — URL-only source", () => {
expect(screen.getByText("This file type can't be previewed.")).toBeTruthy();
});
+ it("renders an <img> for a caller-declared image whose filename is a caption (MUL-7518)", () => {
+ // A body image's "filename" is the markdown caption — prose, with no
+ // extension to read. The caller knows the slot is an image and says so.
+ const url = "https://cdn.example.test/chart.png?Signature=s";
+ render(
+ <AttachmentPreviewModal
+ source={{ kind: "url", url, filename: "报告图表", forceKind: "image" }}
+ open
+ onClose={() => {}}
+ />,
+ );
+ expect(screen.queryByText("This file type can't be previewed.")).toBeNull();
+ expect(document.querySelector("img")?.getAttribute("src")).toBe(url);
+ });
+
it("Download button opens the raw URL externally when no attachment id is available", () => {
const url = "https://cdn.example.test/orphan.pdf?Signature=s";
render(
@@ -702,6 +717,34 @@ describe("useAttachmentPreview — tryOpen gate", () => {
expect(opened).toBe(false);
});
+ it("accepts a URL source whose kind the caller declares, extension or not (MUL-7518)", () => {
+ const { result } = renderHook(() => useAttachmentPreview());
+ let opened = false;
+ hookAct(() => {
+ opened = result.current.tryOpen({
+ kind: "url",
+ url: "https://x/chart.png",
+ filename: "报告图表",
+ forceKind: "image",
+ });
+ });
+ expect(opened).toBe(true);
+ });
+
+ it("still rejects a declared text kind from a URL source — the id gate wins", () => {
+ const { result } = renderHook(() => useAttachmentPreview());
+ let opened = true;
+ hookAct(() => {
+ opened = result.current.tryOpen({
+ kind: "url",
+ url: "https://x/notes",
+ filename: "notes",
+ forceKind: "markdown",
+ });
+ });
+ expect(opened).toBe(false);
+ });
+
it("rejects a source whose filename isn't a previewable type", () => {
const { result } = renderHook(() => useAttachmentPreview());
let opened = true;
diff --git a/packages/views/editor/attachment-preview-modal.tsx b/packages/views/editor/attachment-preview-modal.tsx
index 0b489deea2..5ea4bcd8a3 100644
--- a/packages/views/editor/attachment-preview-modal.tsx
+++ b/packages/views/editor/attachment-preview-modal.tsx
@@ -97,7 +97,22 @@ import { CodeBlockStatic } from "./code-block-static";
export type PreviewSource =
| { kind: "full"; attachment: Attachment }
- | { kind: "url"; url: string; filename: string };
+ | {
+ kind: "url";
+ url: string;
+ filename: string;
+ /**
+ * What the call site already knows this source to be. A URL-only source
+ * has no content-type, so without it the modal can only re-derive the
+ * kind from `filename` — and for a body image that "filename" is the
+ * markdown caption, which is prose, not a file name. `![报告图表](…png)`
+ * then reads as an extension-less unknown and the reader is told the
+ * image can't be previewed (MUL-7518). Callers that know the slot is
+ * definitionally an image (markdown `![]()`, the Tiptap image node, any
+ * member of an image sequence) pass it through instead of guessing.
+ */
+ forceKind?: PreviewKind;
+ };
// PreviewKinds that can render from a URL-only source. Text-based kinds
// (markdown / html / text) need the /content proxy which is ID-keyed.
@@ -111,6 +126,13 @@ interface PreviewState {
contentType: string;
mediaUrl: string;
attachmentId: string | null;
+ /**
+ * The kind every consumer dispatches on — resolved once, here, so the
+ * tryOpen gate and the rendered panel can never disagree about what the
+ * source is. A URL-only source's `forceKind` wins over autodetect; a full
+ * attachment always has server metadata to detect from.
+ */
+ kind: PreviewKind | null;
}
function resolvePreviewMediaUrl(attachment: Attachment): string {
@@ -134,6 +156,10 @@ function normalize(source: PreviewSource): PreviewState {
contentType: source.attachment.content_type,
mediaUrl: resolvePreviewMediaUrl(source.attachment),
attachmentId: source.attachment.id,
+ kind: getPreviewKind(
+ source.attachment.content_type,
+ source.attachment.filename,
+ ),
};
}
return {
@@ -141,6 +167,7 @@ function normalize(source: PreviewSource): PreviewState {
contentType: "",
mediaUrl: resolvePublicFileUrl(source.url) ?? source.url,
attachmentId: null,
+ kind: source.forceKind ?? getPreviewKind("", source.filename),
};
}
@@ -209,8 +236,7 @@ export function useAttachmentPreview(): AttachmentPreviewHandle {
setPreviewOpen(true);
}, []);
const tryOpen = useCallback((source: PreviewSource) => {
- const state = normalize(source);
- const kind = getPreviewKind(state.contentType, state.filename);
+ const { kind } = normalize(source);
if (!kind) return false;
// URL-only sources cannot drive text kinds — the /content proxy is ID-keyed.
if (source.kind === "url" && !URL_ONLY_KINDS.has(kind)) return false;
@@ -356,7 +382,7 @@ export function AttachmentPreviewModal({
return () => document.removeEventListener("keydown", handler);
}, [open, onClose, onPrev, onNext]);
- const kind = getPreviewKind(state.contentType, state.filename);
+ const kind = state.kind;
// Download dispatcher: re-sign through `getAttachment` when an id is
// available; otherwise fall back to opening the (possibly stale) URL
diff --git a/packages/views/editor/attachment.test.tsx b/packages/views/editor/attachment.test.tsx
index 0b3c9768c6..512e2d29d9 100644
--- a/packages/views/editor/attachment.test.tsx
+++ b/packages/views/editor/attachment.test.tsx
@@ -698,6 +698,26 @@ describe("Attachment — image dispatch", () => {
expect(screen.queryByText("Uploading")).toBeNull();
});
+ it("View opens the image preview when the caption is prose, not a filename (MUL-7518)", () => {
+ // Outside an image sequence the dispatcher falls back to its own
+ // single-image preview. It must carry the kind it already resolved over
+ // to the modal — re-reading "报告图表" as a filename finds no extension
+ // and used to leave the reader on "can't be previewed".
+ renderWithQuery(
+ <Attachment
+ attachment={{
+ kind: "url",
+ url: "https://external.example/chart.png",
+ filename: "报告图表",
+ forceKind: "image",
+ }}
+ />,
+ );
+ fireEvent.click(screen.getByTitle("View"));
+ expect(screen.queryByText("This file type can't be previewed.")).toBeNull();
+ expect(screen.getByRole("dialog").querySelector("img")).toBeTruthy();
+ });
+
it("external image (no resolver match) renders <img> and falls back to openByUrl on Download", () => {
renderWithQuery(
<Attachment
diff --git a/packages/views/editor/attachment.tsx b/packages/views/editor/attachment.tsx
index 6c995977cb..cfc2617985 100644
--- a/packages/views/editor/attachment.tsx
+++ b/packages/views/editor/attachment.tsx
@@ -375,6 +375,12 @@ export function Attachment({
kind: "url",
url: mediaUrl,
filename: state.filename,
+ // This dispatcher has already decided what the slot is — from the
+ // call site's `forceKind`, or from a content-type the modal's
+ // URL-only source doesn't carry. Hand that answer over instead of
+ // letting the modal re-guess from `filename`, which for a body image
+ // is the markdown caption (MUL-7518).
+ forceKind: kind ?? undefined,
});
}
};
diff --git a/packages/views/editor/image-sequence-context.test.tsx b/packages/views/editor/image-sequence-context.test.tsx
index a77b73c0af..ea311d433a 100644
--- a/packages/views/editor/image-sequence-context.test.tsx
+++ b/packages/views/editor/image-sequence-context.test.tsx
@@ -49,6 +49,8 @@ vi.mock("sonner", () => ({ toast: { error: toastErrorMock } }));
const STRINGS: Record<string, Record<string, string>> = {
image: {
download: "Download",
+ view: "View",
+ copy_link: "Copy link",
canvas_label: "Image canvas",
previous: "Previous image",
next: "Next image",
@@ -85,6 +87,8 @@ import {
ImageSequenceProvider,
useImageSequencePreview,
} from "./image-sequence-context";
+import { Attachment as InlineAttachment } from "./attachment";
+import { AttachmentDownloadProvider } from "./attachment-download-context";
function render(ui: ReactElement) {
const qc = new QueryClient({
@@ -328,6 +332,69 @@ describe("ImageSequenceProvider", () => {
});
});
+// An agent that rewrites an issue body swaps the markdown image URL but does
+// not register the new file as an attachment of that issue, so these images
+// reach the viewer with no server metadata at all — the caption is the only
+// "filename" there is. It is a caption, not a file name (MUL-7518).
+describe("body images with no attachment record", () => {
+ function CaptionedBody({ captions }: { captions: string[] }) {
+ const urls = captions.map((_, i) => `https://cdn.example.test/chart-${i}.png`);
+ const content = captions
+ .map((caption, i) => `![${caption}](${urls[i]})`)
+ .join("\n\n");
+ return (
+ <AttachmentDownloadProvider attachments={[]}>
+ <ImageSequenceProvider
+ items={collectImageSequence([{ content, attachments: [] }])}
+ >
+ {captions.map((caption, i) => (
+ <InlineAttachment
+ key={urls[i]}
+ attachment={{
+ kind: "url",
+ url: urls[i]!,
+ filename: caption,
+ forceKind: "image",
+ }}
+ />
+ ))}
+ </ImageSequenceProvider>
+ </AttachmentDownloadProvider>
+ );
+ }
+
+ it.each(["报告图表", ""])(
+ "zooms a body image whose caption is %j, not a filename",
+ (caption) => {
+ render(<CaptionedBody captions={[caption]} />);
+
+ act(() => {
+ fireEvent.click(screen.getAllByTitle("View")[0]!);
+ });
+
+ expect(
+ screen.queryByText("This file type can't be previewed."),
+ ).toBeNull();
+ expect(screen.getByRole("dialog").querySelector("img")).not.toBeNull();
+ },
+ );
+
+ it("still pages between captioned body images", () => {
+ render(<CaptionedBody captions={["报告图表", "对比图"]} />);
+
+ act(() => {
+ fireEvent.click(screen.getAllByTitle("View")[0]!);
+ });
+ expectCounter("1 / 2");
+
+ act(() => {
+ fireEvent.click(nextButton());
+ });
+ expectCounter("2 / 2");
+ expect(screen.getByRole("dialog").querySelector("img")).not.toBeNull();
+ });
+});
+
describe("useImageSequencePreview without a provider", () => {
it("reports false so the caller can fall back to a single preview", () => {
const seen: boolean[] = [];
diff --git a/packages/views/editor/image-sequence-context.tsx b/packages/views/editor/image-sequence-context.tsx
index ee7a6ebb54..e61bd52888 100644
--- a/packages/views/editor/image-sequence-context.tsx
+++ b/packages/views/editor/image-sequence-context.tsx
@@ -75,7 +75,18 @@ export function useImageSequencePreview(): ImageSequenceApi {
function toPreviewSource(item: ImageSequenceItem): PreviewSource {
return item.attachment
? { kind: "full", attachment: item.attachment }
- : { kind: "url", url: item.url, filename: item.filename };
+ : // Everything in a sequence is an image by construction —
+ // `collectImageSequence` admits nothing else — so the viewer says so
+ // rather than letting the modal re-derive it from `filename`. For an
+ // item with no attachment record that field holds the markdown caption
+ // (`![报告图表](…)`), which is prose and has no extension to read
+ // (MUL-7518).
+ {
+ kind: "url",
+ url: item.url,
+ filename: item.filename,
+ forceKind: "image",
+ };
}
interface Session {