13
13
* limitations under the License.
14
14
*/
15
15
16
- import { noContextMenu } from "../display_utils.js" ;
16
+ import { noContextMenu , stopEvent } from "../display_utils.js" ;
17
17
18
18
class Comment {
19
19
#commentStandaloneButton = null ;
@@ -34,6 +34,8 @@ class Comment {
34
34
35
35
#deleted = false ;
36
36
37
+ #popupPosition = null ;
38
+
37
39
constructor ( editor ) {
38
40
this . #editor = editor ;
39
41
}
@@ -42,7 +44,7 @@ class Comment {
42
44
const button = ( this . #commentToolbarButton =
43
45
document . createElement ( "button" ) ) ;
44
46
button . className = "comment" ;
45
- return this . #render( button ) ;
47
+ return this . #render( button , false ) ;
46
48
}
47
49
48
50
renderForStandalone ( ) {
@@ -66,23 +68,118 @@ class Comment {
66
68
}
67
69
}
68
70
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
+ ) ;
70
90
}
71
91
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 ) {
73
137
if ( ! this . #editor. _uiManager . hasCommentManager ( ) ) {
74
138
return null ;
75
139
}
76
140
77
141
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
+ }
79
152
80
153
const signal = this . #editor. _uiManager . _signal ;
81
154
if ( ! ( signal instanceof AbortSignal ) || signal . aborted ) {
82
155
return comment ;
83
156
}
84
157
85
158
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
+ }
86
183
comment . addEventListener ( "pointerdown" , event => event . stopPropagation ( ) , {
87
184
signal,
88
185
} ) ;
@@ -92,7 +189,7 @@ class Comment {
92
189
if ( comment === this . #commentToolbarButton) {
93
190
this . edit ( ) ;
94
191
} else {
95
- this . #editor. _uiManager . toggleComment ( this . #editor ) ;
192
+ this . #editor. toggleComment ( /* isSelected = */ true ) ;
96
193
}
97
194
} ;
98
195
comment . addEventListener ( "click" , onClick , { capture : true , signal } ) ;
@@ -107,18 +204,55 @@ class Comment {
107
204
{ signal }
108
205
) ;
109
206
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
+
110
228
return comment ;
111
229
}
112
230
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 ;
118
236
} 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 ;
120
242
}
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
+ ) ;
122
256
}
123
257
124
258
finish ( ) {
0 commit comments