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: fix some issues
Adorian Doran017994b5a7be
diff --git a/apps/client/src/widgets/react/ImageViewer.tsx b/apps/client/src/widgets/react/ImageViewer.tsxindex c034b7ed70..8d09eb7ab2 100644--- a/apps/client/src/widgets/react/ImageViewer.tsx+++ b/apps/client/src/widgets/react/ImageViewer.tsx@@ -65,12 +65,12 @@ export default function ImageViewer({ src, imgClassName, alt = "", minScale = 0.}, [ apiRef ]);// Recompute the cursor/rendering flags and the displayed (native-relative) zoom percentage.+ // The setters bail out on identical values, so no manual change checks are needed.const updateZoomState = (scale: number) => {const { pannable: nextPannable, largeZoom: nextLargeZoom, nativeScale } = evaluateImageZoom(scale, imgRef.current);- if (nextPannable !== pannable) setPannable(nextPannable);- if (nextLargeZoom !== largeZoom) setLargeZoom(nextLargeZoom);- const percent = Math.round(nativeScale * 100);- if (percent !== zoomPercent) setZoomPercent(percent);+ setPannable(nextPannable);+ setLargeZoom(nextLargeZoom);+ setZoomPercent(Math.round(nativeScale * 100));};// Reveal (or fail) the image, driven by decode() rather than the load event. decode() resolves once@@ -100,7 +100,7 @@ export default function ImageViewer({ src, imgClassName, alt = "", minScale = 0.img.decode().then(reveal, () => settle(() => setLoadingError(true)));return () => settle(() => {});- }, [ src ]); // eslint-disable-line react-hooks/exhaustive-deps -- updateZoomState identity changes each render+ }, [ src ]);useImageViewerKeyboard(zoomRef, rootRef);useStaticTooltip(zoomOutRef, { title: t("image_buttons.zoom_out"), placement: "top" });@@ -144,7 +144,13 @@ export default function ImageViewer({ src, imgClassName, alt = "", minScale = 0.setLoaded(true);updateZoomState(zoomRef.current?.instance?.state?.scale ?? 1);}}- onError={() => setLoadingError(true)}+ onError={() => {+ // decode()'s rejection reports failures where supported — and goes through `settle`,+ // clearing the fallback timer so it can't race and reveal a failed image. This only+ // covers the rare environment without decode().+ if (typeof imgRef.current?.decode === "function") return;+ setLoadingError(true);+ }}/></TransformComponent></TransformWrapper>@@ -157,19 +163,25 @@ export default function ImageViewer({ src, imgClassName, alt = "", minScale = 0.<div className="image-viewer-controls"><buttonref={zoomOutRef}+ type="button"className="icon-action bx bx-zoom-out"+ aria-label={t("image_buttons.zoom_out")}onClick={() => zoomRef.current?.zoomOut(BUTTON_ZOOM_STEP)}/><buttonref={zoomLevelRef}+ type="button"className="image-viewer-zoom-level"+ aria-label={t("image_buttons.reset_zoom")}onClick={() => zoomRef.current?.resetTransform()}>{zoomPercent}%</button><buttonref={zoomInRef}+ type="button"className="icon-action bx bx-zoom-in"+ aria-label={t("image_buttons.zoom_in")}onClick={() => zoomRef.current?.zoomIn(BUTTON_ZOOM_STEP)}/></div>diff --git a/apps/client/src/widgets/react/SiblingNavigator.tsx b/apps/client/src/widgets/react/SiblingNavigator.tsxindex 4a08e1d6eb..0e55698ae5 100644--- a/apps/client/src/widgets/react/SiblingNavigator.tsx+++ b/apps/client/src/widgets/react/SiblingNavigator.tsx@@ -9,7 +9,7 @@ import froca from "../../services/froca";import { t } from "../../services/i18n";import { isMobile } from "../../services/utils";import { useStaticTooltip, useTriliumEvent } from "./hooks";-import { codeToSiblingDirection, getParentFromNotePath, getSiblingNavigation, isTextEntryTarget } from "./sibling_navigation";+import { codeToSiblingDirection, getParentFromNotePath, getSiblingNavigation, isInteractiveTarget, isTextEntryTarget } from "./sibling_navigation";const NO_KEYS: readonly string[] = [];@@ -62,11 +62,12 @@ export function useSiblingNavigation(note: FNote | undefined, noteContext: NoteC}let active = true;- void froca.getNote(parent.parentNoteId)+ 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 ]);@@ -78,6 +79,10 @@ export function useSiblingNavigation(note: FNote | undefined, noteContext: NoteCif (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);@@ -125,13 +130,17 @@ export default function SiblingNavigator({ note, noteContext, siblingType, previ<div className="sibling-navigator"><buttonref={previousRef}+ type="button"className="icon-action bx bx-chevron-left"+ aria-label={previousText}onClick={() => navigation.navigatePrevious()}/><span className="sibling-navigator-index">{navigation.index}/{navigation.total}</span><buttonref={nextRef}+ type="button"className="icon-action bx bx-chevron-right"+ aria-label={nextText}onClick={() => navigation.navigateNext()}/></div>@@ -157,10 +166,13 @@ function useSiblingKeyboard(useEffect(() => {const onKeyDown = (e: KeyboardEvent) => {- if (e.defaultPrevented || e.ctrlKey || e.metaKey || e.altKey) return;+ if (e.defaultPrevented || e.ctrlKey || e.metaKey || e.altKey || e.shiftKey) return;const { navigation, noteContext, keyboardTarget, extraPreviousKeys, extraNextKeys } = stateRef.current;- if (!navigation || isTextEntryTarget(e.target as Element | null)) return;+ const target = e.target as Element | null;+ if (!navigation || isTextEntryTarget(target)) return;+ // Don't hijack Space from a focused button or other interactive control — it activates them.+ if (e.code === "Space" && isInteractiveTarget(target)) return;if (keyboardTarget) {if (!keyboardTarget.current?.contains(document.activeElement)) return;} else if (noteContext && !noteContext.isActive()) {diff --git a/apps/client/src/widgets/react/sibling_navigation.spec.ts b/apps/client/src/widgets/react/sibling_navigation.spec.tsindex df61068bc0..28a4cbc6f2 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, isTextEntryTarget } from "./sibling_navigation";+import { codeToSiblingDirection, getParentFromNotePath, getSiblingNavigation, isInteractiveTarget, isTextEntryTarget } from "./sibling_navigation";describe("getParentFromNotePath", () => {it("splits the in-tab path into parent path + parent note id", () => {@@ -69,3 +69,17 @@ describe("isTextEntryTarget", () => {expect(isTextEntryTarget(null)).toBe(false);});});++describe("isInteractiveTarget", () => {+ it("is true for buttons, links and role=button", () => {+ expect(isInteractiveTarget({ tagName: "BUTTON" })).toBe(true);+ expect(isInteractiveTarget({ tagName: "a" })).toBe(true);+ expect(isInteractiveTarget({ tagName: "DIV", getAttribute: () => "button" })).toBe(true);+ });++ it("is false for plain elements and nullish input", () => {+ expect(isInteractiveTarget({ tagName: "DIV", getAttribute: () => null })).toBe(false);+ expect(isInteractiveTarget({ tagName: "IMG" })).toBe(false);+ expect(isInteractiveTarget(null)).toBe(false);+ });+});diff --git a/apps/client/src/widgets/react/sibling_navigation.ts b/apps/client/src/widgets/react/sibling_navigation.tsindex 5e36a67055..cc3502f108 100644--- a/apps/client/src/widgets/react/sibling_navigation.ts+++ b/apps/client/src/widgets/react/sibling_navigation.ts@@ -47,3 +47,10 @@ export function isTextEntryTarget(target: { tagName?: string; isContentEditable?const tag = target.tagName.toUpperCase();return tag === "INPUT" || tag === "TEXTAREA" || tag === "SELECT" || target.isContentEditable === true;}++/** Whether the target is an interactive control that uses Space/Enter for activation (so we must not hijack Space from it). */+export function isInteractiveTarget(target: { tagName?: string; getAttribute?(name: string): string | null } | null | undefined): boolean {+ if (!target?.tagName) return false;+ const tag = target.tagName.toUpperCase();+ return tag === "BUTTON" || tag === "A" || tag === "SUMMARY" || target.getAttribute?.("role") === "button";+}