@@ -9,7 +9,7 @@ import {submitEventSubmitter, queryElemSiblings, hideElem, showElem, animateOnce
99import { POST , GET } from '../modules/fetch.ts' ;
1010import { createTippy } from '../modules/tippy.ts' ;
1111import { invertFileFolding } from './file-fold.ts' ;
12- import { parseDom } from '../utils.ts' ;
12+ import { parseDom , sleep } from '../utils.ts' ;
1313import { registerGlobalSelectorFunc } from '../modules/observer.ts' ;
1414
1515const { i18n} = window . config ;
@@ -219,40 +219,32 @@ function initRepoDiffShowMore() {
219219}
220220
221221async function loadUntilFound ( ) {
222- const hashTargetSelector = window . location . hash ;
223- if ( ! hashTargetSelector . startsWith ( '#diff-' ) && ! hashTargetSelector . startsWith ( '#issuecomment-' ) ) {
222+ const currentHash = window . location . hash ;
223+ if ( ! currentHash . startsWith ( '#diff-' ) && ! currentHash . startsWith ( '#issuecomment-' ) ) {
224224 return ;
225225 }
226226
227- let wasExpanded = false ;
228-
229- while ( true ) {
227+ while ( window . location . hash === currentHash ) {
230228 // use getElementById to avoid querySelector throws an error when the hash is invalid
231229 // eslint-disable-next-line unicorn/prefer-query-selector
232- const targetElement = document . getElementById ( hashTargetSelector . substring ( 1 ) ) ;
230+ const targetElement = document . getElementById ( currentHash . substring ( 1 ) ) ;
233231 if ( targetElement ) {
234- // If we just expanded content, we need to re-trigger :target CSS
235- if ( wasExpanded ) {
236- const currentHash = window . location . hash ;
237- window . location . hash = '' ;
238- setTimeout ( ( ) => {
239- window . location . hash = currentHash ;
240- } , 10 ) ;
241- } else {
242- targetElement . scrollIntoView ( ) ;
243- }
232+ // re-trigger :target CSS and browser will scroll to the element
233+ window . location . hash = '' ;
234+ setTimeout ( ( ) => { window . location . hash = currentHash } , 0 ) ;
244235 return ;
245236 }
246237
247- // If looking for a comment, try to expand the section that contains it
248- if ( hashTargetSelector . startsWith ( '#issuecomment-' ) ) {
249- const commentId = hashTargetSelector . substring ( '#issuecomment-' . length ) ;
238+ // If looking for a hidden comment, try to expand the section that contains it
239+ if ( currentHash . startsWith ( '#issuecomment-' ) ) {
240+ const commentId = currentHash . substring ( '#issuecomment-' . length ) ;
250241 const expandButton = findExpandButtonForComment ( commentId ) ;
251242 if ( expandButton ) {
243+ // Avoid infinite loop, do not re-click the button if already clicked
244+ if ( expandButton . hasAttribute ( 'data-auto-load-clicked' ) ) return ;
245+ expandButton . setAttribute ( 'data-auto-load-clicked' , 'true' ) ;
252246 expandButton . click ( ) ;
253- wasExpanded = true ;
254- // Wait for HTMX to load the content
255- await new Promise ( ( resolve ) => setTimeout ( resolve , 500 ) ) ;
247+ await sleep ( 500 ) ; // Wait for HTMX to load the content. FIXME: need to drop htmx in the future
256248 continue ; // Try again to find the element
257249 }
258250 }
@@ -273,16 +265,13 @@ async function loadUntilFound() {
273265function findExpandButtonForComment ( commentId : string ) : HTMLElement | null {
274266 const expandButtons = document . querySelectorAll ( '.code-expander-button[data-hidden-comment-ids]' ) ;
275267 for ( const button of expandButtons ) {
276- const hiddenIds = button . getAttribute ( 'data-hidden-comment-ids' ) ;
277- if ( hiddenIds ) {
278- // Parse the comma-separated list of IDs
279- const ids = hiddenIds . replace ( / [ [ \] ] / g, '' ) . split ( ' ' ) . filter ( Boolean ) ;
280- if ( ids . includes ( commentId ) ) {
281- return button as HTMLElement ;
282- }
268+ const hiddenIdsAttr = button . getAttribute ( 'data-hidden-comment-ids' ) ;
269+ if ( ! hiddenIdsAttr ) continue ;
270+ const hiddenIds = hiddenIdsAttr . split ( ',' ) . filter ( Boolean ) ;
271+ if ( hiddenIds . includes ( commentId ) ) {
272+ return button as HTMLElement ;
283273 }
284274 }
285-
286275 return null ;
287276}
288277
0 commit comments