@@ -18,6 +18,7 @@ import {
1818 applyOpacity ,
1919 CSSConstants ,
2020 findContrastColor ,
21+ MathClamp ,
2122 noContextMenu ,
2223 PDFDateString ,
2324 renderRichText ,
@@ -56,7 +57,8 @@ class CommentManager {
5657 eventBus ,
5758 linkService ,
5859 this . #popup,
59- dateFormat
60+ dateFormat ,
61+ ltr
6062 ) ;
6163 this . #popup. sidebar = this . #sidebar;
6264 CommentManager . #hasForcedColors = hasForcedColors ;
@@ -160,10 +162,21 @@ class CommentSidebar {
160162
161163 #uiManager = null ;
162164
165+ #minWidth = 0 ;
166+
167+ #maxWidth = 0 ;
168+
169+ #initialWidth = 0 ;
170+
171+ #width = 0 ;
172+
173+ #ltr;
174+
163175 constructor (
164176 {
165177 learnMoreUrl,
166178 sidebar,
179+ sidebarResizer,
167180 commentsList,
168181 commentCount,
169182 sidebarTitle,
@@ -173,7 +186,8 @@ class CommentSidebar {
173186 eventBus ,
174187 linkService ,
175188 popup ,
176- dateFormat
189+ dateFormat ,
190+ ltr
177191 ) {
178192 this . #sidebar = sidebar ;
179193 this . #sidebarTitle = sidebarTitle ;
@@ -184,7 +198,16 @@ class CommentSidebar {
184198 this . #closeButton = closeButton ;
185199 this . #popup = popup ;
186200 this . #dateFormat = dateFormat ;
201+ this . #ltr = ltr ;
187202
203+ const style = window . getComputedStyle ( sidebar ) ;
204+ this . #minWidth = parseFloat ( style . getPropertyValue ( "--sidebar-min-width" ) ) ;
205+ this . #maxWidth = parseFloat ( style . getPropertyValue ( "--sidebar-max-width" ) ) ;
206+ this . #initialWidth = this . #width = parseFloat (
207+ style . getPropertyValue ( "--sidebar-width" )
208+ ) ;
209+
210+ this . #makeSidebarResizable( sidebarResizer ) ;
188211 closeButton . addEventListener ( "click" , ( ) => {
189212 eventBus . dispatch ( "switchannotationeditormode" , {
190213 source : this ,
@@ -205,6 +228,63 @@ class CommentSidebar {
205228 this . #sidebar. hidden = true ;
206229 }
207230
231+ #makeSidebarResizable( resizer ) {
232+ let pointerMoveAC ;
233+ const cancelResize = ( ) => {
234+ this . #width = MathClamp ( this . #width, this . #minWidth, this . #maxWidth) ;
235+ this . #sidebar. classList . remove ( "resizing" ) ;
236+ pointerMoveAC ?. abort ( ) ;
237+ pointerMoveAC = null ;
238+ } ;
239+ resizer . addEventListener ( "pointerdown" , e => {
240+ if ( pointerMoveAC ) {
241+ cancelResize ( ) ;
242+ return ;
243+ }
244+ const { clientX } = e ;
245+ stopEvent ( e ) ;
246+ let prevX = clientX ;
247+ pointerMoveAC = new AbortController ( ) ;
248+ const { signal } = pointerMoveAC ;
249+ const sign = this . #ltr ? - 1 : 1 ;
250+ const sidebar = this . #sidebar;
251+ const sidebarStyle = sidebar . style ;
252+ sidebar . classList . add ( "resizing" ) ;
253+ const parentStyle = sidebar . parentElement . style ;
254+ parentStyle . minWidth = 0 ;
255+ window . addEventListener ( "contextmenu" , noContextMenu , { signal } ) ;
256+ window . addEventListener (
257+ "pointermove" ,
258+ ev => {
259+ if ( ! pointerMoveAC ) {
260+ return ;
261+ }
262+ stopEvent ( ev ) ;
263+ const { clientX : x } = ev ;
264+ const newWidth = ( this . #width += sign * ( x - prevX ) ) ;
265+ prevX = x ;
266+ if ( newWidth > this . #maxWidth || newWidth < this . #minWidth) {
267+ return ;
268+ }
269+ sidebarStyle . width = `${ newWidth . toFixed ( 3 ) } px` ;
270+ parentStyle . insetInlineStart = `${ ( this . #initialWidth - newWidth ) . toFixed ( 3 ) } px` ;
271+ } ,
272+ { signal, capture : true }
273+ ) ;
274+ window . addEventListener ( "blur" , cancelResize , { signal } ) ;
275+ window . addEventListener (
276+ "pointerup" ,
277+ ev => {
278+ if ( pointerMoveAC ) {
279+ cancelResize ( ) ;
280+ stopEvent ( ev ) ;
281+ }
282+ } ,
283+ { signal }
284+ ) ;
285+ } ) ;
286+ }
287+
208288 setUIManager ( uiManager ) {
209289 this . #uiManager = uiManager ;
210290 }
0 commit comments