Skip to content

Build your personal knowledge base with Trilium Notes

fix(pdf): centre an annotation from its rectangle in the document

Elian Doran07ecccbe4155
diff --git a/packages/pdfjs-viewer/src/annotations.spec.ts b/packages/pdfjs-viewer/src/annotations.spec.ts
index ef56a9cc65..8c1389c634 100644
--- a/packages/pdfjs-viewer/src/annotations.spec.ts
+++ b/packages/pdfjs-viewer/src/annotations.spec.ts
@@ -353,29 +353,39 @@ describe("scrolling to an annotation", () => {
afterEach(() => uninstallViewerApp());
- /** Appends the element pdf.js would render for an annotation. */
- function renderAnnotation(id: string) {
- const el = document.createElement("div");
- el.setAttribute("data-annotation-id", id);
- viewer.viewerEl.append(el);
- return el;
+ /** Both fixture pages are US Letter, 792 points tall, stacked 1000 px apart. */
+ const PAGES = { 0: { offsetTop: 0, clientHeight: 792 }, 1: { offsetTop: 1000, clientHeight: 792 } };
+
+ /** Where the last scroll request puts the centre of the container, in container pixels. */
+ function centreOfLastScroll() {
+ const { left, top } = viewer.scrollRequests.mock.lastCall?.[0] ?? {};
+ return { x: left + 600 / 2, y: top + 800 / 2 };
}
- it("scrolls straight to an annotation that is already rendered", async () => {
- viewer = await installViewerApp(allFeaturesPdf());
+ /** Replaces what pdf.js holds for the annotations edited in this session. */
+ function editedInSession(entries: [ string, Record<string, unknown> ][]) {
+ vi.spyOn(viewer.pdfDocument.annotationStorage, "serializable", "get").mockReturnValue({
+ map: new Map(entries), hash: "x", transfer: []
+ } as any);
+ }
+
+ it("centres an annotation, from its rectangle in the document", async () => {
+ viewer = await installViewerApp(allFeaturesPdf(), PAGES);
await setupPdfAnnotations();
- renderAnnotation("5R");
- viewer.sendFromParent({ type: "trilium-scroll-to-annotation", annotationId: "5R", pageNumber: 1 });
+ // Object 18 is the ink stroke at [400 400 500 450] on page 2. Nothing has rendered it,
+ // which no longer matters: the page's viewport places it.
+ viewer.sendFromParent({ type: "trilium-scroll-to-annotation", annotationId: "18R", pageNumber: 2 });
await vi.waitFor(() => expect(viewer.scrollRequests).toHaveBeenCalled());
+ // PDF y grows upwards, so y 425 on a 792-point page is 367 px from its top.
+ expect(centreOfLastScroll()).toEqual({ x: 450, y: 1000 + 367 });
expect(viewer.scrollRequests).toHaveBeenCalledWith(expect.objectContaining({ behavior: "smooth" }));
});
it("ignores a scroll request from another origin", async () => {
- viewer = await installViewerApp(allFeaturesPdf());
+ viewer = await installViewerApp(allFeaturesPdf(), PAGES);
await setupPdfAnnotations();
- renderAnnotation("5R");
window.dispatchEvent(new MessageEvent("message", {
data: { type: "trilium-scroll-to-annotation", annotationId: "5R", pageNumber: 1 },
@@ -386,57 +396,48 @@ describe("scrolling to an annotation", () => {
expect(viewer.scrollRequests).not.toHaveBeenCalled();
});
- it("scrolls to an annotation that only exists in the editor", async () => {
- viewer = await installViewerApp(allFeaturesPdf());
+ it("finds an annotation where it was moved to, or drawn, in this session", async () => {
+ viewer = await installViewerApp(allFeaturesPdf(), PAGES);
await setupPdfAnnotations();
- // pdf.js gives an editor no data-annotation-id — it is not in the document yet — so the
- // element carries the editor's own id and that is what the sidebar entry holds.
- const editorEl = document.createElement("div");
- editorEl.id = "pdfjs_internal_editor_0";
- viewer.viewerEl.append(editorEl);
+ editedInSession([
+ [ "pdfjs_internal_editor_0", { annotationType: 9, id: null, pageIndex: 0, rect: [ 0, 0, 100, 100 ] } ],
+ [ "pdfjs_internal_editor_1", { annotationType: 9, id: "5R", pageIndex: 0, rect: [ 300, 392, 300, 392 ] } ]
+ ]);
viewer.sendFromParent({
type: "trilium-scroll-to-annotation", annotationId: "pdfjs_internal_editor_0", pageNumber: 1
});
+ await vi.waitFor(() => expect(viewer.scrollRequests).toHaveBeenCalledTimes(1));
+ expect(centreOfLastScroll()).toEqual({ x: 50, y: 742 });
- await vi.waitFor(() => expect(viewer.scrollRequests).toHaveBeenCalled());
+ viewer.sendFromParent({ type: "trilium-scroll-to-annotation", annotationId: "5R", pageNumber: 1 });
+ await vi.waitFor(() => expect(viewer.scrollRequests).toHaveBeenCalledTimes(2));
+ expect(centreOfLastScroll()).toEqual({ x: 300, y: 400 });
});
- it("jumps to the page and waits for an annotation that has not rendered yet", async () => {
- viewer = await installViewerApp(allFeaturesPdf());
+ it("turns to the page when the annotation is gone, or none is named", async () => {
+ viewer = await installViewerApp(allFeaturesPdf(), PAGES);
await setupPdfAnnotations();
+ const app = window.PDFViewerApplication;
+ editedInSession([ [ "pdfjs_internal_editor_0", { id: "17R", pageIndex: 1, deleted: true } ] ]);
- viewer.sendFromParent({ type: "trilium-scroll-to-annotation", annotationId: "14R", pageNumber: 2 });
+ viewer.sendFromParent({ type: "trilium-scroll-to-annotation", annotationId: "17R", pageNumber: 2 });
+ await vi.waitFor(() => expect(app?.pdfViewer.currentPageNumber).toBe(2));
- // Nothing to scroll to yet, so the viewer is asked to page there first.
- await vi.waitFor(() => expect(window.PDFViewerApplication?.pdfViewer.currentPageNumber).toBe(2));
- expect(viewer.scrollRequests).not.toHaveBeenCalled();
-
- // Other things render meanwhile; only the annotation itself ends the wait.
- viewer.viewerEl.append(document.createElement("div"));
- await new Promise((resolve) => setTimeout(resolve, 0));
- expect(viewer.scrollRequests).not.toHaveBeenCalled();
- // Once pdf.js renders the annotation, the observer picks it up.
- renderAnnotation("14R");
- await vi.waitFor(() => expect(viewer.scrollRequests).toHaveBeenCalled());
- });
-
- it("turns to the page alone when no annotation is named", async () => {
- viewer = await installViewerApp(allFeaturesPdf());
- await setupPdfAnnotations();
+ viewer.sendFromParent({ type: "trilium-scroll-to-annotation", annotationId: "99R", pageNumber: 1 });
+ await vi.waitFor(() => expect(app?.pdfViewer.currentPageNumber).toBe(1));
viewer.sendFromParent({ type: "trilium-scroll-to-annotation", pageNumber: 2 });
+ await vi.waitFor(() => expect(app?.pdfViewer.currentPageNumber).toBe(2));
- await vi.waitFor(() => expect(window.PDFViewerApplication?.pdfViewer.currentPageNumber).toBe(2));
- // A page reference ends at the top of the page. Nothing is looked up by id, not even the
- // literal "undefined" a missing id turns into.
- renderAnnotation("undefined");
- await new Promise((resolve) => setTimeout(resolve, 0));
+ // A link to a page an older version of the document had.
+ viewer.sendFromParent({ type: "trilium-scroll-to-annotation", annotationId: "5R", pageNumber: 9 });
+ await vi.waitFor(() => expect(app?.pdfViewer.currentPageNumber).toBe(9));
expect(viewer.scrollRequests).not.toHaveBeenCalled();
});
it("waits for pdf.js to apply its initial view, which would undo an earlier scroll", async () => {
- viewer = await installViewerApp(allFeaturesPdf());
+ viewer = await installViewerApp(allFeaturesPdf(), PAGES);
const app = window.PDFViewerApplication;
if (!app) throw new Error("viewer not installed");
let pagesLoaded = () => {};
@@ -444,17 +445,17 @@ describe("scrolling to an annotation", () => {
trackInitialView(app);
await setupPdfAnnotations();
- viewer.sendFromParent({ type: "trilium-scroll-to-annotation", annotationId: "14R", pageNumber: 2 });
+ viewer.sendFromParent({ type: "trilium-scroll-to-annotation", annotationId: "18R", pageNumber: 2 });
await new Promise((resolve) => setTimeout(resolve, 10));
- expect(app.pdfViewer.currentPageNumber).toBe(1);
+ expect(viewer.scrollRequests).not.toHaveBeenCalled();
// pdf.js restores the last-read position at `documentinit`, and again once every page is
// sized when the pages differ in size.
viewer.eventBus.dispatch("documentinit", { source: null });
await new Promise((resolve) => setTimeout(resolve, 10));
- expect(app.pdfViewer.currentPageNumber).toBe(1);
+ expect(viewer.scrollRequests).not.toHaveBeenCalled();
pagesLoaded();
- await vi.waitFor(() => expect(app.pdfViewer.currentPageNumber).toBe(2));
+ await vi.waitFor(() => expect(viewer.scrollRequests).toHaveBeenCalled());
});
});
diff --git a/packages/pdfjs-viewer/src/annotations.ts b/packages/pdfjs-viewer/src/annotations.ts
index 9b40e76c7b..2d5d963d54 100644
--- a/packages/pdfjs-viewer/src/annotations.ts
+++ b/packages/pdfjs-viewer/src/annotations.ts
@@ -241,54 +241,63 @@ function sendAnnotations(annotations: PdfAnnotationInfo[]) {
} satisfies PdfViewerAnnotationsMessage, window.location.origin);
}
-/** Scrolls to an annotation, or to the top of `pageNumber` when `annotationId` is not given. */
+/**
+ * Centres an annotation in the viewer, or turns to the top of `pageNumber` when `annotationId` is
+ * not given or names no annotation on the page.
+ *
+ * Works from the annotation's rectangle in the document rather than from its rendered element:
+ * pdf.js renders a page only once it nears the viewport, and while an annotation tool is active it
+ * hides each editable annotation's element behind an editor.
+ */
async function scrollToAnnotation(annotationId: string | undefined, pageNumber: number) {
await initialViewApplied;
const app = window.PDFViewerApplication;
if (!app) return;
- const container = app.pdfViewer.container;
- if (!annotationId) {
+ const rect = annotationId ? await findAnnotationRect(annotationId, pageNumber) : null;
+ const pageView = app.pdfViewer.getPageView(pageNumber - 1);
+ if (!rect || !pageView) {
app.pdfViewer.currentPageNumber = pageNumber;
return;
}
- function scrollToEl(el: Element) {
- const containerRect = container.getBoundingClientRect();
- const elRect = el.getBoundingClientRect();
- const offsetTop = elRect.top - containerRect.top + container.scrollTop;
- container.scrollTo({
- top: offsetTop - container.clientHeight / 2 + elRect.height / 2,
- behavior: "smooth"
- });
- }
+ // The viewport maps PDF units, which grow upwards, to the page's CSS pixels at the current
+ // zoom and rotation.
+ const [ x1, y1 ] = pageView.viewport.convertToViewportPoint(rect[0], rect[1]);
+ const [ x2, y2 ] = pageView.viewport.convertToViewportPoint(rect[2], rect[3]);
+ const { div } = pageView;
+ const container = app.pdfViewer.container;
+ container.scrollTo({
+ left: div.offsetLeft + div.clientLeft + (x1 + x2 - container.clientWidth) / 2,
+ top: div.offsetTop + div.clientTop + (y1 + y2 - container.clientHeight) / 2,
+ behavior: "smooth"
+ });
+}
- // An annotation the document holds renders with its id in an attribute; one that so far
- // exists only in the editor renders as an element carrying the editor id directly.
- function findRendered(id: string) {
- return document.querySelector(`[data-annotation-id="${CSS.escape(id)}"]`)
- ?? document.getElementById(id);
- }
+/**
+ * The rectangle of an annotation in PDF units, or `null` if it is not on the page or was deleted.
+ * An annotation edited in this session is read from `annotationStorage`, which holds where it is
+ * now and is keyed by the editor's id, with `id` naming the document annotation it edits.
+ */
+async function findAnnotationRect(annotationId: string, pageNumber: number): Promise<number[] | null> {
+ const app = window.PDFViewerApplication;
+ if (!app) return null;
- // Try to find the element directly (nearby pages are pre-rendered)
- const el = findRendered(annotationId);
- if (el) {
- scrollToEl(el);
- return;
+ const storage: any = app.pdfDocument.annotationStorage;
+ for (const [ id, serialized ] of storage?.serializable?.map ?? []) {
+ if (id === annotationId || serialized.id === annotationId) {
+ return serialized.deleted ? null : serialized.rect ?? null;
+ }
}
- // Element not in DOM yet — jump to the page and wait for it to render
- app.pdfViewer.currentPageNumber = pageNumber;
- const observer = new MutationObserver(() => {
- const el = findRendered(annotationId);
- if (el) {
- observer.disconnect();
- scrollToEl(el);
- }
- });
- observer.observe(document.getElementById("viewer")!, { childList: true, subtree: true });
- // Clean up if annotation never appears
- setTimeout(() => observer.disconnect(), 3000);
+ try {
+ const page = await app.pdfDocument.getPage(pageNumber);
+ const annotations = await page.getAnnotations({ intent: "display" });
+ return annotations.find((ann: Record<string, any>) => ann.id === annotationId)?.rect ?? null;
+ } catch {
+ // A page number past the end of the document, from a link to an older version of it.
+ return null;
+ }
}
export function rgbToHex(rgb: Uint8ClampedArray | Record<number, number> | number[]): string {
diff --git a/packages/pdfjs-viewer/src/test/viewer_app.ts b/packages/pdfjs-viewer/src/test/viewer_app.ts
index a4c223573b..dd4c2ec294 100644
--- a/packages/pdfjs-viewer/src/test/viewer_app.ts
+++ b/packages/pdfjs-viewer/src/test/viewer_app.ts
@@ -3,7 +3,7 @@
*
* Only the parts pdf.js builds inside its viewer application — which no public entry point
* lets us construct standalone — are stubbed here: the page-view geometry and the current
- * page number. Everything with a real upstream implementation is the real thing:
+ * page number. A page view's `viewport` is pdf.js' own, at a scale of 1. Everything with a real upstream implementation is the real thing:
*
* - `pdfDocument` is a genuine `PDFDocumentProxy` from `getDocument()`, so `getOutline()`,
* `getAttachments()`, `getOptionalContentConfig()` and `getAnnotations()` return whatever
@@ -55,11 +55,14 @@ export interface InstalledViewer {
/**
* Loads `data` with real pdf.js and exposes it through a minimal viewer application.
*
- * `pageGeometry` maps a zero-based page index to its on-screen box; only the active-heading
- * tracking in `toc.ts` reads it, and pages without an entry behave as "not yet rendered".
+ * `pageGeometry` maps a zero-based page index to its on-screen box, which the active-heading
+ * tracking in `toc.ts` and the scroll to an annotation read. Pages without an entry have no page
+ * view.
*/
export async function installViewerApp(data: Uint8Array, pageGeometry: Record<number, PageGeometry> = {}): Promise<InstalledViewer> {
const pdfDocument = await getDocument({ data }).promise;
+ const viewports = await Promise.all(Array.from({ length: pdfDocument.numPages }, async (_, index) =>
+ (await pdfDocument.getPage(index + 1)).getViewport({ scale: 1 })));
const eventBus = new EventBus();
// Resolved once and shared, mirroring the real viewer: pdf.js returns a new config object
@@ -76,6 +79,7 @@ export async function installViewerApp(data: Uint8Array, pageGeometry: Record<nu
document.body.append(container);
// happy-dom leaves layout at zero; the tracker only needs a readable clientHeight.
Object.defineProperty(container, "clientHeight", { value: 800, configurable: true });
+ Object.defineProperty(container, "clientWidth", { value: 600, configurable: true });
// happy-dom performs no scrolling, so record the requests instead. Positions asserted
// against this will be zero-based, since getBoundingClientRect() is also flat.
const scrollRequests = vi.fn();
@@ -115,7 +119,7 @@ export async function installViewerApp(data: Uint8Array, pageGeometry: Record<nu
const div = document.createElement("div");
Object.defineProperty(div, "offsetTop", { value: geometry.offsetTop });
Object.defineProperty(div, "clientHeight", { value: geometry.clientHeight });
- return { div };
+ return { div, viewport: viewports[pageIndex] };
}
},
pdfLinkService,
diff --git a/packages/pdfjs-viewer/src/typings.d.ts b/packages/pdfjs-viewer/src/typings.d.ts
index 9c40982930..79b142136f 100644
--- a/packages/pdfjs-viewer/src/typings.d.ts
+++ b/packages/pdfjs-viewer/src/typings.d.ts
@@ -35,7 +35,11 @@ declare global {
optionalContentConfigPromise: Promise<OptionalContentConfig>;
getPageView(pageIndex: number): {
div: HTMLDivElement;
- };
+ viewport: {
+ /** Maps a point in PDF units to CSS pixels on the page. */
+ convertToViewportPoint(x: number, y: number): number[];
+ };
+ } | undefined;
container: HTMLElement;
};
pdfLinkService: {