Skip to content

Commit 7adbb76

Browse files
committed
remove legacy script block and simplify loadUntilFound
1 parent b1ba89d commit 7adbb76

File tree

6 files changed

+39
-89
lines changed

6 files changed

+39
-89
lines changed

routers/web/repo/commit.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ func Diff(ctx *context.Context) {
365365
return
366366
}
367367

368-
ctx.Data["DiffFiles"] = transformDiffTreeForUI(diffTree, nil)
368+
ctx.PageData["DiffFiles"] = transformDiffTreeForUI(diffTree, nil)
369369
}
370370

371371
statuses, _, err := git_model.GetLatestCommitStatus(ctx, ctx.Repo.Repository.ID, commitID, db.ListOptionsAll)

routers/web/repo/compare.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -640,7 +640,7 @@ func PrepareCompareDiff(
640640
return false
641641
}
642642

643-
ctx.Data["DiffFiles"] = transformDiffTreeForUI(diffTree, nil)
643+
ctx.PageData["DiffFiles"] = transformDiffTreeForUI(diffTree, nil)
644644
}
645645

646646
headCommit, err := ci.HeadGitRepo.GetCommit(headCommitID)

routers/web/repo/pull.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -836,7 +836,7 @@ func viewPullFiles(ctx *context.Context, specifiedStartCommit, specifiedEndCommi
836836
}
837837
}
838838

839-
ctx.Data["DiffFiles"] = transformDiffTreeForUI(diffTree, filesViewedState)
839+
ctx.PageData["DiffFiles"] = transformDiffTreeForUI(diffTree, filesViewedState)
840840
}
841841

842842
ctx.Data["Diff"] = diff

templates/repo/diff/box.tmpl

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -57,28 +57,6 @@
5757
<div>{{ctx.Locale.Tr "repo.pulls.showing_specified_commit_range" (ShortSha .BeforeCommitID) (ShortSha .AfterCommitID)}} - <a href="{{$.Issue.Link}}/files?style={{if $.IsSplitStyle}}split{{else}}unified{{end}}&whitespace={{$.WhitespaceBehavior}}&show-outdated={{$.ShowOutdatedComments}}">{{ctx.Locale.Tr "repo.pulls.show_all_commits"}}</a></div>
5858
</div>
5959
{{end}}
60-
<script id="diff-data-script" type="module">
61-
const diffDataFiles = [{{range $i, $file := .DiffFiles}}
62-
{Name:"{{$file.Name}}",NameHash:"{{$file.NameHash}}",Status:{{$file.Status}},IsSubmodule:{{$file.IsSubmodule}},IsViewed:{{$file.IsViewed}}},{{end}}];
63-
64-
const diffData = {
65-
isIncomplete: {{.Diff.IsIncomplete}},
66-
};
67-
68-
// for first time loading, the diffFileInfo is a plain object
69-
// after the Vue component is mounted, the diffFileInfo is a reactive object
70-
let diffFileInfo = window.config.pageData.diffFileInfo || {
71-
files:[],
72-
fileTreeIsVisible: false,
73-
fileListIsVisible: false,
74-
isLoadingNewData: false,
75-
selectedItem: '',
76-
};
77-
diffFileInfo = Object.assign(diffFileInfo, diffData);
78-
diffFileInfo.files.push(...diffDataFiles);
79-
window.config.pageData.diffFileInfo = diffFileInfo;
80-
</script>
81-
<div id="diff-file-list"></div>
8260
{{end}}
8361
<div id="diff-container">
8462
{{if $showFileTree}}

web_src/js/features/repo-diff.ts

Lines changed: 29 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import {fomanticQuery} from '../modules/fomantic/base.ts';
2121
import {createTippy} from '../modules/tippy.ts';
2222
import {invertFileFolding} from './file-fold.ts';
2323

24-
const {pageData, i18n} = window.config;
24+
const {i18n} = window.config;
2525

2626
function 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

198196
function 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-
252231
async 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

288258
function initRepoDiffHashChangeListener() {
289-
const el = document.querySelector('#diff-file-tree');
290-
if (!el) return;
291-
292259
window.addEventListener('hashchange', loadUntilFound);
293260
loadUntilFound();
294261
}
295262

296263
export function initRepoDiffView() {
297264
initRepoDiffConversationForm();
298-
if (!$('#diff-file-list').length) return;
265+
if (!$('#diff-file-boxes').length) return;
299266
initDiffFileTree();
300267
initDiffCommitSelect();
301268
initRepoDiffShowMore();

web_src/js/modules/stores.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
import {reactive} from 'vue';
22
import type {Reactive} from 'vue';
33

4+
const {pageData} = window.config;
5+
46
let diffTreeStoreReactive: Reactive<Record<string, any>>;
57
export function diffTreeStore() {
68
if (!diffTreeStoreReactive) {
7-
diffTreeStoreReactive = reactive(window.config.pageData.diffFileInfo);
8-
window.config.pageData.diffFileInfo = diffTreeStoreReactive;
9+
diffTreeStoreReactive = reactive({
10+
files: pageData.DiffFiles,
11+
fileTreeIsVisible: false,
12+
selectedItem: '',
13+
});
914
}
1015
return diffTreeStoreReactive;
1116
}

0 commit comments

Comments
 (0)