Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix(pdf): keep a link's jump when the viewer is resized right after it
Elian Doran42b5ca0b5cf3
diff --git a/packages/pdfjs-viewer/src/annotations.spec.ts b/packages/pdfjs-viewer/src/annotations.spec.tsindex 8c1389c634..0eaec94a13 100644--- a/packages/pdfjs-viewer/src/annotations.spec.ts+++ b/packages/pdfjs-viewer/src/annotations.spec.ts@@ -380,7 +380,23 @@ describe("scrolling to an annotation", () => {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("has pdf.js record the new position at once, so a resize in the same frame keeps it", async () => {+ // On a resize, pdf.js reapplies an "auto" zoom and scrolls back to the position it last+ // recorded, which it otherwise records only on the next frame's scroll event.+ viewer = await installViewerApp(allFeaturesPdf(), PAGES);+ await setupPdfAnnotations();+ const app = window.PDFViewerApplication;++ viewer.sendFromParent({ type: "trilium-scroll-to-annotation", annotationId: "18R", pageNumber: 2 });+ await vi.waitFor(() => expect(app?.pdfViewer.update).toHaveBeenCalledOnce());+ expect(viewer.scrollRequests).toHaveBeenCalledWith(expect.objectContaining({ behavior: "instant" }));+ expect(viewer.scrollRequests.mock.invocationCallOrder[0])+ .toBeLessThan(vi.mocked(app?.pdfViewer.update as () => void).mock.invocationCallOrder[0]);++ viewer.sendFromParent({ type: "trilium-scroll-to-annotation", pageNumber: 1 });+ await vi.waitFor(() => expect(app?.pdfViewer.update).toHaveBeenCalledTimes(2));});it("ignores a scroll request from another origin", async () => {diff --git a/packages/pdfjs-viewer/src/annotations.ts b/packages/pdfjs-viewer/src/annotations.tsindex 2d5d963d54..f20b2a65ea 100644--- a/packages/pdfjs-viewer/src/annotations.ts+++ b/packages/pdfjs-viewer/src/annotations.ts@@ -248,6 +248,10 @@ function sendAnnotations(annotations: PdfAnnotationInfo[]) {* 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.+ *+ * The scroll is instant and followed by `pdfViewer.update()`, which records the new position. On a+ * resize, pdf.js reapplies an "auto" zoom and scrolls back to the position it last recorded, and a+ * viewer shown again after being hidden resizes in the same frame as the scroll.*/async function scrollToAnnotation(annotationId: string | undefined, pageNumber: number) {await initialViewApplied;@@ -258,6 +262,7 @@ async function scrollToAnnotation(annotationId: string | undefined, pageNumber:const pageView = app.pdfViewer.getPageView(pageNumber - 1);if (!rect || !pageView) {app.pdfViewer.currentPageNumber = pageNumber;+ app.pdfViewer.update();return;}@@ -270,8 +275,9 @@ async function scrollToAnnotation(annotationId: string | undefined, pageNumber:container.scrollTo({left: div.offsetLeft + div.clientLeft + (x1 + x2 - container.clientWidth) / 2,top: div.offsetTop + div.clientTop + (y1 + y2 - container.clientHeight) / 2,- behavior: "smooth"+ behavior: "instant"});+ app.pdfViewer.update();}/**diff --git a/packages/pdfjs-viewer/src/test/viewer_app.ts b/packages/pdfjs-viewer/src/test/viewer_app.tsindex dd4c2ec294..84c94d7b30 100644--- a/packages/pdfjs-viewer/src/test/viewer_app.ts+++ b/packages/pdfjs-viewer/src/test/viewer_app.ts@@ -110,6 +110,7 @@ export async function installViewerApp(data: Uint8Array, pageGeometry: Record<nupdfViewer: {container,currentPageNumber: 1,+ update: vi.fn(),optionalContentConfigPromise,getPageView: (pageIndex: number) => {const geometry = pageGeometry[pageIndex];diff --git a/packages/pdfjs-viewer/src/typings.d.ts b/packages/pdfjs-viewer/src/typings.d.tsindex 79b142136f..4258b4882f 100644--- a/packages/pdfjs-viewer/src/typings.d.ts+++ b/packages/pdfjs-viewer/src/typings.d.ts@@ -27,6 +27,8 @@ declare global {currentPageNumber: number;/** Settles once every page has been sized. */pagesPromise: Promise<void>;+ /** Works out the visible pages and records the scroll position. */+ update(): void;/*** pdf.js' own type, rather than a local restatement of it — the previous* hand-written shape declared the config object directly and omitted the