Skip to content

Build your personal knowledge base with Trilium Notes

client/media player: add video navigation support

Adorian Doran4b089e69a255
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.json
index 30a2858462..c86550f4e1 100644
--- a/apps/client/src/translations/en/translation.json
+++ b/apps/client/src/translations/en/translation.json
@@ -1176,6 +1176,8 @@
"pause": "Pause (Space)",
"back-10s": "Back 10s (Left arrow key)",
"forward-30s": "Forward 30s",
+ "previous-video": "Previous video: {{- title}}",
+ "next-video": "Next video: {{- title}}",
"mute": "Mute (M)",
"unmute": "Unmute (M)",
"playback-speed": "Playback speed",
diff --git a/apps/client/src/widgets/react/SiblingNavigator.tsx b/apps/client/src/widgets/react/SiblingNavigator.tsx
index 84eadae5c9..a3b0a5b201 100644
--- a/apps/client/src/widgets/react/SiblingNavigator.tsx
+++ b/apps/client/src/widgets/react/SiblingNavigator.tsx
@@ -36,7 +36,7 @@ interface SiblingNavigatorProps {
extraNextKeys?: readonly string[];
}
-interface SiblingNavigationState {
+export interface SiblingNavigationState {
index: number;
total: number;
previousTitle: string;
@@ -78,7 +78,7 @@ export default function SiblingNavigator({ note, noteContext, viewScope, sibling
// 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);
+ : noteSiblingProvider(note, noteContext, { type: siblingType });
const navigation = useSiblingNavigation(provider);
useSiblingKeyboard(navigation, noteContext, keyboardTarget, extraPreviousKeys, extraNextKeys);
@@ -161,19 +161,27 @@ export function useSiblingNavigation(provider: SiblingNavigationProvider): Sibli
};
}
-/** 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 {
+/** Filter for note siblings: a note type (defaults to the current note's), optionally narrowed by a mime prefix. */
+export interface NoteSiblingFilter {
+ type?: string;
+ mimePrefix?: string;
+}
+
+/** Iterator over the current note's matching siblings within the parent of the current tab (clone-aware). */
+export function noteSiblingProvider(note: FNote | undefined, noteContext: NoteContext | undefined, filter: NoteSiblingFilter = {}): SiblingNavigationProvider {
const notePath = noteContext?.notePath;
- const type = siblingType ?? note?.type;
+ const type = filter.type ?? note?.type;
+ const { mimePrefix } = filter;
const parent = getParentFromNotePath(notePath);
+ const matches = (sibling: FNote) => sibling.type === type && (!mimePrefix || sibling.mime.startsWith(mimePrefix));
return {
currentId: note?.noteId,
- depsKey: `note:${parent?.parentPath ?? ""}:${type ?? ""}`,
+ depsKey: `note:${parent?.parentPath ?? ""}:${type ?? ""}:${mimePrefix ?? ""}`,
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 }));
+ return children.filter(matches).map((child) => ({ id: child.noteId, title: child.title }));
},
navigateTo: (id) => { if (parent) void noteContext?.setNote(`${parent.parentPath}/${id}`); },
shouldRefresh: (loadResults, currentSiblingIds) => {
@@ -205,26 +213,29 @@ function attachmentSiblingProvider(note: FNote | undefined, noteContext: NoteCon
/**
* 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 —
- * to previous/next. It acts only when a `keyboardTarget` (if given) holds focus, otherwise when the
- * note context is the active tab/pane, and never while the user is typing in a text field.
+ * viewer remounting on each navigation) maps PageUp/PageDown — Home/End too unless `edgeKeys` is false
+ * (media players reserve those for seeking) — plus any caller-provided extra keys, to previous/next. It
+ * acts only when a `keyboardTarget` (if given) holds focus, otherwise when the note context is the
+ * active tab/pane, and never while the user is typing in a text field.
*/
-function useSiblingKeyboard(
+export function useSiblingKeyboard(
navigation: SiblingNavigationState | null,
noteContext: NoteContext | undefined,
keyboardTarget: RefObject<HTMLElement | null> | undefined,
extraPreviousKeys: readonly string[],
- extraNextKeys: readonly string[]
+ extraNextKeys: readonly string[],
+ options: { edgeKeys?: boolean } = {}
) {
+ const { edgeKeys = true } = options;
// Read the freshest values from inside the listener without re-attaching it on every change.
- const stateRef = useRef({ navigation, noteContext, keyboardTarget, extraPreviousKeys, extraNextKeys });
- stateRef.current = { navigation, noteContext, keyboardTarget, extraPreviousKeys, extraNextKeys };
+ const stateRef = useRef({ navigation, noteContext, keyboardTarget, extraPreviousKeys, extraNextKeys, edgeKeys });
+ stateRef.current = { navigation, noteContext, keyboardTarget, extraPreviousKeys, extraNextKeys, edgeKeys };
useEffect(() => {
const onKeyDown = (e: KeyboardEvent) => {
if (e.defaultPrevented || e.ctrlKey || e.metaKey || e.altKey || e.shiftKey) return;
- const { navigation, noteContext, keyboardTarget, extraPreviousKeys, extraNextKeys } = stateRef.current;
+ const { navigation, noteContext, keyboardTarget, extraPreviousKeys, extraNextKeys, edgeKeys } = stateRef.current;
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.
@@ -235,7 +246,7 @@ function useSiblingKeyboard(
return;
}
- const direction = codeToSiblingDirection(e.code, extraPreviousKeys, extraNextKeys);
+ const direction = codeToSiblingDirection(e.code, extraPreviousKeys, extraNextKeys, edgeKeys);
if (!direction) return;
e.preventDefault();
if (direction === "previous") navigation.navigatePrevious();
diff --git a/apps/client/src/widgets/react/sibling_navigation.spec.ts b/apps/client/src/widgets/react/sibling_navigation.spec.ts
index ea492449ff..23ad47472b 100644
--- a/apps/client/src/widgets/react/sibling_navigation.spec.ts
+++ b/apps/client/src/widgets/react/sibling_navigation.spec.ts
@@ -54,6 +54,13 @@ describe("codeToSiblingDirection", () => {
expect(codeToSiblingDirection("Space", [], [])).toBeNull();
expect(codeToSiblingDirection("KeyA", [ "Backspace" ], [ "Space" ])).toBeNull();
});
+
+ it("omits Home/End when edge keys are disabled, keeping PageUp/PageDown", () => {
+ expect(codeToSiblingDirection("Home", [], [], false)).toBeNull();
+ expect(codeToSiblingDirection("End", [], [], false)).toBeNull();
+ expect(codeToSiblingDirection("PageUp", [], [], false)).toBe("previous");
+ expect(codeToSiblingDirection("PageDown", [], [], false)).toBe("next");
+ });
});
describe("isTextEntryTarget", () => {
diff --git a/apps/client/src/widgets/react/sibling_navigation.ts b/apps/client/src/widgets/react/sibling_navigation.ts
index 8b4391ff28..7352e9bb0e 100644
--- a/apps/client/src/widgets/react/sibling_navigation.ts
+++ b/apps/client/src/widgets/react/sibling_navigation.ts
@@ -29,15 +29,16 @@ export function getSiblingNavigation(siblingNoteIds: string[], currentNoteId: st
export type SiblingDirection = "previous" | "next" | "first" | "last";
/**
- * Maps a `KeyboardEvent.code` to a navigation direction. PageUp/PageDown (prev/next) and Home/End
- * (first/last) are built in; a renderer can supply extra codes for prev/next (e.g. the image viewer
- * adds `Backspace`/`Space`). Using `code` keeps it keyboard-layout independent.
+ * Maps a `KeyboardEvent.code` to a navigation direction. PageUp/PageDown (prev/next) are built in, as
+ * are Home/End (first/last) unless `includeEdges` is false (e.g. media players reserve Home/End for
+ * seeking); a renderer can supply extra codes for prev/next (e.g. the image viewer adds
+ * `Backspace`/`Space`). Using `code` keeps it keyboard-layout independent.
*/
-export function codeToSiblingDirection(code: string, extraPrevious: readonly string[], extraNext: readonly string[]): SiblingDirection | null {
+export function codeToSiblingDirection(code: string, extraPrevious: readonly string[], extraNext: readonly string[], includeEdges = true): SiblingDirection | null {
if (code === "PageUp" || extraPrevious.includes(code)) return "previous";
if (code === "PageDown" || extraNext.includes(code)) return "next";
- if (code === "Home") return "first";
- if (code === "End") return "last";
+ if (includeEdges && code === "Home") return "first";
+ if (includeEdges && code === "End") return "last";
return null;
}
diff --git a/apps/client/src/widgets/type_widgets/File.tsx b/apps/client/src/widgets/type_widgets/File.tsx
index 2fa982a083..e33c77b7eb 100644
--- a/apps/client/src/widgets/type_widgets/File.tsx
+++ b/apps/client/src/widgets/type_widgets/File.tsx
@@ -18,7 +18,7 @@ export default function FileTypeWidget({ note, parentComponent, noteContext }: T
} else if (note.mime === "application/pdf") {
return noteContext && <PdfPreview blob={blob} note={note} componentId={parentComponent?.componentId} noteContext={noteContext} />;
} else if (note.mime.startsWith("video/")) {
- return <VideoPreview note={note} />;
+ return <VideoPreview note={note} noteContext={noteContext} />;
} else if (note.mime.startsWith("audio/")) {
return <AudioPreview note={note} />;
}
diff --git a/apps/client/src/widgets/type_widgets/file/MediaPlayer.tsx b/apps/client/src/widgets/type_widgets/file/MediaPlayer.tsx
index 5518bcd263..31e166e323 100644
--- a/apps/client/src/widgets/type_widgets/file/MediaPlayer.tsx
+++ b/apps/client/src/widgets/type_widgets/file/MediaPlayer.tsx
@@ -3,10 +3,15 @@ import "./MediaPlayer.css";
import { RefObject } from "preact";
import { useEffect, useState } from "preact/hooks";
+import type NoteContext from "../../../components/note_context";
+import type FNote from "../../../entities/fnote";
import { t } from "../../../services/i18n";
import ActionButton from "../../react/ActionButton";
import Dropdown from "../../react/Dropdown";
import Icon from "../../react/Icon";
+import { noteSiblingProvider, type SiblingNavigationState, useSiblingKeyboard, useSiblingNavigation } from "../../react/SiblingNavigator";
+
+const NO_KEYS: readonly string[] = [];
export function SeekBar({ mediaRef }: { mediaRef: RefObject<HTMLVideoElement | HTMLAudioElement> }) {
const [currentTime, setCurrentTime] = useState(0);
@@ -129,6 +134,29 @@ export function VolumeControl({ mediaRef }: { mediaRef: RefObject<HTMLVideoEleme
);
}
+/**
+ * Sibling navigation across the note's same-mime media siblings (e.g. all `video/` files in the parent),
+ * with PageUp/PageDown wired to previous/next. Edge keys (Home/End) and Space are left to the player.
+ */
+export function useMediaSiblingNavigation(note: FNote, noteContext: NoteContext | undefined, mimePrefix: string) {
+ const navigation = useSiblingNavigation(noteSiblingProvider(note, noteContext, { mimePrefix }));
+ useSiblingKeyboard(navigation, noteContext, undefined, NO_KEYS, NO_KEYS, { edgeKeys: false });
+ return navigation;
+}
+
+/** "Skip to previous/next sibling" control, styled like the other media buttons; renders nothing without siblings. */
+export function MediaSiblingButton({ navigation, direction, tooltipI18nKey }: { navigation: SiblingNavigationState | null, direction: "previous" | "next", tooltipI18nKey: string }) {
+ if (!navigation) return null;
+ const isPrevious = direction === "previous";
+ return (
+ <ActionButton
+ icon={isPrevious ? "bx bx-skip-previous" : "bx bx-skip-next"}
+ text={t(tooltipI18nKey, { title: isPrevious ? navigation.previousTitle : navigation.nextTitle })}
+ onClick={() => (isPrevious ? navigation.navigatePrevious() : navigation.navigateNext())}
+ />
+ );
+}
+
export function SkipButton({ mediaRef, seconds, icon, text }: { mediaRef: RefObject<HTMLVideoElement | HTMLAudioElement>, seconds: number, icon: string, text: string }) {
const skip = () => {
const media = mediaRef.current;
diff --git a/apps/client/src/widgets/type_widgets/file/Video.tsx b/apps/client/src/widgets/type_widgets/file/Video.tsx
index 5bf1e260e9..aa501ed1ab 100644
--- a/apps/client/src/widgets/type_widgets/file/Video.tsx
+++ b/apps/client/src/widgets/type_widgets/file/Video.tsx
@@ -3,16 +3,17 @@ import "./Video.css";
import { RefObject } from "preact";
import { MutableRef, useCallback, useEffect, useRef, useState } from "preact/hooks";
+import type NoteContext from "../../../components/note_context";
import FNote from "../../../entities/fnote";
import { t } from "../../../services/i18n";
import { getUrlForDownload } from "../../../services/open";
import ActionButton from "../../react/ActionButton";
import NoItems from "../../react/NoItems";
-import { LoopButton, PlaybackSpeed, PlayPauseButton, SeekBar, SkipButton, VolumeControl } from "./MediaPlayer";
+import { LoopButton, MediaSiblingButton, PlaybackSpeed, PlayPauseButton, SeekBar, SkipButton, useMediaSiblingNavigation, VolumeControl } from "./MediaPlayer";
const AUTO_HIDE_DELAY = 3000;
-export default function VideoPreview({ note }: { note: FNote }) {
+export default function VideoPreview({ note, noteContext }: { note: FNote, noteContext?: NoteContext }) {
const wrapperRef = useRef<HTMLDivElement>(null);
const videoRef = useRef<HTMLVideoElement>(null);
const [playing, setPlaying] = useState(false);
@@ -38,6 +39,7 @@ export default function VideoPreview({ note }: { note: FNote }) {
}, [togglePlayback]);
const onKeyDown = useKeyboardShortcuts(videoRef, wrapperRef, togglePlayback, flashControls);
+ const siblingNavigation = useMediaSiblingNavigation(note, noteContext, "video/");
if (error) {
return <NoItems icon="bx bx-video-off" text={t("media.unsupported-format", { mime: note.mime.replace("/", "-") })} />;
@@ -64,9 +66,11 @@ export default function VideoPreview({ note }: { note: FNote }) {
</div>
<div className="center">
<div className="spacer" />
+ <MediaSiblingButton navigation={siblingNavigation} direction="previous" tooltipI18nKey="media.previous-video" />
<SkipButton mediaRef={videoRef} seconds={-10} icon="bx bx-rewind" text={t("media.back-10s")} />
<PlayPauseButton playing={playing} togglePlayback={togglePlayback} />
<SkipButton mediaRef={videoRef} seconds={30} icon="bx bx-fast-forward" text={t("media.forward-30s")} />
+ <MediaSiblingButton navigation={siblingNavigation} direction="next" tooltipI18nKey="media.next-video" />
<LoopButton mediaRef={videoRef} />
</div>
<div className="right">