diff --git a/frontend/src/components/GameDetails/ArtworkSubtab.vue b/frontend/src/components/GameDetails/ArtworkSubtab.vue index 3c49dfd59..947fa193e 100644 --- a/frontend/src/components/GameDetails/ArtworkSubtab.vue +++ b/frontend/src/components/GameDetails/ArtworkSubtab.vue @@ -9,7 +9,7 @@ import { REmptyState } from "@/lib"; import type { DetailedRom } from "@/stores/roms"; import { resolveRomArtwork } from "@/utils/romArtwork"; -const props = defineProps<{ rom: DetailedRom }>(); +const props = defineProps<{ rom: DetailedRom; suspended?: boolean }>(); const { t } = useI18n(); const { isPinned, togglePin } = usePinnedMedia(() => props.rom); @@ -29,6 +29,7 @@ const artwork = computed(() => resolveRomArtwork(props.rom)); v-else :items="artwork" captions + :suspended="suspended" :is-pinned="isPinned" @toggle-pin="togglePin" /> diff --git a/frontend/src/components/GameDetails/MediaShelf.test.ts b/frontend/src/components/GameDetails/MediaShelf.test.ts index d5a5d0252..40fbf8d8a 100644 --- a/frontend/src/components/GameDetails/MediaShelf.test.ts +++ b/frontend/src/components/GameDetails/MediaShelf.test.ts @@ -34,6 +34,24 @@ function stubLoad() { } describe("MediaShelf", () => { + it("renders a YouTube item as an embed, kept out of the lightbox", () => { + const wrapper = mount(MediaShelf, { + props: { + items: [{ key: "yt", label: "YouTube", url: "", youtubeId: "abc123" }], + suspended: true, + }, + global: { stubs: { RCarousel: true, RIcon: true, YoutubeEmbed: true } }, + }); + const embed = wrapper.getComponent({ name: "YoutubeEmbed" }); + + expect(embed.props()).toMatchObject({ + videoId: "abc123", + title: "YouTube", + suspended: true, + }); + expect(wrapper.find("button.r-media-shelf__btn").exists()).toBe(false); + }); + it("does not start loading a clip until it is played", () => { const wrapper = mountShelf([clip]); diff --git a/frontend/src/components/GameDetails/MediaShelf.vue b/frontend/src/components/GameDetails/MediaShelf.vue index f67e02fbc..58e2563f9 100644 --- a/frontend/src/components/GameDetails/MediaShelf.vue +++ b/frontend/src/components/GameDetails/MediaShelf.vue @@ -4,6 +4,7 @@ import { computed, reactive, ref, type VNode } from "vue"; import { useI18n } from "vue-i18n"; import MediaPinButton from "@/components/GameDetails/MediaPinButton.vue"; +import YoutubeEmbed from "@/components/GameDetails/YoutubeEmbed.vue"; import { RCarousel, RIcon } from "@/lib"; import { claimMedia, releaseMedia } from "@/utils/releaseMedia"; @@ -14,6 +15,8 @@ export type MediaShelfItem = { isVideo?: boolean; /** Still shown in a clip's box until it plays. */ poster?: string; + /** Renders a YouTube embed instead of an image or clip; `url` is unused. */ + youtubeId?: string; }; defineOptions({ inheritAttrs: false }); @@ -23,12 +26,16 @@ const props = defineProps<{ captions?: boolean; compact?: boolean; isPinned?: ((key: string) => boolean) | undefined; + /** The shelf sits in a hidden panel, so embeds pause. */ + suspended?: boolean; }>(); const emit = defineEmits<{ "toggle-pin": [key: string] }>(); const { t } = useI18n(); -const images = computed(() => props.items.filter((item) => !item.isVideo)); +const images = computed(() => + props.items.filter((item) => !item.isVideo && !item.youtubeId), +); // Lightbox indexes into the image-only list, so map a clicked card to its // position there (videos are skipped). @@ -83,10 +90,16 @@ function onClipPlay(event: Event, key: string) { >
+
- + {{ item.label }}
diff --git a/frontend/src/components/GameDetails/MediaTab.vue b/frontend/src/components/GameDetails/MediaTab.vue index 8533b813e..eaf943069 100644 --- a/frontend/src/components/GameDetails/MediaTab.vue +++ b/frontend/src/components/GameDetails/MediaTab.vue @@ -265,7 +265,7 @@ async function deleteSoundtrack(fileId: number) { class="r-media__panel" v-bind="panelAttrs('artwork')" > - + diff --git a/frontend/src/components/GameDetails/YoutubeEmbed.test.ts b/frontend/src/components/GameDetails/YoutubeEmbed.test.ts new file mode 100644 index 000000000..4a94bb187 --- /dev/null +++ b/frontend/src/components/GameDetails/YoutubeEmbed.test.ts @@ -0,0 +1,154 @@ +import { mount, type VueWrapper } from "@vue/test-utils"; +import { createPinia, setActivePinia } from "pinia"; +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; +import { nextTick } from "vue"; +import YoutubeEmbed from "@/components/GameDetails/YoutubeEmbed.vue"; +import storeHeartbeat from "@/stores/heartbeat"; +import { claimMedia, releaseMedia } from "@/utils/releaseMedia"; + +vi.mock("vue-i18n"); + +type Player = { postMessage: ReturnType }; +type EmbedWrapper = VueWrapper & { player: Player }; + +const mounted: VueWrapper[] = []; + +// The test DOM loads no iframe pages, so each frame gets a stand-in window +// that records what the page posts to the player. +function mountEmbed(props: { suspended?: boolean } = {}): EmbedWrapper { + const wrapper = mount(YoutubeEmbed, { + props: { videoId: "abc123", title: "YouTube", ...props }, + attachTo: document.body, + }); + const player: Player = { postMessage: vi.fn() }; + Object.defineProperty(wrapper.get("iframe").element, "contentWindow", { + value: player, + }); + mounted.push(wrapper); + return Object.assign(wrapper, { player }); +} + +// Posts what the player sends after the "listening" handshake. +function playerSays( + wrapper: EmbedWrapper, + data: unknown, + origin = "https://www.youtube.com", +) { + window.dispatchEvent( + new MessageEvent("message", { + data: JSON.stringify(data), + origin, + source: wrapper.player as unknown as Window, + }), + ); +} + +function sentCommands(wrapper: EmbedWrapper) { + return wrapper.player.postMessage.mock.calls + .map( + ([msg]) => JSON.parse(msg as string) as { event: string; func?: string }, + ) + .filter((msg) => msg.event === "command") + .map((msg) => msg.func); +} + +beforeEach(() => setActivePinia(createPinia())); +afterEach(() => mounted.splice(0).forEach((w) => w.unmount())); + +describe("YoutubeEmbed", () => { + it("loads the player lazily through the base URL with the JS API on", () => { + const heartbeat = storeHeartbeat(); + heartbeat.value = { + ...heartbeat.value, + FRONTEND: { + ...heartbeat.value.FRONTEND, + YOUTUBE_BASE_URL: "https://yt.example", + }, + }; + const frame = mountEmbed().get("iframe"); + const src = new URL(frame.attributes("src")!); + + expect(`${src.origin}${src.pathname}`).toBe( + "https://yt.example/embed/abc123", + ); + expect(src.searchParams.get("enablejsapi")).toBe("1"); + expect(frame.attributes("loading")).toBe("lazy"); + expect(frame.attributes("credentialless")).toBeDefined(); + }); + + it("tells the player it is listening once the frame loads", async () => { + const wrapper = mountEmbed(); + + await wrapper.get("iframe").trigger("load"); + + expect( + JSON.parse(wrapper.player.postMessage.mock.calls[0]![0] as string), + ).toMatchObject({ + event: "listening", + }); + expect(wrapper.player.postMessage.mock.calls[0]![1]).toBe( + "https://www.youtube.com", + ); + }); + + it("toggles playback from the host button", async () => { + const wrapper = mountEmbed(); + + await wrapper.get("button").trigger("click"); + playerSays(wrapper, { event: "onStateChange", info: 1 }); + await nextTick(); + await wrapper.get("button").trigger("click"); + + expect(sentCommands(wrapper)).toEqual(["playVideo", "pauseVideo"]); + }); + + it("ignores state messages from another origin", async () => { + const wrapper = mountEmbed(); + + playerSays( + wrapper, + { event: "onStateChange", info: 1 }, + "https://evil.example", + ); + await nextTick(); + await wrapper.get("button").trigger("click"); + + expect(sentCommands(wrapper)).toEqual(["playVideo"]); + }); + + it("pauses when a local clip starts playing", () => { + const wrapper = mountEmbed(); + playerSays(wrapper, { + event: "infoDelivery", + info: { playerState: 1 }, + }); + + const clip = document.createElement("video"); + vi.spyOn(clip, "load").mockImplementation(() => undefined); + claimMedia(clip, vi.fn()); + + expect(sentCommands(wrapper)).toEqual(["pauseVideo"]); + releaseMedia(clip); + }); + + it("releases a local clip when it starts playing", () => { + const clip = document.createElement("video"); + clip.setAttribute("src", "/clip.mp4"); + vi.spyOn(clip, "load").mockImplementation(() => undefined); + const onRelease = vi.fn(); + claimMedia(clip, onRelease); + + playerSays(mountEmbed(), { event: "onStateChange", info: 1 }); + + expect(clip.hasAttribute("src")).toBe(false); + expect(onRelease).toHaveBeenCalledTimes(1); + }); + + it("pauses when its panel is hidden", async () => { + const wrapper = mountEmbed(); + + await wrapper.setProps({ suspended: true }); + + expect(sentCommands(wrapper)).toEqual(["pauseVideo"]); + }); +}); diff --git a/frontend/src/components/GameDetails/YoutubeEmbed.vue b/frontend/src/components/GameDetails/YoutubeEmbed.vue new file mode 100644 index 000000000..dc72c1dcc --- /dev/null +++ b/frontend/src/components/GameDetails/YoutubeEmbed.vue @@ -0,0 +1,170 @@ + + +