File tree Expand file tree Collapse file tree 3 files changed +44
-0
lines changed
Expand file tree Collapse file tree 3 files changed +44
-0
lines changed Original file line number Diff line number Diff line change @@ -186,6 +186,11 @@ export class PullRequestOverviewPanel extends IssueOverviewPanel<PullRequestMode
186186 if ( this . _panel . visible && this . _item ) {
187187 PullRequestOverviewPanel . _onVisible . fire ( this . _item ) ;
188188 }
189+
190+ // If the panel becomes active, send a message to restore focus to the comment textarea if needed
191+ if ( this . _panel . active ) {
192+ this . _postMessage ( { command : 'pr.restoreFocus' } ) ;
193+ }
189194 }
190195
191196 private setVisibilityContext ( ) {
Original file line number Diff line number Diff line change @@ -410,6 +410,17 @@ export class PRContext {
410410 pendingReview . focus ( ) ;
411411 }
412412 return ;
413+ case 'pr.restoreFocus' :
414+ // Restore focus to the comment textarea if it had focus before the webview lost focus
415+ const commentTextarea = document . getElementById ( 'comment-textarea' ) ;
416+ if ( commentTextarea instanceof HTMLTextAreaElement ) {
417+ // Check if there's a stored focus state
418+ const shouldRestoreFocus = sessionStorage . getItem ( 'comment-textarea-had-focus' ) === 'true' ;
419+ if ( shouldRestoreFocus ) {
420+ commentTextarea . focus ( ) ;
421+ }
422+ }
423+ return ;
413424 case 'pr.submitting-review' :
414425 return this . updatePR ( { busy : true , lastReviewType : message . lastReviewType } ) ;
415426 case 'pr.append-review' :
Original file line number Diff line number Diff line change @@ -21,6 +21,34 @@ export function Root({ children }) {
2121 ctx . onchange = setPR ;
2222 setPR ( ctx . pr ) ;
2323 } , [ ] ) ;
24+
25+ // Track focus on the comment textarea to restore it when the webview regains focus
26+ useEffect ( ( ) => {
27+ const handleFocusIn = ( e : FocusEvent ) => {
28+ if ( e . target instanceof HTMLTextAreaElement && e . target . id === 'comment-textarea' ) {
29+ sessionStorage . setItem ( 'comment-textarea-had-focus' , 'true' ) ;
30+ }
31+ } ;
32+
33+ const handleFocusOut = ( e : FocusEvent ) => {
34+ if ( e . target instanceof HTMLTextAreaElement && e . target . id === 'comment-textarea' ) {
35+ // Only clear the flag if we're switching to a non-textarea element
36+ // This prevents clearing when the webview loses focus
37+ if ( e . relatedTarget instanceof HTMLElement ) {
38+ sessionStorage . setItem ( 'comment-textarea-had-focus' , 'false' ) ;
39+ }
40+ }
41+ } ;
42+
43+ document . addEventListener ( 'focusin' , handleFocusIn ) ;
44+ document . addEventListener ( 'focusout' , handleFocusOut ) ;
45+
46+ return ( ) => {
47+ document . removeEventListener ( 'focusin' , handleFocusIn ) ;
48+ document . removeEventListener ( 'focusout' , handleFocusOut ) ;
49+ } ;
50+ } , [ ] ) ;
51+
2452 window . onscroll = debounce ( ( ) => {
2553 ctx . postMessage ( {
2654 command : 'scroll' ,
You can’t perform that action at this time.
0 commit comments