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): not reacting to new annotations
Elian Doran992f72b13940
diff --git a/packages/pdfjs-viewer/src/annotations.ts b/packages/pdfjs-viewer/src/annotations.tsindex ae8840e980..e7130ca8f7 100644--- a/packages/pdfjs-viewer/src/annotations.ts+++ b/packages/pdfjs-viewer/src/annotations.ts@@ -124,49 +124,75 @@ export function setupAnnotationLiveUpdates() {async function extractAndSendAnnotations() {const app = window.PDFViewerApplication;+ try {+ const annotations = await extractFromDocument(app.pdfDocument);+ applyEditorOverrides(annotations, app.pdfDocument.annotationStorage);+ sendAnnotations(annotations);+ } catch (error) {+ console.error("Error extracting annotations:", error);+ sendAnnotations([]);+ }+}+/**+ * Re-extract annotations from freshly saved PDF bytes.+ * Opens a temporary document to read the latest data (including+ * newly created highlights with their overlaidText), then closes it.+ */+export async function extractFromSavedData(data: ArrayBuffer | Uint8Array) {try {- const numPages = app.pdfDocument.numPages;- const storage = app.pdfDocument.annotationStorage;- const annotations: PdfAnnotationInfo[] = [];-- for (let i = 1; i <= numPages; i++) {- const page = await app.pdfDocument.getPage(i);- const pageAnnotations = await page.getAnnotations({ intent: "display" });-- for (const ann of pageAnnotations) {- const processed = processAnnotation(ann, i);- 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;- }- }+ const tempDoc = await (globalThis as any).pdfjsLib.getDocument({ data }).promise;+ const annotations = await extractFromDocument(tempDoc);+ tempDoc.destroy();+ sendAnnotations(annotations);+ } catch (error) {+ console.error("Error extracting annotations from saved data:", error);+ }+}++async function extractFromDocument(pdfDocument: any): Promise<PdfAnnotationInfo[]> {+ const numPages = pdfDocument.numPages;+ const annotations: PdfAnnotationInfo[] = [];+ for (let i = 1; i <= numPages; i++) {+ const page = await pdfDocument.getPage(i);+ const pageAnnotations = await page.getAnnotations({ intent: "display" });++ for (const ann of pageAnnotations) {+ const processed = processAnnotation(ann, i);+ if (processed) {annotations.push(processed);}}+ }- window.parent.postMessage({- type: "pdfjs-viewer-annotations",- annotations- } satisfies PdfViewerAnnotationsMessage, window.location.origin);- } catch (error) {- console.error("Error extracting annotations:", error);- window.parent.postMessage({- type: "pdfjs-viewer-annotations",- annotations: []- } satisfies PdfViewerAnnotationsMessage, window.location.origin);+ return annotations;+}++function applyEditorOverrides(annotations: PdfAnnotationInfo[], storage: any) {+ for (const ann of annotations) {+ const editor = storage.getEditor?.(ann.id);+ if (!editor) continue;+ if (editor.deleted) {+ annotations.splice(annotations.indexOf(ann), 1);+ continue;+ }+ if (editor.color) {+ ann.color = editor.color;+ }+ if (editor.comment?.text) {+ ann.contents = editor.comment.text;+ }}}+function sendAnnotations(annotations: PdfAnnotationInfo[]) {+ window.parent.postMessage({+ type: "pdfjs-viewer-annotations",+ annotations+ } satisfies PdfViewerAnnotationsMessage, window.location.origin);+}+function scrollToAnnotation(annotationId: string, pageNumber: number) {const app = window.PDFViewerApplication;diff --git a/packages/pdfjs-viewer/src/custom.ts b/packages/pdfjs-viewer/src/custom.tsindex a12b9afbd1..d3d80a756b 100644--- a/packages/pdfjs-viewer/src/custom.ts+++ b/packages/pdfjs-viewer/src/custom.ts@@ -3,7 +3,7 @@ import { extractAndSendToc, setupScrollToHeading, setupActiveHeadingTracking } fimport { setupPdfPages } from "./pages";import { setupPdfAttachments } from "./attachments";import { setupPdfLayers } from "./layers";-import { setupPdfAnnotations, setupAnnotationLiveUpdates } from "./annotations";+import { setupPdfAnnotations, setupAnnotationLiveUpdates, extractFromSavedData } from "./annotations";async function main() {const urlParams = new URLSearchParams(window.location.search);@@ -114,6 +114,9 @@ function manageSave() {ntxId: window.TRILIUM_NTX_ID,noteId: window.TRILIUM_NOTE_ID} satisfies PdfDocumentBlobResultMessage, window.location.origin);+ // Re-extract annotations from the saved data so new+ // highlights get their overlaidText populated.+ extractFromSavedData(data);}});