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".
This commit is contained in:
Jonas Jenwald 2022-12-14 13:19:41 +01:00
parent 5df341ed7e
commit 8ac94d6519

View File

@ -39,6 +39,7 @@ import {
PixelsPerInch, PixelsPerInch,
RenderingCancelledException, RenderingCancelledException,
setLayerDimensions, setLayerDimensions,
shadow,
SVGGraphics, SVGGraphics,
} from "pdfjs-lib"; } from "pdfjs-lib";
import { import {
@ -143,11 +144,7 @@ class PDFPageView {
this.annotationLayerFactory = options.annotationLayerFactory; this.annotationLayerFactory = options.annotationLayerFactory;
this.annotationEditorLayerFactory = options.annotationEditorLayerFactory; this.annotationEditorLayerFactory = options.annotationEditorLayerFactory;
this.xfaLayerFactory = options.xfaLayerFactory; this.xfaLayerFactory = options.xfaLayerFactory;
this.textHighlighter = this._textHighlighterFactory = options.textHighlighterFactory;
options.textHighlighterFactory?.createTextHighlighter({
pageIndex: this.id - 1,
eventBus: this.eventBus,
});
this.structTreeLayerFactory = options.structTreeLayerFactory; this.structTreeLayerFactory = options.structTreeLayerFactory;
if ( if (
typeof PDFJSDev === "undefined" || typeof PDFJSDev === "undefined" ||
@ -247,6 +244,17 @@ class PDFPageView {
this.pdfPage?.cleanup(); this.pdfPage?.cleanup();
} }
get _textHighlighter() {
return shadow(
this,
"_textHighlighter",
this._textHighlighterFactory?.createTextHighlighter({
pageIndex: this.id - 1,
eventBus: this.eventBus,
})
);
}
async #renderAnnotationLayer() { async #renderAnnotationLayer() {
let error = null; let error = null;
try { try {
@ -283,7 +291,7 @@ class PDFPageView {
let error = null; let error = null;
try { try {
const result = await this.xfaLayer.render(this.viewport, "display"); const result = await this.xfaLayer.render(this.viewport, "display");
if (result?.textDivs && this.textHighlighter) { if (result?.textDivs && this._textHighlighter) {
this.#buildXfaTextContentItems(result.textDivs); this.#buildXfaTextContentItems(result.textDivs);
} }
} catch (ex) { } catch (ex) {
@ -362,8 +370,8 @@ class PDFPageView {
for (const item of text.items) { for (const item of text.items) {
items.push(item.str); items.push(item.str);
} }
this.textHighlighter.setTextMapping(textDivs, items); this._textHighlighter.setTextMapping(textDivs, items);
this.textHighlighter.enable(); this._textHighlighter.enable();
} }
/** /**
@ -630,7 +638,7 @@ class PDFPageView {
if (this.xfaLayer && (!keepXfaLayer || !this.xfaLayer.div)) { if (this.xfaLayer && (!keepXfaLayer || !this.xfaLayer.div)) {
this.xfaLayer.cancel(); this.xfaLayer.cancel();
this.xfaLayer = null; this.xfaLayer = null;
this.textHighlighter?.disable(); this._textHighlighter?.disable();
} }
} }
@ -744,7 +752,7 @@ class PDFPageView {
this._accessibilityManager ||= new TextAccessibilityManager(); this._accessibilityManager ||= new TextAccessibilityManager();
this.textLayer = this.textLayerFactory.createTextLayerBuilder({ this.textLayer = this.textLayerFactory.createTextLayerBuilder({
highlighter: this.textHighlighter, highlighter: this._textHighlighter,
accessibilityManager: this._accessibilityManager, accessibilityManager: this._accessibilityManager,
isOffscreenCanvasSupported: this.isOffscreenCanvasSupported, isOffscreenCanvasSupported: this.isOffscreenCanvasSupported,
}); });