Skip to content

Commit

Permalink
make processLinks sync by using textContent from textLayer
Browse files Browse the repository at this point in the history
  • Loading branch information
ryzokuken committed Dec 10, 2024
1 parent 654ed71 commit f3737b5
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 34 deletions.
41 changes: 18 additions & 23 deletions web/autolinker.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import { createValidAbsoluteUrl, Util } from "../src/shared/util.js";
import {
getOriginalIndex,
normalizedTextContent,
} from "./pdf_find_controller.js";
import { getOriginalIndex, normalize } from "./pdf_find_controller.js";

class Autolinker {
static #urlRegex =
Expand Down Expand Up @@ -71,25 +68,23 @@ class Autolinker {
return linkAnnotations;
}

static processLinks(pdfPageView) {
return pdfPageView.pdfPage.getTextContent().then(content => {
const [text, diffs] = normalizedTextContent(content);
const matches = text.matchAll(Autolinker.#urlRegex);
return Array.from(matches, match => {
const url = createValidAbsoluteUrl(match[0]);
if (url) {
const [index, length] = getOriginalIndex(
diffs,
match.index,
match[0].length
);
return this.#addLinkAnnotations(url.href, index, length, pdfPageView);
}
return url;
})
.filter(annotation => annotation !== null)
.flat();
});
static processLinks(pdfPageView, textContent) {
const [text, diffs] = normalize(textContent.join(""));
const matches = text.matchAll(Autolinker.#urlRegex);
return Array.from(matches, match => {
const url = createValidAbsoluteUrl(match[0]);
if (url) {
const [index, length] = getOriginalIndex(
diffs,
match.index,
match[0].length
);
return this.#addLinkAnnotations(url.href, index, length, pdfPageView);
}
return url;
})
.filter(annotation => annotation !== null)
.flat();
}
}

Expand Down
7 changes: 1 addition & 6 deletions web/pdf_find_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -1175,9 +1175,4 @@ class PDFFindController {
}
}

export {
FindState,
getOriginalIndex,
normalizedTextContent,
PDFFindController,
};
export { FindState, getOriginalIndex, normalize, PDFFindController };
11 changes: 7 additions & 4 deletions web/pdf_page_view.js
Original file line number Diff line number Diff line change
Expand Up @@ -463,22 +463,24 @@ class PDFPageView {

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

let error = null;
let textContent;
try {
await this.textLayer.render(this.viewport);
textContent = 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,7 +1100,8 @@ class PDFPageView {
if (this.annotationLayer) {
await textLayerP;
if (this.#enableAutolinking) {
this.#linkAnnotations = await Autolinker.processLinks(this);
const textContent = await textLayerP;
this.#linkAnnotations = Autolinker.processLinks(this, textContent);
}
await this.#renderAnnotationLayer();
}
Expand Down
4 changes: 3 additions & 1 deletion web/text_layer_builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ 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 @@ -80,7 +81,7 @@ class TextLayerBuilder {
onBefore: this.hide.bind(this),
});
this.show();
return;
return [];
}

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

hide() {
Expand Down

0 comments on commit f3737b5

Please sign in to comment.