@@ -186,7 +186,8 @@ class AnnotationElement {
186186 this . parent = parameters . parent ;
187187
188188 if ( isRenderable ) {
189- this . container = this . _createContainer ( ignoreBorder ) ;
189+ this . contentElement = this . container =
190+ this . _createContainer ( ignoreBorder ) ;
190191 }
191192 if ( createQuadrilaterals ) {
192193 this . _createQuadrilaterals ( ) ;
@@ -1008,6 +1009,7 @@ class LinkAnnotationElement extends AnnotationElement {
10081009
10091010 this . container . classList . add ( "linkAnnotation" ) ;
10101011 if ( isBound ) {
1012+ this . contentElement = link ;
10111013 this . container . append ( link ) ;
10121014 }
10131015
@@ -1517,6 +1519,7 @@ class TextWidgetAnnotationElement extends WidgetAnnotationElement {
15171519 element . hidden = true ;
15181520 }
15191521 GetElementsByNameSet . add ( element ) ;
1522+ this . contentElement = element ;
15201523 element . setAttribute ( "data-element-id" , id ) ;
15211524
15221525 element . disabled = this . data . readOnly ;
@@ -3070,7 +3073,7 @@ class FreeTextAnnotationElement extends AnnotationElement {
30703073 this . container . classList . add ( "freeTextAnnotation" ) ;
30713074
30723075 if ( this . textContent ) {
3073- const content = document . createElement ( "div" ) ;
3076+ const content = ( this . contentElement = document . createElement ( "div" ) ) ;
30743077 content . classList . add ( "annotationTextContent" ) ;
30753078 content . setAttribute ( "role" , "comment" ) ;
30763079 for ( const line of this . textContent ) {
@@ -3787,7 +3790,7 @@ class AnnotationLayer {
37873790 }
37883791
37893792 async #appendElement( element , id , popupElements ) {
3790- const contentElement = element . firstChild || element ;
3793+ const { contentElement, container } = element ;
37913794 const annotationId = ( contentElement . id = `${ AnnotationPrefix } ${ id } ` ) ;
37923795 const ariaAttributes =
37933796 await this . #structTreeLayer?. getAriaAttributes ( annotationId ) ;
@@ -3799,18 +3802,31 @@ class AnnotationLayer {
37993802
38003803 if ( popupElements ) {
38013804 // Set the popup just after the first element associated with the popup.
3802- popupElements . at ( - 1 ) . container . after ( element ) ;
3805+ popupElements . at ( - 1 ) . container . after ( container ) ;
38033806 } else {
3804- this . div . append ( element ) ;
3805- this . #accessibilityManager?. moveElementInDOM (
3806- this . div ,
3807- element ,
3808- contentElement ,
3809- /* isRemovable = */ false
3810- ) ;
3807+ this . #moveElementInDOM( container , contentElement ) ;
38113808 }
38123809 }
38133810
3811+ #moveElementInDOM( container , contentElement ) {
3812+ this . div . append ( container ) ;
3813+ this . #accessibilityManager?. moveElementInDOM (
3814+ this . div ,
3815+ container ,
3816+ contentElement ,
3817+ /* isRemovable = */ false ,
3818+ /* filter = */ node => node . nodeName === "SECTION" ,
3819+ /* inserter = */ ( prevNode , node ) => {
3820+ if ( prevNode . nextElementSibling . nodeName === "BUTTON" ) {
3821+ // In case we have a comment button, insert after the button.
3822+ prevNode . nextElementSibling . after ( node ) ;
3823+ } else {
3824+ prevNode . after ( node ) ;
3825+ }
3826+ }
3827+ ) ;
3828+ }
3829+
38143830 /**
38153831 * Render a new annotation layer with all annotation elements.
38163832 *
@@ -3877,7 +3893,7 @@ class AnnotationLayer {
38773893 if ( data . hidden ) {
38783894 rendered . style . visibility = "hidden" ;
38793895 }
3880- await this . #appendElement( rendered , data . id , elementParams . elements ) ;
3896+ await this . #appendElement( element , data . id , elementParams . elements ) ;
38813897 element . extraPopupElement ?. popup ?. renderCommentButton ( ) ;
38823898
38833899 if ( element . _isEditable ) {
@@ -3913,8 +3929,8 @@ class AnnotationLayer {
39133929 if ( ! element . isRenderable ) {
39143930 continue ;
39153931 }
3916- const rendered = element . render ( ) ;
3917- await this . #appendElement( rendered , data . id , null ) ;
3932+ element . render ( ) ;
3933+ await this . #appendElement( element , data . id , null ) ;
39183934 }
39193935 }
39203936
@@ -3999,18 +4015,32 @@ class AnnotationLayer {
39994015 linkService : this . #linkService,
40004016 annotationStorage : this . #annotationStorage,
40014017 } ) ;
4002- const htmlElement = element . render ( ) ;
4003- div . append ( htmlElement ) ;
4004- this . #accessibilityManager?. moveElementInDOM (
4005- div ,
4006- htmlElement ,
4007- htmlElement ,
4008- /* isRemovable = */ false
4009- ) ;
4018+ const rendered = element . render ( ) ;
4019+ rendered . id = `${ AnnotationPrefix } ${ id } ` ;
4020+ this . #moveElementInDOM( rendered , rendered ) ;
40104021 element . createOrUpdatePopup ( ) ;
40114022 return element ;
40124023 }
40134024
4025+ togglePointerEvents ( enabled = false ) {
4026+ this . div . classList . toggle ( "disabled" , ! enabled ) ;
4027+ }
4028+
4029+ updateFakeAnnotations ( editors ) {
4030+ if ( editors . length === 0 ) {
4031+ return ;
4032+ }
4033+ // In order to ensure that the annotations are correctly moved in the DOM
4034+ // we need to make sure that this has been laid out.
4035+ window . requestAnimationFrame ( ( ) =>
4036+ setTimeout ( ( ) => {
4037+ for ( const editor of editors ) {
4038+ editor . updateFakeAnnotationElement ( this ) ;
4039+ }
4040+ } , 10 )
4041+ ) ;
4042+ }
4043+
40144044 /**
40154045 * @private
40164046 */
0 commit comments