Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit f3737b5

Browse files
committedDec 10, 2024·
make processLinks sync by using textContent from textLayer
1 parent 654ed71 commit f3737b5

File tree

4 files changed

+29
-34
lines changed

4 files changed

+29
-34
lines changed
 

‎web/autolinker.js

+18-23
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
import { createValidAbsoluteUrl, Util } from "../src/shared/util.js";
2-
import {
3-
getOriginalIndex,
4-
normalizedTextContent,
5-
} from "./pdf_find_controller.js";
2+
import { getOriginalIndex, normalize } from "./pdf_find_controller.js";
63

74
class Autolinker {
85
static #urlRegex =
@@ -71,25 +68,23 @@ class Autolinker {
7168
return linkAnnotations;
7269
}
7370

74-
static processLinks(pdfPageView) {
75-
return pdfPageView.pdfPage.getTextContent().then(content => {
76-
const [text, diffs] = normalizedTextContent(content);
77-
const matches = text.matchAll(Autolinker.#urlRegex);
78-
return Array.from(matches, match => {
79-
const url = createValidAbsoluteUrl(match[0]);
80-
if (url) {
81-
const [index, length] = getOriginalIndex(
82-
diffs,
83-
match.index,
84-
match[0].length
85-
);
86-
return this.#addLinkAnnotations(url.href, index, length, pdfPageView);
87-
}
88-
return url;
89-
})
90-
.filter(annotation => annotation !== null)
91-
.flat();
92-
});
71+
static processLinks(pdfPageView, textContent) {
72+
const [text, diffs] = normalize(textContent.join(""));
73+
const matches = text.matchAll(Autolinker.#urlRegex);
74+
return Array.from(matches, match => {
75+
const url = createValidAbsoluteUrl(match[0]);
76+
if (url) {
77+
const [index, length] = getOriginalIndex(
78+
diffs,
79+
match.index,
80+
match[0].length
81+
);
82+
return this.#addLinkAnnotations(url.href, index, length, pdfPageView);
83+
}
84+
return url;
85+
})
86+
.filter(annotation => annotation !== null)
87+
.flat();
9388
}
9489
}
9590

‎web/pdf_find_controller.js

+1-6
Original file line numberDiff line numberDiff line change
@@ -1175,9 +1175,4 @@ class PDFFindController {
11751175
}
11761176
}
11771177

1178-
export {
1179-
FindState,
1180-
getOriginalIndex,
1181-
normalizedTextContent,
1182-
PDFFindController,
1183-
};
1178+
export { FindState, getOriginalIndex, normalize, PDFFindController };

‎web/pdf_page_view.js

+7-4
Original file line numberDiff line numberDiff line change
@@ -463,22 +463,24 @@ class PDFPageView {
463463

464464
async #renderTextLayer() {
465465
if (!this.textLayer) {
466-
return;
466+
return [];
467467
}
468468

469469
let error = null;
470+
let textContent;
470471
try {
471-
await this.textLayer.render(this.viewport);
472+
textContent = await this.textLayer.render(this.viewport);
472473
} catch (ex) {
473474
if (ex instanceof AbortException) {
474-
return;
475+
return [];
475476
}
476477
console.error("#renderTextLayer:", ex);
477478
error = ex;
478479
}
479480
this.#dispatchLayerRendered("textlayerrendered", error);
480481

481482
this.#renderStructTreeLayer();
483+
return textContent;
482484
}
483485

484486
/**
@@ -1098,7 +1100,8 @@ class PDFPageView {
10981100
if (this.annotationLayer) {
10991101
await textLayerP;
11001102
if (this.#enableAutolinking) {
1101-
this.#linkAnnotations = await Autolinker.processLinks(this);
1103+
const textContent = await textLayerP;
1104+
this.#linkAnnotations = Autolinker.processLinks(this, textContent);
11021105
}
11031106
await this.#renderAnnotationLayer();
11041107
}

‎web/text_layer_builder.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ class TextLayerBuilder {
7272
* Renders the text layer.
7373
* @param {PageViewport} viewport
7474
* @param {Object} [textContentParams]
75+
* @returns {Array<string>}
7576
*/
7677
async render(viewport, textContentParams = null) {
7778
if (this.#renderingDone && this.#textLayer) {
@@ -80,7 +81,7 @@ class TextLayerBuilder {
8081
onBefore: this.hide.bind(this),
8182
});
8283
this.show();
83-
return;
84+
return [];
8485
}
8586

8687
this.cancel();
@@ -112,6 +113,7 @@ class TextLayerBuilder {
112113
this.#onAppend?.(this.div);
113114
this.highlighter?.enable();
114115
this.accessibilityManager?.enable();
116+
return textContentItemsStr;
115117
}
116118

117119
hide() {

0 commit comments

Comments
 (0)
Please sign in to comment.