From 7849bc058358bb1dc8a1057f4afc69bbfc9fefc4 Mon Sep 17 00:00:00 2001 From: Ujjwal Sharma Date: Mon, 16 Dec 2024 23:03:14 +0100 Subject: [PATCH] fix bug with textContents and check links for duplicates --- web/annotation_layer_builder.js | 21 ++++++++++++++------- web/autolinker.js | 2 +- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/web/annotation_layer_builder.js b/web/annotation_layer_builder.js index 35376709def4c..8c4653ad6e990 100644 --- a/web/annotation_layer_builder.js +++ b/web/annotation_layer_builder.js @@ -25,7 +25,7 @@ // eslint-disable-next-line max-len /** @typedef {import("../src/display/editor/tools.js").AnnotationEditorUIManager} AnnotationEditorUIManager */ -import { AnnotationLayer } from "pdfjs-lib"; +import { AnnotationLayer, Util } from "pdfjs-lib"; import { PresentationModeState } from "./ui_utils.js"; /** @@ -119,12 +119,19 @@ class AnnotationLayerBuilder { return; } - if (annotations.length === 0) { - // Because we could end up creating duplicate annotations, avoid injecting - // any if there are already annotations in the layer. In the future, this - // should be something smarter so we could avoid overlaps. - annotations.push(...linkAnnotations); - } + const uniqueLinks = linkAnnotations.filter(link => { + for (const annotation of annotations) { + if ( + annotation.subtype === "Link" && + annotation.url === link.url && + Util.intersect(annotation.rect, link.rect) !== null + ) { + return false; + } + } + return true; + }); + annotations.push(...uniqueLinks); // Create an annotation layer div and render the annotations // if there is at least one annotation. diff --git a/web/autolinker.js b/web/autolinker.js index a86b4878fdbca..3906129aeab78 100644 --- a/web/autolinker.js +++ b/web/autolinker.js @@ -70,7 +70,7 @@ class Autolinker { static processLinks(pdfPageView) { const [text, diffs] = normalize( - pdfPageView._textHighlighter.textContentItemsStr.join("") + pdfPageView._textHighlighter.textContentItemsStr.join("\n") ); const matches = text.matchAll(Autolinker.#urlRegex); const links = [];