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/shortcut hints: add shortcut hints for the image viewer
Adorian Dorane82010eb6f5c
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.jsonindex 1c3e212992..7527cc24b7 100644--- a/apps/client/src/translations/en/translation.json+++ b/apps/client/src/translations/en/translation.json@@ -997,7 +997,19 @@"next": "Next image: {{- title}}"},"image_viewer": {- "loading_error": "There was an error loading the image."+ "loading_error": "There was an error loading the image.",+ "hints": {+ "title": "Zoom & Pan",+ "zoom_in": "Zoom in",+ "zoom_out": "Zoom out",+ "pan": "Pan",+ "pan_fast": "Faster panning",+ "navigation": "Navigation",+ "next_image": "Next image",+ "previous_image": "Previous image",+ "first_image": "First image",+ "last_image": "Last image"+ }},"zpetne_odkazy": {"backlink_one": "{{count}} Backlink",diff --git a/apps/client/src/widgets/react/ImageViewer.spec.tsx b/apps/client/src/widgets/react/ImageViewer.spec.tsxindex 4ae4604a14..46fa87bb49 100644--- a/apps/client/src/widgets/react/ImageViewer.spec.tsx+++ b/apps/client/src/widgets/react/ImageViewer.spec.tsx@@ -1,6 +1,11 @@import { type ComponentChildren, render } from "preact";+import { act } from "preact/test-utils";import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";+import Component from "../../components/component";+import { collectShortcutHints } from "../../services/shortcut_hints";+import { ParentComponent } from "./react_utils";+// Capture the props handed to the zoom library without mounting it (it needs real layout).const { transformWrapperSpy } = vi.hoisted(() => ({ transformWrapperSpy: vi.fn() }));@@ -15,8 +20,12 @@ vi.mock("react-zoom-pan-pinch", () => ({)}));-// Avoid pulling the real hooks module (bootstrap tooltips + app context) into the test.-vi.mock("./hooks", () => ({ useStaticTooltip: () => {} }));+// Keep the real hooks (useContextualShortcutHints is exercised below), but stub the bootstrap-Tooltip+// one, which needs real layout.+vi.mock("./hooks", async (importOriginal) => ({+ ...(await importOriginal<typeof import("./hooks")>()),+ useStaticTooltip: () => {}+}));import ImageViewer, { evaluateImageZoom } from "./ImageViewer";@@ -48,6 +57,30 @@ describe("ImageViewer", () => {expect(img?.className).toBe("note-detail-image-view");});+ it("registers its contextual shortcut hints on the host component", () => {+ const host = new Component();+ const container = document.createElement("div");+ act(() => render(+ <ParentComponent.Provider value={host}><ImageViewer src="x" /></ParentComponent.Provider>,+ container+ ));++ const sections = collectShortcutHints(host);+ expect(sections).toHaveLength(2);+ expect(sections[0].hints.map(h => h.labelKey)).toEqual([+ "image_viewer.hints.zoom_in",+ "image_viewer.hints.zoom_out",+ "image_viewer.hints.pan",+ "image_viewer.hints.pan_fast"+ ]);+ expect(sections[1].hints.map(h => h.labelKey)).toEqual([+ "image_viewer.hints.next_image",+ "image_viewer.hints.previous_image",+ "image_viewer.hints.first_image",+ "image_viewer.hints.last_image"+ ]);+ });+it("wires up the interactive zoom behavior", () => {renderViewer({ src: "x" });const props = transformWrapperSpy.mock.calls[0][0];diff --git a/apps/client/src/widgets/react/ImageViewer.tsx b/apps/client/src/widgets/react/ImageViewer.tsxindex f94fee0d89..510b0cc181 100644--- a/apps/client/src/widgets/react/ImageViewer.tsx+++ b/apps/client/src/widgets/react/ImageViewer.tsx@@ -35,7 +35,17 @@ const IMAGE_VIEWER_HINTS: ShortcutHintDefinition = [hints: [{ keys: ["Ctrl++", "E"], labelKey: "image_viewer.hints.zoom_in" },{ keys: ["Ctrl+-", "Q"], labelKey: "image_viewer.hints.zoom_out" },- { keys: ["W", "A", "S", "D", "Up", "Down", "Left", "Right"], labelKey: "image_viewer.hints.pan" }+ { keys: ["W", "A", "S", "D", "Up", "Down", "Left", "Right"], labelKey: "image_viewer.hints.pan" },+ { keys: ["Shift"], labelKey: "image_viewer.hints.pan_fast" }+ ]+ },+ {+ titleKey: "image_viewer.hints.navigation",+ hints: [+ { keys: ["Space", "PageDown"], labelKey: "image_viewer.hints.next_image" },+ { keys: ["Backspace", "PageUp"], labelKey: "image_viewer.hints.previous_image" },+ { keys: ["Home"], labelKey: "image_viewer.hints.first_image" },+ { keys: ["End"], labelKey: "image_viewer.hints.last_image" }]}];