Skip to content

Build your personal knowledge base with Trilium Notes

fix(pdf): navigate to the correct position of an annotation

Elian Doran4d2c346b804a
diff --git a/apps/client/src/components/note_context.ts b/apps/client/src/components/note_context.ts
index 3c8e191497..a215491eee 100644
--- a/apps/client/src/components/note_context.ts
+++ b/apps/client/src/components/note_context.ts
@@ -54,7 +54,7 @@ export interface NoteContextDataMap {
};
pdfAnnotations: {
annotations: PdfAnnotationInfo[];
- scrollToAnnotation(pageNumber: number): void;
+ scrollToAnnotation(annotationId: string, pageNumber: number): void;
};
saveState: {
state: SaveState;
diff --git a/apps/client/src/widgets/sidebar/pdf/PdfAnnotations.tsx b/apps/client/src/widgets/sidebar/pdf/PdfAnnotations.tsx
index a81a4dc50d..74064c2110 100644
--- a/apps/client/src/widgets/sidebar/pdf/PdfAnnotations.tsx
+++ b/apps/client/src/widgets/sidebar/pdf/PdfAnnotations.tsx
@@ -76,12 +76,12 @@ function PdfAnnotationItem({
onNavigate
}: {
annotation: PdfAnnotationInfo;
- onNavigate: (pageNumber: number) => void;
+ onNavigate: (annotationId: string, pageNumber: number) => void;
}) {
const icon = TYPE_ICONS[annotation.type] ?? "bx bx-comment";
return (
- <div className="pdf-annotation-item" onClick={() => onNavigate(annotation.pageNumber)}>
+ <div className="pdf-annotation-item" onClick={() => onNavigate(annotation.id, annotation.pageNumber)}>
<Icon icon={icon} style={annotation.color ? { color: annotation.color } : undefined} />
<div className="pdf-annotation-info">
{annotation.highlightedText && (
diff --git a/apps/client/src/widgets/type_widgets/file/Pdf.tsx b/apps/client/src/widgets/type_widgets/file/Pdf.tsx
index 83a9ef5570..582dd39693 100644
--- a/apps/client/src/widgets/type_widgets/file/Pdf.tsx
+++ b/apps/client/src/widgets/type_widgets/file/Pdf.tsx
@@ -157,9 +157,10 @@ export default function PdfPreview({ note, blob, componentId, noteContext }: {
if (event.data.type === "pdfjs-viewer-annotations") {
noteContext.setContextData("pdfAnnotations", {
annotations: event.data.annotations,
- scrollToAnnotation: (pageNumber: number) => {
+ scrollToAnnotation: (annotationId: string, pageNumber: number) => {
iframeRef.current?.contentWindow?.postMessage({
type: "trilium-scroll-to-annotation",
+ annotationId,
pageNumber
}, window.location.origin);
}
diff --git a/packages/pdfjs-viewer/src/annotations.ts b/packages/pdfjs-viewer/src/annotations.ts
index f9276ef679..986eae4b4f 100644
--- a/packages/pdfjs-viewer/src/annotations.ts
+++ b/packages/pdfjs-viewer/src/annotations.ts
@@ -91,7 +91,7 @@ export async function setupPdfAnnotations() {
if (event.origin !== window.location.origin) return;
if (event.data?.type === "trilium-scroll-to-annotation") {
- scrollToAnnotation(event.data.pageNumber);
+ scrollToAnnotation(event.data.annotationId, event.data.pageNumber);
}
});
}
@@ -128,9 +128,28 @@ async function extractAndSendAnnotations() {
}
}
-function scrollToAnnotation(pageNumber: number) {
+function scrollToAnnotation(annotationId: string, pageNumber: number) {
const app = window.PDFViewerApplication;
+
+ // Try to find the element directly (nearby pages are pre-rendered)
+ const el = document.querySelector(`[data-annotation-id="${CSS.escape(annotationId)}"]`);
+ if (el) {
+ el.scrollIntoView({ behavior: "smooth", block: "center" });
+ return;
+ }
+
+ // 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 = document.querySelector(`[data-annotation-id="${CSS.escape(annotationId)}"]`);
+ if (el) {
+ observer.disconnect();
+ el.scrollIntoView({ behavior: "smooth", block: "center" });
+ }
+ });
+ observer.observe(document.getElementById("viewer")!, { childList: true, subtree: true });
+ // Clean up if annotation never appears
+ setTimeout(() => observer.disconnect(), 3000);
}
export function rgbToHex(rgb: Uint8ClampedArray | Record<number, number> | number[]): string {