Skip to content

Build your personal knowledge base with Trilium Notes

fix(pdf): address review findings on annotation links

Claude24671d678d6f
diff --git a/apps/client/src/widgets/type_widgets/file/Pdf.spec.tsx b/apps/client/src/widgets/type_widgets/file/Pdf.spec.tsx
index 6f191eae94..e80d446c5c 100644
--- a/apps/client/src/widgets/type_widgets/file/Pdf.spec.tsx
+++ b/apps/client/src/widgets/type_widgets/file/Pdf.spec.tsx
@@ -63,6 +63,7 @@ beforeEach(() => {
contextData = {};
noteContext = {
ntxId: "ntx-1",
+ note: NOTE,
notePath: "root/parent/note-1",
viewScope: {},
setContextData: vi.fn((key: string, value: unknown) => { contextData[key] = value; }),
@@ -207,7 +208,8 @@ describe("PdfPreview", () => {
show(false);
contextData = {};
vi.mocked(noteContext.setContextData).mockClear();
- // Neither reaches the note the pane shows now; the page change is kept for the return.
+ // A hidden viewer records the page change for the return and holds the link's jump until
+ // it is shown.
fromThisViewer({ type: "pdfjs-viewer-current-page", currentPage: 7 });
noteContext.viewScope = { page: "4" };
eventHandlers.get("noteSwitched")?.({ noteContext: { ntxId: "ntx-1", note: NOTE } });
@@ -224,6 +226,33 @@ describe("PdfPreview", () => {
{ type: "trilium-scroll-to-annotation", annotationId: undefined, pageNumber: 4 }, window.location.origin);
});
+ it("keeps one PDF's data out of the pane once it shows another PDF", () => {
+ // `NoteDetailWrapper` hands the kept viewer `isVisible` at once and the next note's props
+ // one render later, so the viewer is shown while it still holds the previous PDF.
+ const posted = recordPostsToViewer();
+ fromThisViewer({ type: "pdfjs-viewer-page-info", totalPages: 12, currentPage: 3 });
+ fromThisViewer({ type: "pdfjs-viewer-annotations", annotations: [] });
+
+ function show(note: FNote, isVisible: boolean) {
+ act(() => render(
+ <PdfPreview note={note} noteContext={noteContext} blob={null} componentId="cmp" isVisible={isVisible} />,
+ container));
+ }
+
+ show(NOTE, false);
+ const otherNote = { ...NOTE, noteId: "note-2" } as unknown as FNote;
+ (noteContext as { note: FNote }).note = otherNote;
+ noteContext.viewScope = { page: "4" };
+ contextData = {};
+ vi.mocked(noteContext.setContextData).mockClear();
+
+ show(NOTE, true);
+ fromThisViewer({ type: "pdfjs-viewer-current-page", currentPage: 5 });
+ show(otherNote, true);
+ expect(noteContext.setContextData).not.toHaveBeenCalled();
+ expect(posted).not.toHaveBeenCalled();
+ });
+
it("copies a reference to a page, or to an annotation the document already holds", () => {
fromThisViewer({ type: "pdfjs-viewer-page-info", totalPages: 12, currentPage: 1 });
(contextData.pdfPages as any).copyReference(7);
diff --git a/apps/client/src/widgets/type_widgets/file/Pdf.tsx b/apps/client/src/widgets/type_widgets/file/Pdf.tsx
index 6e1c5152d3..3e32b35c22 100644
--- a/apps/client/src/widgets/type_widgets/file/Pdf.tsx
+++ b/apps/client/src/widgets/type_widgets/file/Pdf.tsx
@@ -49,11 +49,20 @@ export default function PdfPreview({ note, blob, componentId, noteContext, isVis
function publish<K extends PdfContextDataKey>(key: K, value: NoteContextDataMap[K]) {
publishedRef.current.set(key, value);
- if (isVisibleRef.current) {
+ if (isShown()) {
noteContext.setContextData(key, value);
}
}
+ /**
+ * Whether the pane shows this viewer's PDF. `NoteDetailWrapper` passes `isVisible` at once
+ * and the next note's props a render later, so a viewer kept from another PDF is briefly
+ * visible while `noteContext.note` is already the new one.
+ */
+ function isShown() {
+ return isVisibleRef.current && noteContext.note?.noteId === note.noteId;
+ }
+
function getPublished<K extends PdfContextDataKey>(key: K) {
return publishedRef.current.get(key) as NoteContextDataMap[K] | undefined;
}
@@ -64,7 +73,7 @@ export default function PdfPreview({ note, blob, componentId, noteContext, isVis
*/
function revealLinkTarget() {
const viewScope = noteContext.viewScope;
- if (!isVisibleRef.current || !publishedRef.current.has("pdfAnnotations") || !viewScope?.page) return;
+ if (!isShown() || !publishedRef.current.has("pdfAnnotations") || !viewScope?.page) return;
const pageNumber = Number.parseInt(viewScope.page, 10);
const annotationId = viewScope.annotation;
@@ -278,7 +287,7 @@ export default function PdfPreview({ note, blob, componentId, noteContext, isVis
useEffect(() => () => publishedRef.current.clear(), [ note.noteId ]);
useEffect(() => {
- if (!isVisible) return;
+ if (!isShown()) return;
for (const key of publishedRef.current.keys()) {
republish(key);
}
diff --git a/packages/pdfjs-viewer/src/annotations.spec.ts b/packages/pdfjs-viewer/src/annotations.spec.ts
index 0eaec94a13..1774fec396 100644
--- a/packages/pdfjs-viewer/src/annotations.spec.ts
+++ b/packages/pdfjs-viewer/src/annotations.spec.ts
@@ -382,6 +382,26 @@ describe("scrolling to an annotation", () => {
expect(centreOfLastScroll()).toEqual({ x: 450, y: 1000 + 367 });
});
+ it("turns to the annotation's page before measuring it, for single-page mode", async () => {
+ // In single-page scroll mode, setting `currentPageNumber` puts the page's element into
+ // the viewer, which holds only the current page.
+ viewer = await installViewerApp(allFeaturesPdf(), PAGES);
+ await setupPdfAnnotations();
+ const turnToPage = vi.fn();
+ Object.defineProperty(window.PDFViewerApplication?.pdfViewer, "currentPageNumber", {
+ get: () => 1,
+ set: turnToPage
+ });
+
+ viewer.sendFromParent({ type: "trilium-scroll-to-annotation", annotationId: "18R", pageNumber: 2 });
+
+ await vi.waitFor(() => expect(viewer.scrollRequests).toHaveBeenCalled());
+ expect(turnToPage).toHaveBeenCalledExactlyOnceWith(2);
+ expect(turnToPage.mock.invocationCallOrder[0])
+ .toBeLessThan(viewer.scrollRequests.mock.invocationCallOrder[0]);
+ expect(centreOfLastScroll()).toEqual({ x: 450, y: 1000 + 367 });
+ });
+
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.
diff --git a/packages/pdfjs-viewer/src/annotations.ts b/packages/pdfjs-viewer/src/annotations.ts
index f20b2a65ea..f03900d31a 100644
--- a/packages/pdfjs-viewer/src/annotations.ts
+++ b/packages/pdfjs-viewer/src/annotations.ts
@@ -259,9 +259,11 @@ async function scrollToAnnotation(annotationId: string | undefined, pageNumber:
if (!app) return;
const rect = annotationId ? await findAnnotationRect(annotationId, pageNumber) : null;
+ // In single-page scroll mode the viewer holds only the current page's element, so the page
+ // is turned to before its element is measured.
+ app.pdfViewer.currentPageNumber = pageNumber;
const pageView = app.pdfViewer.getPageView(pageNumber - 1);
if (!rect || !pageView) {
- app.pdfViewer.currentPageNumber = pageNumber;
app.pdfViewer.update();
return;
}