Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
client/image viewer: add support for navigating through attachments
Adorian Doran419144d3cdfa
diff --git a/apps/client/src/widgets/react/SiblingNavigator.tsx b/apps/client/src/widgets/react/SiblingNavigator.tsxindex 0e55698ae5..0485552598 100644--- a/apps/client/src/widgets/react/SiblingNavigator.tsx+++ b/apps/client/src/widgets/react/SiblingNavigator.tsx@@ -7,20 +7,24 @@ import type NoteContext from "../../components/note_context";import type FNote from "../../entities/fnote";import froca from "../../services/froca";import { t } from "../../services/i18n";+import type { ViewScope } from "../../services/link";+import type LoadResults from "../../services/load_results";import { isMobile } from "../../services/utils";import { useStaticTooltip, useTriliumEvent } from "./hooks";-import { codeToSiblingDirection, getParentFromNotePath, getSiblingNavigation, isInteractiveTarget, isTextEntryTarget } from "./sibling_navigation";+import { codeToSiblingDirection, getParentFromNotePath, getSiblingNavigation, isInteractiveTarget, isTextEntryTarget, sameRoleAttachments } from "./sibling_navigation";const NO_KEYS: readonly string[] = [];interface SiblingNavigatorProps {note?: FNote;noteContext?: NoteContext;- /** Match siblings of this type; defaults to the current note's type. */+ /** When it carries an `attachmentId`, navigation operates over the note's attachments instead of its siblings. */+ viewScope?: ViewScope;+ /** For note siblings, match this type; defaults to the current note's type. (Ignored for attachments.) */siblingType?: string;- /** i18n translation key (resolved via `t()`) for the previous-button tooltip; receives the target note's title as `{{title}}`. E.g. `"image_navigation.previous"`. */+ /** i18n translation key (resolved via `t()`) for the previous-button tooltip; receives the target's title as `{{title}}`. E.g. `"image_navigation.previous"`. */previousTooltipI18nKey: string;- /** i18n translation key (resolved via `t()`) for the next-button tooltip; receives the target note's title as `{{title}}`. E.g. `"image_navigation.next"`. */+ /** i18n translation key (resolved via `t()`) for the next-button tooltip; receives the target's title as `{{title}}`. E.g. `"image_navigation.next"`. */nextTooltipI18nKey: string;/*** Optional element to scope keyboard navigation to: keys act only while focus is inside it. When@@ -44,81 +48,43 @@ interface SiblingNavigationState {}/**- * Computes prev/next navigation among the current note's same-type siblings within the parent of the- * current tab (clone-aware), exposing the target notes' titles. Pass `siblingType` to match a different- * type. Reusable for any note type; returns null when there is nothing to navigate. Served from froca.+ * The iterator behind sibling navigation: where the ordered siblings come from, which one is current,+ * how to move to one, and which entity changes should refresh the list. Swapping it switches navigation+ * between notes and attachments — see {@link noteSiblingProvider} / {@link attachmentSiblingProvider}.*/-export function useSiblingNavigation(note: FNote | undefined, noteContext: NoteContext | undefined, siblingType?: string): SiblingNavigationState | null {- const notePath = noteContext?.notePath;- const type = siblingType ?? note?.type;- const [ siblings, setSiblings ] = useState<{ noteId: string; title: string }[]>([]);- const [ refreshCounter, setRefreshCounter ] = useState(0);-- useEffect(() => {- const parent = getParentFromNotePath(notePath);- if (!parent || !type) {- setSiblings([]);- return;- }-- let active = true;- froca.getNote(parent.parentNoteId)- .then((parentNote) => parentNote?.getChildNotes() ?? [])- .then((children) => {- if (active) setSiblings(children.filter((child) => child.type === type).map((child) => ({ noteId: child.noteId, title: child.title })));- })- .catch(() => { if (active) setSiblings([]); });- return () => { active = false; };- }, [ notePath, type, refreshCounter ]);-- useTriliumEvent("entitiesReloaded", ({ loadResults }) => {- const parent = getParentFromNotePath(notePath);- if (!parent) return;- const branchesChanged = loadResults.getBranchRows().some((branch) => branch.parentNoteId === parent.parentNoteId);- const siblingChanged = loadResults.getNoteIds().some((noteId) => siblings.some((sibling) => sibling.noteId === noteId));- if (branchesChanged || siblingChanged) setRefreshCounter((counter) => counter + 1);- });-- // froca replaces every cached FNote on a full reload (e.g. a protected-session unlock), so re-fetch- // to refresh the sibling titles held as plain strings in state.- useTriliumEvent("frocaReloaded", () => setRefreshCounter((counter) => counter + 1));-- const parent = getParentFromNotePath(notePath);- if (!note || !parent) return null;- const navigation = getSiblingNavigation(siblings.map((sibling) => sibling.noteId), note.noteId);- if (!navigation) return null;-- const titleOf = (noteId: string) => siblings.find((sibling) => sibling.noteId === noteId)?.title ?? "";- const navigateTo = (noteId: string) => void noteContext?.setNote(`${parent.parentPath}/${noteId}`);- return {- index: navigation.index,- total: navigation.total,- previousTitle: titleOf(navigation.previous),- nextTitle: titleOf(navigation.next),- navigatePrevious: () => navigateTo(navigation.previous),- navigateNext: () => navigateTo(navigation.next),- navigateFirst: () => navigateTo(navigation.first),- navigateLast: () => navigateTo(navigation.last)- };+export interface SiblingNavigationProvider {+ /** Id of the item currently shown (a noteId or attachmentId); undefined when there is nothing to navigate. */+ currentId: string | undefined;+ /** Changes whenever the loaded collection or current item changes; used as the load effect's key. */+ depsKey: string;+ /** Loads the ordered siblings (including the current item) as `{ id, title }`. */+ loadSiblings(): Promise<{ id: string; title: string }[]>;+ /** Navigates the current tab to the sibling with the given id. */+ navigateTo(id: string): void;+ /** Whether an `entitiesReloaded` event touches this collection (given the currently-loaded ids). */+ shouldRefresh(loadResults: LoadResults, currentSiblingIds: string[]): boolean;}/**- * Floating previous/next navigation between a note's same-type siblings (within the current tab's- * parent), with a "<index>/<total>" indicator and tooltips naming the target note. The tooltip text- * comes from the caller-provided i18n keys (`previousTooltipI18nKey`/`nextTooltipI18nKey`), so each note type can- * phrase it ("Previous image: …", "Previous video: …"). Renders nothing when there is no sibling to- * move between.+ * Floating previous/next navigation between a note's same-type siblings — or, when shown over a single+ * attachment, the note's same-role attachments — with a "<index>/<total>" indicator and tooltips naming+ * the target. The iterator is chosen automatically from the view (no caller wiring). Tooltip text comes+ * from the caller-provided i18n keys. Renders nothing when there is no sibling to move between.*/-export default function SiblingNavigator({ note, noteContext, siblingType, previousTooltipI18nKey, nextTooltipI18nKey, keyboardTarget, extraPreviousKeys = NO_KEYS, extraNextKeys = NO_KEYS }: SiblingNavigatorProps) {+export default function SiblingNavigator({ note, noteContext, viewScope, siblingType, previousTooltipI18nKey, nextTooltipI18nKey, keyboardTarget, extraPreviousKeys = NO_KEYS, extraNextKeys = NO_KEYS }: SiblingNavigatorProps) {const previousRef = useRef<HTMLButtonElement>(null);const nextRef = useRef<HTMLButtonElement>(null);- const navigation = useSiblingNavigation(note, noteContext, siblingType);+ // Viewing a single attachment → cycle the note's attachments; otherwise its note siblings.+ const provider = viewScope?.attachmentId+ ? attachmentSiblingProvider(note, noteContext, viewScope)+ : noteSiblingProvider(note, noteContext, siblingType);+ const navigation = useSiblingNavigation(provider);useSiblingKeyboard(navigation, noteContext, keyboardTarget, extraPreviousKeys, extraNextKeys);const previousText = navigation ? t(previousTooltipI18nKey, { title: navigation.previousTitle }) : "";const nextText = navigation ? t(nextTooltipI18nKey, { title: navigation.nextTitle }) : "";- // Memoize so the bootstrap tooltip is only recreated when the target note's name actually changes.+ // Memoize so the bootstrap tooltip is only recreated when the target's name actually changes.const previousConfig = useMemo(() => ({ title: previousText, placement: "bottom" as const }), [ previousText ]);const nextConfig = useMemo(() => ({ title: nextText, placement: "bottom" as const }), [ nextText ]);useStaticTooltip(previousRef, previousConfig);@@ -147,6 +113,94 @@ export default function SiblingNavigator({ note, noteContext, siblingType, previ);}+/**+ * Generic sibling-navigation engine over a {@link SiblingNavigationProvider}: loads the siblings, keeps+ * them fresh across entity/froca reloads, and exposes the current index/total plus prev/next/first/last+ * navigation. Returns null when there is nothing to navigate.+ */+export function useSiblingNavigation(provider: SiblingNavigationProvider): SiblingNavigationState | null {+ const [ siblings, setSiblings ] = useState<{ id: string; title: string }[]>([]);+ const [ refreshCounter, setRefreshCounter ] = useState(0);+ const providerRef = useRef(provider);+ providerRef.current = provider;++ const { currentId, depsKey } = provider;++ useEffect(() => {+ let active = true;+ providerRef.current.loadSiblings()+ .then((loaded) => { if (active) setSiblings(loaded); })+ .catch(() => { if (active) setSiblings([]); });+ return () => { active = false; };+ }, [ depsKey, refreshCounter ]);++ useTriliumEvent("entitiesReloaded", ({ loadResults }) => {+ if (providerRef.current.shouldRefresh(loadResults, siblings.map((sibling) => sibling.id))) {+ setRefreshCounter((counter) => counter + 1);+ }+ });++ // froca replaces every cached entity on a full reload (e.g. a protected-session unlock), refreshing titles.+ useTriliumEvent("frocaReloaded", () => setRefreshCounter((counter) => counter + 1));++ if (!currentId) return null;+ const navigation = getSiblingNavigation(siblings.map((sibling) => sibling.id), currentId);+ if (!navigation) return null;++ const titleOf = (id: string) => siblings.find((sibling) => sibling.id === id)?.title ?? "";+ const navigateTo = (id: string) => providerRef.current.navigateTo(id);+ return {+ index: navigation.index,+ total: navigation.total,+ previousTitle: titleOf(navigation.previous),+ nextTitle: titleOf(navigation.next),+ navigatePrevious: () => navigateTo(navigation.previous),+ navigateNext: () => navigateTo(navigation.next),+ navigateFirst: () => navigateTo(navigation.first),+ navigateLast: () => navigateTo(navigation.last)+ };+}++/** Iterator over the current note's same-type siblings within the parent of the current tab (clone-aware). */+function noteSiblingProvider(note: FNote | undefined, noteContext: NoteContext | undefined, siblingType: string | undefined): SiblingNavigationProvider {+ const notePath = noteContext?.notePath;+ const type = siblingType ?? note?.type;+ const parent = getParentFromNotePath(notePath);+ return {+ currentId: note?.noteId,+ depsKey: `note:${notePath ?? ""}:${type ?? ""}`,+ loadSiblings: async () => {+ if (!parent || !type) return [];+ const parentNote = await froca.getNote(parent.parentNoteId);+ const children = (await parentNote?.getChildNotes()) ?? [];+ return children.filter((child) => child.type === type).map((child) => ({ id: child.noteId, title: child.title }));+ },+ navigateTo: (id) => { if (parent) void noteContext?.setNote(`${parent.parentPath}/${id}`); },+ shouldRefresh: (loadResults, currentSiblingIds) => {+ if (!parent) return false;+ const branchesChanged = loadResults.getBranchRows().some((branch) => branch.parentNoteId === parent.parentNoteId);+ const siblingChanged = loadResults.getNoteIds().some((noteId) => currentSiblingIds.includes(noteId));+ return branchesChanged || siblingChanged;+ }+ };+}++/** Iterator over the note's same-role attachments, with the role taken from the currently-shown attachment. */+function attachmentSiblingProvider(note: FNote | undefined, noteContext: NoteContext | undefined, viewScope: ViewScope): SiblingNavigationProvider {+ const notePath = noteContext?.notePath;+ const attachmentId = viewScope.attachmentId;+ return {+ currentId: attachmentId,+ depsKey: `attachment:${note?.noteId ?? ""}:${attachmentId ?? ""}`,+ loadSiblings: async () => {+ if (!note) return [];+ return sameRoleAttachments(Array.from(await note.getAttachments()), attachmentId);+ },+ navigateTo: (id) => { if (notePath) void noteContext?.setNote(notePath, { viewScope: { ...viewScope, attachmentId: id } }); },+ shouldRefresh: (loadResults) => !!note && loadResults.getAttachmentRows().some((row) => row.ownerId === note.noteId)+ };+}+/*** Drives sibling navigation from the keyboard. A single document-level listener (stable across the* viewer remounting on each navigation) maps PageUp/PageDown — plus any caller-provided extra keys —diff --git a/apps/client/src/widgets/react/sibling_navigation.spec.ts b/apps/client/src/widgets/react/sibling_navigation.spec.tsindex 28a4cbc6f2..ea492449ff 100644--- a/apps/client/src/widgets/react/sibling_navigation.spec.ts+++ b/apps/client/src/widgets/react/sibling_navigation.spec.ts@@ -1,6 +1,6 @@import { describe, expect, it } from "vitest";-import { codeToSiblingDirection, getParentFromNotePath, getSiblingNavigation, isInteractiveTarget, isTextEntryTarget } from "./sibling_navigation";+import { codeToSiblingDirection, getParentFromNotePath, getSiblingNavigation, isInteractiveTarget, isTextEntryTarget, sameRoleAttachments } from "./sibling_navigation";describe("getParentFromNotePath", () => {it("splits the in-tab path into parent path + parent note id", () => {@@ -83,3 +83,22 @@ describe("isInteractiveTarget", () => {expect(isInteractiveTarget(null)).toBe(false);});});++describe("sameRoleAttachments", () => {+ const attachments = [+ { attachmentId: "a", role: "image", title: "A" },+ { attachmentId: "b", role: "file", title: "B" },+ { attachmentId: "c", role: "image", title: "C" }+ ];++ it("keeps only attachments sharing the current one's role, in order, as { id, title }", () => {+ expect(sameRoleAttachments(attachments, "a")).toEqual([ { id: "a", title: "A" }, { id: "c", title: "C" } ]);+ expect(sameRoleAttachments(attachments, "b")).toEqual([ { id: "b", title: "B" } ]);+ });++ it("returns empty when the current attachment is absent or unset", () => {+ expect(sameRoleAttachments(attachments, "x")).toEqual([]);+ expect(sameRoleAttachments(attachments, undefined)).toEqual([]);+ expect(sameRoleAttachments([], "a")).toEqual([]);+ });+});diff --git a/apps/client/src/widgets/react/sibling_navigation.ts b/apps/client/src/widgets/react/sibling_navigation.tsindex cc3502f108..8b4391ff28 100644--- a/apps/client/src/widgets/react/sibling_navigation.ts+++ b/apps/client/src/widgets/react/sibling_navigation.ts@@ -54,3 +54,13 @@ export function isInteractiveTarget(target: { tagName?: string; getAttribute?(naconst tag = target.tagName.toUpperCase();return tag === "BUTTON" || tag === "A" || tag === "SUMMARY" || target.getAttribute?.("role") === "button";}++/**+ * From a note's ordered attachments and the currently-shown one, keeps those sharing its role (so the+ * viewer cycles e.g. image-with-image), mapped to `{ id, title }`. Empty when the current one is absent.+ */+export function sameRoleAttachments(attachments: readonly { attachmentId: string; role: string; title: string }[], currentAttachmentId: string | undefined): { id: string; title: string }[] {+ const role = attachments.find((attachment) => attachment.attachmentId === currentAttachmentId)?.role;+ if (!role) return [];+ return attachments.filter((attachment) => attachment.role === role).map((attachment) => ({ id: attachment.attachmentId, title: attachment.title }));+}diff --git a/apps/client/src/widgets/type_widgets/Attachment.css b/apps/client/src/widgets/type_widgets/Attachment.cssindex ff1f8391bc..bccf523d84 100644--- a/apps/client/src/widgets/type_widgets/Attachment.css+++ b/apps/client/src/widgets/type_widgets/Attachment.css@@ -115,6 +115,11 @@object-fit: unset;}+/* Anchor the sibling-navigation overlay to the viewer. */+.attachment-content-wrapper.attachment-image-viewer {+ position: relative;+}+.attachment-detail-wrapper.scheduled-for-deletion .attachment-content-wrapper img {filter: contrast(10%);}diff --git a/apps/client/src/widgets/type_widgets/Attachment.tsx b/apps/client/src/widgets/type_widgets/Attachment.tsxindex 0bf34b5887..f9a0a89c01 100644--- a/apps/client/src/widgets/type_widgets/Attachment.tsx+++ b/apps/client/src/widgets/type_widgets/Attachment.tsx@@ -5,6 +5,7 @@ import { t } from "i18next";import { useContext, useEffect, useRef, useState } from "preact/hooks";import appContext from "../../components/app_context";+import type NoteContext from "../../components/note_context";import FAttachment from "../../entities/fattachment";import FNote from "../../entities/fnote";import imageContextMenu from "../../menus/image_context_menu";@@ -12,7 +13,7 @@ import content_renderer from "../../services/content_renderer";import dialog from "../../services/dialog";import froca from "../../services/froca";import image from "../../services/image";-import link from "../../services/link";+import link, { type ViewScope } from "../../services/link";import open from "../../services/open";import options from "../../services/options";import server from "../../services/server";@@ -31,6 +32,7 @@ import ImageViewer from "../react/ImageViewer";import NoItems from "../react/NoItems";import NoteLink from "../react/NoteLink";import { ParentComponent, refToJQuerySelector } from "../react/react_utils";+import SiblingNavigator from "../react/SiblingNavigator";import { TextPreview } from "./File";import { TypeWidgetProps } from "./type_widget";@@ -103,7 +105,7 @@ function AttachmentListHeader({ noteId }: { noteId: string }) {/*** Displays information about a single attachment.*/-export function AttachmentDetail({ note, viewScope }: TypeWidgetProps) {+export function AttachmentDetail({ note, viewScope, noteContext }: TypeWidgetProps) {const [ attachment, setAttachment ] = useState<FAttachment | null | undefined>(undefined);useEffect(() => {@@ -130,7 +132,7 @@ export function AttachmentDetail({ note, viewScope }: TypeWidgetProps) {<div className="attachment-wrapper">{attachment !== null ? (- attachment && <AttachmentInfo attachment={attachment} isFullDetail />+ attachment && <AttachmentInfo attachment={attachment} isFullDetail ownerNote={note} noteContext={noteContext} viewScope={viewScope} />) : (<strong>{t("attachment_detail.attachment_deleted")}</strong>)}@@ -139,7 +141,7 @@ export function AttachmentDetail({ note, viewScope }: TypeWidgetProps) {);}-function AttachmentInfo({ attachment, isFullDetail }: { attachment: FAttachment, isFullDetail?: boolean }) {+function AttachmentInfo({ attachment, isFullDetail, ownerNote, noteContext, viewScope }: { attachment: FAttachment, isFullDetail?: boolean, ownerNote?: FNote, noteContext?: NoteContext, viewScope?: ViewScope }) {const contentWrapper = useRef<HTMLDivElement>(null);const imageViewerWrapper = useRef<HTMLDivElement>(null);const [ title, setTitle ] = useState(attachment.title);@@ -240,6 +242,15 @@ function AttachmentInfo({ attachment, isFullDetail }: { attachment: FAttachment,{isZoomableImage ? (<div key="image-viewer" ref={imageViewerWrapper} className="attachment-content-wrapper attachment-image-viewer"><ImageViewer key={`${attachment.attachmentId}-${attachment.utcDateModified}`} src={imageSrc} alt={attachment.title} />+ <SiblingNavigator+ note={ownerNote}+ noteContext={noteContext}+ viewScope={viewScope}+ previousTooltipI18nKey="image_navigation.previous"+ nextTooltipI18nKey="image_navigation.next"+ extraPreviousKeys={[ "Backspace" ]}+ extraNextKeys={[ "Space" ]}+ /></div>) : (<div key="rendered" ref={contentWrapper} className="attachment-content-wrapper" />