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): list text boxes in the annotations sidebar
Elian Doranc18867725390
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.jsonindex c2cb288662..efd041ca4b 100644--- a/apps/client/src/translations/en/translation.json+++ b/apps/client/src/translations/en/translation.json@@ -3615,7 +3615,8 @@"annotations_other": "{{count}} annotations","annotation_drawing": "Drawing on page {{pageNumber}}","annotation_highlight": "Highlight on page {{pageNumber}}",- "annotation_note": "Note on page {{pageNumber}}"+ "annotation_note": "Note on page {{pageNumber}}",+ "annotation_text_box": "Text box on page {{pageNumber}}"},"platform_indicator": {"available_on": "Available on {{platform}}"diff --git a/apps/client/src/widgets/sidebar/pdf/PdfAnnotations.spec.tsx b/apps/client/src/widgets/sidebar/pdf/PdfAnnotations.spec.tsxindex 92c2793288..4f1947c05f 100644--- a/apps/client/src/widgets/sidebar/pdf/PdfAnnotations.spec.tsx+++ b/apps/client/src/widgets/sidebar/pdf/PdfAnnotations.spec.tsx@@ -70,19 +70,36 @@ describe("PdfAnnotations", () => {const container = renderPanel([annotation({ id: "18R", type: "ink", pageNumber: 4 }),annotation({ id: "17R", type: "highlight", pageNumber: 2 }),+ annotation({ id: "19R", type: "freetext", pageNumber: 3 }),annotation({ id: "5R", highlightedText: "quoted words", contents: "A remark" })]);const rows = [ ...container.querySelectorAll(".pdf-annotation-item") ];- expect(rows).toHaveLength(3);+ expect(rows).toHaveLength(4);expect(rows[0].querySelector(".pdf-annotation-untitled")?.textContent).toBe(`pdf.annotation_drawing({"pageNumber":4})`);expect(rows[1].querySelector(".pdf-annotation-untitled")?.textContent).toBe(`pdf.annotation_highlight({"pageNumber":2})`);+ expect(rows[2].querySelector(".pdf-annotation-untitled")?.textContent)+ .toBe(`pdf.annotation_text_box({"pageNumber":3})`);// An annotation with text of its own is described by that text, not by its kind.- expect(rows[2].textContent).toContain("quoted words");- expect(rows[2].textContent).toContain("A remark");- expect(rows[2].querySelector(".pdf-annotation-untitled")).toBeNull();+ expect(rows[3].textContent).toContain("quoted words");+ expect(rows[3].textContent).toContain("A remark");+ expect(rows[3].querySelector(".pdf-annotation-untitled")).toBeNull();+ });++ it("shows a free-text box under its own icon rather than as a comment", () => {+ // The comment icon marks a remark somebody attached to another annotation; a text box's+ // words are the annotation itself, so it keeps the text icon.+ const container = renderPanel([+ annotation({ id: "19R", type: "freetext", contents: "Typed in the box" }),+ annotation({ id: "5R", type: "highlight", contents: "A remark" })+ ]);++ const rows = [ ...container.querySelectorAll(".pdf-annotation-item") ];+ expect(rows[0].textContent).toContain("Typed in the box");+ expect(rows[0].querySelector(".tn-icon")?.className).toContain("bx-text");+ expect(rows[1].querySelector(".tn-icon")?.className).toContain("bxs-comment-detail");});it("flips the row's text to light on a dark annotation colour", () => {diff --git a/apps/client/src/widgets/sidebar/pdf/PdfAnnotations.tsx b/apps/client/src/widgets/sidebar/pdf/PdfAnnotations.tsxindex 7554b8ef96..6b8cd8762d 100644--- a/apps/client/src/widgets/sidebar/pdf/PdfAnnotations.tsx+++ b/apps/client/src/widgets/sidebar/pdf/PdfAnnotations.tsx@@ -9,6 +9,7 @@ import RightPanelWidget from "../RightPanelWidget";const TYPE_ICONS: Record<string, string> = {text: "bx bxs-comment-detail",+ freetext: "bx bx-text",highlight: "bx bx-highlight",ink: "bx bx-pen",};@@ -49,7 +50,9 @@ function PdfAnnotationItem({annotation: PdfAnnotationInfo;onNavigate: (annotationId: string, pageNumber: number) => void;}) {- const icon = annotation.contents+ // Contents on a highlight or a drawing is a remark somebody attached to it, so the row reads+ // as a comment. A free-text box's contents are the box itself, so it keeps its own icon.+ const icon = annotation.contents && annotation.type !== "freetext"? "bx bxs-comment-detail": TYPE_ICONS[annotation.type] ?? "bx bx-comment";// A drawing has no text of its own, and a highlight only gets one where the page has@@ -99,6 +102,8 @@ function describeUntitled({ type, pageNumber }: PdfAnnotationInfo) {switch (type) {case "ink":return t("pdf.annotation_drawing", { pageNumber });+ case "freetext":+ return t("pdf.annotation_text_box", { pageNumber });case "highlight":return t("pdf.annotation_highlight", { pageNumber });default:diff --git a/packages/pdfjs-viewer/src/annotations.spec.ts b/packages/pdfjs-viewer/src/annotations.spec.tsindex 96ade4c488..472a4dc73e 100644--- a/packages/pdfjs-viewer/src/annotations.spec.ts+++ b/packages/pdfjs-viewer/src/annotations.spec.ts@@ -63,8 +63,22 @@ describe("processAnnotation", () => {const link = { ...SAMPLE_HIGHLIGHT, annotationType: 2 }; // LINKexpect(processAnnotation(link, 1)).toBeNull();- const freeText = { ...SAMPLE_HIGHLIGHT, annotationType: 3 }; // FREETEXT- expect(processAnnotation(freeText, 1)).toBeNull();+ const widget = { ...SAMPLE_HIGHLIGHT, annotationType: 20 }; // WIDGET (a form field)+ expect(processAnnotation(widget, 1)).toBeNull();+ });++ it("reads a free-text box's own words as its contents", () => {+ const freeText = {+ ...SAMPLE_HIGHLIGHT,+ annotationType: AnnotationType.FREETEXT,+ contentsObj: { str: "Typed in the box", dir: "ltr" },+ overlaidText: undefined+ };++ const result = processAnnotation(freeText, 2)!;+ expect(result.type).toBe("freetext");+ expect(result.contents).toBe("Typed in the box");+ expect(result.highlightedText).toBe("");});it("keeps an annotation carrying no text at all", () => {@@ -151,6 +165,12 @@ describe("extraction from a real document", () => {highlightedText: "",pageNumber: 2,color: "#000000"+ }),+ expect.objectContaining({+ id: "19R",+ type: "freetext",+ contents: "Typed in the box",+ pageNumber: 2})];@@ -160,7 +180,7 @@ describe("extraction from a real document", () => {await setupPdfAnnotations();// Page 2's link is the only annotation filtered out — the highlight with nothing to- // show and the ink drawing both belong in the sidebar, named by kind (#11059).+ // show, the ink drawing and the free-text box all belong in the sidebar (#11059).expect(viewer.lastMessageOfType("pdfjs-viewer-annotations").annotations).toEqual(EXPECTED);});@@ -198,7 +218,7 @@ describe("extraction from a real document", () => {await setupPdfAnnotations();const { annotations } = viewer.lastMessageOfType("pdfjs-viewer-annotations");- expect(annotations.map((annotation: any) => annotation.id)).toEqual([ "14R", "17R", "18R" ]);+ expect(annotations.map((annotation: any) => annotation.id)).toEqual([ "14R", "17R", "18R", "19R" ]);});it("reports an empty list when extraction fails", async () => {@@ -250,7 +270,8 @@ describe("re-extraction from saved bytes", () => {expect.objectContaining({ contents: "A remark" }),expect.objectContaining({ contents: "A sticky note" }),expect.objectContaining({ id: "17R", type: "highlight" }),- expect.objectContaining({ id: "18R", type: "ink" })+ expect.objectContaining({ id: "18R", type: "ink" }),+ expect.objectContaining({ id: "19R", type: "freetext", contents: "Typed in the box" })]);// The temporary document owns a worker, so failing to destroy it leaks one per save.expect(destroy).toHaveBeenCalledTimes(1);diff --git a/packages/pdfjs-viewer/src/annotations.ts b/packages/pdfjs-viewer/src/annotations.tsindex 559f19fe1a..f5e8758b1b 100644--- a/packages/pdfjs-viewer/src/annotations.ts+++ b/packages/pdfjs-viewer/src/annotations.ts@@ -1,24 +1,27 @@// PDF annotation type constants (from PDF spec / pdfjs-dist AnnotationType)export const AnnotationType = {TEXT: 1,+ FREETEXT: 3,HIGHLIGHT: 9,INK: 15,} as const;/**- * Annotation types we display in the sidebar. Ink is here because pdf.js writes a free-hand- * highlight — one drawn with the highlight tool instead of over selected text — as an Ink- * annotation, not a Highlight one, so leaving it out hid every highlight not made by selecting- * text. It also covers the pen tool, whose drawings belong in the list just as much.+ * Annotation types we display in the sidebar — one per tool the viewer's editing toolbar offers,+ * plus the sticky notes other editors leave behind. The mapping from tool to type is not the+ * obvious one: a highlight drawn free-hand rather than over selected text is written as Ink, and+ * the text tool produces FreeText, so covering only Highlight hid most of what users draw.*/const COMMENT_TYPES = new Set<number>([AnnotationType.TEXT,+ AnnotationType.FREETEXT,AnnotationType.HIGHLIGHT,AnnotationType.INK,]);const TYPE_NAMES: Record<number, string> = {[AnnotationType.TEXT]: "text",+ [AnnotationType.FREETEXT]: "freetext",[AnnotationType.HIGHLIGHT]: "highlight",[AnnotationType.INK]: "ink",};diff --git a/packages/pdfjs-viewer/src/test/fixture_pdf.ts b/packages/pdfjs-viewer/src/test/fixture_pdf.tsindex f988f4c5d6..3ef817c2c8 100644--- a/packages/pdfjs-viewer/src/test/fixture_pdf.ts+++ b/packages/pdfjs-viewer/src/test/fixture_pdf.ts@@ -38,8 +38,9 @@ export function buildPdf(objects: string[]): Uint8Array {export const ATTACHMENT_TEXT = "hello attachment payload";/**- * The all-features fixture: two pages, a two-level outline, a highlight plus a text, an ink- * and a link annotation, an embedded attachment, and two optional-content groups (one hidden).+ * The all-features fixture: two pages, a two-level outline, a highlight plus a text, an ink, a+ * free-text and a link annotation, an embedded attachment, and two optional-content groups (one+ * hidden).** Kept as a single document so one `getDocument()` call serves every contract test; the* per-feature expectations live in the specs.@@ -56,8 +57,8 @@ export function allFeaturesPdf(): Uint8Array {// 3 — page 1, carrying both annotations."<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] /Annots [5 0 R 14 0 R] >>",// 4 — page 2, target of the second outline entry, carrying a link (the one type the- // sidebar filters out) alongside the two textless annotations it still lists.- "<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] /Annots [16 0 R 17 0 R 18 0 R] >>",+ // sidebar filters out) alongside the annotations it lists without a comment.+ "<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] /Annots [16 0 R 17 0 R 18 0 R 19 0 R] >>",// 5 — highlight annotation with a comment, author and colour."<< /Type /Annot /Subtype /Highlight /Rect [100 700 200 720] "+ "/Contents (A remark) /T (Alice) /C [1 0 0] "@@ -93,7 +94,11 @@ export function allFeaturesPdf(): Uint8Array {+ "/QuadPoints [200 220 300 220 200 200 300 200] >>",// 18 — an ink annotation, which is what pdf.js writes for a free-hand highlight."<< /Type /Annot /Subtype /Ink /Rect [400 400 500 450] /C [0 0 0] "- + "/InkList [[400 400 450 450 500 400]] >>"+ + "/InkList [[400 400 450 450 500 400]] >>",+ // 19 — a free-text box, what the toolbar's text tool writes. Its own words are its+ // contents, so unlike ink it reaches the sidebar with something to show.+ "<< /Type /Annot /Subtype /FreeText /Rect [100 300 300 340] "+ + "/Contents (Typed in the box) /DA (/Helv 12 Tf 0 g) >>"]);}diff --git a/packages/pdfjs-viewer/src/upstream_contract.spec.ts b/packages/pdfjs-viewer/src/upstream_contract.spec.tsindex 704bc11d1b..6e263d5035 100644--- a/packages/pdfjs-viewer/src/upstream_contract.spec.ts+++ b/packages/pdfjs-viewer/src/upstream_contract.spec.ts@@ -43,6 +43,7 @@ describe("annotation type constants", () => {// annotations.ts re-declares these rather than importing them, because the viewer// bundle owns pdf.js at runtime; that copy has to be kept honest from here.expect(AnnotationType.TEXT).toBe(UpstreamAnnotationType.TEXT);+ expect(AnnotationType.FREETEXT).toBe(UpstreamAnnotationType.FREETEXT);expect(AnnotationType.HIGHLIGHT).toBe(UpstreamAnnotationType.HIGHLIGHT);expect(AnnotationType.INK).toBe(UpstreamAnnotationType.INK);