Skip to content

Commit 22911a1

Browse files
GiteaBotHesterG
andauthored
Imrove scroll behavior to hash issuecomment(scroll position, auto expand if file is folded, and on refreshing) (#23513) (#23540)
Backport #23513 by @HesterG Close #23466 Right now on pull request "files Changed" tab, if a file is viewed, when the comments' links are visited, the comment will not be shown as the file is folded after viewed. This PR is to improve the behavior, to make the comment seen even the related file is folded, like on github. And right now scroll position will be remembered and hence it won’t scroll to hashed comment after refreshing, this PR also adjust the scroll position remembering behavior: When there is hash comment in url, do not remember the scroll position. Before: https://user-images.githubusercontent.com/17645053/225512079-6cf79581-9346-44cf-95d6-06919642e6a8.mov After: https://user-images.githubusercontent.com/17645053/225523753-3f6728f2-977b-4ed0-a65c-63dcef2ace80.mov Update - long comment's behavior after using `scrollTop ` (Comment div scroll to the position which is 30px below the diff header, or 30px below top on conversation tab): https://user-images.githubusercontent.com/17645053/225614460-0602c1a6-229c-41f4-84d2-334e78251486.mov Co-authored-by: Hester Gong <[email protected]>
1 parent 4b763d8 commit 22911a1

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

web_src/js/features/repo-issue.js

+21-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {initEasyMDEImagePaste} from './comp/ImagePaste.js';
66
import {initCompMarkupContentPreviewTab} from './comp/MarkupContentPreview.js';
77
import {initTooltip, showTemporaryTooltip, createTippy} from '../modules/tippy.js';
88
import {hideElem, showElem, toggleElem} from '../utils/dom.js';
9+
import {setFileFolding} from './file-fold.js';
910

1011
const {appSubUrl, csrfToken} = window.config;
1112

@@ -436,17 +437,36 @@ export async function handleReply($el) {
436437

437438
export function initRepoPullRequestReview() {
438439
if (window.location.hash && window.location.hash.startsWith('#issuecomment-')) {
440+
// set scrollRestoration to 'manual' when there is a hash in url, so that the scroll position will not be remembered after refreshing
441+
if (window.history.scrollRestoration !== 'manual') {
442+
window.history.scrollRestoration = 'manual';
443+
}
439444
const commentDiv = $(window.location.hash);
440445
if (commentDiv) {
441446
// get the name of the parent id
442447
const groupID = commentDiv.closest('div[id^="code-comments-"]').attr('id');
443448
if (groupID && groupID.startsWith('code-comments-')) {
444449
const id = groupID.slice(14);
450+
const ancestorDiffBox = commentDiv.closest('.diff-file-box');
451+
// on pages like conversation, there is no diff header
452+
const diffHeader = ancestorDiffBox.find('.diff-file-header');
453+
// offset is for scrolling
454+
let offset = 30;
455+
if (diffHeader[0]) {
456+
offset += $('.diff-detail-box').outerHeight() + diffHeader.outerHeight();
457+
}
445458
$(`#show-outdated-${id}`).addClass('gt-hidden');
446459
$(`#code-comments-${id}`).removeClass('gt-hidden');
447460
$(`#code-preview-${id}`).removeClass('gt-hidden');
448461
$(`#hide-outdated-${id}`).removeClass('gt-hidden');
449-
commentDiv[0].scrollIntoView();
462+
// if the comment box is folded, expand it
463+
if (ancestorDiffBox.attr('data-folded') && ancestorDiffBox.attr('data-folded') === 'true') {
464+
setFileFolding(ancestorDiffBox[0], ancestorDiffBox.find('.fold-file')[0], false);
465+
}
466+
window.scrollTo({
467+
top: commentDiv.offset().top - offset,
468+
behavior: 'instant'
469+
});
450470
}
451471
}
452472
}

0 commit comments

Comments
 (0)