Skip to content

Build your personal knowledge base with Trilium Notes

fix(pdf): annotation color changes not considered

Elian Doran2a668b82b1ce
diff --git a/packages/pdfjs-viewer/src/annotations.ts b/packages/pdfjs-viewer/src/annotations.ts
index 986eae4b4f..ae8840e980 100644
--- a/packages/pdfjs-viewer/src/annotations.ts
+++ b/packages/pdfjs-viewer/src/annotations.ts
@@ -96,11 +96,38 @@ export async function setupPdfAnnotations() {
});
}
+/**
+ * Must be called AFTER manageSave() so we can chain onto the
+ * onSetModified callback it installs.
+ */
+export function setupAnnotationLiveUpdates() {
+ const app = window.PDFViewerApplication!;
+ const storage = app.pdfDocument.annotationStorage;
+
+ let debounceTimer: number | null = null;
+ const debouncedRefresh = () => {
+ if (debounceTimer) clearTimeout(debounceTimer);
+ debounceTimer = window.setTimeout(() => extractAndSendAnnotations(), 500);
+ };
+
+ // Chain onto the existing onSetModified set by manageSave.
+ // Fires when annotations are added/removed.
+ const previousOnSetModified = (storage as any).onSetModified;
+ (storage as any).onSetModified = () => {
+ previousOnSetModified?.();
+ debouncedRefresh();
+ };
+
+ // Fires when editor properties change (e.g. color, thickness).
+ app.eventBus.on("annotationeditorparamschanged", debouncedRefresh);
+}
+
async function extractAndSendAnnotations() {
const app = window.PDFViewerApplication;
try {
const numPages = app.pdfDocument.numPages;
+ const storage = app.pdfDocument.annotationStorage;
const annotations: PdfAnnotationInfo[] = [];
for (let i = 1; i <= numPages; i++) {
@@ -109,9 +136,21 @@ async function extractAndSendAnnotations() {
for (const ann of pageAnnotations) {
const processed = processAnnotation(ann, i);
- if (processed) {
- annotations.push(processed);
+ if (!processed) continue;
+
+ // Check if there's an active editor overriding this annotation
+ const editor = (storage as any).getEditor?.(ann.id);
+ if (editor) {
+ if (editor.deleted) continue;
+ if (editor.color) {
+ processed.color = editor.color;
+ }
+ if (editor.comment?.text) {
+ processed.contents = editor.comment.text;
+ }
}
+
+ annotations.push(processed);
}
}
diff --git a/packages/pdfjs-viewer/src/custom.ts b/packages/pdfjs-viewer/src/custom.ts
index f42a8a1ba9..a12b9afbd1 100644
--- a/packages/pdfjs-viewer/src/custom.ts
+++ b/packages/pdfjs-viewer/src/custom.ts
@@ -3,7 +3,7 @@ import { extractAndSendToc, setupScrollToHeading, setupActiveHeadingTracking } f
import { setupPdfPages } from "./pages";
import { setupPdfAttachments } from "./attachments";
import { setupPdfLayers } from "./layers";
-import { setupPdfAnnotations } from "./annotations";
+import { setupPdfAnnotations, setupAnnotationLiveUpdates } from "./annotations";
async function main() {
const urlParams = new URLSearchParams(window.location.search);
@@ -45,6 +45,8 @@ async function main() {
setupPdfPages();
setupPdfAttachments();
setupPdfLayers();
+ // Must be after manageSave() so we chain onto its onSetModified
+ setupAnnotationLiveUpdates();
});
}
await app.initializedPromise;