Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions REUSE.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ path = [
"vitest.config.ts",
"examples/**",
"tests/**",
"e2e/fixtures/**",
"dist/**",
"img/**",
]
Expand Down
3 changes: 3 additions & 0 deletions docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Binary file added e2e/fixtures/mixed-pages.pdf
Binary file not shown.
154 changes: 154 additions & 0 deletions e2e/page-navigation.spec.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
20 changes: 20 additions & 0 deletions examples/basic/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ SPDX-License-Identifier: AGPL-3.0-or-later

<DocumentsList :documents="fileNames" @remove="removeDocument" />

<p v-if="currentPage" class="page-status" role="status" data-testid="current-page">
Document {{ currentPage.docIndex + 1 }} · Page {{ currentPage.pageIndex + 1 }}
</p>

<section class="viewer">
<PDFElements
ref="pdf"
Expand All @@ -28,6 +32,7 @@ SPDX-License-Identifier: AGPL-3.0-or-later
:init-file-names="fileNames"
:initial-scale="1.1"
@pdf-elements:end-init="onReady"
@pdf-elements:page-change="currentPage = $event"
>
<template #element-signature="{ object }">
<SignatureBox
Expand Down Expand Up @@ -91,6 +96,7 @@ export default defineComponent({
pdfReady: false,
showElementsViewer: false,
isAddingElement: false,
currentPage: null as { docIndex: number; pageIndex: number } | null,
}
},
computed: {
Expand Down Expand Up @@ -149,6 +155,7 @@ export default defineComponent({
},

forceRenderDocuments() {
this.currentPage = null
this.componentKey += 1
},

Expand Down Expand Up @@ -214,6 +221,19 @@ export default defineComponent({
flex-direction: column;
}

/* Announces the current page to assistive tech without changing the layout. */
.page-status {
position: absolute;
width: 1px;
height: 1px;
margin: -1px;
padding: 0;
overflow: hidden;
clip: rect(0, 0, 0, 0);
white-space: nowrap;
border: 0;
}

.viewer {
flex: 1;
padding: 20px;
Expand Down
Loading