@@ -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 */
5051class 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