Skip to content

Build your personal knowledge base with Trilium Notes

client/media player/preview render: prevent clicks on the player surface to open the note

Adorian Dorane4a637ccbcc9
diff --git a/apps/client/src/services/link.spec.ts b/apps/client/src/services/link.spec.ts
index df9e8097f7..7bf1898eaa 100644
--- a/apps/client/src/services/link.spec.ts
+++ b/apps/client/src/services/link.spec.ts
@@ -674,6 +674,25 @@ describe("module-level click handlers", () => {
$a.remove();
});
+ it("leaves navigation to content that handles its own clicks (no-link-navigation)", () => {
+ // A media player inside a collection card: the card is itself a link (it wires its own click straight
+ // to goToLink), so pressing play in the player must not also open the note.
+ const $card = $("<div class='block-link' data-href='#root/aaaaaaaaaaaa'></div>");
+ $card.append("<div class='no-link-navigation'><button class='play'></button></div>");
+ $("body").append($card);
+
+ const clickOn = (el: HTMLElement | undefined) =>
+ linkService.goToLink($.Event("click", { which: 1, target: el }) as unknown as JQuery.ClickEvent);
+
+ clickOn($card.find(".play")[0]);
+ expect(openInCurrentNoteContext).not.toHaveBeenCalled();
+
+ // A click on the card outside that content still navigates.
+ clickOn($card[0]);
+ expect(openInCurrentNoteContext).toHaveBeenCalled();
+ $card.remove();
+ });
+
it("opens the link context menu on contextmenu of an internal link", () => {
const $a = $("<a href='#root/aaaaaaaaaaaa'>link</a>");
$("body").append($a);
diff --git a/apps/client/src/services/link.ts b/apps/client/src/services/link.ts
index 2dd242977f..f6929d07df 100644
--- a/apps/client/src/services/link.ts
+++ b/apps/client/src/services/link.ts
@@ -279,8 +279,22 @@ export function parseNavigationStateFromUrl(url: string | undefined) {
};
}
+/**
+ * Interactive content that handles its own clicks opts out of link navigation by carrying this class. It is
+ * needed where such content sits inside a link — a media player in a collection card, whose card is itself a
+ * `.block-link` — so that pressing play doesn't also open the note. Marking the content is what lets its
+ * clicks keep bubbling to the document, which the Bootstrap dropdowns inside it rely on; stopping propagation
+ * at the player would kill link navigation and those dropdowns alike.
+ */
+const NO_LINK_NAVIGATION_SELECTOR = ".no-link-navigation";
+
function goToLink(evt: MouseEvent | JQuery.ClickEvent | JQuery.MouseDownEvent) {
- const $link = $(evt.target as any).closest("a,.block-link");
+ const $target = $(evt.target as any);
+ if ($target.closest(NO_LINK_NAVIGATION_SELECTOR).length) {
+ return false;
+ }
+
+ const $link = $target.closest("a,.block-link");
const hrefLink = $link.attr("href") || $link.attr("data-href");
return goToLinkExt(evt, hrefLink, $link);
diff --git a/apps/client/src/widgets/type_widgets/file/Audio.tsx b/apps/client/src/widgets/type_widgets/file/Audio.tsx
index da098ec39d..26f5a35758 100644
--- a/apps/client/src/widgets/type_widgets/file/Audio.tsx
+++ b/apps/client/src/widgets/type_widgets/file/Audio.tsx
@@ -5,7 +5,7 @@ import Icon from "../../react/Icon";
import NoItems from "../../react/NoItems";
import { loadWaveform } from "./audio_waveform";
import { AudioVisualizer } from "./AudioVisualizer";
-import { preloadFor } from "./media_environment";
+import { playerRootClasses, preloadFor } from "./media_environment";
import { MediaPlayerProps, MediaSiblingButton, PlaybackSpeed, PlayModeButton, PlayPauseButton, SkipButton, useMediaPlayMode, useMediaSessionController, VolumeControl } from "./MediaPlayer";
import { WaveformSeekBar } from "./WaveformSeekBar";
@@ -44,7 +44,7 @@ export default function AudioPreview({ source, environment, note, noteContext, i
}
return (
- <div ref={wrapperRef} className={`audio-preview-wrapper media-env-${environment}`} onKeyDown={onKeyDown} tabIndex={0}>
+ <div ref={wrapperRef} className={`audio-preview-wrapper ${playerRootClasses(environment)}`} onKeyDown={onKeyDown} tabIndex={0}>
<audio
class="audio-preview"
src={source.streamUrl}
diff --git a/apps/client/src/widgets/type_widgets/file/MediaPreview.spec.tsx b/apps/client/src/widgets/type_widgets/file/MediaPreview.spec.tsx
index 82bcd9ab82..b23e1d882d 100644
--- a/apps/client/src/widgets/type_widgets/file/MediaPreview.spec.tsx
+++ b/apps/client/src/widgets/type_widgets/file/MediaPreview.spec.tsx
@@ -107,4 +107,17 @@ describe("MediaPreview", () => {
await act(async () => render(<MediaPreview entity={videoNote} environment="embedded" />, container));
expect(container.querySelector(".play-mode-dropdown")).toBeNull();
});
+
+ it("keeps a preview's clicks to itself, so pressing play in a collection card doesn't open the note", async () => {
+ // Both the placeholder and the player it becomes opt out of link navigation (see services/link.ts).
+ await act(async () => render(<MediaPreview entity={videoNote} environment="preview" />, container));
+ expect(container.querySelector(".media-proxy.no-link-navigation")).not.toBeNull();
+
+ await act(async () => proxyPlayButton()?.click());
+ expect(container.querySelector(".video-preview-wrapper.no-link-navigation")).not.toBeNull();
+
+ // An embedded player isn't inside a link, and must not suppress navigation.
+ await act(async () => render(<MediaPreview entity={videoNote} environment="embedded" />, container));
+ expect(container.querySelector(".no-link-navigation")).toBeNull();
+ });
});
diff --git a/apps/client/src/widgets/type_widgets/file/MediaProxy.tsx b/apps/client/src/widgets/type_widgets/file/MediaProxy.tsx
index e4b0039cbb..c951187a68 100644
--- a/apps/client/src/widgets/type_widgets/file/MediaProxy.tsx
+++ b/apps/client/src/widgets/type_widgets/file/MediaProxy.tsx
@@ -14,7 +14,7 @@ export default function MediaProxy({ source, onActivate }: { source: MediaSource
const isVideo = source.mime.startsWith("video/");
return (
- <div className="media-proxy">
+ <div className="media-proxy no-link-navigation">
<Icon icon={isVideo ? "bx bx-movie-play" : "bx bx-music"} className="media-proxy-icon" />
<ActionButton
className="media-proxy-play"
diff --git a/apps/client/src/widgets/type_widgets/file/Video.tsx b/apps/client/src/widgets/type_widgets/file/Video.tsx
index 06b3c534c3..28524b68d6 100644
--- a/apps/client/src/widgets/type_widgets/file/Video.tsx
+++ b/apps/client/src/widgets/type_widgets/file/Video.tsx
@@ -6,7 +6,7 @@ import { MutableRef, useCallback, useEffect, useRef, useState } from "preact/hoo
import { t } from "../../../services/i18n";
import ActionButton from "../../react/ActionButton";
import NoItems from "../../react/NoItems";
-import { preloadFor } from "./media_environment";
+import { playerRootClasses, preloadFor } from "./media_environment";
import { MediaPlayerProps, MediaSiblingButton, PlaybackSpeed, PlayModeButton, PlayPauseButton, SeekBar, SkipButton, useMediaPlayMode, useMediaSessionController, VolumeControl } from "./MediaPlayer";
const AUTO_HIDE_DELAY = 3000;
@@ -70,7 +70,7 @@ export default function VideoPreview({ source, environment, note, noteContext, i
}
return (
- <div ref={wrapperRef} className={`video-preview-wrapper media-env-${environment} ${controlsVisible ? "" : "controls-hidden"}`} tabIndex={0} onClick={onVideoClick} onKeyDown={onKeyDown} onPointerDown={onPointerDown} onPointerMove={onPointerMove}>
+ <div ref={wrapperRef} className={`video-preview-wrapper ${playerRootClasses(environment)} ${controlsVisible ? "" : "controls-hidden"}`} tabIndex={0} onClick={onVideoClick} onKeyDown={onKeyDown} onPointerDown={onPointerDown} onPointerMove={onPointerMove}>
<video
ref={videoRef}
class="video-preview"
diff --git a/apps/client/src/widgets/type_widgets/file/media_environment.spec.ts b/apps/client/src/widgets/type_widgets/file/media_environment.spec.ts
index 61c6403591..3e8f019995 100644
--- a/apps/client/src/widgets/type_widgets/file/media_environment.spec.ts
+++ b/apps/client/src/widgets/type_widgets/file/media_environment.spec.ts
@@ -1,6 +1,6 @@
import { describe, expect, it } from "vitest";
-import { loadsEagerly, preloadFor } from "./media_environment";
+import { loadsEagerly, playerRootClasses, preloadFor } from "./media_environment";
describe("media environment", () => {
it("only a preview is lazy — it must not create a media element until the user asks for one", () => {
@@ -14,4 +14,10 @@ describe("media environment", () => {
expect(preloadFor("embedded")).toBe("metadata");
expect(preloadFor("preview")).toBe("metadata");
});
+
+ it("makes only a preview opt out of link navigation, since its host card is itself a link", () => {
+ expect(playerRootClasses("preview")).toBe("media-env-preview no-link-navigation");
+ expect(playerRootClasses("embedded")).toBe("media-env-embedded");
+ expect(playerRootClasses("standalone")).toBe("media-env-standalone");
+ });
});
diff --git a/apps/client/src/widgets/type_widgets/file/media_environment.ts b/apps/client/src/widgets/type_widgets/file/media_environment.ts
index 250e458795..489050ee7e 100644
--- a/apps/client/src/widgets/type_widgets/file/media_environment.ts
+++ b/apps/client/src/widgets/type_widgets/file/media_environment.ts
@@ -18,3 +18,12 @@ export function loadsEagerly(environment: MediaEnvironment): boolean {
export function preloadFor(environment: MediaEnvironment): "auto" | "metadata" {
return environment === "standalone" ? "auto" : "metadata";
}
+
+/**
+ * Classes for a player's root element. A preview sits inside a clickable host (a collection card is itself a
+ * link), so it opts out of link navigation — otherwise pressing play, seeking or opening the speed dropdown
+ * would also open the note. See `no-link-navigation` in services/link.ts.
+ */
+export function playerRootClasses(environment: MediaEnvironment): string {
+ return `media-env-${environment}${environment === "preview" ? " no-link-navigation" : ""}`;
+}