@@ -21,7 +21,7 @@ import {fomanticQuery} from '../modules/fomantic/base.ts';
2121import { createTippy } from '../modules/tippy.ts' ;
2222import { invertFileFolding } from './file-fold.ts' ;
2323
24- const { pageData , i18n} = window . config ;
24+ const { i18n} = window . config ;
2525
2626function initRepoDiffFileViewToggle ( ) {
2727 $ ( '.file-view-toggle' ) . on ( 'click' , function ( ) {
@@ -168,39 +168,35 @@ function onShowMoreFiles() {
168168 initDiffHeaderPopup ( ) ;
169169}
170170
171- async function loadMoreFiles ( url : string ) {
172- const target = document . querySelector ( 'a#diff-show-more-files' ) ;
173- if ( target ?. classList . contains ( 'disabled' ) || pageData . diffFileInfo . isLoadingNewData ) {
174- return ;
171+ async function loadMoreFiles ( btn : Element ) : Promise < boolean > {
172+ if ( btn . classList . contains ( 'disabled' ) ) {
173+ return false ;
175174 }
176175
177- pageData . diffFileInfo . isLoadingNewData = true ;
178- target ?. classList . add ( 'disabled' ) ;
179-
176+ btn . classList . add ( 'disabled' ) ;
177+ const url = btn . getAttribute ( 'data-href' ) ;
180178 try {
181179 const response = await GET ( url ) ;
182180 const resp = await response . text ( ) ;
183181 const $resp = $ ( resp ) ;
184182 // the response is a full HTML page, we need to extract the relevant contents:
185- // 1. append the newly loaded file list items to the existing list
183+ // * append the newly loaded file list items to the existing list
186184 $ ( '#diff-incomplete' ) . replaceWith ( $resp . find ( '#diff-file-boxes' ) . children ( ) ) ;
187185
188186 onShowMoreFiles ( ) ;
187+ return true ;
189188 } catch ( error ) {
190189 console . error ( 'Error:' , error ) ;
191190 showErrorToast ( 'An error occurred while loading more files.' ) ;
192191 } finally {
193- target ?. classList . remove ( 'disabled' ) ;
194- pageData . diffFileInfo . isLoadingNewData = false ;
192+ btn . classList . remove ( 'disabled' ) ;
195193 }
196194}
197195
198196function initRepoDiffShowMore ( ) {
199- $ ( document ) . on ( 'click' , 'a#diff-show-more-files' , ( e ) => {
197+ addDelegatedEventListener ( document , 'click' , 'a#diff-show-more-files' , ( el , e ) => {
200198 e . preventDefault ( ) ;
201-
202- const linkLoadMore = e . target . getAttribute ( 'data-href' ) ;
203- loadMoreFiles ( linkLoadMore ) ;
199+ loadMoreFiles ( el ) ;
204200 } ) ;
205201
206202 $ ( document ) . on ( 'click' , 'a.diff-load-button' , async ( e ) => {
@@ -232,70 +228,41 @@ function initRepoDiffShowMore() {
232228 } ) ;
233229}
234230
235- function shouldStopLoading ( ) {
236- if ( ! window . location . hash ) {
237- return true ; // No hash to look for
238- }
239-
240- // eslint-disable-next-line unicorn/prefer-query-selector
241- const targetElement = document . getElementById ( window . location . hash . substring ( 1 ) ) ;
242- if ( targetElement ) {
243- return true ; // The element is already loaded
244- }
245-
246- return ! pageData . diffFileInfo . isIncomplete ;
247- }
248-
249- // This is a flag to ensure that only one loadUntilFound is running at a time
250- let isLoadUntilFoundRunning = false ;
251-
252231async function loadUntilFound ( ) {
253- // this ensures that only one loadUntilFound is running at a time
254- if ( isLoadUntilFoundRunning === true ) {
232+ const hashTargetSelector = window . location . hash ;
233+ if ( ! hashTargetSelector . startsWith ( '#diff-' ) && ! hashTargetSelector . startsWith ( '#issuecomment-' ) ) {
255234 return ;
256235 }
257- isLoadUntilFoundRunning = true ;
258-
259- try {
260- while ( ! shouldStopLoading ( ) ) {
261- const showMoreButton = document . querySelector ( '#diff-show-more-files' ) ;
262- if ( ! showMoreButton ) {
263- console . error ( 'Could not find the show more files button' ) ;
264- return ;
265- }
266-
267- const url = showMoreButton . getAttribute ( 'data-href' ) ;
268- if ( ! url ) {
269- console . error ( 'Could not find the data-href attribute of the show more files button' ) ;
270- return ;
271- }
272236
273- // Load more files, await ensures we don't block progress
274- await loadMoreFiles ( url ) ;
237+ while ( true ) {
238+ // use getElementById to avoid querySelector throws an error when the hash is invalid
239+ // eslint-disable-next-line unicorn/prefer-query-selector
240+ const targetElement = document . getElementById ( hashTargetSelector . substring ( 1 ) ) ;
241+ if ( targetElement ) {
242+ targetElement . scrollIntoView ( ) ;
243+ return ;
275244 }
276245
277- if ( window . location . hash ) {
278- const targetElement = document . querySelector ( window . location . hash ) ;
279- if ( targetElement ) {
280- targetElement . scrollIntoView ( ) ;
281- }
246+ // the button will be refreshed after each "load more", so query it every time
247+ const showMoreButton = document . querySelector ( '#diff-show-more-files' ) ;
248+ if ( ! showMoreButton ) {
249+ return ; // nothing more to load
282250 }
283- } finally {
284- isLoadUntilFoundRunning = false ;
251+
252+ // Load more files, await ensures we don't block progress
253+ const ok = await loadMoreFiles ( showMoreButton ) ;
254+ if ( ! ok ) return ; // failed to load more files
285255 }
286256}
287257
288258function initRepoDiffHashChangeListener ( ) {
289- const el = document . querySelector ( '#diff-file-tree' ) ;
290- if ( ! el ) return ;
291-
292259 window . addEventListener ( 'hashchange' , loadUntilFound ) ;
293260 loadUntilFound ( ) ;
294261}
295262
296263export function initRepoDiffView ( ) {
297264 initRepoDiffConversationForm ( ) ;
298- if ( ! $ ( '#diff-file-list ' ) . length ) return ;
265+ if ( ! $ ( '#diff-file-boxes ' ) . length ) return ;
299266 initDiffFileTree ( ) ;
300267 initDiffCommitSelect ( ) ;
301268 initRepoDiffShowMore ( ) ;
0 commit comments