Skip to content

Commit d1fc0d7

Browse files
committed
fine tune
1 parent 590df91 commit d1fc0d7

File tree

5 files changed

+36
-17
lines changed

5 files changed

+36
-17
lines changed

routers/web/repo/compare.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -995,10 +995,15 @@ func ExcerptBlob(ctx *context.Context) {
995995
if err != nil {
996996
log.Error("GetIssueByIndex error: %v", err)
997997
} else if issue.IsPull {
998+
// FIXME: DIFF-CONVERSATION-DATA: the following data assignment is fragile
998999
ctx.Data["Issue"] = issue
9991000
ctx.Data["CanBlockUser"] = func(blocker, blockee *user_model.User) bool {
10001001
return user_service.CanBlockUser(ctx, ctx.Doer, blocker, blockee)
10011002
}
1003+
// and "diff/comment_form.tmpl" (reply comment) needs them
1004+
ctx.Data["PageIsPullFiles"] = true
1005+
ctx.Data["AfterCommitID"] = diffBlobExcerptData.AfterCommitID
1006+
10021007
allComments, err := issues_model.FetchCodeComments(ctx, issue, ctx.Doer, ctx.FormBool("show_outdated"))
10031008
if err != nil {
10041009
log.Error("FetchCodeComments error: %v", err)

services/gitdiff/gitdiff.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ func (d *DiffLine) RenderBlobExcerptButtons(fileNameHash string, data *DiffBlobE
191191
link += fmt.Sprintf("&pull_issue_index=%d", data.PullIssueIndex)
192192
}
193193
return htmlutil.HTMLFormat(
194-
`<button class="code-expander-button" hx-target="closest tr" hx-get="%s" data-hidden-comment-ids="%s">%s</button>`,
194+
`<button class="code-expander-button" hx-target="closest tr" hx-get="%s" data-hidden-comment-ids="%s">%s</button>`,
195195
link, dataHiddenCommentIDs, svg.RenderHTML(svgName),
196196
)
197197
}

templates/repo/issue/view_content/conversation.tmpl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
{{/* FIXME: DIFF-CONVERSATION-DATA: in the future this template should be refactor to avoid called by {{... "." $}}
2+
At the moment, two kinds of request handler call this template:
3+
* ExcerptBlob -> blob_excerpt.tmpl -> this
4+
* Other compare and diff pages -> ... -> {section_unified.tmpl|section_split.tmpl} -> this)
5+
The variables in "ctx.Data" are different in each case, making this template fragile, hard to read and maintain. */}}
16
{{if len .comments}}
27
{{$comment := index .comments 0}}
38
{{$invalid := $comment.Invalidated}}

web_src/css/review.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@
127127
background-color: var(--color-primary);
128128
color: var(--color-primary-contrast);
129129
border-radius: 8px !important;
130-
left: 4px;
130+
left: -12px;
131131
top: 6px;
132132
text-align: center;
133133
}

web_src/js/features/repo-diff.ts

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -218,31 +218,40 @@ function initRepoDiffShowMore() {
218218
});
219219
}
220220

221-
async function loadUntilFound() {
221+
async function onLocationHashChange() {
222+
// try to scroll to the target element by the current hash
222223
const currentHash = window.location.hash;
223-
if (!currentHash.startsWith('#diff-') && !currentHash.startsWith('#issuecomment-')) {
224-
return;
225-
}
224+
if (!currentHash.startsWith('#diff-') && !currentHash.startsWith('#issuecomment-')) return;
225+
226+
// avoid reentrance when we are changing the hash to scroll and trigger ":target" selection
227+
const attrAutoScrollRunning = 'data-auto-scroll-running';
228+
if (document.body.hasAttribute(attrAutoScrollRunning)) return;
226229

227-
while (window.location.hash === currentHash) {
230+
const targetElementId = currentHash.substring(1);
231+
while (currentHash === window.location.hash) {
228232
// use getElementById to avoid querySelector throws an error when the hash is invalid
229233
// eslint-disable-next-line unicorn/prefer-query-selector
230-
const targetElement = document.getElementById(currentHash.substring(1));
234+
const targetElement = document.getElementById(targetElementId);
231235
if (targetElement) {
232-
// re-trigger :target CSS and browser will scroll to the element
236+
// need to change hash to re-trigger ":target" CSS selector, let's manually scroll to it
237+
targetElement.scrollIntoView();
238+
document.body.setAttribute(attrAutoScrollRunning, 'true');
233239
window.location.hash = '';
234-
setTimeout(() => { window.location.hash = currentHash }, 0);
240+
window.location.hash = currentHash;
241+
setTimeout(() => document.body.removeAttribute(attrAutoScrollRunning), 0);
235242
return;
236243
}
237244

238245
// 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);
246+
const issueCommentPrefix = '#issuecomment-';
247+
if (currentHash.startsWith(issueCommentPrefix)) {
248+
const commentId = currentHash.substring(issueCommentPrefix.length);
241249
const expandButton = findExpandButtonForComment(commentId);
242250
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');
251+
// avoid infinite loop, do not re-click the button if already clicked
252+
const attrAutoLoadClicked = 'data-auto-load-clicked';
253+
if (expandButton.hasAttribute(attrAutoLoadClicked)) return;
254+
expandButton.setAttribute(attrAutoLoadClicked, 'true');
246255
expandButton.click();
247256
await sleep(500); // Wait for HTMX to load the content. FIXME: need to drop htmx in the future
248257
continue; // Try again to find the element
@@ -276,8 +285,8 @@ function findExpandButtonForComment(commentId: string): HTMLElement | null {
276285
}
277286

278287
function initRepoDiffHashChangeListener() {
279-
window.addEventListener('hashchange', loadUntilFound);
280-
loadUntilFound();
288+
window.addEventListener('hashchange', onLocationHashChange);
289+
onLocationHashChange();
281290
}
282291

283292
export function initRepoDiffView() {

0 commit comments

Comments
 (0)