From 8727a04ae588545c8e954c143b9fc64384712461 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Wed, 12 Feb 2025 12:06:34 +0100 Subject: [PATCH] Move the auto-link handling into its own `PDFPageView` helper-method, and dispatch a "linkannotationsadded" event This is similar to a lot of existing functionality for various layers, and the new event might be helpful in e.g. the integration-tests. --- web/pdf_page_view.js | 36 ++++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/web/pdf_page_view.js b/web/pdf_page_view.js index b371efeabb08f..602ddc189c0c2 100644 --- a/web/pdf_page_view.js +++ b/web/pdf_page_view.js @@ -536,6 +536,28 @@ class PDFPageView { this._textHighlighter.enable(); } + async #injectLinkAnnotations(textLayerPromise) { + let error = null; + try { + await textLayerPromise; + + if (!this.annotationLayer) { + return; // Rendering was cancelled while the textLayerPromise resolved. + } + await this.annotationLayer.injectLinkAnnotations({ + inferredLinks: Autolinker.processLinks(this), + viewport: this.viewport, + structTreeLayer: this.structTreeLayer, + }); + } catch (ex) { + console.error("#injectLinkAnnotations:", ex); + error = ex; + } + if (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) { + this.#dispatchLayerRendered("linkannotationsadded", error); + } + } + #resetCanvas() { const { canvas } = this; if (!canvas) { @@ -1122,18 +1144,8 @@ class PDFPageView { if (this.annotationLayer) { await this.#renderAnnotationLayer(); - if (this.#enableAutoLinking) { - try { - await textLayerPromise; - - this.annotationLayer?.injectLinkAnnotations({ - inferredLinks: Autolinker.processLinks(this), - viewport: this.viewport, - structTreeLayer: this.structTreeLayer, - }); - } catch (ex) { - console.error("enableAutoLinking:", ex); - } + if (this.#enableAutoLinking && this.annotationLayer) { + await this.#injectLinkAnnotations(textLayerPromise); } }