Merge pull request #21396 from Snuffleupagus/injectLinkAnnotations-move-call

Prevent intermittent issues when invoking the `PDFPageView.prototype.#injectLinkAnnotations` method
This commit is contained in:
Tim van der Meij 2026-06-07 14:42:14 +02:00 committed by GitHub
commit d9eea18876
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -461,7 +461,9 @@ class PDFPageView extends BasePDFPageView {
});
}
async #renderAnnotationLayer() {
async #renderAnnotationLayer(textLayerPromise = null) {
const { annotationLayer, textLayer } = this;
let error = null;
try {
await this.annotationLayer.render({
@ -476,6 +478,10 @@ class PDFPageView extends BasePDFPageView {
} finally {
this.#dispatchLayerRendered("annotationlayerrendered", error);
}
if (this.#enableAutoLinking && textLayerPromise) {
this.#injectLinkAnnotations(textLayerPromise, annotationLayer, textLayer);
}
}
async #renderAnnotationEditorLayer() {
@ -532,9 +538,6 @@ class PDFPageView extends BasePDFPageView {
}
async #renderTextLayer() {
if (!this.textLayer) {
return;
}
let error = null;
try {
await this.textLayer.render({
@ -568,10 +571,6 @@ class PDFPageView extends BasePDFPageView {
* aria-owns to work.
*/
async #renderStructTreeLayer() {
if (!this.textLayer) {
return;
}
const treeDom = await this.structTreeLayer?.render();
if (treeDom) {
this.l10n.pause();
@ -595,24 +594,25 @@ class PDFPageView extends BasePDFPageView {
this._textHighlighter.enable();
}
async #injectLinkAnnotations(textLayerPromise) {
async #injectLinkAnnotations(textLayerPromise, annotationLayer, textLayer) {
let error = null;
try {
await textLayerPromise;
if (!this.annotationLayer) {
if (
annotationLayer !== this.annotationLayer ||
textLayer !== this.textLayer
) {
return; // Rendering was cancelled while the textLayerPromise resolved.
}
await this.annotationLayer.injectLinkAnnotations(
await annotationLayer.injectLinkAnnotations(
Autolinker.processLinks(this)
);
} catch (ex) {
console.error("#injectLinkAnnotations:", ex);
error = ex;
}
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) {
this.#dispatchLayerRendered("linkannotationsadded", error);
}
this.#dispatchLayerRendered("linkannotationsadded", error);
}
_resetCanvas() {
@ -1192,14 +1192,10 @@ class PDFPageView extends BasePDFPageView {
viewport.rawDims
);
const textLayerPromise = this.#renderTextLayer();
const textLayerPromise = this.textLayer ? this.#renderTextLayer() : null;
if (this.annotationLayer) {
await this.#renderAnnotationLayer();
if (this.#enableAutoLinking && this.annotationLayer && this.textLayer) {
await this.#injectLinkAnnotations(textLayerPromise);
}
await this.#renderAnnotationLayer(textLayerPromise);
}
this.drawLayer ||= new DrawLayerBuilder({
@ -1216,7 +1212,6 @@ class PDFPageView extends BasePDFPageView {
if (!annotationEditorUIManager) {
return;
}
if (
this.annotationLayer ||
this.#annotationMode === AnnotationMode.DISABLE