diff --git a/REUSE.toml b/REUSE.toml index 988e4e3..83dc43f 100644 --- a/REUSE.toml +++ b/REUSE.toml @@ -21,6 +21,7 @@ path = [ "vitest.config.ts", "examples/**", "tests/**", + "e2e/fixtures/**", "dist/**", "img/**", ] diff --git a/docs/API.md b/docs/API.md index 6e3fe7e..d0a5249 100644 --- a/docs/API.md +++ b/docs/API.md @@ -44,10 +44,13 @@ For the logical field / visual widget data model (plain serializable types, rela - `pdf-elements:end-init` - Emitted when PDF is loaded. - `pdf-elements:adding-ended` - Emitted when interactive placement ends. Payload: `{ reason: 'placed', object, docIndex, pageIndex }` on success or `{ reason: 'cancelled' }` when placement is cancelled. +- `pdf-elements:page-change` - Emitted when the current page changes: on scroll, zoom, container resize, once page layout is known after load, and on `goToPage`. The current page is the one with the highest visible percentage (horizontal clipping included); it is kept while it stays fully visible. Payload: `{ docIndex, pageIndex }` (zero-based). ## Exposed methods - `startAddingElement(templateObject)` - Starts interactive placement mode. +- `goToPage(pageIndex, docIndex = 0)` - Scrolls the page to the top of the viewer (the viewer itself, its nearest scrollable ancestor, or the window) and makes it the current page. Zero-based indices. Returns `false` only for pages that do not exist. When the page layout is not known yet (right after load) the request is kept and runs as soon as it is, the latest request winning; page dimensions come from the PDF.js viewport, so it does not wait for canvas rendering. +- `getCurrentPage()` - Returns `{ docIndex, pageIndex }` for the current page (see `pdf-elements:page-change`), or `null` while no page layout is known yet. - `cancelAdding()` - Cancels the current placement session and emits `pdf-elements:adding-ended` with `{ reason: 'cancelled' }` when a session was active. ## Slots diff --git a/e2e/fixtures/mixed-pages.pdf b/e2e/fixtures/mixed-pages.pdf new file mode 100644 index 0000000..031ee31 Binary files /dev/null and b/e2e/fixtures/mixed-pages.pdf differ diff --git a/e2e/page-navigation.spec.ts b/e2e/page-navigation.spec.ts new file mode 100644 index 0000000..226e9a2 --- /dev/null +++ b/e2e/page-navigation.spec.ts @@ -0,0 +1,154 @@ +// SPDX-FileCopyrightText: 2026 LibreCode coop and contributors +// SPDX-License-Identifier: AGPL-3.0-or-later + +import path from "node:path"; +import { test, expect, type Page } from "@playwright/test"; + +// 5 pages with different sizes and orientations (portrait, landscape, small, portrait, wide). +const fixture = path.resolve("e2e/fixtures/mixed-pages.pdf"); +const SAMPLE_URL = "https://mozilla.github.io/pdf.js/web/compressed.tracemonkey-pldi-09.pdf"; + +async function loadFixture(page: Page) { + await page.route(SAMPLE_URL, (route) => route.fulfill({ + path: fixture, + contentType: "application/pdf", + headers: { "access-control-allow-origin": "*" }, + })); + await page.goto("/"); + await page.getByRole("button", { name: "Load sample PDF" }).click(); + await expect(page.locator(".page-wrapper")).toHaveCount(5); + await expect(page.getByTestId("current-page")).toHaveText("Document 1 · Page 1"); +} + +/** Public API of the mounted viewer, as a host would call it. */ +const goToPage = (page: Page, pageIndex: number, docIndex = 0) => page.evaluate( + ([p, d]) => { + const root = document.querySelector(".pdf-elements-root") as any; + return root.__vueParentComponent.proxy.goToPage(p, d) as boolean; + }, + [pageIndex, docIndex], +); + +/** + * Geometry of a page relative to what the user can see. The demo viewer grows with + * its content, so the window scrolls; a host with a fixed height scrolls the viewer. + */ +const pageInViewport = (page: Page, wrapperIndex: number) => page.evaluate((i) => { + const root = document.querySelector(".pdf-elements-root") as HTMLElement; + const scrolls = root.scrollHeight > root.clientHeight + 1; + const top = scrolls ? root.getBoundingClientRect().top : 0; + const height = scrolls ? root.clientHeight : window.innerHeight; + const rect = (document.querySelectorAll(".page-wrapper")[i] as HTMLElement).getBoundingClientRect(); + return { top: rect.top - top, bottom: rect.bottom - top, viewHeight: height }; +}, wrapperIndex); + +const offsetFromTop = async (page: Page, wrapperIndex: number) => (await pageInViewport(page, wrapperIndex)).top; + +const scrollToStart = (page: Page) => page.evaluate(() => { + const root = document.querySelector(".pdf-elements-root") as HTMLElement; + root.scrollTop = 0; + window.scrollTo(0, 0); +}); + +const currentPage = (page: Page) => page.getByTestId("current-page"); + +test.describe("page navigation", () => { + test("goToPage scrolls pages of different sizes and orientations to the top", async ({ page }) => { + await loadFixture(page); + + for (const [pageIndex, label] of [[1, "Page 2"], [2, "Page 3"], [3, "Page 4"]] as const) { + expect(await goToPage(page, pageIndex)).toBe(true); + await expect(currentPage(page)).toHaveText(`Document 1 · ${label}`); + expect(Math.abs(await offsetFromTop(page, pageIndex))).toBeLessThan(2); + } + + expect(await goToPage(page, 0)).toBe(true); + await expect(currentPage(page)).toHaveText("Document 1 · Page 1"); + }); + + test("the last page becomes current even when it cannot reach the top", async ({ page }) => { + await loadFixture(page); + expect(await goToPage(page, 4)).toBe(true); + await expect(currentPage(page)).toHaveText("Document 1 · Page 5"); + }); + + test("rejects pages that do not exist", async ({ page }) => { + await loadFixture(page); + expect(await goToPage(page, 5)).toBe(false); + expect(await goToPage(page, -1)).toBe(false); + expect(await goToPage(page, 0, 1)).toBe(false); + await expect(currentPage(page)).toHaveText("Document 1 · Page 1"); + }); + + test("follows scrolling and keeps the navigated page current after a resize", async ({ page }) => { + await loadFixture(page); + expect(await goToPage(page, 2)).toBe(true); + await expect(currentPage(page)).toHaveText("Document 1 · Page 3"); + + await page.setViewportSize({ width: 700, height: 500 }); + await expect(currentPage(page)).toHaveText(/Document 1 · Page \d/); + const shown = Number((await currentPage(page).innerText()).match(/Page (\d)/)![1]) - 1; + const rect = await pageInViewport(page, shown); + // whatever page is reported must really be on screen + expect(rect.bottom).toBeGreaterThan(0); + expect(rect.top).toBeLessThan(rect.viewHeight); + + await scrollToStart(page); + await expect(currentPage(page)).toHaveText("Document 1 · Page 1"); + }); + + test("stays consistent after zooming", async ({ page }) => { + await loadFixture(page); + expect(await goToPage(page, 1)).toBe(true); + await expect(currentPage(page)).toHaveText("Document 1 · Page 2"); + + await page.evaluate(() => { + const root = document.querySelector(".pdf-elements-root") as any; + root.__vueParentComponent.proxy.commitZoom(0.5); + }); + // geometry is re-reserved from the PDF.js viewport: the page is still reachable at the top + expect(await goToPage(page, 3)).toBe(true); + await expect(currentPage(page)).toHaveText("Document 1 · Page 4"); + expect(Math.abs(await offsetFromTop(page, 3))).toBeLessThan(2); + }); + + test("navigates to the right page when zoom and goToPage run in the same tick", async ({ page }) => { + await loadFixture(page); + const accepted = await page.evaluate(() => { + const proxy = (document.querySelector(".pdf-elements-root") as any).__vueParentComponent.proxy; + proxy.commitZoom(0.5); + return proxy.goToPage(2) as boolean; + }); + expect(accepted).toBe(true); + + // The request waits for the pages to re-reserve their size, then lands on the new layout. + await expect(currentPage(page)).toHaveText("Document 1 · Page 3"); + await expect.poll(async () => Math.abs(await offsetFromTop(page, 2))).toBeLessThan(2); + // ...and stays there once the zoomed pages finish rendering. + await page.waitForTimeout(1000); + await expect(currentPage(page)).toHaveText("Document 1 · Page 3"); + }); + + test("applies a navigation requested while the viewer is hidden once it is shown", async ({ page }) => { + await loadFixture(page); + const viewer = page.locator(".viewer"); + await viewer.evaluate((el) => { (el as HTMLElement).style.display = "none"; }); + // e.g. the viewer sits in an inactive tab: the request is accepted but there is no layout yet + expect(await goToPage(page, 2)).toBe(true); + await viewer.evaluate((el) => { (el as HTMLElement).style.display = ""; }); + + await expect(currentPage(page)).toHaveText("Document 1 · Page 3"); + await expect.poll(async () => Math.abs(await offsetFromTop(page, 2))).toBeLessThan(2); + }); + + test("navigates across multiple documents", async ({ page }) => { + await loadFixture(page); + await page.locator('input[type="file"]').setInputFiles(fixture); + await expect(page.locator(".page-wrapper")).toHaveCount(10); + + expect(await goToPage(page, 1, 1)).toBe(true); + await expect(currentPage(page)).toHaveText("Document 2 · Page 2"); + expect(Math.abs(await offsetFromTop(page, 6))).toBeLessThan(2); + expect(await goToPage(page, 0, 2)).toBe(false); + }); +}); diff --git a/examples/basic/App.vue b/examples/basic/App.vue index 047614f..943c1bf 100644 --- a/examples/basic/App.vue +++ b/examples/basic/App.vue @@ -20,6 +20,10 @@ SPDX-License-Identifier: AGPL-3.0-or-later +

+ Document {{ currentPage.docIndex + 1 }} · Page {{ currentPage.pageIndex + 1 }} +

+