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 deleted annotations out of the sidebar
Elian Doranedcc7123caab
diff --git a/packages/pdfjs-viewer/src/annotations.spec.ts b/packages/pdfjs-viewer/src/annotations.spec.tsindex 955ad3880b..b789d6e0a1 100644--- a/packages/pdfjs-viewer/src/annotations.spec.ts+++ b/packages/pdfjs-viewer/src/annotations.spec.ts@@ -144,20 +144,28 @@ describe("extraction from a real document", () => {expect(highlight).toMatchObject({ id: "5R", color: "#00ff00", contents: "Edited in the viewer" });});- it("DEFECT: keeps the second of two consecutively deleted annotations", async () => {+ it("removes deleted annotations even when they are adjacent", async () => {viewer = await installViewerApp(allFeaturesPdf());vi.spyOn(viewer.pdfDocument.annotationStorage, "getEditor").mockReturnValue({ deleted: true });await setupPdfAnnotations();- // `applyEditorOverrides` splices from the array it is iterating with `for...of`, so- // removing an element shifts the next one into the consumed index and skips it. Both- // fixture annotations are marked deleted, so the sidebar should end up empty — instead- // the second survives and shows an annotation the user already removed. Pinned rather- // than silently accepted; the fix is to build a filtered array instead of splicing.+ // Both fixture annotations are adjacent in the page's /Annots order, which is what+ // used to break: removing one from the array under iteration shifted the next into+ // the consumed index, so it was skipped and stayed in the sidebar — showing an+ // annotation the user had deleted until a save round-trip replaced the list.+ expect(viewer.lastMessageOfType("pdfjs-viewer-annotations").annotations).toEqual([]);+ });++ it("keeps the annotations that were not deleted", async () => {+ viewer = await installViewerApp(allFeaturesPdf());+ vi.spyOn(viewer.pdfDocument.annotationStorage, "getEditor")+ .mockImplementation((id: string) => (id === "5R" ? { deleted: true } : null));++ await setupPdfAnnotations();+const { annotations } = viewer.lastMessageOfType("pdfjs-viewer-annotations");- expect(annotations).toHaveLength(1);- expect(annotations[0].id).toBe("14R");+ expect(annotations.map((annotation: any) => annotation.id)).toEqual([ "14R" ]);});it("reports an empty list when extraction fails", async () => {diff --git a/packages/pdfjs-viewer/src/annotations.ts b/packages/pdfjs-viewer/src/annotations.tsindex 6917667216..909eaf224c 100644--- a/packages/pdfjs-viewer/src/annotations.ts+++ b/packages/pdfjs-viewer/src/annotations.ts@@ -91,8 +91,7 @@ async function extractAndSendAnnotations() {const app = window.PDFViewerApplication;try {const annotations = await extractFromDocument(app.pdfDocument);- applyEditorOverrides(annotations, app.pdfDocument.annotationStorage);- sendAnnotations(annotations);+ sendAnnotations(applyEditorOverrides(annotations, app.pdfDocument.annotationStorage));} catch (error) {console.error("Error extracting annotations:", error);sendAnnotations([]);@@ -139,21 +138,32 @@ async function extractFromDocument(pdfDocument: any): Promise<PdfAnnotationInfo[return annotations;}-function applyEditorOverrides(annotations: PdfAnnotationInfo[], storage: any) {+/**+ * Layers the in-session editing state over the annotations read from the document: until a+ * save is written back, deletions and edits live only in the editor, not in the file.+ *+ * Returns a new array rather than removing entries from the one being iterated — splicing+ * shifted the following annotation into the index the loop had just consumed, skipping it, so+ * deleting adjacent annotations left every second one in the sidebar until the next save.+ */+function applyEditorOverrides(annotations: PdfAnnotationInfo[], storage: any): PdfAnnotationInfo[] {+ const remaining: PdfAnnotationInfo[] = [];+for (const ann of annotations) {const editor = storage.getEditor?.(ann.id);- if (!editor) continue;- if (editor.deleted) {- annotations.splice(annotations.indexOf(ann), 1);+ if (editor?.deleted) {continue;}- if (editor.color) {+ if (editor?.color) {ann.color = editor.color;}- if (editor.comment?.text) {+ if (editor?.comment?.text) {ann.contents = editor.comment.text;}+ remaining.push(ann);}++ return remaining;}function sendAnnotations(annotations: PdfAnnotationInfo[]) {