Skip to content

Commit

Permalink
simplify fetching textContents
Browse files Browse the repository at this point in the history
  • Loading branch information
ryzokuken committed Dec 16, 2024
1 parent a20d5d9 commit 50ad6b1
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 13 deletions.
6 changes: 4 additions & 2 deletions web/autolinker.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,10 @@ class Autolinker {
return linkAnnotations;
}

static processLinks(pdfPageView, textContent) {
const [text, diffs] = normalize(textContent.join(""));
static processLinks(pdfPageView) {
const [text, diffs] = normalize(
pdfPageView._textHighlighter.textContentItemsStr.join("")
);
const matches = text.matchAll(Autolinker.#urlRegex);
const links = [];
for (const match of matches) {
Expand Down
13 changes: 5 additions & 8 deletions web/pdf_page_view.js
Original file line number Diff line number Diff line change
Expand Up @@ -463,24 +463,22 @@ class PDFPageView {

async #renderTextLayer() {
if (!this.textLayer) {
return [];
return;
}

let error = null;
let textContent;
try {
textContent = await this.textLayer.render(this.viewport);
await this.textLayer.render(this.viewport);
} catch (ex) {
if (ex instanceof AbortException) {
return [];
return;
}
console.error("#renderTextLayer:", ex);
error = ex;
}
this.#dispatchLayerRendered("textlayerrendered", error);

this.#renderStructTreeLayer();
return textContent;
}

/**
Expand Down Expand Up @@ -1098,10 +1096,9 @@ class PDFPageView {
const textLayerP = this.#renderTextLayer();

if (this.annotationLayer) {
await textLayerP;
if (this.#enableAutolinking) {
const textContent = await textLayerP;
this.#linkAnnotations = Autolinker.processLinks(this, textContent);
await textLayerP;
this.#linkAnnotations = Autolinker.processLinks(this);
}
await this.#renderAnnotationLayer();
}
Expand Down
4 changes: 1 addition & 3 deletions web/text_layer_builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ class TextLayerBuilder {
* Renders the text layer.
* @param {PageViewport} viewport
* @param {Object} [textContentParams]
* @returns {Array<string>}
*/
async render(viewport, textContentParams = null) {
if (this.#renderingDone && this.#textLayer) {
Expand All @@ -81,7 +80,7 @@ class TextLayerBuilder {
onBefore: this.hide.bind(this),
});
this.show();
return [];
return;
}

this.cancel();
Expand Down Expand Up @@ -113,7 +112,6 @@ class TextLayerBuilder {
this.#onAppend?.(this.div);
this.highlighter?.enable();
this.accessibilityManager?.enable();
return textContentItemsStr;
}

hide() {
Expand Down

0 comments on commit 50ad6b1

Please sign in to comment.