@@ -2125,6 +2125,8 @@ class PopupElement {
21252125
21262126 #popup = null ;
21272127
2128+ #popupAbortController = null ;
2129+
21282130 #position = null ;
21292131
21302132 #rect = null ;
@@ -2166,18 +2168,7 @@ class PopupElement {
21662168 this . #dateObj = PDFDateString . toDateObject ( modificationDate ) ;
21672169
21682170 this . trigger = elements . flatMap ( e => e . getElementsToTriggerPopup ( ) ) ;
2169- // Attach the event listeners to the trigger element.
2170- for ( const element of this . trigger ) {
2171- element . addEventListener ( "click" , this . #boundToggle) ;
2172- element . addEventListener ( "mouseenter" , this . #boundShow) ;
2173- element . addEventListener ( "mouseleave" , this . #boundHide) ;
2174- element . classList . add ( "popupTriggerArea" ) ;
2175- }
2176-
2177- // Attach the event listener to toggle the popup with the keyboard.
2178- for ( const element of elements ) {
2179- element . container ?. addEventListener ( "keydown" , this . #boundKeyDown) ;
2180- }
2171+ this . #addEventListeners( ) ;
21812172
21822173 this . #container. hidden = true ;
21832174 if ( open ) {
@@ -2195,6 +2186,29 @@ class PopupElement {
21952186 }
21962187 }
21972188
2189+ #addEventListeners( ) {
2190+ if ( this . #popupAbortController) {
2191+ return ;
2192+ }
2193+ this . #popupAbortController = new AbortController ( ) ;
2194+ const { signal } = this . #popupAbortController;
2195+
2196+ // Attach the event listeners to the trigger element.
2197+ for ( const element of this . trigger ) {
2198+ element . addEventListener ( "click" , this . #boundToggle, { signal } ) ;
2199+ element . addEventListener ( "mouseenter" , this . #boundShow, { signal } ) ;
2200+ element . addEventListener ( "mouseleave" , this . #boundHide, { signal } ) ;
2201+ element . classList . add ( "popupTriggerArea" ) ;
2202+ }
2203+
2204+ // Attach the event listener to toggle the popup with the keyboard.
2205+ for ( const element of this . #elements) {
2206+ element . container ?. addEventListener ( "keydown" , this . #boundKeyDown, {
2207+ signal,
2208+ } ) ;
2209+ }
2210+ }
2211+
21982212 render ( ) {
21992213 if ( this . #popup) {
22002214 return ;
@@ -2338,7 +2352,12 @@ class PopupElement {
23382352 }
23392353 }
23402354
2341- updateEdited ( { rect, popupContent } ) {
2355+ updateEdited ( { rect, popupContent, deleted } ) {
2356+ if ( deleted ) {
2357+ this . remove ( ) ;
2358+ return ;
2359+ }
2360+ this . #addEventListeners( ) ;
23422361 this . #updates ||= {
23432362 contentsObj : this . #contentsObj,
23442363 richText : this . #richText,
@@ -2366,6 +2385,18 @@ class PopupElement {
23662385 this . #position = null ;
23672386 }
23682387
2388+ remove ( ) {
2389+ this . #popupAbortController?. abort ( ) ;
2390+ this . #popupAbortController = null ;
2391+ this . #popup?. remove ( ) ;
2392+ this . #popup = null ;
2393+ this . #wasVisible = false ;
2394+ this . #pinned = false ;
2395+ for ( const element of this . trigger ) {
2396+ element . classList . remove ( "popupTriggerArea" ) ;
2397+ }
2398+ }
2399+
23692400 #setPosition( ) {
23702401 if ( this . #position !== null ) {
23712402 return ;
@@ -2465,6 +2496,7 @@ class PopupElement {
24652496 }
24662497
24672498 maybeShow ( ) {
2499+ this . #addEventListeners( ) ;
24682500 if ( ! this . #wasVisible) {
24692501 return ;
24702502 }
0 commit comments