mirror of
https://github.com/rommapp/romm.git
synced 2026-12-04 12:52:19 +01:00
fix(v2): mount one PDF viewer at a time in the Media tab
vue3-pdf-app wraps a single global pdf.js application and looks up its containers by fixed element ids. The Manual and Walkthrough subtabs stay mounted via v-show, so with a PDF in each, both PdfViewers ran against the same app: the walkthrough rendered into the manual's container (or not at all), and pages laid out while hidden came up blank. Only the last shown PDF subtab now mounts a PdfViewer, and only once it has been shown. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
<script setup lang="ts">
|
||||
// ManualSubtab — the Media tab's Manual panel. Surfaces the scraped primary
|
||||
// ManualSubtab: the Media tab's Manual panel. Surfaces the scraped primary
|
||||
// manual plus any manual-category files sitting in the ROM folder, picking the
|
||||
// viewer (PDF or Markdown) by extension. An entry selector appears when more
|
||||
// than one manual exists. The panel doubles as a drag-and-drop upload target.
|
||||
@@ -36,6 +36,9 @@ const props = defineProps<{
|
||||
/** Drop the header Upload button when the parent renders it elsewhere
|
||||
* (through the exposed `openUpload`). */
|
||||
hideUpload?: boolean;
|
||||
/** Whether this panel may mount the PDF viewer (one per page, see
|
||||
* MediaTab). */
|
||||
pdfActive?: boolean;
|
||||
}>();
|
||||
const emitter = inject<Emitter<Events>>("emitter");
|
||||
const snackbar = useSnackbar();
|
||||
@@ -269,7 +272,7 @@ function requestDeleteManual() {
|
||||
@delete="requestDeleteManual"
|
||||
/>
|
||||
<PdfViewer
|
||||
v-else
|
||||
v-else-if="pdfActive"
|
||||
:key="selectedManual.url"
|
||||
:pdf-url="selectedManual.url"
|
||||
:rom-id="rom.id"
|
||||
@@ -297,7 +300,7 @@ function requestDeleteManual() {
|
||||
scrollbar-color: var(--r-color-border-strong) transparent;
|
||||
}
|
||||
|
||||
/* Header — hosts the manual entry selector (when more than one manual) and
|
||||
/* Header: hosts the manual entry selector (when more than one manual) and
|
||||
the Upload button, pushed to the right. */
|
||||
.r-v2-manual__head {
|
||||
display: flex;
|
||||
@@ -306,7 +309,7 @@ function requestDeleteManual() {
|
||||
flex-shrink: 0;
|
||||
}
|
||||
|
||||
/* Manual entry selector — capped width so it doesn't stretch to fill the
|
||||
/* Manual entry selector: capped width so it doesn't stretch to fill the
|
||||
row. */
|
||||
.r-v2-manual__select {
|
||||
max-width: 360px;
|
||||
@@ -338,7 +341,7 @@ html[data-bp~="sm-and-down"] .r-v2-manual__fill {
|
||||
min-height: 20rem;
|
||||
}
|
||||
|
||||
/* Viewer — fills the available panel height so the inner PDF / Markdown uses
|
||||
/* Viewer: fills the available panel height so the inner PDF / Markdown uses
|
||||
100% and only its own scroll triggers. */
|
||||
.r-v2-manual__viewer {
|
||||
flex: 1;
|
||||
|
||||
119
frontend/src/v2/components/GameDetails/MediaTab.test.ts
Normal file
119
frontend/src/v2/components/GameDetails/MediaTab.test.ts
Normal file
@@ -0,0 +1,119 @@
|
||||
import { flushPromises, mount } from "@vue/test-utils";
|
||||
import { createPinia, setActivePinia } from "pinia";
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { defineComponent, nextTick, ref } from "vue";
|
||||
import type { DetailedRom } from "@/stores/roms";
|
||||
import MediaTab from "./MediaTab.vue";
|
||||
|
||||
const query = ref<Record<string, string>>({});
|
||||
|
||||
vi.mock("vue-i18n", () => ({
|
||||
useI18n: () => ({ t: (key: string) => key }),
|
||||
}));
|
||||
vi.mock("vue-router", async (importOriginal) => ({
|
||||
...(await importOriginal<typeof import("vue-router")>()),
|
||||
useRoute: () => ({
|
||||
get query() {
|
||||
return query.value;
|
||||
},
|
||||
}),
|
||||
useRouter: () => ({
|
||||
replace: vi.fn(async ({ query: next }) => {
|
||||
query.value = next;
|
||||
}),
|
||||
push: vi.fn(),
|
||||
}),
|
||||
}));
|
||||
vi.mock("@/v2/composables/useRomSync", () => ({
|
||||
useRomSync: () => ({ refetchRom: vi.fn() }),
|
||||
}));
|
||||
vi.mock("@/v2/composables/useRomSoundtrack", () => ({
|
||||
useRomSoundtrack: () => ({
|
||||
tracks: ref([]),
|
||||
loading: ref(false),
|
||||
fallbackArtUrl: ref(null),
|
||||
}),
|
||||
}));
|
||||
|
||||
function panelStub(name: string) {
|
||||
return {
|
||||
__esModule: true,
|
||||
default: defineComponent({
|
||||
name,
|
||||
props: { pdfActive: Boolean },
|
||||
setup: () => ({ canUpload: false, openUpload: () => {} }),
|
||||
template: `<div />`,
|
||||
}),
|
||||
};
|
||||
}
|
||||
vi.mock("@/v2/components/GameDetails/ManualSubtab.vue", () =>
|
||||
panelStub("ManualSubtab"),
|
||||
);
|
||||
vi.mock("@/v2/components/GameDetails/WalkthroughSubtab.vue", () =>
|
||||
panelStub("WalkthroughSubtab"),
|
||||
);
|
||||
vi.mock("@/v2/components/GameDetails/ScreenshotsSubtab.vue", () =>
|
||||
panelStub("ScreenshotsSubtab"),
|
||||
);
|
||||
vi.mock("@/v2/components/GameDetails/ArtworkSubtab.vue", () =>
|
||||
panelStub("ArtworkSubtab"),
|
||||
);
|
||||
vi.mock("@/v2/components/Soundtrack/Panel.vue", () =>
|
||||
panelStub("SoundtrackPanel"),
|
||||
);
|
||||
|
||||
async function mountTab(subtab?: string) {
|
||||
query.value = subtab ? { tab: "media", subtab } : { tab: "media" };
|
||||
const wrapper = mount(MediaTab, {
|
||||
props: {
|
||||
rom: {
|
||||
id: 1,
|
||||
files: [],
|
||||
has_soundtrack: false,
|
||||
} as unknown as DetailedRom,
|
||||
},
|
||||
global: { stubs: { SubtabNav: true, RDropzone: true, REmptyState: true } },
|
||||
});
|
||||
await flushPromises();
|
||||
return wrapper;
|
||||
}
|
||||
|
||||
function pdfActive(wrapper: Awaited<ReturnType<typeof mountTab>>) {
|
||||
return {
|
||||
manual: wrapper.findComponent({ name: "ManualSubtab" }).props("pdfActive"),
|
||||
walkthrough: wrapper
|
||||
.findComponent({ name: "WalkthroughSubtab" })
|
||||
.props("pdfActive"),
|
||||
};
|
||||
}
|
||||
|
||||
async function selectSubtab(subtab: string) {
|
||||
query.value = { tab: "media", subtab };
|
||||
await nextTick();
|
||||
await flushPromises();
|
||||
}
|
||||
|
||||
describe("MediaTab PDF viewer ownership", () => {
|
||||
beforeEach(() => {
|
||||
setActivePinia(createPinia());
|
||||
});
|
||||
|
||||
it("lets only the visited PDF subtab mount a viewer", async () => {
|
||||
const wrapper = await mountTab();
|
||||
expect(pdfActive(wrapper)).toEqual({ manual: true, walkthrough: false });
|
||||
|
||||
await selectSubtab("walkthrough");
|
||||
expect(pdfActive(wrapper)).toEqual({ manual: false, walkthrough: true });
|
||||
});
|
||||
|
||||
it("keeps the last PDF subtab's viewer across non-PDF subtabs", async () => {
|
||||
const wrapper = await mountTab("walkthrough");
|
||||
await selectSubtab("screenshots");
|
||||
expect(pdfActive(wrapper)).toEqual({ manual: false, walkthrough: true });
|
||||
});
|
||||
|
||||
it("mounts no viewer until a PDF subtab is shown", async () => {
|
||||
const wrapper = await mountTab("artwork");
|
||||
expect(pdfActive(wrapper)).toEqual({ manual: false, walkthrough: false });
|
||||
});
|
||||
});
|
||||
@@ -12,7 +12,7 @@
|
||||
//
|
||||
// The soundtrack player is reused from v1 for now.
|
||||
import { RBtn, RDropzone, REmptyState } from "@v2/lib";
|
||||
import { computed, defineAsyncComponent, ref } from "vue";
|
||||
import { computed, defineAsyncComponent, ref, watch } from "vue";
|
||||
import { useI18n } from "vue-i18n";
|
||||
import type { DetailedRom } from "@/stores/roms";
|
||||
import SubtabNav, {
|
||||
@@ -79,6 +79,17 @@ const subTab = useSubtabQuery<Subtab>(
|
||||
"manual",
|
||||
);
|
||||
|
||||
// vue3-pdf-app drives one global pdf.js app, so only the last shown PDF subtab
|
||||
// mounts a PdfViewer (and only once visible, as pdf.js sizes pages to it).
|
||||
const pdfSubtab = ref<"manual" | "walkthrough" | null>(null);
|
||||
watch(
|
||||
subTab,
|
||||
(value) => {
|
||||
if (value === "manual" || value === "walkthrough") pdfSubtab.value = value;
|
||||
},
|
||||
{ immediate: true },
|
||||
);
|
||||
|
||||
// ---------- Subtab nav ----------
|
||||
const subtabDefs = computed<SubtabNavItem<Subtab>[]>(() => [
|
||||
{
|
||||
@@ -188,14 +199,19 @@ async function deleteSoundtrack(fileId: number) {
|
||||
<div class="r-v2-media__content">
|
||||
<!-- All subtab sections stay mounted (v-show, not v-if). The manual,
|
||||
soundtrack and screenshots panels are heavy defineAsyncComponent
|
||||
loads — un/remounting them on every subtab switch causes a visible
|
||||
loads; un/remounting them on every subtab switch causes a visible
|
||||
main-thread freeze (the PDF parser is the worst offender). With
|
||||
v-show the cost is paid once on Media tab entry and switching is a
|
||||
CSS toggle. -->
|
||||
<!-- Manual subtab — its own component (PDF / Markdown viewer with an
|
||||
CSS toggle (bar the PDF viewer, see `pdfSubtab`). -->
|
||||
<!-- Manual subtab: its own component (PDF / Markdown viewer with an
|
||||
entry selector; scrolls independently). -->
|
||||
<section v-show="subTab === 'manual'" class="r-v2-media__panel">
|
||||
<ManualSubtab ref="manualPanel" :rom="rom" :hide-upload="smAndDown" />
|
||||
<ManualSubtab
|
||||
ref="manualPanel"
|
||||
:rom="rom"
|
||||
:hide-upload="smAndDown"
|
||||
:pdf-active="pdfSubtab === 'manual'"
|
||||
/>
|
||||
</section>
|
||||
|
||||
<!-- Walkthrough subtab: uploaded or GameFAQs-fetched documents, with
|
||||
@@ -205,16 +221,17 @@ async function deleteSoundtrack(fileId: number) {
|
||||
ref="walkthroughPanel"
|
||||
:rom="rom"
|
||||
:hide-upload="smAndDown"
|
||||
:pdf-active="pdfSubtab === 'walkthrough'"
|
||||
/>
|
||||
</section>
|
||||
|
||||
<!-- Screenshots subtab — its own component (ROM / Mine / Community
|
||||
<!-- Screenshots subtab: its own component (ROM / Mine / Community
|
||||
sections, per-user public/private). -->
|
||||
<section v-show="subTab === 'screenshots'" class="r-v2-media__panel">
|
||||
<ScreenshotsSubtab :rom="rom" />
|
||||
</section>
|
||||
|
||||
<!-- Artwork subtab — read-only gallery of scraped art assets
|
||||
<!-- Artwork subtab: read-only gallery of scraped art assets
|
||||
(bezel / logo / marquee / box art / fan art / videos). -->
|
||||
<section v-show="subTab === 'artwork'" class="r-v2-media__panel">
|
||||
<ArtworkSubtab :rom="rom" />
|
||||
@@ -288,7 +305,7 @@ async function deleteSoundtrack(fileId: number) {
|
||||
gap: 24px;
|
||||
/* Fills the parent tab panel exactly so the PDF viewer (inside the
|
||||
manual section) can size to 100% without forcing the outer panel
|
||||
to scroll — the PDF has its own internal scroll. */
|
||||
to scroll; the PDF has its own internal scroll. */
|
||||
height: 100%;
|
||||
min-height: 0;
|
||||
}
|
||||
@@ -308,7 +325,7 @@ async function deleteSoundtrack(fileId: number) {
|
||||
min-height: 0;
|
||||
}
|
||||
|
||||
/* Panels — each subtab section fills the content height so its
|
||||
/* Panels: each subtab section fills the content height so its
|
||||
children (manual / soundtrack / screenshots) can stretch to 100%
|
||||
without forcing an outer scrollbar. */
|
||||
.r-v2-media__panel {
|
||||
@@ -357,7 +374,7 @@ html[data-bp~="sm-and-down"] .r-v2-media {
|
||||
gap: 14px;
|
||||
}
|
||||
|
||||
/* Soundtrack — the v1 player has its own internal styling; wrap in an
|
||||
/* Soundtrack: the v1 player has its own internal styling; wrap in an
|
||||
elevated container so it blends with v2 tokens. */
|
||||
.r-v2-media__soundtrack {
|
||||
border: 1px solid var(--r-color-border);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
<script setup lang="ts">
|
||||
// WalkthroughSubtab — the Media tab's Walkthrough panel. Surfaces
|
||||
// WalkthroughSubtab: the Media tab's Walkthrough panel. Surfaces
|
||||
// walkthrough-category files (uploaded, or fetched from a GameFAQs guide URL),
|
||||
// picking the viewer (PDF / Markdown / Text) by extension. Mirrors
|
||||
// ManualSubtab's chrome, plus an "add from GameFAQs URL" affordance.
|
||||
@@ -52,6 +52,9 @@ const props = defineProps<{
|
||||
/** Drop the Upload button when the parent renders it elsewhere (through
|
||||
* the exposed `openUpload`). */
|
||||
hideUpload?: boolean;
|
||||
/** Whether this panel may mount the PDF viewer (one per page, see
|
||||
* MediaTab). */
|
||||
pdfActive?: boolean;
|
||||
}>();
|
||||
const snackbar = useSnackbar();
|
||||
const confirm = useConfirm();
|
||||
@@ -273,7 +276,7 @@ async function requestDelete() {
|
||||
@delete="requestDelete"
|
||||
/>
|
||||
<PdfViewer
|
||||
v-else
|
||||
v-else-if="pdfActive"
|
||||
:key="selected.url"
|
||||
:pdf-url="selected.url"
|
||||
:rom-id="rom.id"
|
||||
|
||||
Reference in New Issue
Block a user