From 8ac94d65199d4e3fd6050b0470ad292cd4b4fa3e Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Wed, 14 Dec 2022 13:19:41 +0100 Subject: [PATCH] Initialize the `TextHighlighter`-instance lazily in `PDFPageView` Depending on e.g. the `textLayerMode` option it might not actually be necessary to always initialize this eagerly. *Please note:* Unfortunately we cannot `shadow` a private field, hence why this is only made semi-"private". --- web/pdf_page_view.js | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/web/pdf_page_view.js b/web/pdf_page_view.js index f9c8a5e12..ab68c7ea2 100644 --- a/web/pdf_page_view.js +++ b/web/pdf_page_view.js @@ -39,6 +39,7 @@ import { PixelsPerInch, RenderingCancelledException, setLayerDimensions, + shadow, SVGGraphics, } from "pdfjs-lib"; import { @@ -143,11 +144,7 @@ class PDFPageView { this.annotationLayerFactory = options.annotationLayerFactory; this.annotationEditorLayerFactory = options.annotationEditorLayerFactory; this.xfaLayerFactory = options.xfaLayerFactory; - this.textHighlighter = - options.textHighlighterFactory?.createTextHighlighter({ - pageIndex: this.id - 1, - eventBus: this.eventBus, - }); + this._textHighlighterFactory = options.textHighlighterFactory; this.structTreeLayerFactory = options.structTreeLayerFactory; if ( typeof PDFJSDev === "undefined" || @@ -247,6 +244,17 @@ class PDFPageView { this.pdfPage?.cleanup(); } + get _textHighlighter() { + return shadow( + this, + "_textHighlighter", + this._textHighlighterFactory?.createTextHighlighter({ + pageIndex: this.id - 1, + eventBus: this.eventBus, + }) + ); + } + async #renderAnnotationLayer() { let error = null; try { @@ -283,7 +291,7 @@ class PDFPageView { let error = null; try { const result = await this.xfaLayer.render(this.viewport, "display"); - if (result?.textDivs && this.textHighlighter) { + if (result?.textDivs && this._textHighlighter) { this.#buildXfaTextContentItems(result.textDivs); } } catch (ex) { @@ -362,8 +370,8 @@ class PDFPageView { for (const item of text.items) { items.push(item.str); } - this.textHighlighter.setTextMapping(textDivs, items); - this.textHighlighter.enable(); + this._textHighlighter.setTextMapping(textDivs, items); + this._textHighlighter.enable(); } /** @@ -630,7 +638,7 @@ class PDFPageView { if (this.xfaLayer && (!keepXfaLayer || !this.xfaLayer.div)) { this.xfaLayer.cancel(); this.xfaLayer = null; - this.textHighlighter?.disable(); + this._textHighlighter?.disable(); } } @@ -744,7 +752,7 @@ class PDFPageView { this._accessibilityManager ||= new TextAccessibilityManager(); this.textLayer = this.textLayerFactory.createTextLayerBuilder({ - highlighter: this.textHighlighter, + highlighter: this._textHighlighter, accessibilityManager: this._accessibilityManager, isOffscreenCanvasSupported: this.isOffscreenCanvasSupported, });