Skip to content

Build your personal knowledge base with Trilium Notes

feat(llm): open chat PDFs in the lightbox as well

Elian Dorand1a05ca3a811
diff --git a/apps/client/src/components/app_context.ts b/apps/client/src/components/app_context.ts
index 275d147b69..333a6d96d3 100644
--- a/apps/client/src/components/app_context.ts
+++ b/apps/client/src/components/app_context.ts
@@ -22,9 +22,9 @@ import { AddLinkOpts } from "../widgets/dialogs/add_link.jsx";
import type { ConfirmWithMessageOptions, ConfirmWithTitleOptions } from "../widgets/dialogs/confirm.js";
import type { ResolveOptions } from "../widgets/dialogs/delete_notes.js";
import { IconPickerOpts } from "../widgets/dialogs/icon_picker.jsx";
-import type { ImageLightboxOptions } from "../widgets/dialogs/image_lightbox.jsx";
import { IncludeNoteOpts } from "../widgets/dialogs/include_note.jsx";
import type { InfoProps } from "../widgets/dialogs/info.jsx";
+import type { LightboxOptions } from "../widgets/dialogs/lightbox.jsx";
import type { MarkdownImportOpts } from "../widgets/dialogs/markdown_import.jsx";
import { ChooseNoteTypeCallback } from "../widgets/dialogs/note_type_chooser.jsx";
import type { PrintPreviewData } from "../widgets/dialogs/print_preview.jsx";
@@ -157,7 +157,7 @@ export type CommandMappings = {
showItemPickerDialog: ItemPickerDialogOptions;
showNotePickerDialog: NotePickerDialogOptions;
showInfoDialog: InfoProps;
- showImageLightbox: ImageLightboxOptions;
+ showLightbox: LightboxOptions;
showConfirmDialog: ConfirmWithMessageOptions;
showRecentChanges: CommandData & { ancestorNoteId: string };
showDeletedNotes: CommandData & { ancestorNoteId?: string };
diff --git a/apps/client/src/layouts/layout_commons.tsx b/apps/client/src/layouts/layout_commons.tsx
index d2d0ae7545..3c00ac670c 100644
--- a/apps/client/src/layouts/layout_commons.tsx
+++ b/apps/client/src/layouts/layout_commons.tsx
@@ -33,7 +33,7 @@ export function applyModals(rootContainer: RootContainer) {
.child(<LazyDialog triggerEvents={["showRevisions"]} loader={() => import("../widgets/dialogs/revisions.js")} />)
.child(<LazyDialog triggerEvents={["showDeleteNotesDialog"]} loader={() => import("../widgets/dialogs/delete_notes.js")} />)
.child(<LazyDialog triggerEvents={["showPrintPreview"]} loader={() => import("../widgets/dialogs/print_preview.jsx")} />)
- .child(<LazyDialog triggerEvents={["showImageLightbox"]} loader={() => import("../widgets/dialogs/image_lightbox.js")} />)
+ .child(<LazyDialog triggerEvents={["showLightbox"]} loader={() => import("../widgets/dialogs/lightbox.js")} />)
.child(<LazyDialog triggerEvents={["showInfoDialog"]} loader={() => import("../widgets/dialogs/info.js")} />)
.child(<LazyDialog triggerEvents={["showConfirmDialog", "showConfirmDeleteNoteBoxWithNoteDialog"]} loader={() => import("../widgets/dialogs/confirm.js")} />)
.child(<LazyDialog triggerEvents={["showPromptDialog"]} loader={() => import("../widgets/dialogs/prompt.js")} />)
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.json
index 055f752502..7c071f5773 100644
--- a/apps/client/src/translations/en/translation.json
+++ b/apps/client/src/translations/en/translation.json
@@ -1229,9 +1229,12 @@
"first_image": "First image",
"last_image": "Last image"
},
- "viewport": "Image view",
- "lightbox_title": "Image",
- "open_original": "Open the original image in a new tab"
+ "viewport": "Image view"
+ },
+ "lightbox": {
+ "image": "Image",
+ "pdf": "PDF",
+ "open_original": "Open the original in a new tab"
},
"shortcut_hints": {
"show_button": "Show shortcut hints",
diff --git a/apps/client/src/widgets/dialogs/image_lightbox.tsx b/apps/client/src/widgets/dialogs/image_lightbox.tsx
deleted file mode 100644
index 341ffe0485..0000000000
--- a/apps/client/src/widgets/dialogs/image_lightbox.tsx
+++ /dev/null
@@ -1,52 +0,0 @@
-import "./image_lightbox.css";
-
-import { useState } from "preact/hooks";
-
-import { t } from "../../services/i18n";
-import { useTriliumEvent } from "../react/hooks";
-import ImageViewer from "../react/ImageViewer";
-import Modal from "../react/Modal";
-import { ZOOM_PAN_HINTS } from "../react/zoom_pan_keyboard";
-
-export interface ImageLightboxOptions {
- /** The URL of the image to show. */
- src: string;
- /** Shown in the dialog header and used as the image's alt text. */
- title?: string;
-}
-
-/**
- * Shows a single image in a modal, zoomable and pannable through {@link ImageViewer}. Summon it from
- * anywhere with `appContext.triggerEvent("showImageLightbox", { src, title })`.
- */
-export default function ImageLightboxDialog() {
- const [ opts, setOpts ] = useState<ImageLightboxOptions>();
- const [ shown, setShown ] = useState(false);
-
- useTriliumEvent("showImageLightbox", (opts) => {
- setOpts(opts);
- setShown(true);
- });
-
- const title = opts?.title || t("image_viewer.lightbox_title");
-
- return (
- <Modal
- className="image-lightbox-dialog"
- size="xl"
- title={title}
- show={shown}
- onHidden={() => setShown(false)}
- isFullPageOnMobile
- customTitleBarButtons={[ opts ? {
- title: t("image_viewer.open_original"),
- iconClassName: "bx-link-external",
- onClick: () => window.open(opts.src, "_blank", "noopener,noreferrer")
- } : null ]}
- >
- {shown && opts && (
- <ImageViewer src={opts.src} alt={title} shortcutHints={ZOOM_PAN_HINTS} />
- )}
- </Modal>
- );
-}
diff --git a/apps/client/src/widgets/dialogs/image_lightbox.css b/apps/client/src/widgets/dialogs/lightbox.css
similarity index 66%
rename from apps/client/src/widgets/dialogs/image_lightbox.css
rename to apps/client/src/widgets/dialogs/lightbox.css
index 6319436f2b..79f49419a0 100644
--- a/apps/client/src/widgets/dialogs/image_lightbox.css
+++ b/apps/client/src/widgets/dialogs/lightbox.css
@@ -1,4 +1,4 @@
-.modal.image-lightbox-dialog {
+.modal.lightbox-dialog {
.modal-body {
height: 80vh;
padding: 0;
@@ -6,7 +6,7 @@
}
}
-body.mobile .modal.image-lightbox-dialog .modal-body {
+body.mobile .modal.lightbox-dialog .modal-body {
flex: 1;
height: auto;
min-height: 0;
diff --git a/apps/client/src/widgets/dialogs/lightbox.tsx b/apps/client/src/widgets/dialogs/lightbox.tsx
new file mode 100644
index 0000000000..18a1ab8419
--- /dev/null
+++ b/apps/client/src/widgets/dialogs/lightbox.tsx
@@ -0,0 +1,61 @@
+import "./lightbox.css";
+
+import { useState } from "preact/hooks";
+
+import { t } from "../../services/i18n";
+import { useTriliumEvent } from "../react/hooks";
+import ImageViewer from "../react/ImageViewer";
+import Modal from "../react/Modal";
+import { ZOOM_PAN_HINTS } from "../react/zoom_pan_keyboard";
+import PdfViewer from "../type_widgets/file/PdfViewer";
+
+export interface LightboxOptions {
+ /**
+ * The URL of the file to show. For a PDF it must be root-relative, as `getPdfUrl()` returns it,
+ * because the viewer resolves relative URLs against its own directory.
+ */
+ src: string;
+ /** Defaults to `"image"`. */
+ kind?: "image" | "pdf";
+ /** Shown in the dialog header and used as an image's alt text. */
+ title?: string;
+}
+
+/**
+ * Shows a single image or PDF in a modal: an image zoomable and pannable through {@link ImageViewer},
+ * a PDF read-only in {@link PdfViewer}. Summon it from anywhere with
+ * `appContext.triggerEvent("showLightbox", { src, kind, title })`.
+ */
+export default function LightboxDialog() {
+ const [ opts, setOpts ] = useState<LightboxOptions>();
+ const [ shown, setShown ] = useState(false);
+
+ useTriliumEvent("showLightbox", (opts) => {
+ setOpts(opts);
+ setShown(true);
+ });
+
+ const kind = opts?.kind ?? "image";
+ const title = opts?.title || t(`lightbox.${kind}`);
+
+ return (
+ <Modal
+ className="lightbox-dialog"
+ size="xl"
+ title={title}
+ show={shown}
+ onHidden={() => setShown(false)}
+ isFullPageOnMobile
+ customTitleBarButtons={[ opts ? {
+ title: t("lightbox.open_original"),
+ iconClassName: "bx-link-external",
+ onClick: () => window.open(opts.src, "_blank", "noopener,noreferrer")
+ } : null ]}
+ >
+ {shown && opts && (kind === "pdf"
+ ? <PdfViewer pdfUrl={opts.src} />
+ : <ImageViewer src={opts.src} alt={title} shortcutHints={ZOOM_PAN_HINTS} />
+ )}
+ </Modal>
+ );
+}
diff --git a/apps/client/src/widgets/react/ImageLightboxLink.spec.tsx b/apps/client/src/widgets/react/LightboxLink.spec.tsx
similarity index 68%
rename from apps/client/src/widgets/react/ImageLightboxLink.spec.tsx
rename to apps/client/src/widgets/react/LightboxLink.spec.tsx
index 75ef570ba7..920fa892bc 100644
--- a/apps/client/src/widgets/react/ImageLightboxLink.spec.tsx
+++ b/apps/client/src/widgets/react/LightboxLink.spec.tsx
@@ -3,9 +3,9 @@ import { act } from "preact/test-utils";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import appContext from "../../components/app_context";
-import ImageLightboxLink from "./ImageLightboxLink";
+import LightboxLink from "./LightboxLink";
-describe("ImageLightboxLink", () => {
+describe("LightboxLink", () => {
let container: HTMLElement;
beforeEach(() => {
@@ -21,16 +21,18 @@ describe("ImageLightboxLink", () => {
it("opens the lightbox on a plain click and passes modified clicks on to the link handler", () => {
const triggerEvent = vi.spyOn(appContext, "triggerEvent").mockResolvedValue(undefined);
+ const lightbox = { src: "/api/attachments/abc/open", kind: "pdf" as const, title: "report.pdf" };
act(() => render(
- <ImageLightboxLink src="api/attachments/abc/image/cat.png" title="cat.png">
- <img alt="cat.png" />
- </ImageLightboxLink>, container));
+ <LightboxLink lightbox={lightbox} href="#root/n1?viewMode=attachments&attachmentId=abc">
+ report.pdf
+ </LightboxLink>, container));
const link = container.querySelector("a");
- expect(link?.getAttribute("href")).toBe("api/attachments/abc/image/cat.png");
+ expect(link?.getAttribute("href")).toBe("#root/n1?viewMode=attachments&attachmentId=abc");
expect(link?.getAttribute("target")).toBe("_blank");
+ expect(link?.getAttribute("title")).toBe("report.pdf");
- // `goToLink` in `services/link.ts` listens on the document and opens `api/` links in a new tab.
+ // `goToLink` in `services/link.ts` listens on the document and opens links in a new tab.
const reachedDocument: string[] = [];
const onDocumentEvent = (e: Event) => reachedDocument.push(e.type);
document.addEventListener("click", onDocumentEvent);
@@ -39,10 +41,7 @@ describe("ImageLightboxLink", () => {
try {
act(() => { link?.dispatchEvent(new MouseEvent("click", { bubbles: true, cancelable: true, button: 0 })); });
act(() => { link?.dispatchEvent(new MouseEvent("dblclick", { bubbles: true, cancelable: true, button: 0 })); });
- expect(triggerEvent).toHaveBeenCalledExactlyOnceWith("showImageLightbox", {
- src: "api/attachments/abc/image/cat.png",
- title: "cat.png"
- });
+ expect(triggerEvent).toHaveBeenCalledExactlyOnceWith("showLightbox", lightbox);
expect(reachedDocument).toEqual([]);
act(() => {
@@ -55,4 +54,13 @@ describe("ImageLightboxLink", () => {
document.removeEventListener("dblclick", onDocumentEvent);
}
});
+
+ it("links to the file itself when no href is given", () => {
+ act(() => render(
+ <LightboxLink lightbox={{ src: "api/attachments/abc/image/cat.png" }}>
+ <img alt="cat.png" />
+ </LightboxLink>, container));
+
+ expect(container.querySelector("a")?.getAttribute("href")).toBe("api/attachments/abc/image/cat.png");
+ });
});
diff --git a/apps/client/src/widgets/react/ImageLightboxLink.tsx b/apps/client/src/widgets/react/LightboxLink.tsx
similarity index 56%
rename from apps/client/src/widgets/react/ImageLightboxLink.tsx
rename to apps/client/src/widgets/react/LightboxLink.tsx
index 8cc8217c8c..7cfb0d26bb 100644
--- a/apps/client/src/widgets/react/ImageLightboxLink.tsx
+++ b/apps/client/src/widgets/react/LightboxLink.tsx
@@ -1,35 +1,36 @@
import type { ComponentChildren, TargetedMouseEvent } from "preact";
import appContext from "../../components/app_context";
+import type { LightboxOptions } from "../dialogs/lightbox";
-interface ImageLightboxLinkProps {
- /** The URL of the full-size image, opened in the lightbox and used as the link's `href`. */
- src: string;
- /** The image's name, shown in the lightbox header and as the link's tooltip. */
- title?: string;
+interface LightboxLinkProps {
+ /** What the lightbox shows; its `title` is also the link's tooltip. */
+ lightbox: LightboxOptions;
+ /** Where a modified or middle click goes. Defaults to `lightbox.src`. */
+ href?: string;
className?: string;
children: ComponentChildren;
}
/**
- * Wraps an image preview so that a click opens `src` in the image lightbox dialog. A click with a
- * modifier key, or a middle click, goes on to `goToLink()` in `services/link.ts`, which opens the raw
- * image in a new tab.
+ * Wraps a preview of an image or PDF so that a click opens it in the lightbox dialog. A click with a
+ * modifier key, or a middle click, goes on to `goToLink()` in `services/link.ts`, which opens `href`
+ * in a new tab.
*/
-export default function ImageLightboxLink({ src, title, className, children }: ImageLightboxLinkProps) {
+export default function LightboxLink({ lightbox, href = lightbox.src, className, children }: LightboxLinkProps) {
function onClick(e: TargetedMouseEvent<HTMLAnchorElement>) {
if (e.button !== 0 || e.ctrlKey || e.metaKey || e.shiftKey || e.altKey) return;
stopLinkNavigation(e);
- appContext.triggerEvent("showImageLightbox", { src, title });
+ appContext.triggerEvent("showLightbox", lightbox);
}
return (
<a
- href={src}
+ href={href}
target="_blank"
rel="noopener noreferrer"
className={className}
- title={title}
+ title={lightbox.title}
onClick={onClick}
onDblClick={stopLinkNavigation}
>
diff --git a/apps/client/src/widgets/type_widgets/llm_chat/ChatInputBar.css b/apps/client/src/widgets/type_widgets/llm_chat/ChatInputBar.css
index 796766876c..c1f669b150 100644
--- a/apps/client/src/widgets/type_widgets/llm_chat/ChatInputBar.css
+++ b/apps/client/src/widgets/type_widgets/llm_chat/ChatInputBar.css
@@ -64,10 +64,12 @@
width: 56px;
}
-.llm-chat-attachment-image-link {
+.llm-chat-attachment-preview-link {
display: block;
width: 100%;
height: 100%;
+ color: inherit;
+ text-decoration: none;
cursor: zoom-in;
}
diff --git a/apps/client/src/widgets/type_widgets/llm_chat/ChatInputBar.tsx b/apps/client/src/widgets/type_widgets/llm_chat/ChatInputBar.tsx
index eb32098bf7..43258d0177 100644
--- a/apps/client/src/widgets/type_widgets/llm_chat/ChatInputBar.tsx
+++ b/apps/client/src/widgets/type_widgets/llm_chat/ChatInputBar.tsx
@@ -15,7 +15,7 @@ import CKEditor, { type CKEditorApi } from "../../react/CKEditor.js";
import Dropdown from "../../react/Dropdown.js";
import { FormListHeader, FormListItem } from "../../react/FormList.js";
import { useLegacyImperativeHandlers, useTriliumOption } from "../../react/hooks.js";
-import ImageLightboxLink from "../../react/ImageLightboxLink.js";
+import LightboxLink from "../../react/LightboxLink.js";
import MaskedIcon from "../../react/MaskedIcon.js";
import AddProviderModal, { type LlmProviderConfig, type ProviderStep } from "../options/llm/AddProviderModal.js";
import { providerIconUrl } from "../options/llm/provider_icons.js";
@@ -25,9 +25,9 @@ import { editorHtmlToMarkdown } from "./chat_input_markdown.js";
import { shortModelName } from "./model_name.js";
import ReasoningEffortDropdown from "./ReasoningEffortDropdown.js";
import { SafeImage } from "./retry_image.js";
-import { useChatAttachments } from "./useChatAttachments.js";
+import { getAttachmentLightbox, useChatAttachments } from "./useChatAttachments.js";
import { type ModelOption, resolveSelectedModel } from "../../../services/llm_providers.js";
-import { type UseLlmChatReturn } from "./useLlmChat.js";
+import { type AttachmentBlock, type UseLlmChatReturn } from "./useLlmChat.js";
const READ_ONLY_LOCK = "llm-chat-streaming";
@@ -300,16 +300,7 @@ export default function ChatInputBar({
className={`llm-chat-attachment-chip llm-chat-attachment-chip-${att.type}`}
title={att.title}
>
- {att.type === "image" ? (
- <ImageLightboxLink src={att.url} title={att.title} className="llm-chat-attachment-image-link">
- <SafeImage src={att.url} alt={att.title} />
- </ImageLightboxLink>
- ) : (
- <div className="llm-chat-attachment-file">
- <span className={`bx ${att.type === "file" ? "bxs-file-pdf" : "bxs-file-blank"} llm-chat-attachment-file-icon`} />
- <span className="llm-chat-attachment-file-name">{att.title}</span>
- </div>
- )}
+ <AttachmentChipPreview att={att} />
<button
type="button"
className="llm-chat-attachment-remove"
@@ -597,6 +588,23 @@ export default function ChatInputBar({
);
}
+/** The inside of a pending attachment's chip; an image or PDF opens in the lightbox when clicked. */
+function AttachmentChipPreview({ att }: { att: AttachmentBlock }) {
+ const lightbox = getAttachmentLightbox(att);
+ const preview = att.type === "image"
+ ? <SafeImage src={att.url} alt={att.title} />
+ : (
+ <div className="llm-chat-attachment-file">
+ <span className={`bx ${att.type === "file" ? "bxs-file-pdf" : "bxs-file-blank"} llm-chat-attachment-file-icon`} />
+ <span className="llm-chat-attachment-file-name">{att.title}</span>
+ </div>
+ );
+
+ return lightbox
+ ? <LightboxLink lightbox={lightbox} href={att.url} className="llm-chat-attachment-preview-link">{preview}</LightboxLink>
+ : preview;
+}
+
/**
* One of the model's per-conversation capability switches, as an icon toggle whose state
* reads without opening anything. The label is the tooltip.
diff --git a/apps/client/src/widgets/type_widgets/llm_chat/ChatMessage.tsx b/apps/client/src/widgets/type_widgets/llm_chat/ChatMessage.tsx
index 454bd68265..712be9e412 100644
--- a/apps/client/src/widgets/type_widgets/llm_chat/ChatMessage.tsx
+++ b/apps/client/src/widgets/type_widgets/llm_chat/ChatMessage.tsx
@@ -10,7 +10,7 @@ import utils from "../../../services/utils.js";
import { ExtendedAdmonition } from "../../react/Admonition.js";
import Button from "../../react/Button.js";
import { useResizeObserver } from "../../react/hooks.js";
-import ImageLightboxLink from "../../react/ImageLightboxLink.js";
+import LightboxLink from "../../react/LightboxLink.js";
import LoadingSpinner from "../../react/LoadingSpinner.js";
import { ReadOnlyTextContent } from "../text/ReadOnlyText.js";
import { formatErrorDetails } from "./chat_error.js";
@@ -21,6 +21,7 @@ import { type ContentBlock, type FileBlock, getMessageText, type ImageBlock, typ
import { shortModelName } from "./model_name.js";
import { SafeImage } from "./retry_image.js";
import ToolCallCard from "./ToolCallCard.js";
+import { getAttachmentLightbox } from "./useChatAttachments.js";
function shortenNumber(n: number): string {
if (n >= 1_000_000) return `${(n / 1_000_000).toFixed(1)}M`;
@@ -356,20 +357,34 @@ function renderContentBlocks(blocks: ContentBlock[], isStreaming?: boolean) {
if (group.type === "image") {
return (
- <ImageLightboxLink
+ <LightboxLink
key={group.index}
- src={group.block.url}
- title={group.block.title}
+ lightbox={{ src: group.block.url, title: group.block.title }}
className="llm-chat-message-image"
>
<SafeImage src={group.block.url} alt={group.block.title} />
- </ImageLightboxLink>
+ </LightboxLink>
);
}
if (group.type === "file" || group.type === "text_file") {
const icon = group.type === "file" ? "bxs-file-pdf" : "bxs-file-blank";
- return (
+ const lightbox = getAttachmentLightbox(group.block);
+ const content = <>
+ <span className={`bx ${icon}`} />
+ <span className="llm-chat-message-file-name">{group.block.title}</span>
+ </>;
+
+ return lightbox ? (
+ <LightboxLink
+ key={group.index}
+ lightbox={lightbox}
+ href={group.block.url}
+ className="llm-chat-message-file"
+ >
+ {content}
+ </LightboxLink>
+ ) : (
<a
key={group.index}
href={group.block.url}
@@ -378,8 +393,7 @@ function renderContentBlocks(blocks: ContentBlock[], isStreaming?: boolean) {
className="llm-chat-message-file"
title={group.block.title}
>
- <span className={`bx ${icon}`} />
- <span className="llm-chat-message-file-name">{group.block.title}</span>
+ {content}
</a>
);
}
diff --git a/apps/client/src/widgets/type_widgets/llm_chat/useChatAttachments.spec.ts b/apps/client/src/widgets/type_widgets/llm_chat/useChatAttachments.spec.ts
new file mode 100644
index 0000000000..dc1bd6ddd4
--- /dev/null
+++ b/apps/client/src/widgets/type_widgets/llm_chat/useChatAttachments.spec.ts
@@ -0,0 +1,19 @@
+import { describe, expect, it } from "vitest";
+
+import { getAttachmentLightbox } from "./useChatAttachments";
+
+describe("getAttachmentLightbox", () => {
+ it("previews images and PDFs, and nothing else", () => {
+ expect(getAttachmentLightbox({
+ type: "image", attachmentId: "img1", mime: "image/png", title: "cat.png", url: "api/attachments/img1/image/cat.png"
+ })).toEqual({ src: "api/attachments/img1/image/cat.png", title: "cat.png" });
+
+ expect(getAttachmentLightbox({
+ type: "file", attachmentId: "pdf1", mime: "application/pdf", title: "report.pdf", url: "#root/n1?attachmentId=pdf1"
+ })).toEqual({ src: "/api/attachments/pdf1/open", kind: "pdf", title: "report.pdf" });
+
+ expect(getAttachmentLightbox({
+ type: "text_file", attachmentId: "txt1", mime: "text/plain", title: "notes.txt", url: "#root/n1?attachmentId=txt1"
+ })).toBeUndefined();
+ });
+});
diff --git a/apps/client/src/widgets/type_widgets/llm_chat/useChatAttachments.ts b/apps/client/src/widgets/type_widgets/llm_chat/useChatAttachments.ts
index 95d72f6170..de348c266f 100644
--- a/apps/client/src/widgets/type_widgets/llm_chat/useChatAttachments.ts
+++ b/apps/client/src/widgets/type_widgets/llm_chat/useChatAttachments.ts
@@ -4,6 +4,8 @@ import { useCallback, useRef } from "preact/hooks";
import { t } from "../../../services/i18n.js";
import server from "../../../services/server.js";
import toast from "../../../services/toast.js";
+import type { LightboxOptions } from "../../dialogs/lightbox.js";
+import { getPdfUrl } from "../file/PdfViewer.js";
import type { FileBlock, ImageBlock, TextFileBlock } from "./llm_chat_types.js";
import type { AttachmentBlock, UseLlmChatReturn } from "./useLlmChat.js";
@@ -224,3 +226,17 @@ export function useChatAttachments(chat: UseLlmChatReturn): UseChatAttachmentsRe
handleDragOver
};
}
+
+/**
+ * What the lightbox shows for an attachment: an image, or a PDF read through
+ * `attachments/<id>/open`. Other files have no preview and return `undefined`.
+ */
+export function getAttachmentLightbox(att: AttachmentBlock): LightboxOptions | undefined {
+ if (att.type === "image") {
+ return { src: att.url, title: att.title };
+ }
+ if (att.type === "file" && att.mime === "application/pdf") {
+ return { src: getPdfUrl(`attachments/${att.attachmentId}/open`), kind: "pdf", title: att.title };
+ }
+ return undefined;
+}
diff --git a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/AI.html b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/AI.html
index 98d60f2278..a74a269c9e 100644
--- a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/AI.html
+++ b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/AI.html
@@ -199,10 +199,12 @@ class="admonition note">
<li>Images have a small thumbnail for easy identifications.</li>
<li>Every attachment can be deleted by pressing their corresponding X button.</li>
</ul>
-<p>Clicking an image, either its thumbnail above the text box or the image
- shown in a sent message, opens it in a viewer where it can be zoomed and
- panned. The button next to the viewer's close button opens the original
- image in a new tab; so does <kbd>Ctrl</kbd>-clicking the image.</p>
+<p>Clicking an image or a PDF, either its chip above the text box or where
+ it appears in a sent message, opens it in a viewer: an image can be zoomed
+ and panned there, a PDF read page by page. The button next to the viewer's
+ close button opens the original file in a new browser tab. <kbd>Ctrl</kbd>-clicking
+ instead opens an image in a new browser tab, and a PDF as an attachment
+ in a new Trilium tab.</p>
<p>When an attachment is present, the LLM is instructed to consider the attachment
with priority, even if it has access to the current note.</p>
<aside class="admonition note">
diff --git a/docs/User Guide/User Guide/AI.md b/docs/User Guide/User Guide/AI.md
index 35261a7dfb..73742b9d47 100644
--- a/docs/User Guide/User Guide/AI.md
+++ b/docs/User Guide/User Guide/AI.md
@@ -144,7 +144,7 @@ Once one or more attachments are uploaded, they will appear directly above the t
* Images have a small thumbnail for easy identifications.
* Every attachment can be deleted by pressing their corresponding X button.
-Clicking an image, either its thumbnail above the text box or the image shown in a sent message, opens it in a viewer where it can be zoomed and panned. The button next to the viewer's close button opens the original image in a new tab; so does <kbd>Ctrl</kbd>\-clicking the image.
+Clicking an image or a PDF, either its chip above the text box or where it appears in a sent message, opens it in a viewer: an image can be zoomed and panned there, a PDF read page by page. The button next to the viewer's close button opens the original file in a new browser tab. <kbd>Ctrl</kbd>\-clicking instead opens an image in a new browser tab, and a PDF as an attachment in a new Trilium tab.
When an attachment is present, the LLM is instructed to consider the attachment with priority, even if it has access to the current note.