Skip to content

Build your personal knowledge base with Trilium Notes

fix(client/video): follow this video's screen, not anyone's

Elian Doranb65d72480bec
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.json
index 47442da047..e9e0d68e04 100644
--- a/apps/client/src/translations/en/translation.json
+++ b/apps/client/src/translations/en/translation.json
@@ -1625,8 +1625,6 @@
"rotate": "Rotate",
"picture-in-picture": "Picture-in-picture",
"exit-picture-in-picture": "Exit picture-in-picture",
- "fullscreen": "Fullscreen",
- "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",
diff --git a/apps/client/src/widgets/type_widgets/file/Video.spec.tsx b/apps/client/src/widgets/type_widgets/file/Video.spec.tsx
index 6ed17233b8..155bdbc4e6 100644
--- a/apps/client/src/widgets/type_widgets/file/Video.spec.tsx
+++ b/apps/client/src/widgets/type_widgets/file/Video.spec.tsx
@@ -294,6 +294,18 @@ describe("VideoPreview", () => {
expect(document.exitFullscreen).toHaveBeenCalled();
});
+ it("leaves the button alone while something else has the screen", async () => {
+ renderPlayer();
+
+ // A map or another player in the split beside this one, which this button has no say
+ // over: it went on offering fullscreen, not the way out of someone else's.
+ setFullscreenElement(document.createElement("div"));
+ await act(async () => { document.dispatchEvent(new Event("fullscreenchange")); });
+
+ expect(container.querySelector(".bx-fullscreen")).not.toBeNull();
+ expect(container.querySelector(".bx-exit-fullscreen")).toBeNull();
+ });
+
it("offers picture-in-picture only where the browser supports it", async () => {
renderPlayer();
// happy-dom has no PiP, matching Firefox: the button is left out entirely.
diff --git a/apps/client/src/widgets/type_widgets/file/Video.tsx b/apps/client/src/widgets/type_widgets/file/Video.tsx
index c61a6ca340..2300fa129a 100644
--- a/apps/client/src/widgets/type_widgets/file/Video.tsx
+++ b/apps/client/src/widgets/type_widgets/file/Video.tsx
@@ -6,6 +6,7 @@ import { MutableRef, useCallback, useEffect, useRef, useState } from "preact/hoo
import { t } from "../../../services/i18n";
import { isMobile } from "../../../services/utils";
import ActionButton from "../../react/ActionButton";
+import { useFullscreen } from "../../react/hooks";
import NoItems from "../../react/NoItems";
import ShortcutHintButton from "../../shortcut_hints/shortcut_hint_button";
import MediaFileActions from "./MediaFileActions";
@@ -336,30 +337,20 @@ function PictureInPictureButton({ videoRef }: { videoRef: RefObject<HTMLVideoEle
}
function FullscreenButton({ targetRef }: { targetRef: RefObject<HTMLElement> }) {
- const [isFullscreen, setIsFullscreen] = useState(false);
-
+ // `useFullscreen` follows an element rather than a ref, a ref being filled after the render that
+ // asked for it and causing none of its own — so the wrapper is put into state once it is there.
+ const [ target, setTarget ] = useState<HTMLElement | null>(null);
useEffect(() => {
- const onFullscreenChange = () => setIsFullscreen(!!document.fullscreenElement);
- document.addEventListener("fullscreenchange", onFullscreenChange);
- return () => document.removeEventListener("fullscreenchange", onFullscreenChange);
- }, []);
-
- const toggleFullscreen = () => {
- const target = targetRef.current;
- if (!target) return;
+ setTarget(targetRef.current);
+ }, [ targetRef ]);
- if (document.fullscreenElement) {
- document.exitFullscreen();
- } else {
- target.requestFullscreen();
- }
- };
+ const [ isFullscreen, toggleFullscreen ] = useFullscreen(target);
return (
<ActionButton
icon={isFullscreen ? "bx bx-exit-fullscreen" : "bx bx-fullscreen"}
- text={isFullscreen ? t("media.exit-fullscreen") : t("media.fullscreen")}
- onClick={toggleFullscreen}
+ text={isFullscreen ? t("common.exit_fullscreen") : t("common.fullscreen")}
+ onClick={() => void toggleFullscreen()}
/>
);
}