Merge pull request #20928 from calixteman/pass_global_signal_text_layer

Pass the global signal the text layer builder in order to remove all the listeners defined here
This commit is contained in:
calixteman 2026-03-20 18:36:25 +01:00 committed by GitHub
commit 977e4f2c4a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 45 additions and 16 deletions

View File

@ -108,6 +108,7 @@ import { XfaLayerBuilder } from "./xfa_layer_builder.js";
* text that look like URLs. The default value is `true`. * text that look like URLs. The default value is `true`.
* @property {CommentManager} [commentManager] - The comment manager instance. * @property {CommentManager} [commentManager] - The comment manager instance.
* to. * to.
* @property {AbortSignal} [abortSignal]
*/ */
const DEFAULT_LAYER_PROPERTIES = const DEFAULT_LAYER_PROPERTIES =
@ -135,6 +136,8 @@ const LAYERS_ORDER = new Map([
]); ]);
class PDFPageView extends BasePDFPageView { class PDFPageView extends BasePDFPageView {
#abortSignal = null;
#annotationMode = AnnotationMode.ENABLE_FORMS; #annotationMode = AnnotationMode.ENABLE_FORMS;
#canvasWrapper = null; #canvasWrapper = null;
@ -181,6 +184,7 @@ class PDFPageView extends BasePDFPageView {
this.renderingId = "page" + this.id; this.renderingId = "page" + this.id;
this.#layerProperties = options.layerProperties || DEFAULT_LAYER_PROPERTIES; this.#layerProperties = options.layerProperties || DEFAULT_LAYER_PROPERTIES;
this.#abortSignal = options.abortSignal || null;
this.pdfPage = null; this.pdfPage = null;
this.pageLabel = null; this.pageLabel = null;
@ -285,6 +289,7 @@ class PDFPageView extends BasePDFPageView {
defaultViewport: this.viewport, defaultViewport: this.viewport,
id, id,
layerProperties: this.#layerProperties, layerProperties: this.#layerProperties,
abortSignal: this.#abortSignal,
scale: this.scale, scale: this.scale,
optionalContentConfigPromise: this._optionalContentConfigPromise, optionalContentConfigPromise: this._optionalContentConfigPromise,
textLayerMode: this.#textLayerMode, textLayerMode: this.#textLayerMode,
@ -1056,6 +1061,7 @@ class PDFPageView extends BasePDFPageView {
this.#addLayer(textLayerDiv, "textLayer"); this.#addLayer(textLayerDiv, "textLayer");
this.l10n.resume(); this.l10n.resume();
}, },
abortSignal: this.#abortSignal,
}); });
} }

View File

@ -253,6 +253,8 @@ class PDFViewer {
#enableAutoLinking = true; #enableAutoLinking = true;
#abortSignal = null;
#eventAbortController = null; #eventAbortController = null;
#minDurationToUpdateCanvas = 0; #minDurationToUpdateCanvas = 0;
@ -385,6 +387,7 @@ class PDFViewer {
} }
const { abortSignal } = options; const { abortSignal } = options;
this.#abortSignal = abortSignal || null;
abortSignal?.addEventListener( abortSignal?.addEventListener(
"abort", "abort",
() => { () => {
@ -1068,6 +1071,7 @@ class PDFViewer {
enableAutoLinking: this.#enableAutoLinking, enableAutoLinking: this.#enableAutoLinking,
minDurationToUpdateCanvas: this.#minDurationToUpdateCanvas, minDurationToUpdateCanvas: this.#minDurationToUpdateCanvas,
commentManager: this.#commentManager, commentManager: this.#commentManager,
abortSignal: this.#abortSignal,
}); });
this._pages.push(pageView); this._pages.push(pageView);
} }

View File

@ -33,6 +33,7 @@ import { removeNullCharacters } from "./ui_utils.js";
* @property {TextAccessibilityManager} [accessibilityManager] * @property {TextAccessibilityManager} [accessibilityManager]
* @property {boolean} [enablePermissions] * @property {boolean} [enablePermissions]
* @property {function} [onAppend] * @property {function} [onAppend]
* @property {AbortSignal} [abortSignal]
*/ */
/** /**
@ -48,6 +49,8 @@ import { removeNullCharacters } from "./ui_utils.js";
* contain text that matches the PDF text they are overlaying. * contain text that matches the PDF text they are overlaying.
*/ */
class TextLayerBuilder { class TextLayerBuilder {
#abortSignal = null;
#enablePermissions = false; #enablePermissions = false;
#onAppend = null; #onAppend = null;
@ -69,12 +72,14 @@ class TextLayerBuilder {
accessibilityManager = null, accessibilityManager = null,
enablePermissions = false, enablePermissions = false,
onAppend = null, onAppend = null,
abortSignal = null,
}) { }) {
this.pdfPage = pdfPage; this.pdfPage = pdfPage;
this.highlighter = highlighter; this.highlighter = highlighter;
this.accessibilityManager = accessibilityManager; this.accessibilityManager = accessibilityManager;
this.#enablePermissions = enablePermissions === true; this.#enablePermissions = enablePermissions === true;
this.#onAppend = onAppend; this.#onAppend = onAppend;
this.#abortSignal = abortSignal;
this.div = document.createElement("div"); this.div = document.createElement("div");
this.div.tabIndex = 0; this.div.tabIndex = 0;
@ -163,24 +168,33 @@ class TextLayerBuilder {
*/ */
#bindMouse(end) { #bindMouse(end) {
const { div } = this; const { div } = this;
const abortSignal = this.#abortSignal;
div.addEventListener("mousedown", () => { div.addEventListener(
div.classList.add("selecting"); "mousedown",
}); () => {
div.classList.add("selecting");
},
{ signal: abortSignal }
);
div.addEventListener("copy", event => { div.addEventListener(
if (!this.#enablePermissions) { "copy",
const selection = document.getSelection(); event => {
event.clipboardData.setData( if (!this.#enablePermissions) {
"text/plain", const selection = document.getSelection();
removeNullCharacters(normalizeUnicode(selection.toString())) event.clipboardData.setData(
); "text/plain",
} removeNullCharacters(normalizeUnicode(selection.toString()))
stopEvent(event); );
}); }
stopEvent(event);
},
{ signal: abortSignal }
);
TextLayerBuilder.#textLayers.set(div, end); TextLayerBuilder.#textLayers.set(div, end);
TextLayerBuilder.#enableGlobalSelectionListener(); TextLayerBuilder.#enableGlobalSelectionListener(abortSignal);
} }
static #removeGlobalSelectionListener(textLayerDiv) { static #removeGlobalSelectionListener(textLayerDiv) {
@ -192,13 +206,18 @@ class TextLayerBuilder {
} }
} }
static #enableGlobalSelectionListener() { static #enableGlobalSelectionListener(globalAbortSignal) {
if (this.#selectionChangeAbortController) { if (this.#selectionChangeAbortController) {
// document-level event listeners already installed // document-level event listeners already installed
return; return;
} }
this.#selectionChangeAbortController = new AbortController(); this.#selectionChangeAbortController = new AbortController();
const { signal } = this.#selectionChangeAbortController; const signal = globalAbortSignal
? AbortSignal.any([
this.#selectionChangeAbortController.signal,
globalAbortSignal,
])
: this.#selectionChangeAbortController.signal;
const reset = (end, textLayer) => { const reset = (end, textLayer) => {
if (typeof PDFJSDev === "undefined" || !PDFJSDev.test("MOZCENTRAL")) { if (typeof PDFJSDev === "undefined" || !PDFJSDev.test("MOZCENTRAL")) {