@@ -6,6 +6,7 @@ import {initEasyMDEImagePaste} from './comp/ImagePaste.js';
66import { initCompMarkupContentPreviewTab } from './comp/MarkupContentPreview.js' ;
77import { initTooltip , showTemporaryTooltip } from '../modules/tippy.js' ;
88import { hideElem , showElem , toggleElem } from '../utils/dom.js' ;
9+ import { setFileFolding } from './file-fold.js' ;
910
1011const { appSubUrl, csrfToken} = window . config ;
1112
@@ -437,17 +438,36 @@ export async function handleReply($el) {
437438
438439export function initRepoPullRequestReview ( ) {
439440 if ( window . location . hash && window . location . hash . startsWith ( '#issuecomment-' ) ) {
441+ // set scrollRestoration to 'manual' when there is a hash in url, so that the scroll position will not be remembered after refreshing
442+ if ( window . history . scrollRestoration !== 'manual' ) {
443+ window . history . scrollRestoration = 'manual' ;
444+ }
440445 const commentDiv = $ ( window . location . hash ) ;
441446 if ( commentDiv ) {
442447 // get the name of the parent id
443448 const groupID = commentDiv . closest ( 'div[id^="code-comments-"]' ) . attr ( 'id' ) ;
444449 if ( groupID && groupID . startsWith ( 'code-comments-' ) ) {
445450 const id = groupID . slice ( 14 ) ;
451+ const ancestorDiffBox = commentDiv . closest ( '.diff-file-box' ) ;
452+ // on pages like conversation, there is no diff header
453+ const diffHeader = ancestorDiffBox . find ( '.diff-file-header' ) ;
454+ // offset is for scrolling
455+ let offset = 30 ;
456+ if ( diffHeader [ 0 ] ) {
457+ offset += $ ( '.diff-detail-box' ) . outerHeight ( ) + diffHeader . outerHeight ( ) ;
458+ }
446459 $ ( `#show-outdated-${ id } ` ) . addClass ( 'gt-hidden' ) ;
447460 $ ( `#code-comments-${ id } ` ) . removeClass ( 'gt-hidden' ) ;
448461 $ ( `#code-preview-${ id } ` ) . removeClass ( 'gt-hidden' ) ;
449462 $ ( `#hide-outdated-${ id } ` ) . removeClass ( 'gt-hidden' ) ;
450- commentDiv [ 0 ] . scrollIntoView ( ) ;
463+ // if the comment box is folded, expand it
464+ if ( ancestorDiffBox . attr ( 'data-folded' ) && ancestorDiffBox . attr ( 'data-folded' ) === 'true' ) {
465+ setFileFolding ( ancestorDiffBox [ 0 ] , ancestorDiffBox . find ( '.fold-file' ) [ 0 ] , false ) ;
466+ }
467+ window . scrollTo ( {
468+ top : commentDiv . offset ( ) . top - offset ,
469+ behavior : 'instant'
470+ } ) ;
451471 }
452472 }
453473 }
0 commit comments