Skip to content

Commit 977e4f2

Browse files
authored
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
2 parents 5a240f7 + e1f02be commit 977e4f2

3 files changed

Lines changed: 45 additions & 16 deletions

File tree

web/pdf_page_view.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ import { XfaLayerBuilder } from "./xfa_layer_builder.js";
108108
* text that look like URLs. The default value is `true`.
109109
* @property {CommentManager} [commentManager] - The comment manager instance.
110110
* to.
111+
* @property {AbortSignal} [abortSignal]
111112
*/
112113

113114
const DEFAULT_LAYER_PROPERTIES =
@@ -135,6 +136,8 @@ const LAYERS_ORDER = new Map([
135136
]);
136137

137138
class PDFPageView extends BasePDFPageView {
139+
#abortSignal = null;
140+
138141
#annotationMode = AnnotationMode.ENABLE_FORMS;
139142

140143
#canvasWrapper = null;
@@ -181,6 +184,7 @@ class PDFPageView extends BasePDFPageView {
181184

182185
this.renderingId = "page" + this.id;
183186
this.#layerProperties = options.layerProperties || DEFAULT_LAYER_PROPERTIES;
187+
this.#abortSignal = options.abortSignal || null;
184188

185189
this.pdfPage = null;
186190
this.pageLabel = null;
@@ -285,6 +289,7 @@ class PDFPageView extends BasePDFPageView {
285289
defaultViewport: this.viewport,
286290
id,
287291
layerProperties: this.#layerProperties,
292+
abortSignal: this.#abortSignal,
288293
scale: this.scale,
289294
optionalContentConfigPromise: this._optionalContentConfigPromise,
290295
textLayerMode: this.#textLayerMode,
@@ -1056,6 +1061,7 @@ class PDFPageView extends BasePDFPageView {
10561061
this.#addLayer(textLayerDiv, "textLayer");
10571062
this.l10n.resume();
10581063
},
1064+
abortSignal: this.#abortSignal,
10591065
});
10601066
}
10611067

web/pdf_viewer.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,8 @@ class PDFViewer {
253253

254254
#enableAutoLinking = true;
255255

256+
#abortSignal = null;
257+
256258
#eventAbortController = null;
257259

258260
#minDurationToUpdateCanvas = 0;
@@ -385,6 +387,7 @@ class PDFViewer {
385387
}
386388

387389
const { abortSignal } = options;
390+
this.#abortSignal = abortSignal || null;
388391
abortSignal?.addEventListener(
389392
"abort",
390393
() => {
@@ -1068,6 +1071,7 @@ class PDFViewer {
10681071
enableAutoLinking: this.#enableAutoLinking,
10691072
minDurationToUpdateCanvas: this.#minDurationToUpdateCanvas,
10701073
commentManager: this.#commentManager,
1074+
abortSignal: this.#abortSignal,
10711075
});
10721076
this._pages.push(pageView);
10731077
}

web/text_layer_builder.js

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import { removeNullCharacters } from "./ui_utils.js";
3333
* @property {TextAccessibilityManager} [accessibilityManager]
3434
* @property {boolean} [enablePermissions]
3535
* @property {function} [onAppend]
36+
* @property {AbortSignal} [abortSignal]
3637
*/
3738

3839
/**
@@ -48,6 +49,8 @@ import { removeNullCharacters } from "./ui_utils.js";
4849
* contain text that matches the PDF text they are overlaying.
4950
*/
5051
class TextLayerBuilder {
52+
#abortSignal = null;
53+
5154
#enablePermissions = false;
5255

5356
#onAppend = null;
@@ -69,12 +72,14 @@ class TextLayerBuilder {
6972
accessibilityManager = null,
7073
enablePermissions = false,
7174
onAppend = null,
75+
abortSignal = null,
7276
}) {
7377
this.pdfPage = pdfPage;
7478
this.highlighter = highlighter;
7579
this.accessibilityManager = accessibilityManager;
7680
this.#enablePermissions = enablePermissions === true;
7781
this.#onAppend = onAppend;
82+
this.#abortSignal = abortSignal;
7883

7984
this.div = document.createElement("div");
8085
this.div.tabIndex = 0;
@@ -163,24 +168,33 @@ class TextLayerBuilder {
163168
*/
164169
#bindMouse(end) {
165170
const { div } = this;
171+
const abortSignal = this.#abortSignal;
166172

167-
div.addEventListener("mousedown", () => {
168-
div.classList.add("selecting");
169-
});
173+
div.addEventListener(
174+
"mousedown",
175+
() => {
176+
div.classList.add("selecting");
177+
},
178+
{ signal: abortSignal }
179+
);
170180

171-
div.addEventListener("copy", event => {
172-
if (!this.#enablePermissions) {
173-
const selection = document.getSelection();
174-
event.clipboardData.setData(
175-
"text/plain",
176-
removeNullCharacters(normalizeUnicode(selection.toString()))
177-
);
178-
}
179-
stopEvent(event);
180-
});
181+
div.addEventListener(
182+
"copy",
183+
event => {
184+
if (!this.#enablePermissions) {
185+
const selection = document.getSelection();
186+
event.clipboardData.setData(
187+
"text/plain",
188+
removeNullCharacters(normalizeUnicode(selection.toString()))
189+
);
190+
}
191+
stopEvent(event);
192+
},
193+
{ signal: abortSignal }
194+
);
181195

182196
TextLayerBuilder.#textLayers.set(div, end);
183-
TextLayerBuilder.#enableGlobalSelectionListener();
197+
TextLayerBuilder.#enableGlobalSelectionListener(abortSignal);
184198
}
185199

186200
static #removeGlobalSelectionListener(textLayerDiv) {
@@ -192,13 +206,18 @@ class TextLayerBuilder {
192206
}
193207
}
194208

195-
static #enableGlobalSelectionListener() {
209+
static #enableGlobalSelectionListener(globalAbortSignal) {
196210
if (this.#selectionChangeAbortController) {
197211
// document-level event listeners already installed
198212
return;
199213
}
200214
this.#selectionChangeAbortController = new AbortController();
201-
const { signal } = this.#selectionChangeAbortController;
215+
const signal = globalAbortSignal
216+
? AbortSignal.any([
217+
this.#selectionChangeAbortController.signal,
218+
globalAbortSignal,
219+
])
220+
: this.#selectionChangeAbortController.signal;
202221

203222
const reset = (end, textLayer) => {
204223
if (typeof PDFJSDev === "undefined" || !PDFJSDev.test("MOZCENTRAL")) {

0 commit comments

Comments
 (0)