From d648990b9b966f688e6f823135453c4d892dbda6 Mon Sep 17 00:00:00 2001 From: Ujjwal Sharma Date: Thu, 30 Jan 2025 00:25:50 +0100 Subject: [PATCH] fixup! Enable automatic URL linking --- src/display/annotation_layer.js | 7 ++++++ web/annotation_layer_builder.js | 40 ++++++++++++++++++++++++--------- web/autolinker.js | 17 ++++---------- web/pdf_page_view.js | 10 ++++----- 4 files changed, 46 insertions(+), 28 deletions(-) diff --git a/src/display/annotation_layer.js b/src/display/annotation_layer.js index c953c641e77ac7..8b846432061efc 100644 --- a/src/display/annotation_layer.js +++ b/src/display/annotation_layer.js @@ -3264,6 +3264,13 @@ class AnnotationLayer { this.#setAnnotationCanvasMap(); } + /** + * Add link annotations to the annotation layer. + * + * @param {AddLinkAnnotationsParameters} annotations + * @param {IPDFLinkService} linkService + * @memberof AnnotationLayer + */ async addLinkAnnotations(annotations, linkService) { const elementParams = { data: null, diff --git a/web/annotation_layer_builder.js b/web/annotation_layer_builder.js index cdd20454ee2700..6bd22ab01ad5b2 100644 --- a/web/annotation_layer_builder.js +++ b/web/annotation_layer_builder.js @@ -61,6 +61,13 @@ import { PresentationModeState } from "./ui_utils.js"; * @property {StructTreeLayerBuilder} [structTreeLayer] */ +/** + * @typedef {Object} InjectLinkAnnotationsOptions + * @property {Array} inferredLinks + * @property {PageViewport} viewport + * @property {StructTreeLayerBuilder} [structTreeLayer] + */ + class AnnotationLayerBuilder { #annotations = null; @@ -135,6 +142,9 @@ class AnnotationLayerBuilder { // Create an annotation layer div and render the annotations // if there is at least one annotation. + const div = (this.div = document.createElement("div")); + div.className = "annotationLayer"; + this.#onAppend?.(div); if (annotations.length === 0) { this.hide(); @@ -176,18 +186,14 @@ class AnnotationLayerBuilder { } #initAnnotationLayer(viewport, structTreeLayer) { - const div = (this.div = document.createElement("div")); - div.className = "annotationLayer"; - this.#onAppend?.(div); - this.annotationLayer = new AnnotationLayer({ - div, + div: this.div, accessibilityManager: this._accessibilityManager, annotationCanvasMap: this._annotationCanvasMap, annotationEditorUIManager: this._annotationEditorUIManager, page: this.pdfPage, viewport: viewport.clone({ dontFlip: true }), - structTreeLayer: structTreeLayer || null, + structTreeLayer, }); } @@ -209,12 +215,25 @@ class AnnotationLayerBuilder { return !!this.annotationLayer?.hasEditableAnnotations(); } - async injectLinkAnnotations(inferredLinks, viewport, structTreeLayer) { - const uniqueLinks = this.#annotations + /** + * @param {InjectLinkAnnotationsOptions} options + * @returns {Promise} A promise that is resolved when the inferred links + * are added to the annotation layer. + */ + async injectLinkAnnotations({ + inferredLinks, + viewport, + structTreeLayer = null, + }) { + if (this._cancelled) { + return; + } + + const newLinks = this.#annotations ? this.#checkInferredLinks(inferredLinks) : inferredLinks; - if (!uniqueLinks.length) { + if (!newLinks.length) { return; } @@ -223,7 +242,8 @@ class AnnotationLayerBuilder { setLayerDimensions(this.annotationLayer.div, viewport); } - this.annotationLayer.addLinkAnnotations(uniqueLinks, this.linkService); + await this.annotationLayer.addLinkAnnotations(newLinks, this.linkService); + this.div.hidden = false; } #updatePresentationModeState(state) { diff --git a/web/autolinker.js b/web/autolinker.js index 8480a78ab7eb03..09200f46a96a1a 100644 --- a/web/autolinker.js +++ b/web/autolinker.js @@ -75,21 +75,12 @@ function calculateLinkPosition(range, pdfPageView) { } function createLinkAnnotation({ url, index, length }, pdfPageView, id) { - const convertedMatch = pdfPageView._textHighlighter._convertMatches( - [index], - [length] - )[0]; + const highlighter = pdfPageView._textHighlighter; + const [{ begin, end }] = highlighter._convertMatches([index], [length]); const range = new Range(); - range.setStart( - pdfPageView._textHighlighter.textDivs[convertedMatch.begin.divIdx] - .firstChild, - convertedMatch.begin.offset - ); - range.setEnd( - pdfPageView._textHighlighter.textDivs[convertedMatch.end.divIdx].firstChild, - convertedMatch.end.offset - ); + range.setStart(highlighter.textDivs[begin.divIdx].firstChild, begin.offset); + range.setEnd(highlighter.textDivs[end.divIdx].firstChild, end.offset); return { id: `added_link_${id}`, diff --git a/web/pdf_page_view.js b/web/pdf_page_view.js index 6f2ccb0c632be0..9be250b2bc0e6b 100644 --- a/web/pdf_page_view.js +++ b/web/pdf_page_view.js @@ -1112,11 +1112,11 @@ class PDFPageView { await this.#renderAnnotationLayer(); if (this.#enableAutoLinking) { await textLayerPromise; - this.annotationLayer.injectLinkAnnotations( - Autolinker.processLinks(this), - this.viewport, - this.structTreeLayer - ); + this.annotationLayer.injectLinkAnnotations({ + inferredLinks: Autolinker.processLinks(this), + viewport: this.viewport, + structTreeLayer: this.structTreeLayer, + }); } }