Skip to content

Build your personal knowledge base with Trilium Notes

client/media player: add play mode options

Adorian Doran676461cb3569
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.json
index 0be6fc1075..8e43168764 100644
--- a/apps/client/src/translations/en/translation.json
+++ b/apps/client/src/translations/en/translation.json
@@ -1196,8 +1196,10 @@
"mute": "Mute (M)",
"unmute": "Unmute (M)",
"playback-speed": "Playback speed",
- "loop": "Loop",
- "disable-loop": "Disable loop",
+ "play-mode-title": "Play mode: {{mode}}",
+ "play-mode-once": "Play once",
+ "play-mode-loop": "Loop",
+ "play-mode-auto": "Autoplay",
"rotate": "Rotate",
"picture-in-picture": "Picture-in-picture",
"exit-picture-in-picture": "Exit picture-in-picture",
diff --git a/apps/client/src/widgets/react/Dropdown.tsx b/apps/client/src/widgets/react/Dropdown.tsx
index 407b14a63a..bd84db2571 100644
--- a/apps/client/src/widgets/react/Dropdown.tsx
+++ b/apps/client/src/widgets/react/Dropdown.tsx
@@ -43,6 +43,12 @@ export default function Dropdown({ id, className, buttonClassName, isStatic, chi
const { showTooltip, hideTooltip } = useTooltip(containerRef, {
...titleOptions,
+ // Drive the tooltip text from the config (not just the `title` attribute): Bootstrap reads the
+ // attribute only once on init, so a dynamic title (e.g. the media play-mode button) would otherwise
+ // stay stale. useTooltip recreates the tooltip each render, so this keeps it in sync with the prop.
+ // Fall back to "" (Bootstrap's default) since it rejects an explicit `undefined`; an empty title
+ // simply shows no tooltip.
+ title: title ?? "",
placement: titlePosition ?? "bottom",
fallbackPlacements: [ titlePosition ?? "bottom" ],
trigger: "manual"
diff --git a/apps/client/src/widgets/type_widgets/file/Audio.tsx b/apps/client/src/widgets/type_widgets/file/Audio.tsx
index 01b34a8486..d1402fed01 100644
--- a/apps/client/src/widgets/type_widgets/file/Audio.tsx
+++ b/apps/client/src/widgets/type_widgets/file/Audio.tsx
@@ -6,7 +6,7 @@ import { t } from "../../../services/i18n";
import { getUrlForDownload } from "../../../services/open";
import Icon from "../../react/Icon";
import NoItems from "../../react/NoItems";
-import { LoopButton, MediaSiblingButton, PlaybackSpeed, PlayPauseButton, SeekBar, SkipButton, useMediaSessionController, VolumeControl } from "./MediaPlayer";
+import { MediaSiblingButton, PlaybackSpeed, PlayModeButton, PlayPauseButton, SeekBar, SkipButton, useMediaPlayMode, useMediaSessionController, VolumeControl } from "./MediaPlayer";
export default function AudioPreview({ note, noteContext }: { note: FNote, noteContext?: NoteContext }) {
const wrapperRef = useRef<HTMLDivElement>(null);
@@ -27,6 +27,7 @@ export default function AudioPreview({ note, noteContext }: { note: FNote, noteC
useEffect(() => setError(false), [note.noteId]);
const onError = useCallback(() => setError(true), []);
const siblingNavigation = useMediaSessionController(note, noteContext, "audio/", audioRef);
+ const { mode: playMode, setMode: setPlayMode } = useMediaPlayMode(noteContext, audioRef);
if (error) {
return <NoItems icon="bx bx-volume-mute" text={t("media.unsupported-format", { mime: note.mime.replace("/", "-") })} />;
@@ -51,6 +52,7 @@ export default function AudioPreview({ note, noteContext }: { note: FNote, noteC
<div class="media-buttons-row">
<div className="left">
<PlaybackSpeed mediaRef={audioRef} />
+ <PlayModeButton mode={playMode} onSelectMode={setPlayMode} />
</div>
<div className="center">
@@ -60,7 +62,6 @@ export default function AudioPreview({ note, noteContext }: { note: FNote, noteC
<PlayPauseButton playing={playing} togglePlayback={togglePlayback} />
<SkipButton mediaRef={audioRef} seconds={30} icon="bx bx-fast-forward" text={t("media.forward-30s")} />
<MediaSiblingButton navigation={siblingNavigation} direction="next" tooltipI18nKey="media.next-audio" />
- <LoopButton mediaRef={audioRef} />
</div>
<div className="right">
diff --git a/apps/client/src/widgets/type_widgets/file/MediaPlayer.css b/apps/client/src/widgets/type_widgets/file/MediaPlayer.css
index d7e58a26eb..2564f45325 100644
--- a/apps/client/src/widgets/type_widgets/file/MediaPlayer.css
+++ b/apps/client/src/widgets/type_widgets/file/MediaPlayer.css
@@ -81,6 +81,19 @@
}
}
+.play-mode-dropdown {
+ .dropdown-item {
+ display: flex;
+ align-items: center;
+ gap: 0.6em;
+ }
+
+ /* Pushes the checkmark of the current mode to the trailing edge of the item. */
+ .play-mode-check {
+ margin-left: auto;
+ }
+}
+
.audio-preview-wrapper {
width: 100%;
height: 100%;
diff --git a/apps/client/src/widgets/type_widgets/file/MediaPlayer.tsx b/apps/client/src/widgets/type_widgets/file/MediaPlayer.tsx
index 1da616189a..3fc9082e81 100644
--- a/apps/client/src/widgets/type_widgets/file/MediaPlayer.tsx
+++ b/apps/client/src/widgets/type_widgets/file/MediaPlayer.tsx
@@ -1,19 +1,20 @@
import "./MediaPlayer.css";
import { RefObject } from "preact";
-import { useEffect, useRef, useState } from "preact/hooks";
+import { useCallback, useEffect, useRef, useState } from "preact/hooks";
import type NoteContext from "../../../components/note_context";
import type FNote from "../../../entities/fnote";
+import attributes from "../../../services/attributes";
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 { 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";
-import { getAutoAdvanceTarget, MEDIA_PLAY_MODE_LABEL } from "./media_play_mode";
+import { getAutoAdvanceTarget, MEDIA_PLAY_MODE_ICONS, MEDIA_PLAY_MODE_LABEL, MEDIA_PLAY_MODE_LABEL_KEYS, MEDIA_PLAY_MODES, type MediaPlayMode, playModeFromLabel, playModeToLabel, shouldLoop } from "./media_play_mode";
const NO_KEYS: readonly string[] = [];
@@ -353,33 +354,77 @@ export function SkipButton({ mediaRef, seconds, icon, text }: { mediaRef: RefObj
);
}
-export function LoopButton({ mediaRef }: { mediaRef: RefObject<HTMLVideoElement | HTMLAudioElement> }) {
- const [loop, setLoop] = useState(() => mediaRef.current?.loop ?? false);
+/**
+ * The folder-level play mode (read from the parent's `#mediaNotesPlayMode` label) for the current media note,
+ * kept in sync as that label changes. Derives the element's `loop` from the mode and persists changes back to
+ * the parent — the `auto` mode's actual auto-advance is handled by {@link useMediaSessionController}.
+ */
+export function useMediaPlayMode(noteContext: NoteContext | undefined, mediaRef: RefObject<HTMLVideoElement | HTMLAudioElement>): { mode: MediaPlayMode; setMode: (mode: MediaPlayMode) => void } {
+ const parentNoteId = getParentFromNotePath(noteContext?.notePath)?.parentNoteId;
+ const [ mode, setLocalMode ] = useState<MediaPlayMode>("once");
- useEffect(() => {
- const media = mediaRef.current;
- if (!media) return;
- setLoop(media.loop);
+ const refresh = useCallback(() => {
+ if (!parentNoteId) {
+ setLocalMode("once");
+ return;
+ }
+ froca.getNote(parentNoteId).then((parent) => setLocalMode(playModeFromLabel(parent?.getLabelValue(MEDIA_PLAY_MODE_LABEL)))).catch(() => {});
+ }, [ parentNoteId ]);
- const observer = new MutationObserver(() => setLoop(media.loop));
- observer.observe(media, { attributes: true, attributeFilter: ["loop"] });
- return () => observer.disconnect();
- }, [ mediaRef ]);
+ useEffect(refresh, [ refresh ]);
+ useTriliumEvent("entitiesReloaded", ({ loadResults }) => {
+ if (parentNoteId && loadResults.getAttributeRows().some((attr) => attr.noteId === parentNoteId && attr.name === MEDIA_PLAY_MODE_LABEL)) {
+ refresh();
+ }
+ });
- const toggle = () => {
+ // Looping is derived from the mode (auto-advance for "auto" lives in useMediaSessionController's ended handler).
+ useEffect(() => {
const media = mediaRef.current;
- if (!media) return;
- media.loop = !media.loop;
- setLoop(media.loop);
- };
+ if (media) media.loop = shouldLoop(mode);
+ }, [ mode, mediaRef ]);
+
+ const setMode = useCallback((next: MediaPlayMode) => {
+ setLocalMode(next); // optimistic; the entitiesReloaded refresh confirms it once the parent label is written
+ if (!parentNoteId) return;
+ froca.getNote(parentNoteId).then((parent) => {
+ if (!parent) return;
+ const value = playModeToLabel(next);
+ if (value === null) {
+ attributes.removeOwnedLabelByName(parent, MEDIA_PLAY_MODE_LABEL);
+ } else {
+ void attributes.setLabel(parent.noteId, MEDIA_PLAY_MODE_LABEL, value);
+ }
+ }).catch(() => {});
+ }, [ parentNoteId ]);
+ return { mode, setMode };
+}
+
+/** Replaces the loop toggle: a dropdown to pick the folder's play mode (play once / loop / autoplay). */
+export function PlayModeButton({ mode, onSelectMode }: { mode: MediaPlayMode, onSelectMode: (mode: MediaPlayMode) => void }) {
return (
- <ActionButton
- className={loop ? "active" : ""}
- icon="bx bx-repeat"
- text={loop ? t("media.disable-loop") : t("media.loop")}
- onClick={toggle}
- />
+ <Dropdown
+ iconAction
+ hideToggleArrow
+ className="play-mode-dropdown"
+ text={<Icon icon={MEDIA_PLAY_MODE_ICONS[mode]} />}
+ title={t("media.play-mode-title", { mode: t(MEDIA_PLAY_MODE_LABEL_KEYS[mode]) })}
+ >
+ {MEDIA_PLAY_MODES.map((candidate) => (
+ <li key={candidate}>
+ <button
+ type="button"
+ class={`dropdown-item ${candidate === mode ? "active" : ""}`}
+ onClick={() => onSelectMode(candidate)}
+ >
+ <Icon icon={MEDIA_PLAY_MODE_ICONS[candidate]} />
+ <span class="play-mode-name">{t(MEDIA_PLAY_MODE_LABEL_KEYS[candidate])}</span>
+ {candidate === mode && <Icon icon="bx bx-check" className="play-mode-check" />}
+ </button>
+ </li>
+ ))}
+ </Dropdown>
);
}
diff --git a/apps/client/src/widgets/type_widgets/file/Video.tsx b/apps/client/src/widgets/type_widgets/file/Video.tsx
index 7ba5a9f02c..2936057a01 100644
--- a/apps/client/src/widgets/type_widgets/file/Video.tsx
+++ b/apps/client/src/widgets/type_widgets/file/Video.tsx
@@ -9,7 +9,7 @@ import { t } from "../../../services/i18n";
import { getUrlForDownload } from "../../../services/open";
import ActionButton from "../../react/ActionButton";
import NoItems from "../../react/NoItems";
-import { LoopButton, MediaSiblingButton, PlaybackSpeed, PlayPauseButton, SeekBar, SkipButton, useMediaSessionController, VolumeControl } from "./MediaPlayer";
+import { MediaSiblingButton, PlaybackSpeed, PlayModeButton, PlayPauseButton, SeekBar, SkipButton, useMediaPlayMode, useMediaSessionController, VolumeControl } from "./MediaPlayer";
const AUTO_HIDE_DELAY = 3000;
@@ -40,6 +40,7 @@ export default function VideoPreview({ note, noteContext }: { note: FNote, noteC
const onKeyDown = useKeyboardShortcuts(videoRef, wrapperRef, togglePlayback, flashControls);
const siblingNavigation = useMediaSessionController(note, noteContext, "video/", videoRef);
+ const { mode: playMode, setMode: setPlayMode } = useMediaPlayMode(noteContext, videoRef);
if (error) {
return <NoItems icon="bx bx-video-off" text={t("media.unsupported-format", { mime: note.mime.replace("/", "-") })} />;
@@ -62,6 +63,7 @@ export default function VideoPreview({ note, noteContext }: { note: FNote, noteC
<div class="media-buttons-row">
<div className="left">
<PlaybackSpeed mediaRef={videoRef} />
+ <PlayModeButton mode={playMode} onSelectMode={setPlayMode} />
<RotateButton videoRef={videoRef} />
</div>
<div className="center">
@@ -71,7 +73,6 @@ export default function VideoPreview({ note, noteContext }: { note: FNote, noteC
<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">
<VolumeControl mediaRef={videoRef} />
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.ts
index 880672908e..73c4c41bea 100644
--- 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.ts
@@ -1,6 +1,6 @@
import { describe, expect, it } from "vitest";
-import { type AutoAdvanceNavigation, getAutoAdvanceTarget } from "./media_play_mode";
+import { type AutoAdvanceNavigation, getAutoAdvanceTarget, MEDIA_PLAY_MODES, type MediaPlayMode, playModeFromLabel, playModeToLabel, shouldLoop } from "./media_play_mode";
describe("getAutoAdvanceTarget", () => {
const nav = (index: number, total: number, nextId: string): AutoAdvanceNavigation => ({ index, total, nextId });
@@ -28,3 +28,41 @@ describe("getAutoAdvanceTarget", () => {
expect(getAutoAdvanceTarget("auto", null)).toBeNull();
});
});
+
+describe("playModeFromLabel", () => {
+ it("maps the parent's label value to a mode", () => {
+ expect(playModeFromLabel("loop")).toBe("loop");
+ expect(playModeFromLabel("auto")).toBe("auto");
+ });
+
+ it("treats a missing or unrecognised label as play-once", () => {
+ expect(playModeFromLabel(null)).toBe("once");
+ expect(playModeFromLabel(undefined)).toBe("once");
+ expect(playModeFromLabel("")).toBe("once");
+ expect(playModeFromLabel("once")).toBe("once");
+ expect(playModeFromLabel("bogus")).toBe("once");
+ });
+});
+
+describe("playModeToLabel", () => {
+ it("removes the label for play-once and writes the value otherwise", () => {
+ expect(playModeToLabel("once")).toBeNull();
+ expect(playModeToLabel("loop")).toBe("loop");
+ expect(playModeToLabel("auto")).toBe("auto");
+ });
+
+ it("round-trips with playModeFromLabel for every mode", () => {
+ for (const mode of MEDIA_PLAY_MODES) {
+ expect(playModeFromLabel(playModeToLabel(mode))).toBe(mode);
+ }
+ });
+});
+
+describe("shouldLoop", () => {
+ it("loops only in loop mode", () => {
+ const looping: Record<MediaPlayMode, boolean> = { once: false, loop: true, auto: false };
+ for (const mode of MEDIA_PLAY_MODES) {
+ expect(shouldLoop(mode)).toBe(looping[mode]);
+ }
+ });
+});
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.ts
index 2a770f808c..3e8cb0d16b 100644
--- a/apps/client/src/widgets/type_widgets/file/media_play_mode.ts
+++ b/apps/client/src/widgets/type_widgets/file/media_play_mode.ts
@@ -1,9 +1,37 @@
/** 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 repeats the current note when it ends. */
+export const MEDIA_PLAY_MODE_LOOP = "loop";
+
/** {@link MEDIA_PLAY_MODE_LABEL} value that auto-advances to the next sibling when playback ends. */
export const MEDIA_PLAY_MODE_AUTO = "auto";
+/**
+ * What a media note does when it finishes, configured per folder via {@link MEDIA_PLAY_MODE_LABEL}:
+ * - `once` — stop (no loop, no auto-advance); the parent carries no label.
+ * - `loop` — repeat the current note; parent label is `"loop"`.
+ * - `auto` — advance to the next sibling; parent label is `"auto"`.
+ */
+export type MediaPlayMode = "once" | "loop" | "auto";
+
+/** All modes, in the order shown in the play-mode menu. */
+export const MEDIA_PLAY_MODES: readonly MediaPlayMode[] = [ "once", "loop", "auto" ];
+
+/** Boxicon class for each mode — shown on the button (current mode) and beside each menu item. */
+export const MEDIA_PLAY_MODE_ICONS: Record<MediaPlayMode, string> = {
+ once: "bx bx-arrow-to-right",
+ loop: "bx bx-repeat",
+ auto: "bx bx-arrow-from-left"
+};
+
+/** i18n key for each mode's display name. */
+export const MEDIA_PLAY_MODE_LABEL_KEYS: Record<MediaPlayMode, string> = {
+ once: "media.play-mode-once",
+ loop: "media.play-mode-loop",
+ auto: "media.play-mode-auto"
+};
+
/** The minimal slice of sibling-navigation state {@link getAutoAdvanceTarget} needs. */
export interface AutoAdvanceNavigation {
/** 1-based position of the current item among its siblings. */
@@ -14,6 +42,23 @@ export interface AutoAdvanceNavigation {
nextId: string;
}
+/** The play mode encoded by a parent's {@link MEDIA_PLAY_MODE_LABEL} value; anything unrecognised is "play once". */
+export function playModeFromLabel(labelValue: string | null | undefined): MediaPlayMode {
+ if (labelValue === MEDIA_PLAY_MODE_LOOP) return "loop";
+ if (labelValue === MEDIA_PLAY_MODE_AUTO) return "auto";
+ return "once";
+}
+
+/** The {@link MEDIA_PLAY_MODE_LABEL} value to persist for a mode, or `null` when the label should be removed. */
+export function playModeToLabel(mode: MediaPlayMode): string | null {
+ return mode === "once" ? null : mode;
+}
+
+/** Whether the media element should loop in this mode. */
+export function shouldLoop(mode: MediaPlayMode): boolean {
+ return mode === "loop";
+}
+
/**
* 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