@@ -79,10 +79,9 @@ function setupJumpToUncoveredLineHotkey() {
7979 document . addEventListener ( "keydown" , uncoveredLineKeyListener ) ;
8080}
8181
82- function tryInjectDiffCommitUI ( ) : void {
82+ async function tryInjectDiffCommitUI ( ) : Promise < void > {
8383 const rootElement =
8484 document . getElementById ( "diff-content-parent" ) || document . body ;
85- if ( rootElement . classList . contains ( "qlty-diff-ui" ) ) return ;
8685
8786 const links : Element [ ] = [ ] ;
8887 rootElement . querySelectorAll ( 'a[href^="#diff-"' ) . forEach ( ( link ) => {
@@ -92,32 +91,36 @@ function tryInjectDiffCommitUI(): void {
9291 links . push ( link ) ;
9392 } ) ;
9493
95- if ( links . length === 0 ) {
96- return ; // No links to process
94+ await processFileLinks ( rootElement , links ) ;
95+
96+ if ( isPRPage ( ) ) {
97+ addPRPageBadge ( ) ;
98+ addNextUncoveredLineButton ( ) ;
99+ } else {
100+ addDiffPageBadge ( ) ;
97101 }
98102
99- links . forEach ( async ( link ) => {
103+ console . log ( "[qlty] injected diff UI" ) ;
104+ rootElement . classList . add ( "qlty-diff-ui" ) ;
105+ }
106+
107+ async function processFileLinks ( rootElement : HTMLElement , links : Element [ ] ) {
108+ const promises : Promise < void > [ ] = [ ] ;
109+
110+ for ( const link of links ) {
100111 const path = ( link as HTMLElement ) . innerText . replace ( "\u200E" , "" ) ;
101112 const fileId = link . getAttribute ( "href" ) ?. split ( "#" ) [ 1 ] ?? "" ;
102113
103- await injectIntoFileContainer (
114+ promises . push ( injectIntoFileContainer (
104115 link . parentElement ?. parentElement ?? link ,
105116 path ,
106117 rootElement . querySelectorAll (
107118 `[data-diff-anchor="${ fileId } "] td:nth-last-child(2)` ,
108119 ) ,
109- ) ;
110- } ) ;
111-
112- if ( isPRPage ( ) ) {
113- addPRPageBadge ( ) ;
114- addNextUncoveredLineButton ( ) ;
115- } else {
116- addDiffPageBadge ( ) ;
120+ ) ) ;
117121 }
118122
119- console . log ( "[qlty] injected diff UI" ) ;
120- rootElement . classList . add ( "qlty-diff-ui" ) ;
123+ return Promise . all ( promises ) ;
121124}
122125
123126function isPRPage ( ) : boolean {
@@ -174,6 +177,13 @@ function jumpToNextUncoveredLine() {
174177 el . parentElement ?. classList . contains ( "selected-line" ) ,
175178 ) ;
176179
180+ const currentElement = uncoveredLineDivs [ currentlySelectedIndex ] ;
181+ if ( currentElement ) {
182+ // Click the current line to deselect it; this is necessary to make sure
183+ // the selected line doesn't stay selected (occurs sometimes during back/forward nav)
184+ selectLinkableLine ( currentElement . parentElement ! ) ;
185+ }
186+
177187 const nextIndex = ( currentlySelectedIndex + 1 ) % uncoveredLineDivs . length ;
178188 const nextElement = uncoveredLineDivs [ nextIndex ] ;
179189 nextElement . scrollIntoView ( { behavior : "smooth" , block : "center" } ) ;
@@ -184,6 +194,10 @@ function jumpToNextUncoveredLine() {
184194 return ;
185195 }
186196
197+ selectLinkableLine ( linkableLine ) ;
198+ }
199+
200+ function selectLinkableLine ( linkableLine : HTMLElement ) {
187201 // Click the line to highlight it
188202 // If we don't also trigger a click event, then our line gets unhighlighted on
189203 // subsequent button clicks
0 commit comments