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: fix the panel not showing up
Adorian Doran4a73d35374b3
diff --git a/apps/client/src/widgets/react/ImageViewer.tsx b/apps/client/src/widgets/react/ImageViewer.tsxindex 0dbf856cdd..f94fee0d89 100644--- a/apps/client/src/widgets/react/ImageViewer.tsx+++ b/apps/client/src/widgets/react/ImageViewer.tsx@@ -5,9 +5,10 @@ import { useCallback, useEffect, useRef, useState } from "preact/hooks";import { type ReactZoomPanPinchRef, TransformComponent, TransformWrapper } from "react-zoom-pan-pinch";import { t } from "../../services/i18n";+import type { ShortcutHintDefinition } from "../../services/shortcut_hints";import { isMobile } from "../../services/utils";import ContentErrorMessage from "./ContentErrorMessage";-import { useStaticTooltip } from "./hooks";+import { useContextualShortcutHints, useStaticTooltip } from "./hooks";import { useImageViewerKeyboard } from "./image_viewer_keyboard";interface ImageViewerProps {@@ -28,6 +29,17 @@ const BUTTON_ZOOM_STEP = 0.5;/** Reveal the image even if `decode()` never settles (it can stall for some images, e.g. SVGs). */const REVEAL_FALLBACK_MS = 1000;+const IMAGE_VIEWER_HINTS: ShortcutHintDefinition = [+ {+ titleKey: "image_viewer.hints.title",+ 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" }+ ]+ }+];+/*** Derives the zoom-driven values: whether the image is pannable (zoomed past the fitted size),* whether it's enlarged beyond {@link CRISP_NATIVE_SCALE}× its native resolution, and `nativeScale` —@@ -114,6 +126,7 @@ export default function ImageViewer({ src, imgClassName, alt = "", minScale = 0.}, [ src ]);useImageViewerKeyboard(zoomRef, rootRef);+ useContextualShortcutHints(IMAGE_VIEWER_HINTS);useStaticTooltip(zoomOutRef, { title: t("image_buttons.zoom_out"), placement: "top" });useStaticTooltip(zoomLevelRef, { title: t("image_buttons.reset_zoom"), placement: "top" });useStaticTooltip(zoomInRef, { title: t("image_buttons.zoom_in"), placement: "top" });diff --git a/apps/client/src/widgets/shortcut_hints/shortcut_hints_panel.css b/apps/client/src/widgets/shortcut_hints/shortcut_hints_panel.cssindex ded3fee7f3..39a07a33e8 100644--- a/apps/client/src/widgets/shortcut_hints/shortcut_hints_panel.css+++ b/apps/client/src/widgets/shortcut_hints/shortcut_hints_panel.css@@ -2,7 +2,7 @@position: fixed;right: 1rem;bottom: 1rem;- z-index: 900;+ z-index: 1080;max-width: 340px;max-height: 60vh;overflow-y: auto;diff --git a/apps/client/src/widgets/shortcut_hints/shortcut_hints_panel.spec.tsx b/apps/client/src/widgets/shortcut_hints/shortcut_hints_panel.spec.tsxindex 6620e7faea..740d62e507 100644--- a/apps/client/src/widgets/shortcut_hints/shortcut_hints_panel.spec.tsx+++ b/apps/client/src/widgets/shortcut_hints/shortcut_hints_panel.spec.tsx@@ -55,41 +55,44 @@ describe("ShortcutHintsSections", () => {});describe("ShortcutHintsPanel", () => {+ const request = (host: Component, sections: ShortcutHintSection[]) =>+ act(() => { host.handleEvent("shortcutHintsRequested", { sections }); });+it("stays closed until summoned, opens on the event, and toggles closed on the next", () => {const host = mountPanel();- expect(container.querySelector(".shortcut-hints-panel")).toBeNull();+ expect(document.querySelector(".shortcut-hints-panel")).toBeNull();- act(() => host.handleEvent("shortcutHintsRequested", { sections: SECTIONS }));- expect(container.querySelector(".shortcut-hints-panel")).not.toBeNull();+ request(host, SECTIONS);+ expect(document.querySelector(".shortcut-hints-panel")).not.toBeNull();- act(() => host.handleEvent("shortcutHintsRequested", { sections: SECTIONS }));- expect(container.querySelector(".shortcut-hints-panel")).toBeNull();+ request(host, SECTIONS);+ expect(document.querySelector(".shortcut-hints-panel")).toBeNull();});it("does not open when there are no sections", () => {const host = mountPanel();- act(() => host.handleEvent("shortcutHintsRequested", { sections: [] }));- expect(container.querySelector(".shortcut-hints-panel")).toBeNull();+ request(host, []);+ expect(document.querySelector(".shortcut-hints-panel")).toBeNull();});it("closes on Escape", () => {const host = mountPanel();- act(() => host.handleEvent("shortcutHintsRequested", { sections: SECTIONS }));- expect(container.querySelector(".shortcut-hints-panel")).not.toBeNull();+ request(host, SECTIONS);+ expect(document.querySelector(".shortcut-hints-panel")).not.toBeNull();- act(() => document.dispatchEvent(new KeyboardEvent("keydown", { key: "Escape" })));- expect(container.querySelector(".shortcut-hints-panel")).toBeNull();+ act(() => { document.dispatchEvent(new KeyboardEvent("keydown", { key: "Escape" })); });+ expect(document.querySelector(".shortcut-hints-panel")).toBeNull();});it("auto-dismisses after the timeout", () => {vi.useFakeTimers();try {const host = mountPanel();- act(() => host.handleEvent("shortcutHintsRequested", { sections: SECTIONS }));- expect(container.querySelector(".shortcut-hints-panel")).not.toBeNull();+ request(host, SECTIONS);+ expect(document.querySelector(".shortcut-hints-panel")).not.toBeNull();- act(() => vi.advanceTimersByTime(5000));- expect(container.querySelector(".shortcut-hints-panel")).toBeNull();+ act(() => { vi.advanceTimersByTime(5000); });+ expect(document.querySelector(".shortcut-hints-panel")).toBeNull();} finally {vi.useRealTimers();}@@ -97,10 +100,10 @@ describe("ShortcutHintsPanel", () => {it("closes when the active note context changes", () => {const host = mountPanel();- act(() => host.handleEvent("shortcutHintsRequested", { sections: SECTIONS }));- expect(container.querySelector(".shortcut-hints-panel")).not.toBeNull();+ request(host, SECTIONS);+ expect(document.querySelector(".shortcut-hints-panel")).not.toBeNull();- act(() => host.handleEvent("activeContextChanged", { noteContext: {} }));- expect(container.querySelector(".shortcut-hints-panel")).toBeNull();+ act(() => { host.handleEvent("activeContextChanged", { noteContext: {} as never }); });+ expect(document.querySelector(".shortcut-hints-panel")).toBeNull();});});diff --git a/apps/client/src/widgets/shortcut_hints/shortcut_hints_panel.tsx b/apps/client/src/widgets/shortcut_hints/shortcut_hints_panel.tsxindex c3ee7d619c..b834952621 100644--- a/apps/client/src/widgets/shortcut_hints/shortcut_hints_panel.tsx+++ b/apps/client/src/widgets/shortcut_hints/shortcut_hints_panel.tsx@@ -1,6 +1,7 @@import "./shortcut_hints_panel.css";import { Fragment } from "preact";+import { createPortal } from "preact/compat";import { useCallback, useEffect, useRef, useState } from "preact/hooks";import { t } from "../../services/i18n.js";@@ -64,10 +65,13 @@ export default function ShortcutHintsPanel() {return null;}- return (+ // Portal to <body> so no transformed / contained / overflow-clipped ancestor breaks the fixed+ // positioning or hides it behind content.+ return createPortal(<div ref={panelRef} className="shortcut-hints-panel" onMouseEnter={clearTimer} onMouseLeave={startTimer}><ShortcutHintsSections sections={sections} />- </div>+ </div>,+ document.body);}