From 68ccc0105f0b24bbb51ccbf88c5371244608deb4 Mon Sep 17 00:00:00 2001 From: Luis Amorim Date: Wed, 25 Nov 2026 18:28:55 -0300 Subject: [PATCH 1/3] feat(navigation): expose goToPage, getCurrentPage and page-change event Lets hosts build pagination controls and page thumbnails without relying on internal DOM structure. Signed-off-by: Luis Amorim --- docs/API.md | 3 ++ src/components/PDFElements.vue | 48 ++++++++++++++++++++++++++-- src/index.ts | 1 + src/types.ts | 7 ++++ src/utils/pageBounds.ts | 26 +++++++++++++++ tests/components/PDFElements.spec.ts | 42 ++++++++++++++++++++++++ tests/utils/pageBounds.spec.ts | 18 ++++++++++- 7 files changed, 142 insertions(+), 3 deletions(-) diff --git a/docs/API.md b/docs/API.md index 6e3fe7e..cdcf691 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 page occupying most of the viewport changes (on scroll and after load). 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. Zero-based indices. Returns `false` when the page does not exist or is not rendered. +- `getCurrentPage()` - Returns `{ docIndex, pageIndex }` for the page occupying most of the viewport, or `null` when no page is rendered. - `cancelAdding()` - Cancels the current placement session and emits `pdf-elements:adding-ended` with `{ reason: 'cancelled' }` when a session was active. ## Slots diff --git a/src/components/PDFElements.vue b/src/components/PDFElements.vue index 28e6a2e..d4b7c76 100644 --- a/src/components/PDFElements.vue +++ b/src/components/PDFElements.vue @@ -145,14 +145,14 @@ import PDFPage from './PDFPage.vue' import DraggableElement from './DraggableElement.vue' import { readAsPDF, readAsArrayBuffer } from '../utils/asyncReader' import { clampPosition, getVisibleArea } from '../utils/geometry' -import { getViewportWindow, isPageInViewport } from '../utils/pageBounds' +import { getViewportWindow, isPageInViewport, findMostVisiblePage } from '../utils/pageBounds' import { objectIdExistsInDoc, findObjectPageIndex, updateObjectInDoc, removeObjectFromDoc } from '../utils/objectStore' import { getCachedMeasurement } from '../utils/measurements' import type { PDFDocumentEntry, PDFElementObject, PDFElementsAddingEndedPayload } from '../types' export default defineComponent({ name: 'PDFElements', - emits: ['pdf-elements:end-init', 'pdf-elements:delete-object', 'pdf-elements:object-click', 'pdf-elements:adding-ended'], + emits: ['pdf-elements:end-init', 'pdf-elements:delete-object', 'pdf-elements:object-click', 'pdf-elements:adding-ended', 'pdf-elements:page-change'], components: { PDFPage, DraggableElement, @@ -252,6 +252,7 @@ export default defineComponent({ pendingDragClientPos: null as { x: number; y: number } | null, pageBoundsVersion: 0, lastScrollTop: 0, + currentVisiblePage: null as { docIndex: number; pageIndex: number } | null, lastClientWidth: 0, nextObjectCounter: 0, isDraggingElement: false, @@ -414,6 +415,7 @@ export default defineComponent({ this.selectedPageIndex = 0 this.$emit('pdf-elements:end-init', { docsCount: docs.length }) this.$nextTick(() => { + this.updateCurrentPage() if (this.autoFitZoom) { this.scheduleAutoFitZoom() } @@ -802,10 +804,52 @@ export default defineComponent({ this.scheduleAutoFitZoom() } } + this.updateCurrentPage() this.viewportRafId = 0 }) }, + getPageWrapperElement(docIndex, pageIndex) { + const canvas = this.getPageCanvasElement(docIndex, pageIndex) + return canvas?.closest?.('.page-wrapper') || canvas || null + }, + + getCurrentPage() { + const container = this.$el + if (!container?.getBoundingClientRect) return null + const containerTop = container.getBoundingClientRect().top + const rects = [] + this.pdfDocuments.forEach((doc, docIndex) => { + for (let pageIndex = 0; pageIndex < doc.pages.length; pageIndex++) { + const wrapper = this.getPageWrapperElement(docIndex, pageIndex) + if (!wrapper?.getBoundingClientRect) continue + const rect = wrapper.getBoundingClientRect() + rects.push({ docIndex, pageIndex, top: rect.top - containerTop, bottom: rect.bottom - containerTop }) + } + }) + return findMostVisiblePage(rects, container.clientHeight || 0) + }, + + updateCurrentPage() { + const page = this.getCurrentPage() + if (!page) return + const previous = this.currentVisiblePage + if (previous && previous.docIndex === page.docIndex && previous.pageIndex === page.pageIndex) return + this.currentVisiblePage = page + this.$emit('pdf-elements:page-change', { ...page }) + }, + + goToPage(pageIndex, docIndex = 0) { + const doc = this.pdfDocuments[docIndex] + if (!doc || !Number.isInteger(pageIndex) || pageIndex < 0 || pageIndex >= doc.pages.length) return false + const container = this.$el + const wrapper = this.getPageWrapperElement(docIndex, pageIndex) + if (!container?.getBoundingClientRect || !wrapper?.getBoundingClientRect) return false + const delta = wrapper.getBoundingClientRect().top - container.getBoundingClientRect().top + container.scrollTop = Math.max(0, (container.scrollTop || 0) + delta) + return true + }, + handleMouseMove(event) { if (event?.touches?.length > 1 || this.isPinching) return if (!this.isAddingMode || !this.previewElement) return diff --git a/src/index.ts b/src/index.ts index 33bd198..1ab6f36 100644 --- a/src/index.ts +++ b/src/index.ts @@ -35,6 +35,7 @@ export type { PDFElementsAddingEndedPayload, PDFElementsAddingEndedPlacedPayload, PDFElementsAddingEndedReason, + PDFElementsPageChangePayload, PDFElementsPublicApi, PDFFormField, PDFFormFieldId, diff --git a/src/types.ts b/src/types.ts index 9dbf5fc..c421f4a 100644 --- a/src/types.ts +++ b/src/types.ts @@ -42,6 +42,11 @@ export interface PDFDocumentEntry { allObjects: PDFElementObject[][] } +export interface PDFElementsPageChangePayload { + docIndex: number + pageIndex: number +} + export interface PDFElementsPublicApi { startAddingElement: (templateObject: PDFElementObject) => void addObjectToPage: (object: PDFElementObject, pageIndex?: number, docIndex?: number) => boolean @@ -50,6 +55,8 @@ export interface PDFElementsPublicApi { deleteObject: (docIndex: number, objectId: string) => void duplicateObject: (docIndex: number, objectId: string) => void cancelAdding: () => void + goToPage: (pageIndex: number, docIndex?: number) => boolean + getCurrentPage: () => PDFElementsPageChangePayload | null } /** diff --git a/src/utils/pageBounds.ts b/src/utils/pageBounds.ts index e6ffc1c..d20714e 100644 --- a/src/utils/pageBounds.ts +++ b/src/utils/pageBounds.ts @@ -11,3 +11,29 @@ export function getViewportWindow(scrollTop: number, viewHeight: number, margin export function isPageInViewport(offsetTop: number, offsetHeight: number, minY: number, maxY: number) { return !(offsetTop + offsetHeight < minY || offsetTop > maxY) } + +export interface PageViewportRect { + docIndex: number + pageIndex: number + /** Top edge relative to the top of the scroll viewport. */ + top: number + /** Bottom edge relative to the top of the scroll viewport. */ + bottom: number +} + +/** + * Returns the page occupying the largest visible area of the viewport. + * Ties resolve to the first page in document order. + */ +export function findMostVisiblePage(rects: PageViewportRect[], viewHeight: number) { + let best: PageViewportRect | null = null + let bestVisible = 0 + for (const rect of rects) { + const visible = Math.min(rect.bottom, viewHeight) - Math.max(rect.top, 0) + if (visible > bestVisible) { + best = rect + bestVisible = visible + } + } + return best ? { docIndex: best.docIndex, pageIndex: best.pageIndex } : null +} diff --git a/tests/components/PDFElements.spec.ts b/tests/components/PDFElements.spec.ts index 3866ef7..71ac991 100644 --- a/tests/components/PDFElements.spec.ts +++ b/tests/components/PDFElements.spec.ts @@ -918,3 +918,45 @@ describe('PDFElements business rules', () => { rafSpy.mockRestore() }) }) + +describe('PDFElements page navigation', () => { + const setup = (tops: number[]) => { + const { wrapper, ctx } = makeWrapper() + const doc = makeDoc() + ctx.pdfDocuments = [doc] + const root = ctx.$el as HTMLElement + root.getBoundingClientRect = () => ({ top: 0, bottom: 500 } as DOMRect) + Object.defineProperty(root, 'clientHeight', { value: 500, configurable: true }) + root.scrollTop = 0 + ctx.getPageWrapperElement = vi.fn((_d: number, p: number) => ({ + getBoundingClientRect: () => ({ top: tops[p] - root.scrollTop, bottom: tops[p] + 400 - root.scrollTop }), + })) + return { wrapper, ctx, root } + } + + it('scrolls to the requested page', () => { + const { ctx, root } = setup([0, 450]) + expect(ctx.goToPage(1)).toBe(true) + expect(root.scrollTop).toBe(450) + expect(ctx.getCurrentPage()).toEqual({ docIndex: 0, pageIndex: 1 }) + }) + + it('rejects invalid pages', () => { + const { ctx } = setup([0, 450]) + expect(ctx.goToPage(2)).toBe(false) + expect(ctx.goToPage(-1)).toBe(false) + expect(ctx.goToPage(0, 3)).toBe(false) + }) + + it('emits page-change only when the visible page changes', () => { + const { wrapper, ctx, root } = setup([0, 450]) + ctx.updateCurrentPage() + ctx.updateCurrentPage() + root.scrollTop = 450 + ctx.updateCurrentPage() + expect(wrapper.emitted('pdf-elements:page-change')).toEqual([ + [{ docIndex: 0, pageIndex: 0 }], + [{ docIndex: 0, pageIndex: 1 }], + ]) + }) +}) diff --git a/tests/utils/pageBounds.spec.ts b/tests/utils/pageBounds.spec.ts index 6f94a62..ed662c1 100644 --- a/tests/utils/pageBounds.spec.ts +++ b/tests/utils/pageBounds.spec.ts @@ -2,7 +2,7 @@ // SPDX-License-Identifier: AGPL-3.0-or-later import { describe, it, expect } from 'vitest' -import { getViewportWindow, isPageInViewport } from '../../src/utils/pageBounds' +import { findMostVisiblePage, getViewportWindow, isPageInViewport } from '../../src/utils/pageBounds' describe('pageBounds', () => { it('computes viewport window with margin', () => { @@ -16,3 +16,19 @@ describe('pageBounds', () => { expect(isPageInViewport(400, 50, 0, 300)).toBe(false) }) }) + +describe('findMostVisiblePage', () => { + const rects = [ + { docIndex: 0, pageIndex: 0, top: -400, bottom: 100 }, + { docIndex: 0, pageIndex: 1, top: 100, bottom: 600 }, + ] + + it('picks the page with the largest visible area', () => { + expect(findMostVisiblePage(rects, 500)).toEqual({ docIndex: 0, pageIndex: 1 }) + }) + + it('returns null when nothing is visible', () => { + expect(findMostVisiblePage([], 500)).toBeNull() + expect(findMostVisiblePage([{ docIndex: 0, pageIndex: 0, top: 600, bottom: 900 }], 500)).toBeNull() + }) +}) From 137e380a4e226c1ceb9c2a7c4450ddbf3d408e33 Mon Sep 17 00:00:00 2001 From: Luis Amorim Date: Wed, 25 Nov 2026 18:29:54 -0300 Subject: [PATCH 2/3] feat(thumbnails): add optional page thumbnail sidebar Adds showThumbnails/thumbnailWidth props. Thumbnails render lazily, highlight the current page and navigate via goToPage. Signed-off-by: Luis Amorim --- docs/API.md | 2 + src/components/PDFElements.vue | 39 ++++++- src/components/PDFThumbnails.vue | 143 +++++++++++++++++++++++++ tests/components/PDFThumbnails.spec.ts | 42 ++++++++ 4 files changed, 225 insertions(+), 1 deletion(-) create mode 100644 src/components/PDFThumbnails.vue create mode 100644 tests/components/PDFThumbnails.spec.ts diff --git a/docs/API.md b/docs/API.md index cdcf691..f5be1bc 100644 --- a/docs/API.md +++ b/docs/API.md @@ -24,6 +24,8 @@ For the logical field / visual widget data model (plain serializable types, rela | `ignoreClickOutsideSelectors` | Array | `[]` | CSS selectors that keep the selection active when clicking outside the element | | `pageCountFormat` | String | `'{currentPage} of {totalPages}'` | Format string for page counter | | `autoFitZoom` | Boolean | `false` | Automatically adjust zoom to fit viewport on window resize | +| `showThumbnails` | Boolean | `false` | Show a sticky thumbnail sidebar on the left; clicking a thumbnail calls `goToPage` and the current page is highlighted | +| `thumbnailWidth` | Number | `120` | Thumbnail width in pixels | | `pdfjsOptions` | Object | `{}` | Options passed to PDF.js `getDocument` | ## PDF.js options diff --git a/src/components/PDFElements.vue b/src/components/PDFElements.vue index d4b7c76..c8b5317 100644 --- a/src/components/PDFElements.vue +++ b/src/components/PDFElements.vue @@ -4,7 +4,15 @@ SPDX-License-Identifier: AGPL-3.0-or-later -->