1313 * limitations under the License.
1414 */
1515
16- import { noContextMenu } from "../display_utils.js" ;
16+ import { noContextMenu , stopEvent } from "../display_utils.js" ;
1717
1818class Comment {
1919 #commentStandaloneButton = null ;
@@ -34,6 +34,8 @@ class Comment {
3434
3535 #deleted = false ;
3636
37+ #popupPosition = null ;
38+
3739 constructor ( editor ) {
3840 this . #editor = editor ;
3941 }
@@ -42,7 +44,7 @@ class Comment {
4244 const button = ( this . #commentToolbarButton =
4345 document . createElement ( "button" ) ) ;
4446 button . className = "comment" ;
45- return this . #render( button ) ;
47+ return this . #render( button , false ) ;
4648 }
4749
4850 renderForStandalone ( ) {
@@ -66,23 +68,118 @@ class Comment {
6668 }
6769 }
6870
69- return this . #render( button ) ;
71+ return this . #render( button , true ) ;
72+ }
73+
74+ onUpdatedColor ( ) {
75+ if ( ! this . #commentStandaloneButton) {
76+ return ;
77+ }
78+ const color = this . #editor. commentButtonColor ;
79+ if ( color ) {
80+ this . #commentStandaloneButton. style . backgroundColor = color ;
81+ }
82+ this . #editor. _uiManager . updatePopupColor ( this . #editor) ;
83+ }
84+
85+ get commentButtonWidth ( ) {
86+ return (
87+ ( this . #commentStandaloneButton?. getBoundingClientRect ( ) . width ?? 0 ) /
88+ this . #editor. parent . boundingClientRect . width
89+ ) ;
7090 }
7191
72- #render( comment ) {
92+ get commentPopupPositionInLayer ( ) {
93+ if ( this . #popupPosition) {
94+ return this . #popupPosition;
95+ }
96+ if ( ! this . #commentStandaloneButton) {
97+ return null ;
98+ }
99+ const { x, y, height } =
100+ this . #commentStandaloneButton. getBoundingClientRect ( ) ;
101+ const {
102+ x : parentX ,
103+ y : parentY ,
104+ width : parentWidth ,
105+ height : parentHeight ,
106+ } = this . #editor. parent . boundingClientRect ;
107+ const OFFSET_UNDER_BUTTON = 2 ;
108+ return [
109+ ( x - parentX ) / parentWidth ,
110+ ( y + height + OFFSET_UNDER_BUTTON - parentY ) / parentHeight ,
111+ ] ;
112+ }
113+
114+ set commentPopupPositionInLayer ( pos ) {
115+ this . #popupPosition = pos ;
116+ }
117+
118+ removeStandaloneCommentButton ( ) {
119+ this . #commentStandaloneButton?. remove ( ) ;
120+ this . #commentStandaloneButton = null ;
121+ }
122+
123+ removeToolbarCommentButton ( ) {
124+ this . #commentToolbarButton?. remove ( ) ;
125+ this . #commentToolbarButton = null ;
126+ }
127+
128+ setCommentButtonStates ( { selected, hasPopup } ) {
129+ if ( ! this . #commentStandaloneButton) {
130+ return ;
131+ }
132+ this . #commentStandaloneButton. classList . toggle ( "selected" , selected ) ;
133+ this . #commentStandaloneButton. ariaExpanded = hasPopup ;
134+ }
135+
136+ #render( comment , isStandalone ) {
73137 if ( ! this . #editor. _uiManager . hasCommentManager ( ) ) {
74138 return null ;
75139 }
76140
77141 comment . tabIndex = "0" ;
78- comment . setAttribute ( "data-l10n-id" , "pdfjs-editor-edit-comment-button" ) ;
142+ comment . ariaHasPopup = "dialog" ;
143+
144+ if ( isStandalone ) {
145+ comment . ariaControls = "commentPopup" ;
146+ } else {
147+ comment . ariaControlsElements = [
148+ this . #editor. _uiManager . getCommentDialogElement ( ) ,
149+ ] ;
150+ comment . setAttribute ( "data-l10n-id" , "pdfjs-editor-edit-comment-button" ) ;
151+ }
79152
80153 const signal = this . #editor. _uiManager . _signal ;
81154 if ( ! ( signal instanceof AbortSignal ) || signal . aborted ) {
82155 return comment ;
83156 }
84157
85158 comment . addEventListener ( "contextmenu" , noContextMenu , { signal } ) ;
159+ if ( isStandalone ) {
160+ comment . addEventListener (
161+ "focusin" ,
162+ e => {
163+ this . #editor. _focusEventsAllowed = false ;
164+ stopEvent ( e ) ;
165+ } ,
166+ {
167+ capture : true ,
168+ signal,
169+ }
170+ ) ;
171+ comment . addEventListener (
172+ "focusout" ,
173+ e => {
174+ this . #editor. _focusEventsAllowed = true ;
175+ stopEvent ( e ) ;
176+ } ,
177+ {
178+ capture : true ,
179+ signal,
180+ }
181+ ) ;
182+ }
86183 comment . addEventListener ( "pointerdown" , event => event . stopPropagation ( ) , {
87184 signal,
88185 } ) ;
@@ -92,7 +189,7 @@ class Comment {
92189 if ( comment === this . #commentToolbarButton) {
93190 this . edit ( ) ;
94191 } else {
95- this . #editor. _uiManager . toggleComment ( this . #editor ) ;
192+ this . #editor. toggleComment ( /* isSelected = */ true ) ;
96193 }
97194 } ;
98195 comment . addEventListener ( "click" , onClick , { capture : true , signal } ) ;
@@ -107,18 +204,55 @@ class Comment {
107204 { signal }
108205 ) ;
109206
207+ comment . addEventListener (
208+ "pointerenter" ,
209+ ( ) => {
210+ this . #editor. toggleComment (
211+ /* isSelected = */ false ,
212+ /* visibility = */ true
213+ ) ;
214+ } ,
215+ { signal }
216+ ) ;
217+ comment . addEventListener (
218+ "pointerleave" ,
219+ ( ) => {
220+ this . #editor. toggleComment (
221+ /* isSelected = */ false ,
222+ /* visibility = */ false
223+ ) ;
224+ } ,
225+ { signal }
226+ ) ;
227+
110228 return comment ;
111229 }
112230
113- edit ( ) {
114- const { bottom , left , right } = this . #editor . getClientDimensions ( ) ;
115- const position = { top : bottom } ;
116- if ( this . #editor . _uiManager . direction === "ltr" ) {
117- position . right = right ;
231+ edit ( options ) {
232+ const position = this . commentPopupPositionInLayer ;
233+ let posX , posY ;
234+ if ( position ) {
235+ [ posX , posY ] = position ;
118236 } else {
119- position . left = left ;
237+ // The position is in the editor coordinates.
238+ [ posX , posY ] = this . #editor. commentButtonPosition ;
239+ const { width, height, x, y } = this . #editor;
240+ posX = x + posX * width ;
241+ posY = y + posY * height ;
120242 }
121- this . #editor. _uiManager . editComment ( this . #editor, position ) ;
243+ const parentDimensions = this . #editor. parent . boundingClientRect ;
244+ const {
245+ x : parentX ,
246+ y : parentY ,
247+ width : parentWidth ,
248+ height : parentHeight ,
249+ } = parentDimensions ;
250+ this . #editor. _uiManager . editComment (
251+ this . #editor,
252+ parentX + posX * parentWidth ,
253+ parentY + posY * parentHeight ,
254+ { ...options , parentDimensions }
255+ ) ;
122256 }
123257
124258 finish ( ) {
0 commit comments