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/media player: add support to auto play the next media note
Adorian Doran4405a04d4d61
diff --git a/apps/client/src/widgets/type_widgets/file/MediaPlayer.tsx b/apps/client/src/widgets/type_widgets/file/MediaPlayer.tsxindex 6c074251ea..1da616189a 100644--- a/apps/client/src/widgets/type_widgets/file/MediaPlayer.tsx+++ b/apps/client/src/widgets/type_widgets/file/MediaPlayer.tsx@@ -5,12 +5,15 @@ import { useEffect, useRef, useState } from "preact/hooks";import type NoteContext from "../../../components/note_context";import type FNote from "../../../entities/fnote";+import froca from "../../../services/froca";import { t } from "../../../services/i18n";import ActionButton from "../../react/ActionButton";import Dropdown from "../../react/Dropdown";import { useTriliumEvents } from "../../react/hooks";import Icon from "../../react/Icon";+import { getParentFromNotePath } from "../../react/sibling_navigation";import { noteSiblingProvider, type SiblingNavigationState, useSiblingKeyboard, useSiblingNavigation } from "../../react/SiblingNavigator";+import { getAutoAdvanceTarget, MEDIA_PLAY_MODE_LABEL } from "./media_play_mode";const NO_KEYS: readonly string[] = [];@@ -192,20 +195,38 @@ export function useMediaSessionController(note: FNote, noteContext: NoteContextuseSiblingKeyboard(wrapped, noteContext, undefined, NO_KEYS, NO_KEYS, { edgeKeys: false });+ const hasMediaNav = !!wrapped;+ const wrappedRef = useRef(wrapped);+ wrappedRef.current = wrapped;+// Only one media element plays at a time. Starting playback claims the global "active" slot and notifies// every other mounted player (audio or video) so they pause themselves and hand over the OS Media Session.// The slot is *kept* through pause/end (a paused player keeps the session); it's only released when another- // player claims it or when this player unmounts (its tab is closed).+ // player claims it or when this player unmounts (its tab is closed). When a track ends and the parent folder+ // opted in (`#mediaNotesPlayMode=auto`), auto-advance to the next sibling: navigation reuses this same media+ // element (siblings share the type), so playback — and with it the OS session that keeps a backgrounded /+ // sleeping mobile page alive — continues across the jump rather than the page going idle.useEffect(() => {const media = mediaRef.current;if (!media) return;const claim = () => setActiveMediaPlayer(self);+ const onEnded = () => {+ const navigation = wrappedRef.current;+ if (!navigation) return;+ const parentNoteId = getParentFromNotePath(noteContext?.notePath)?.parentNoteId;+ const playMode = parentNoteId ? froca.notes[parentNoteId]?.getLabelValue(MEDIA_PLAY_MODE_LABEL) : null;+ if (getAutoAdvanceTarget(playMode, navigation)) {+ navigation.navigateNext();+ }+ };media.addEventListener("play", claim);+ media.addEventListener("ended", onEnded);return () => {media.removeEventListener("play", claim);+ media.removeEventListener("ended", onEnded);if (activeMediaPlayer === self) setActiveMediaPlayer(null);};- }, [ self, mediaRef ]);+ }, [ self, mediaRef, noteContext ]);// When another player claims the active slot, pause ourselves and re-render so we release the session.useEffect(() => {@@ -221,9 +242,6 @@ export function useMediaSessionController(note: FNote, noteContext: NoteContext// metadata while this player owns the session (its context's current note, and the one playing). They're// global, so only ever release our own — otherwise the outgoing player's cleanup during a handover would// clobber the incoming player's; the release also clears them so nothing lingers in the OS overlay.- const hasMediaNav = !!wrapped;- const wrappedRef = useRef(wrapped);- wrappedRef.current = wrapped;useEffect(() => {if (!("mediaSession" in navigator)) return;const mediaSession = navigator.mediaSession;diff --git a/apps/client/src/widgets/type_widgets/file/media_play_mode.spec.ts b/apps/client/src/widgets/type_widgets/file/media_play_mode.spec.tsnew file mode 100644index 0000000000..880672908e--- /dev/null+++ b/apps/client/src/widgets/type_widgets/file/media_play_mode.spec.ts@@ -0,0 +1,30 @@+import { describe, expect, it } from "vitest";++import { type AutoAdvanceNavigation, getAutoAdvanceTarget } from "./media_play_mode";++describe("getAutoAdvanceTarget", () => {+ const nav = (index: number, total: number, nextId: string): AutoAdvanceNavigation => ({ index, total, nextId });++ it("advances to the following sibling when the parent is in auto mode", () => {+ expect(getAutoAdvanceTarget("auto", nav(1, 3, "b"))).toBe("b");+ expect(getAutoAdvanceTarget("auto", nav(2, 3, "c"))).toBe("c");+ });++ it("stops at the last sibling instead of wrapping back to the first", () => {+ // nextId wraps to the first sibling here, but auto-advance must not loop the folder.+ expect(getAutoAdvanceTarget("auto", nav(3, 3, "a"))).toBeNull();+ });++ it("does not advance unless the play mode is exactly \"auto\"", () => {+ const navigation = nav(1, 3, "b");+ expect(getAutoAdvanceTarget(undefined, navigation)).toBeNull();+ expect(getAutoAdvanceTarget(null, navigation)).toBeNull();+ expect(getAutoAdvanceTarget("", navigation)).toBeNull();+ expect(getAutoAdvanceTarget("manual", navigation)).toBeNull();+ expect(getAutoAdvanceTarget("Auto", navigation)).toBeNull();+ });++ it("does not advance when there is no sibling navigation (a lone item or none)", () => {+ expect(getAutoAdvanceTarget("auto", null)).toBeNull();+ });+});diff --git a/apps/client/src/widgets/type_widgets/file/media_play_mode.ts b/apps/client/src/widgets/type_widgets/file/media_play_mode.tsnew file mode 100644index 0000000000..2a770f808c--- /dev/null+++ b/apps/client/src/widgets/type_widgets/file/media_play_mode.ts@@ -0,0 +1,28 @@+/** Label, set on a media note's parent, that opts its children into a playback mode. */+export const MEDIA_PLAY_MODE_LABEL = "mediaNotesPlayMode";++/** {@link MEDIA_PLAY_MODE_LABEL} value that auto-advances to the next sibling when playback ends. */+export const MEDIA_PLAY_MODE_AUTO = "auto";++/** The minimal slice of sibling-navigation state {@link getAutoAdvanceTarget} needs. */+export interface AutoAdvanceNavigation {+ /** 1-based position of the current item among its siblings. */+ index: number;+ /** Total number of siblings. */+ total: number;+ /** Id of the next sibling (the navigation wraps, so at the end this is the first sibling). */+ nextId: string;+}++/**+ * When a media note finishes playing, the id of the sibling to play next — or `null` to stop. Auto-advance+ * happens only when the parent opted in via `#mediaNotesPlayMode=auto`, and only to a *following* sibling: the+ * last item stops rather than wrapping back to the first.+ */+export function getAutoAdvanceTarget(playMode: string | null | undefined, navigation: AutoAdvanceNavigation | null): string | null {+ if (playMode !== MEDIA_PLAY_MODE_AUTO || !navigation) {+ return null;+ }+ // `nextId` wraps to the first sibling at the end; only advance while a later sibling actually follows.+ return navigation.index < navigation.total ? navigation.nextId : null;+}