Skip to content

Build your personal knowledge base with Trilium Notes

feat(llm): open chat images in a zoomable lightbox

Elian Doran67aab6f51bee
diff --git a/apps/client/src/components/app_context.ts b/apps/client/src/components/app_context.ts
index 2deb25c72d..275d147b69 100644
--- a/apps/client/src/components/app_context.ts
+++ b/apps/client/src/components/app_context.ts
@@ -22,6 +22,7 @@ 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 { MarkdownImportOpts } from "../widgets/dialogs/markdown_import.jsx";
@@ -156,6 +157,7 @@ export type CommandMappings = {
showItemPickerDialog: ItemPickerDialogOptions;
showNotePickerDialog: NotePickerDialogOptions;
showInfoDialog: InfoProps;
+ showImageLightbox: ImageLightboxOptions;
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 a67ae8640d..d2d0ae7545 100644
--- a/apps/client/src/layouts/layout_commons.tsx
+++ b/apps/client/src/layouts/layout_commons.tsx
@@ -33,6 +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={["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 563bf382fa..055f752502 100644
--- a/apps/client/src/translations/en/translation.json
+++ b/apps/client/src/translations/en/translation.json
@@ -1229,7 +1229,9 @@
"first_image": "First image",
"last_image": "Last image"
},
- "viewport": "Image view"
+ "viewport": "Image view",
+ "lightbox_title": "Image",
+ "open_original": "Open the original image in a new tab"
},
"shortcut_hints": {
"show_button": "Show shortcut hints",
diff --git a/apps/client/src/widgets/dialogs/image_lightbox.css b/apps/client/src/widgets/dialogs/image_lightbox.css
new file mode 100644
index 0000000000..6319436f2b
--- /dev/null
+++ b/apps/client/src/widgets/dialogs/image_lightbox.css
@@ -0,0 +1,14 @@
+.modal.image-lightbox-dialog {
+ .modal-body {
+ height: 80vh;
+ padding: 0;
+ overflow: hidden;
+ }
+}
+
+body.mobile .modal.image-lightbox-dialog .modal-body {
+ flex: 1;
+ height: auto;
+ min-height: 0;
+ overflow: hidden;
+}
diff --git a/apps/client/src/widgets/dialogs/image_lightbox.tsx b/apps/client/src/widgets/dialogs/image_lightbox.tsx
new file mode 100644
index 0000000000..341ffe0485
--- /dev/null
+++ b/apps/client/src/widgets/dialogs/image_lightbox.tsx
@@ -0,0 +1,52 @@
+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/react/ImageLightboxLink.spec.tsx b/apps/client/src/widgets/react/ImageLightboxLink.spec.tsx
new file mode 100644
index 0000000000..75ef570ba7
--- /dev/null
+++ b/apps/client/src/widgets/react/ImageLightboxLink.spec.tsx
@@ -0,0 +1,58 @@
+import { render } from "preact";
+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";
+
+describe("ImageLightboxLink", () => {
+ let container: HTMLElement;
+
+ beforeEach(() => {
+ container = document.createElement("div");
+ document.body.appendChild(container);
+ });
+
+ afterEach(() => {
+ render(null, container);
+ container.remove();
+ vi.restoreAllMocks();
+ });
+
+ 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);
+ act(() => render(
+ <ImageLightboxLink src="api/attachments/abc/image/cat.png" title="cat.png">
+ <img alt="cat.png" />
+ </ImageLightboxLink>, container));
+
+ const link = container.querySelector("a");
+ expect(link?.getAttribute("href")).toBe("api/attachments/abc/image/cat.png");
+ expect(link?.getAttribute("target")).toBe("_blank");
+
+ // `goToLink` in `services/link.ts` listens on the document and opens `api/` links in a new tab.
+ const reachedDocument: string[] = [];
+ const onDocumentEvent = (e: Event) => reachedDocument.push(e.type);
+ document.addEventListener("click", onDocumentEvent);
+ document.addEventListener("dblclick", onDocumentEvent);
+
+ 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(reachedDocument).toEqual([]);
+
+ act(() => {
+ link?.dispatchEvent(new MouseEvent("click", { bubbles: true, cancelable: true, button: 0, ctrlKey: true }));
+ });
+ expect(triggerEvent).toHaveBeenCalledOnce();
+ expect(reachedDocument).toEqual([ "click" ]);
+ } finally {
+ document.removeEventListener("click", onDocumentEvent);
+ document.removeEventListener("dblclick", onDocumentEvent);
+ }
+ });
+});
diff --git a/apps/client/src/widgets/react/ImageLightboxLink.tsx b/apps/client/src/widgets/react/ImageLightboxLink.tsx
new file mode 100644
index 0000000000..8cc8217c8c
--- /dev/null
+++ b/apps/client/src/widgets/react/ImageLightboxLink.tsx
@@ -0,0 +1,45 @@
+import type { ComponentChildren, TargetedMouseEvent } from "preact";
+
+import appContext from "../../components/app_context";
+
+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;
+ 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.
+ */
+export default function ImageLightboxLink({ src, title, className, children }: ImageLightboxLinkProps) {
+ 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 });
+ }
+
+ return (
+ <a
+ href={src}
+ target="_blank"
+ rel="noopener noreferrer"
+ className={className}
+ title={title}
+ onClick={onClick}
+ onDblClick={stopLinkNavigation}
+ >
+ {children}
+ </a>
+ );
+}
+
+/** Keeps the event from the browser and from `goToLink()`, which listens on the document. */
+function stopLinkNavigation(e: MouseEvent) {
+ e.preventDefault();
+ e.stopPropagation();
+}
diff --git a/apps/client/src/widgets/react/ImageViewer.tsx b/apps/client/src/widgets/react/ImageViewer.tsx
index 1964e06ade..61ef198957 100644
--- a/apps/client/src/widgets/react/ImageViewer.tsx
+++ b/apps/client/src/widgets/react/ImageViewer.tsx
@@ -20,6 +20,8 @@ interface ImageViewerProps {
alt?: string;
minScale?: number;
maxScale?: number;
+ /** The shortcut hints to offer; defaults to zoom, pan and navigation between a folder's images. */
+ shortcutHints?: ShortcutHintDefinition;
}
/** Beyond this multiple of the image's native resolution, switch to crisp (non-smoothed) rendering. */
@@ -56,7 +58,9 @@ export function evaluateImageZoom(scale: number, img: { naturalWidth: number; cl
* Interactive image viewer: the image is fit to the viewport on load, then the user can zoom
* (wheel/pinch/buttons/keyboard) and pan (drag/keyboard). Double-clicking resets to the fitted view.
*/
-export default function ImageViewer({ src, imgClassName, alt = "", minScale = 0.5, maxScale = 50 }: ImageViewerProps) {
+export default function ImageViewer({
+ src, imgClassName, alt = "", minScale = 0.5, maxScale = 50, shortcutHints = IMAGE_VIEWER_HINTS
+}: ImageViewerProps) {
const [ pannable, setPannable ] = useState(false);
const [ panning, setPanning ] = useState(false);
const [ largeZoom, setLargeZoom ] = useState(false);
@@ -119,7 +123,7 @@ export default function ImageViewer({ src, imgClassName, alt = "", minScale = 0.
useZoomPanKeyboard(zoom.ref, rootEl);
useZoomPanWheel(zoom.ref, rootEl);
- useContextualShortcutHints(IMAGE_VIEWER_HINTS);
+ useContextualShortcutHints(shortcutHints);
const wrapperClass = [
"image-viewer-viewport",
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 144c0d6ec8..796766876c 100644
--- a/apps/client/src/widgets/type_widgets/llm_chat/ChatInputBar.css
+++ b/apps/client/src/widgets/type_widgets/llm_chat/ChatInputBar.css
@@ -64,6 +64,13 @@
width: 56px;
}
+.llm-chat-attachment-image-link {
+ display: block;
+ width: 100%;
+ height: 100%;
+ cursor: zoom-in;
+}
+
.llm-chat-attachment-chip-image img {
width: 100%;
height: 100%;
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 5790ffea7d..eb32098bf7 100644
--- a/apps/client/src/widgets/type_widgets/llm_chat/ChatInputBar.tsx
+++ b/apps/client/src/widgets/type_widgets/llm_chat/ChatInputBar.tsx
@@ -15,6 +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 MaskedIcon from "../../react/MaskedIcon.js";
import AddProviderModal, { type LlmProviderConfig, type ProviderStep } from "../options/llm/AddProviderModal.js";
import { providerIconUrl } from "../options/llm/provider_icons.js";
@@ -300,7 +301,9 @@ export default function ChatInputBar({
title={att.title}
>
{att.type === "image" ? (
- <SafeImage src={att.url} alt={att.title} />
+ <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`} />
diff --git a/apps/client/src/widgets/type_widgets/llm_chat/ChatMessage.css b/apps/client/src/widgets/type_widgets/llm_chat/ChatMessage.css
index 9d7d95046c..c4209ff30f 100644
--- a/apps/client/src/widgets/type_widgets/llm_chat/ChatMessage.css
+++ b/apps/client/src/widgets/type_widgets/llm_chat/ChatMessage.css
@@ -69,6 +69,7 @@ body.background-effects.theme-supports-background-effects .llm-chat-message-user
display: block;
margin: 0.5rem 0;
max-width: 100%;
+ cursor: zoom-in;
}
.llm-chat-message-image img {
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 d9fcc77c32..454bd68265 100644
--- a/apps/client/src/widgets/type_widgets/llm_chat/ChatMessage.tsx
+++ b/apps/client/src/widgets/type_widgets/llm_chat/ChatMessage.tsx
@@ -10,6 +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 LoadingSpinner from "../../react/LoadingSpinner.js";
import { ReadOnlyTextContent } from "../text/ReadOnlyText.js";
import { formatErrorDetails } from "./chat_error.js";
@@ -355,16 +356,14 @@ function renderContentBlocks(blocks: ContentBlock[], isStreaming?: boolean) {
if (group.type === "image") {
return (
- <a
+ <ImageLightboxLink
key={group.index}
- href={group.block.url}
- target="_blank"
- rel="noopener noreferrer"
- className="llm-chat-message-image"
+ src={group.block.url}
title={group.block.title}
+ className="llm-chat-message-image"
>
<SafeImage src={group.block.url} alt={group.block.title} />
- </a>
+ </ImageLightboxLink>
);
}
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 ddb427b127..98d60f2278 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,6 +199,10 @@ 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>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 115970c62d..35261a7dfb 100644
--- a/docs/User Guide/User Guide/AI.md
+++ b/docs/User Guide/User Guide/AI.md
@@ -144,6 +144,8 @@ 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.
+
When an attachment is present, the LLM is instructed to consider the attachment with priority, even if it has access to the current note.
> [!NOTE]