Skip to content

Build your personal knowledge base with Trilium Notes

client/shortcut hints: update the shortcut hints for media notes

Adorian Doran6b0a8b669f34
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.json
index 7ba7082989..b2ce4b7c33 100644
--- a/apps/client/src/translations/en/translation.json
+++ b/apps/client/src/translations/en/translation.json
@@ -1362,7 +1362,20 @@
"exit-fullscreen": "Exit fullscreen",
"unsupported-format": "Media preview is not available for this file format:\n{{mime}}",
"zoom-to-fit": "Zoom to fill",
- "zoom-reset": "Reset zoom to fill"
+ "zoom-reset": "Reset zoom to fill",
+ "hints": {
+ "playback": "Playback",
+ "play_pause": "Play/Pause",
+ "back_10s": "Back 10s",
+ "forward_10s": "Forward 10s",
+ "jump_start": "Jump to start",
+ "jump_end": "Jump to end",
+ "mute": "Mute",
+ "fullscreen": "Fullscreen",
+ "navigation": "Navigation",
+ "previous": "Previous",
+ "next": "Next"
+ }
},
"protected_session": {
"enter_password_instruction": "Showing protected note requires entering your password:",
diff --git a/apps/client/src/widgets/type_widgets/file/Audio.tsx b/apps/client/src/widgets/type_widgets/file/Audio.tsx
index 2528a3eb03..07c378f41c 100644
--- a/apps/client/src/widgets/type_widgets/file/Audio.tsx
+++ b/apps/client/src/widgets/type_widgets/file/Audio.tsx
@@ -1,13 +1,15 @@
import { MutableRef, useCallback, useEffect, useRef, useState } from "preact/hooks";
import { t } from "../../../services/i18n";
+import { isMobile } from "../../../services/utils";
import Icon from "../../react/Icon";
import NoItems from "../../react/NoItems";
+import ShortcutHintButton from "../../shortcut_hints/shortcut_hint_button";
import { loadWaveform } from "./audio_waveform";
import { AudioVisualizer } from "./AudioVisualizer";
import MediaFileActions from "./MediaFileActions";
import { playerRootClasses, preloadFor, showsFileActions, usesCompactControls } from "./media_environment";
-import { MediaPlayerProps, MediaSiblingButton, PlaybackSpeed, PlayModeButton, PlayPauseButton, SkipButton, useMediaPlayMode, useMediaSessionController, VolumeControl } from "./MediaPlayer";
+import { MediaPlayerProps, MediaSiblingButton, PlaybackSpeed, PlayModeButton, PlayPauseButton, SkipButton, useMediaPlayerShortcutHints, useMediaPlayMode, useMediaSessionController, VolumeControl } from "./MediaPlayer";
import { WaveformSeekBar } from "./WaveformSeekBar";
export default function AudioPreview({ source, entity, environment, noteContext, isVisible = true, autoPlay }: MediaPlayerProps) {
@@ -38,6 +40,7 @@ export default function AudioPreview({ source, entity, environment, noteContext,
const syncPlaying = useCallback(() => setPlaying(!!audioRef.current && !audioRef.current.paused), []);
const { mode: playMode, setMode: setPlayMode } = useMediaPlayMode(noteContext, audioRef);
const siblingNavigation = useMediaSessionController({ source, entity, environment, noteContext, isVisible, autoPlay, mimePrefix: "audio/", mediaRef: audioRef, playMode });
+ useMediaPlayerShortcutHints({ fullscreen: false });
const waveformPeaks = useWaveformPeaks(source.fullUrl);
if (error) {
@@ -99,6 +102,8 @@ export default function AudioPreview({ source, entity, environment, noteContext,
</>
)}
</div>
+
+ {!compact && !isMobile() && <ShortcutHintButton className="media-hint-button" />}
</div>
);
}
diff --git a/apps/client/src/widgets/type_widgets/file/MediaPlayer.css b/apps/client/src/widgets/type_widgets/file/MediaPlayer.css
index 411ff4e900..d1317e8b80 100644
--- a/apps/client/src/widgets/type_widgets/file/MediaPlayer.css
+++ b/apps/client/src/widgets/type_widgets/file/MediaPlayer.css
@@ -1,3 +1,12 @@
+/* Contextual shortcut-hints button, floated in the top-right of the full audio/video player. */
+.media-hint-button {
+ position: absolute;
+ inset-inline-end: 10px;
+ top: 10px;
+ z-index: 2;
+ transition: opacity 300ms ease;
+}
+
.media-preview-controls {
padding: 1.25em;
display: flex;
diff --git a/apps/client/src/widgets/type_widgets/file/MediaPlayer.spec.tsx b/apps/client/src/widgets/type_widgets/file/MediaPlayer.spec.tsx
new file mode 100644
index 0000000000..7ff51349cd
--- /dev/null
+++ b/apps/client/src/widgets/type_widgets/file/MediaPlayer.spec.tsx
@@ -0,0 +1,59 @@
+import { render } from "preact";
+import { act } from "preact/test-utils";
+import { afterEach, describe, expect, it } from "vitest";
+
+import Component from "../../../components/component";
+import { collectShortcutHints } from "../../../services/shortcut_hints";
+import { ParentComponent } from "../../react/react_utils";
+import { useMediaPlayerShortcutHints } from "./MediaPlayer";
+
+let container: HTMLDivElement;
+
+afterEach(() => {
+ act(() => render(null, container));
+ container.remove();
+});
+
+function collectHints(fullscreen: boolean) {
+ const host = new Component();
+ container = document.createElement("div");
+ document.body.appendChild(container);
+
+ function Probe() {
+ useMediaPlayerShortcutHints({ fullscreen });
+ return null;
+ }
+ act(() => render(<ParentComponent.Provider value={host}><Probe /></ParentComponent.Provider>, container));
+ return collectShortcutHints(host);
+}
+
+describe("useMediaPlayerShortcutHints", () => {
+ it("registers playback (incl. fullscreen) and navigation for video", () => {
+ const sections = collectHints(true);
+
+ expect(sections.map(s => s.titleKey)).toEqual([ "media.hints.playback", "media.hints.navigation" ]);
+ expect(sections[0].hints.map(h => h.labelKey)).toEqual([
+ "media.hints.play_pause",
+ "media.hints.back_10s",
+ "media.hints.forward_10s",
+ "media.hints.jump_start",
+ "media.hints.jump_end",
+ "media.hints.mute",
+ "media.hints.fullscreen"
+ ]);
+ expect(sections[1].hints.map(h => h.labelKey)).toEqual([ "media.hints.previous", "media.hints.next" ]);
+ });
+
+ it("omits fullscreen for audio", () => {
+ const sections = collectHints(false);
+
+ expect(sections[0].hints.map(h => h.labelKey)).toEqual([
+ "media.hints.play_pause",
+ "media.hints.back_10s",
+ "media.hints.forward_10s",
+ "media.hints.jump_start",
+ "media.hints.jump_end",
+ "media.hints.mute"
+ ]);
+ });
+});
diff --git a/apps/client/src/widgets/type_widgets/file/MediaPlayer.tsx b/apps/client/src/widgets/type_widgets/file/MediaPlayer.tsx
index 5a1bdfbbaf..91fae114a9 100644
--- a/apps/client/src/widgets/type_widgets/file/MediaPlayer.tsx
+++ b/apps/client/src/widgets/type_widgets/file/MediaPlayer.tsx
@@ -9,11 +9,12 @@ import type FNote from "../../../entities/fnote";
import attributes from "../../../services/attributes";
import froca from "../../../services/froca";
import { t } from "../../../services/i18n";
+import type { ShortcutHint, ShortcutHintDefinition, ShortcutHintSection } from "../../../services/shortcut_hints";
import { isMobile } from "../../../services/utils";
import { logError } from "../../../services/ws";
import ActionButton from "../../react/ActionButton";
import Dropdown from "../../react/Dropdown";
-import { useTriliumEvent, useTriliumEvents } from "../../react/hooks";
+import { useContextualShortcutHints, useTriliumEvent, useTriliumEvents } from "../../react/hooks";
import Icon from "../../react/Icon";
import { getParentFromNotePath } from "../../react/sibling_navigation";
import { noteSiblingProvider, type SiblingNavigationState, useSiblingKeyboard, useSiblingNavigation } from "../../react/SiblingNavigator";
@@ -39,6 +40,40 @@ export interface MediaPlayerProps {
autoPlay?: boolean;
}
+// Navigation is Page Up/Down only — the player reserves Home/End for seeking (edgeKeys: false), and
+// Space/Backspace aren't bound.
+const MEDIA_NAVIGATION_HINTS: ShortcutHintSection = {
+ titleKey: "media.hints.navigation",
+ hints: [
+ { keys: ["PageUp"], labelKey: "media.hints.previous" },
+ { keys: ["PageDown"], labelKey: "media.hints.next" }
+ ]
+};
+
+/**
+ * Registers the media player's contextual keyboard hints. Video passes `fullscreen: true`; audio has
+ * no fullscreen. Mirrors the actual keys bound in Video/Audio's `useKeyboardShortcuts`.
+ */
+export function useMediaPlayerShortcutHints({ fullscreen }: { fullscreen: boolean }) {
+ useContextualShortcutHints((): ShortcutHintDefinition => {
+ const playback: ShortcutHint[] = [
+ { keys: ["Space"], labelKey: "media.hints.play_pause" },
+ { keys: ["Left"], labelKey: "media.hints.back_10s" },
+ { keys: ["Right"], labelKey: "media.hints.forward_10s" },
+ { keys: ["Home"], labelKey: "media.hints.jump_start" },
+ { keys: ["End"], labelKey: "media.hints.jump_end" },
+ { keys: ["M"], labelKey: "media.hints.mute" }
+ ];
+ if (fullscreen) {
+ playback.push({ keys: ["F"], labelKey: "media.hints.fullscreen" });
+ }
+ return [
+ { titleKey: "media.hints.playback", hints: playback },
+ MEDIA_NAVIGATION_HINTS
+ ];
+ });
+}
+
export function SeekBar({ mediaRef }: { mediaRef: RefObject<HTMLVideoElement | HTMLAudioElement> }) {
const [currentTime, setCurrentTime] = useState(0);
const [duration, setDuration] = useState(0);
diff --git a/apps/client/src/widgets/type_widgets/file/Video.css b/apps/client/src/widgets/type_widgets/file/Video.css
index cd3ab78a4c..345a4fe0c6 100644
--- a/apps/client/src/widgets/type_widgets/file/Video.css
+++ b/apps/client/src/widgets/type_widgets/file/Video.css
@@ -19,7 +19,8 @@
&.controls-hidden {
cursor: pointer;
- .media-preview-controls {
+ .media-preview-controls,
+ .media-hint-button {
opacity: 0;
pointer-events: none;
}
diff --git a/apps/client/src/widgets/type_widgets/file/Video.tsx b/apps/client/src/widgets/type_widgets/file/Video.tsx
index fa3a1d98b1..eba840ca5a 100644
--- a/apps/client/src/widgets/type_widgets/file/Video.tsx
+++ b/apps/client/src/widgets/type_widgets/file/Video.tsx
@@ -4,11 +4,13 @@ import { RefObject } from "preact";
import { MutableRef, useCallback, useEffect, useRef, useState } from "preact/hooks";
import { t } from "../../../services/i18n";
+import { isMobile } from "../../../services/utils";
import ActionButton from "../../react/ActionButton";
import NoItems from "../../react/NoItems";
+import ShortcutHintButton from "../../shortcut_hints/shortcut_hint_button";
import MediaFileActions from "./MediaFileActions";
import { playerRootClasses, preloadFor, showsFileActions, showsViewportControls, usesCompactControls } from "./media_environment";
-import { MediaPlayerProps, MediaSiblingButton, PlaybackSpeed, PlayModeButton, PlayPauseButton, SeekBar, SkipButton, useMediaPlayMode, useMediaSessionController, VolumeControl } from "./MediaPlayer";
+import { MediaPlayerProps, MediaSiblingButton, PlaybackSpeed, PlayModeButton, PlayPauseButton, SeekBar, SkipButton, useMediaPlayerShortcutHints, useMediaPlayMode, useMediaSessionController, VolumeControl } from "./MediaPlayer";
const AUTO_HIDE_DELAY = 3000;
@@ -65,6 +67,7 @@ export default function VideoPreview({ source, entity, environment, noteContext,
const onKeyDown = useKeyboardShortcuts(videoRef, wrapperRef, togglePlayback, flashControls);
const { mode: playMode, setMode: setPlayMode } = useMediaPlayMode(noteContext, videoRef);
const siblingNavigation = useMediaSessionController({ source, entity, environment, noteContext, isVisible, autoPlay, mimePrefix: "video/", mediaRef: videoRef, playMode });
+ useMediaPlayerShortcutHints({ fullscreen: true });
const compact = usesCompactControls(environment);
if (error) {
@@ -128,6 +131,8 @@ export default function VideoPreview({ source, entity, environment, noteContext,
</>
)}
</div>
+
+ {!compact && !isMobile() && <ShortcutHintButton className="media-hint-button" />}
</div>
);
}