@@ -202,15 +202,31 @@ class AnnotationElement {
202202 }
203203
204204 get hasPopupData ( ) {
205- return AnnotationElement . _hasPopupData ( this . data ) ;
205+ return (
206+ AnnotationElement . _hasPopupData ( this . data ) ||
207+ ( this . enableComment && ! ! this . commentText )
208+ ) ;
209+ }
210+
211+ get commentData ( ) {
212+ const { data } = this ;
213+ const editor = this . annotationStorage ?. getEditor ( data . id ) ;
214+ if ( editor ) {
215+ return editor . getData ( ) ;
216+ }
217+ return data ;
206218 }
207219
208220 get hasCommentButton ( ) {
209221 return this . enableComment && this . hasPopupElement ;
210222 }
211223
212224 get commentButtonPosition ( ) {
213- const { quadPoints, rect } = this . data ;
225+ const editor = this . annotationStorage ?. getEditor ( this . data . id ) ;
226+ if ( editor ) {
227+ return editor . commentButtonPositionInPage ;
228+ }
229+ const { quadPoints, inkLists, rect } = this . data ;
214230 let maxX = - Infinity ;
215231 let maxY = - Infinity ;
216232 if ( quadPoints ?. length >= 8 ) {
@@ -224,6 +240,21 @@ class AnnotationElement {
224240 }
225241 return [ maxX , maxY ] ;
226242 }
243+ if ( inkLists ?. length >= 1 ) {
244+ for ( const inkList of inkLists ) {
245+ for ( let i = 0 , ii = inkList . length ; i < ii ; i += 2 ) {
246+ if ( inkList [ i + 1 ] > maxY ) {
247+ maxY = inkList [ i + 1 ] ;
248+ maxX = inkList [ i ] ;
249+ } else if ( inkList [ i + 1 ] === maxY ) {
250+ maxX = Math . max ( maxX , inkList [ i ] ) ;
251+ }
252+ }
253+ }
254+ if ( maxX !== Infinity ) {
255+ return [ maxX , maxY ] ;
256+ }
257+ }
227258 if ( rect ) {
228259 return [ rect [ 2 ] , rect [ 3 ] ] ;
229260 }
@@ -2380,7 +2411,6 @@ class PopupElement {
23802411 this . #dateObj = PDFDateString . toDateObject ( modificationDate ) ;
23812412
23822413 if ( commentManager ) {
2383- this . #popupAbortController = new AbortController ( ) ;
23842414 this . #renderCommentButton( ) ;
23852415 } else {
23862416 this . trigger = elements . flatMap ( e => e . getElementsToTriggerPopup ( ) ) ;
@@ -2457,7 +2487,7 @@ class PopupElement {
24572487 button . ariaHasPopup = "dialog" ;
24582488 button . ariaControls = "commentPopup" ;
24592489
2460- const { signal } = this . #popupAbortController;
2490+ const { signal } = ( this . #popupAbortController = new AbortController ( ) ) ;
24612491 button . addEventListener ( "keydown" , this . #boundKeyDown, { signal } ) ;
24622492 button . addEventListener (
24632493 "click" ,
@@ -2488,19 +2518,26 @@ class PopupElement {
24882518 } ,
24892519 { signal }
24902520 ) ;
2491- const { style } = button ;
2492- style . left = `calc(${ this . #commentButtonPosition[ 0 ] } %)` ;
2493- style . top = `calc(${ this . #commentButtonPosition[ 1 ] } % - var(--comment-button-dim))` ;
2494- if ( this . commentButtonColor ) {
2495- style . backgroundColor = this . commentButtonColor ;
2496- }
2521+ this . #updateColor( ) ;
2522+ this . #updateCommentButtonPosition( ) ;
24972523 parentContainer . after ( button ) ;
24982524 }
24992525
2526+ #updateCommentButtonPosition( ) {
2527+ this . #renderCommentButton( ) ;
2528+ const [ x , y ] = this . #commentButtonPosition;
2529+ const { style } = this . #commentButton;
2530+ style . left = `calc(${ x } %)` ;
2531+ style . top = `calc(${ y } % - var(--comment-button-dim))` ;
2532+ }
2533+
2534+ #updateColor( ) {
2535+ this . #renderCommentButton( ) ;
2536+ this . #commentButton. style . backgroundColor = this . commentButtonColor || "" ;
2537+ }
2538+
25002539 get commentButtonColor ( ) {
2501- const {
2502- data : { color, opacity } ,
2503- } = this . #firstElement;
2540+ const { color, opacity } = this . #firstElement. commentData ;
25042541 if ( ! color ) {
25052542 return null ;
25062543 }
@@ -2509,7 +2546,7 @@ class PopupElement {
25092546
25102547 getData ( ) {
25112548 const { richText, color, opacity, creationDate, modificationDate } =
2512- this . #firstElement. data ;
2549+ this . #firstElement. commentData ;
25132550 return {
25142551 contentsObj : { str : this . comment } ,
25152552 richText,
@@ -2743,7 +2780,22 @@ class PopupElement {
27432780
27442781 updateEdited ( { rect, popup, deleted } ) {
27452782 if ( this . #commentManager) {
2746- this . #commentText = deleted ? null : popup . text ;
2783+ if ( deleted ) {
2784+ this . remove ( ) ;
2785+ this . #commentText = null ;
2786+ } else if ( popup ) {
2787+ if ( popup . deleted ) {
2788+ this . remove ( ) ;
2789+ } else {
2790+ this . #updateColor( ) ;
2791+ this . #commentText = popup . text ;
2792+ }
2793+ }
2794+ if ( rect ) {
2795+ this . #commentButtonPosition = null ;
2796+ this . #setCommentButtonPosition( ) ;
2797+ this . #updateCommentButtonPosition( ) ;
2798+ }
27472799 return ;
27482800 }
27492801 if ( deleted || popup ?. deleted ) {
@@ -2758,7 +2810,7 @@ class PopupElement {
27582810 if ( rect ) {
27592811 this . #position = null ;
27602812 }
2761- if ( popup ) {
2813+ if ( popup && popup . text ) {
27622814 this . #richText = this . #makePopupContent( popup . text ) ;
27632815 this . #dateObj = PDFDateString . toDateObject ( popup . date ) ;
27642816 this . #contentsObj = null ;
@@ -3346,31 +3398,6 @@ class InkAnnotationElement extends AnnotationElement {
33463398 addHighlightArea ( ) {
33473399 this . container . classList . add ( "highlightArea" ) ;
33483400 }
3349-
3350- get commentButtonPosition ( ) {
3351- const { inkLists, rect } = this . data ;
3352- if ( inkLists ?. length >= 1 ) {
3353- let maxX = - Infinity ;
3354- let maxY = - Infinity ;
3355- for ( const inkList of inkLists ) {
3356- for ( let i = 0 , ii = inkList . length ; i < ii ; i += 2 ) {
3357- if ( inkList [ i + 1 ] > maxY ) {
3358- maxY = inkList [ i + 1 ] ;
3359- maxX = inkList [ i ] ;
3360- } else if ( inkList [ i + 1 ] === maxY ) {
3361- maxX = Math . max ( maxX , inkList [ i ] ) ;
3362- }
3363- }
3364- }
3365- if ( maxX !== Infinity ) {
3366- return [ maxX , maxY ] ;
3367- }
3368- }
3369- if ( rect ) {
3370- return [ rect [ 2 ] , rect [ 3 ] ] ;
3371- }
3372- return null ;
3373- }
33743401}
33753402
33763403class HighlightAnnotationElement extends AnnotationElement {
0 commit comments