Skip to content

Commit ee99cf6

Browse files
authored
Refactor diffFileInfo / DiffTreeStore (#24998)
Follow #21012, #22399 Replace #24983, fix #24938 Help #24956 Now, the `window.config.pageData.diffFileInfo` itself is a reactive store, so it's quite easy to sync values/states by it, no need to do "doLoadMoreFiles" or "callback". Screenshot: these two buttons both work. After complete loading, the UI is also right. <details> ![image](https://github.com/go-gitea/gitea/assets/2114189/cc6310fd-7f27-45ea-ab4f-24952a87b421) ![image](https://github.com/go-gitea/gitea/assets/2114189/4c11dd67-ac03-4568-8541-91204d27a4e3) ![image](https://github.com/go-gitea/gitea/assets/2114189/38a22cec-41be-41e6-a209-f347b7a4c1de) </details>
1 parent 32185ef commit ee99cf6

File tree

6 files changed

+76
-100
lines changed

6 files changed

+76
-100
lines changed

templates/repo/diff/box.tmpl

+25-27
Original file line numberDiff line numberDiff line change
@@ -45,33 +45,31 @@
4545
{{end}}
4646
</div>
4747
</div>
48-
<script id="diff-data-script">
49-
(() => {
50-
const diffData = {
51-
files: [{{range $i, $file := .Diff.Files}}{Name:"{{$file.Name}}",NameHash:"{{$file.NameHash}}",Type:{{$file.Type}},IsBin:{{$file.IsBin}},Addition:{{$file.Addition}},Deletion:{{$file.Deletion}}},{{end}}],
52-
isIncomplete: {{.Diff.IsIncomplete}},
53-
tooManyFilesMessage: "{{$.locale.Tr "repo.diff.too_many_files"}}",
54-
binaryFileMessage: "{{$.locale.Tr "repo.diff.bin"}}",
55-
showMoreMessage: "{{.locale.Tr "repo.diff.show_more"}}",
56-
statisticsMessage: "{{.locale.Tr "repo.diff.stats_desc_file"}}",
57-
fileTreeIsVisible: false,
58-
fileListIsVisible: false,
59-
isLoadingNewData: false,
60-
diffEnd: {{.Diff.End}},
61-
link: "{{$.Link}}"
62-
};
63-
if(window.config.pageData.diffFileInfo) {
64-
// Page is already loaded - add the data to our existing data
65-
window.config.pageData.diffFileInfo.files.push(...diffData.files);
66-
window.config.pageData.diffFileInfo.isIncomplete = diffData.isIncomplete;
67-
window.config.pageData.diffFileInfo.diffEnd = diffData.diffEnd;
68-
window.config.pageData.diffFileInfo.link = diffData.link;
69-
} else {
70-
// new load of page - populate initial data
71-
window.config.pageData.diffFileInfo = diffData;
72-
}
73-
})();
74-
</script>
48+
<script id="diff-data-script" type="module">
49+
const diffDataFiles = [{{range $i, $file := .Diff.Files}}{Name:"{{$file.Name}}",NameHash:"{{$file.NameHash}}",Type:{{$file.Type}},IsBin:{{$file.IsBin}},Addition:{{$file.Addition}},Deletion:{{$file.Deletion}}},{{end}}];
50+
const diffData = {
51+
isIncomplete: {{.Diff.IsIncomplete}},
52+
tooManyFilesMessage: "{{$.locale.Tr "repo.diff.too_many_files"}}",
53+
binaryFileMessage: "{{$.locale.Tr "repo.diff.bin"}}",
54+
showMoreMessage: "{{.locale.Tr "repo.diff.show_more"}}",
55+
statisticsMessage: "{{.locale.Tr "repo.diff.stats_desc_file"}}",
56+
linkLoadMore: "{{$.Link}}?skip-to={{.Diff.End}}&file-only=true",
57+
};
58+
59+
// for first time loading, the diffFileInfo is a plain object
60+
// after the Vue component is mounted, the diffFileInfo is a reactive object
61+
// keep in mind that this script block would be executed many times when loading more files, by "loadMoreFiles"
62+
let diffFileInfo = window.config.pageData.diffFileInfo || {
63+
files:[],
64+
fileTreeIsVisible: false,
65+
fileListIsVisible: false,
66+
isLoadingNewData: false,
67+
selectedItem: '',
68+
};
69+
diffFileInfo = Object.assign(diffFileInfo, diffData);
70+
diffFileInfo.files.push(...diffDataFiles);
71+
window.config.pageData.diffFileInfo = diffFileInfo;
72+
</script>
7573
<div id="diff-file-list"></div>
7674
<div id="diff-container">
7775
<div id="diff-file-tree" class="gt-hidden"></div>

web_src/js/components/DiffFileList.vue

+12-16
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,32 @@
11
<template>
2-
<ol class="diff-detail-box diff-stats gt-m-0" ref="root" v-if="fileListIsVisible">
3-
<li v-for="file in files" :key="file.NameHash">
2+
<ol class="diff-detail-box diff-stats gt-m-0" ref="root" v-if="store.fileListIsVisible">
3+
<li v-for="file in store.files" :key="file.NameHash">
44
<div class="gt-font-semibold gt-df gt-ac pull-right">
5-
<span v-if="file.IsBin" class="gt-ml-1 gt-mr-3">{{ binaryFileMessage }}</span>
5+
<span v-if="file.IsBin" class="gt-ml-1 gt-mr-3">{{ store.binaryFileMessage }}</span>
66
{{ file.IsBin ? '' : file.Addition + file.Deletion }}
7-
<span v-if="!file.IsBin" class="diff-stats-bar gt-mx-3" :data-tooltip-content="statisticsMessage.replace('%d', (file.Addition + file.Deletion)).replace('%d', file.Addition).replace('%d', file.Deletion)">
7+
<span v-if="!file.IsBin" class="diff-stats-bar gt-mx-3" :data-tooltip-content="store.statisticsMessage.replace('%d', (file.Addition + file.Deletion)).replace('%d', file.Addition).replace('%d', file.Deletion)">
88
<div class="diff-stats-add-bar" :style="{ 'width': diffStatsWidth(file.Addition, file.Deletion) }"/>
99
</span>
1010
</div>
1111
<!-- todo finish all file status, now modify, add, delete and rename -->
1212
<span :class="['status', diffTypeToString(file.Type)]" :data-tooltip-content="diffTypeToString(file.Type)">&nbsp;</span>
1313
<a class="file gt-mono" :href="'#diff-' + file.NameHash">{{ file.Name }}</a>
1414
</li>
15-
<li v-if="isIncomplete" id="diff-too-many-files-stats" class="gt-pt-2">
16-
<span class="file gt-df gt-ac gt-sb">{{ tooManyFilesMessage }}
17-
<a :class="['ui', 'basic', 'tiny', 'button', isLoadingNewData === true ? 'disabled' : '']" id="diff-show-more-files-stats" @click.stop="loadMoreData">{{ showMoreMessage }}</a>
15+
<li v-if="store.isIncomplete" class="gt-pt-2">
16+
<span class="file gt-df gt-ac gt-sb">{{ store.tooManyFilesMessage }}
17+
<a :class="['ui', 'basic', 'tiny', 'button', store.isLoadingNewData ? 'disabled' : '']" @click.stop="loadMoreData">{{ store.showMoreMessage }}</a>
1818
</span>
1919
</li>
2020
</ol>
2121
</template>
2222

2323
<script>
24-
import {doLoadMoreFiles} from '../features/repo-diff.js';
25-
26-
const {pageData} = window.config;
24+
import {loadMoreFiles} from '../features/repo-diff.js';
25+
import {diffTreeStore} from '../modules/stores.js';
2726
2827
export default {
2928
data: () => {
30-
return pageData.diffFileInfo;
29+
return {store: diffTreeStore()};
3130
},
3231
mounted() {
3332
document.getElementById('show-file-list-btn').addEventListener('click', this.toggleFileList);
@@ -37,7 +36,7 @@ export default {
3736
},
3837
methods: {
3938
toggleFileList() {
40-
this.fileListIsVisible = !this.fileListIsVisible;
39+
this.store.fileListIsVisible = !this.store.fileListIsVisible;
4140
},
4241
diffTypeToString(pType) {
4342
const diffTypes = {
@@ -53,10 +52,7 @@ export default {
5352
return `${adds / (adds + dels) * 100}%`;
5453
},
5554
loadMoreData() {
56-
this.isLoadingNewData = true;
57-
doLoadMoreFiles(this.link, this.diffEnd, () => {
58-
this.isLoadingNewData = false;
59-
});
55+
loadMoreFiles(this.store.linkLoadMore);
6056
}
6157
},
6258
};

web_src/js/components/DiffFileTree.vue

+13-29
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,33 @@
11
<template>
2-
<div
3-
v-if="fileTreeIsVisible"
4-
class="gt-mr-3 gt-mt-3 diff-detail-box"
5-
>
2+
<div v-if="store.fileTreeIsVisible" class="gt-mr-3 gt-mt-3 diff-detail-box">
63
<!-- only render the tree if we're visible. in many cases this is something that doesn't change very often -->
74
<div class="ui list">
85
<DiffFileTreeItem v-for="item in fileTree" :key="item.name" :item="item"/>
96
</div>
10-
<div v-if="isIncomplete" id="diff-too-many-files-stats" class="gt-pt-2">
11-
<span class="gt-mr-2">{{ tooManyFilesMessage }}</span><a :class="['ui', 'basic', 'tiny', 'button', isLoadingNewData === true ? 'disabled' : '']" id="diff-show-more-files-stats" @click.stop="loadMoreData">{{ showMoreMessage }}</a>
7+
<div v-if="store.isIncomplete" class="gt-pt-2">
8+
<a :class="['ui', 'basic', 'tiny', 'button', store.isLoadingNewData ? 'disabled' : '']" @click.stop="loadMoreData">{{ store.showMoreMessage }}</a>
129
</div>
1310
</div>
1411
</template>
1512

1613
<script>
1714
import DiffFileTreeItem from './DiffFileTreeItem.vue';
18-
import {doLoadMoreFiles} from '../features/repo-diff.js';
15+
import {loadMoreFiles} from '../features/repo-diff.js';
1916
import {toggleElem} from '../utils/dom.js';
20-
import {DiffTreeStore} from '../modules/stores.js';
17+
import {diffTreeStore} from '../modules/stores.js';
2118
import {setFileFolding} from '../features/file-fold.js';
2219
23-
const {pageData} = window.config;
2420
const LOCAL_STORAGE_KEY = 'diff_file_tree_visible';
2521
2622
export default {
2723
components: {DiffFileTreeItem},
2824
data: () => {
29-
const fileTreeIsVisible = localStorage.getItem(LOCAL_STORAGE_KEY) === 'true';
30-
pageData.diffFileInfo.fileTreeIsVisible = fileTreeIsVisible;
31-
return {
32-
...pageData.diffFileInfo,
33-
store: DiffTreeStore,
34-
};
25+
return {store: diffTreeStore()};
3526
},
3627
computed: {
3728
fileTree() {
3829
const result = [];
39-
for (const file of this.files) {
30+
for (const file of this.store.files) {
4031
// Split file into directories
4132
const splits = file.Name.split('/');
4233
let index = 0;
@@ -98,9 +89,7 @@ export default {
9889
}
9990
},
10091
mounted() {
101-
// replace the pageData.diffFileInfo.files with our watched data so we get updates
102-
pageData.diffFileInfo.files = this.files;
103-
92+
this.store.fileTreeIsVisible = localStorage.getItem(LOCAL_STORAGE_KEY) === 'true';
10493
document.querySelector('.diff-toggle-file-tree-button').addEventListener('click', this.toggleVisibility);
10594
10695
this.hashChangeListener = () => {
@@ -124,12 +113,12 @@ export default {
124113
}
125114
},
126115
toggleVisibility() {
127-
this.updateVisibility(!this.fileTreeIsVisible);
116+
this.updateVisibility(!this.store.fileTreeIsVisible);
128117
},
129118
updateVisibility(visible) {
130-
this.fileTreeIsVisible = visible;
131-
localStorage.setItem(LOCAL_STORAGE_KEY, this.fileTreeIsVisible);
132-
this.updateState(this.fileTreeIsVisible);
119+
this.store.fileTreeIsVisible = visible;
120+
localStorage.setItem(LOCAL_STORAGE_KEY, this.store.fileTreeIsVisible);
121+
this.updateState(this.store.fileTreeIsVisible);
133122
},
134123
updateState(visible) {
135124
const btn = document.querySelector('.diff-toggle-file-tree-button');
@@ -142,12 +131,7 @@ export default {
142131
toggleElem(toHide, visible);
143132
},
144133
loadMoreData() {
145-
this.isLoadingNewData = true;
146-
doLoadMoreFiles(this.link, this.diffEnd, () => {
147-
this.isLoadingNewData = false;
148-
const {pageData} = window.config;
149-
this.diffEnd = pageData.diffFileInfo.diffEnd;
150-
});
134+
loadMoreFiles(this.store.linkLoadMore);
151135
},
152136
},
153137
};

web_src/js/components/DiffFileTreeItem.vue

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040

4141
<script>
4242
import {SvgIcon} from '../svg.js';
43-
import {DiffTreeStore} from '../modules/stores.js';
43+
import {diffTreeStore} from '../modules/stores.js';
4444
4545
export default {
4646
components: {SvgIcon},
@@ -56,7 +56,7 @@ export default {
5656
},
5757
},
5858
data: () => ({
59-
store: DiffTreeStore,
59+
store: diffTreeStore(),
6060
collapsed: false,
6161
}),
6262
methods: {

web_src/js/features/repo-diff.js

+16-23
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {initDiffFileTree} from './repo-diff-filetree.js';
55
import {validateTextareaNonEmpty} from './comp/ComboMarkdownEditor.js';
66
import {initViewedCheckboxListenerFor, countAndUpdateViewedFiles, initExpandAndCollapseFilesButton} from './pull-view-file.js';
77

8-
const {csrfToken} = window.config;
8+
const {csrfToken, pageData} = window.config;
99

1010
function initRepoDiffReviewButton() {
1111
const $reviewBox = $('#review-box');
@@ -119,37 +119,29 @@ function onShowMoreFiles() {
119119
countAndUpdateViewedFiles();
120120
}
121121

122-
export function doLoadMoreFiles(link, diffEnd, callback) {
123-
const url = `${link}?skip-to=${diffEnd}&file-only=true`;
124-
loadMoreFiles(url, callback);
125-
}
126-
127-
function loadMoreFiles(url, callback) {
122+
export function loadMoreFiles(url) {
128123
const $target = $('a#diff-show-more-files');
129-
if ($target.hasClass('disabled')) {
130-
callback();
124+
if ($target.hasClass('disabled') || pageData.diffFileInfo.isLoadingNewData) {
131125
return;
132126
}
127+
128+
pageData.diffFileInfo.isLoadingNewData = true;
133129
$target.addClass('disabled');
134130
$.ajax({
135131
type: 'GET',
136132
url,
137133
}).done((resp) => {
138-
if (!resp) {
139-
$target.removeClass('disabled');
140-
callback(resp);
141-
return;
142-
}
143-
$('#diff-incomplete').replaceWith($(resp).find('#diff-file-boxes').children());
144-
// By simply rerunning the script we add the new data to our existing
145-
// pagedata object. this triggers vue and the filetree and filelist will
146-
// render the new elements.
147-
$('body').append($(resp).find('script#diff-data-script'));
134+
const $resp = $(resp);
135+
// the response is a full HTML page, we need to extract the relevant contents:
136+
// 1. append the newly loaded file list items to the existing list
137+
$('#diff-incomplete').replaceWith($resp.find('#diff-file-boxes').children());
138+
// 2. re-execute the script to append the newly loaded items to the JS variables to refresh the DiffFileTree
139+
$('body').append($resp.find('script#diff-data-script'));
140+
148141
onShowMoreFiles();
149-
callback(resp);
150-
}).fail(() => {
142+
}).always(() => {
151143
$target.removeClass('disabled');
152-
callback();
144+
pageData.diffFileInfo.isLoadingNewData = false;
153145
});
154146
}
155147

@@ -158,7 +150,8 @@ function initRepoDiffShowMore() {
158150
e.preventDefault();
159151

160152
const $target = $(e.target);
161-
loadMoreFiles($target.data('href'), () => {});
153+
const linkLoadMore = $target.attr('data-href');
154+
loadMoreFiles(linkLoadMore);
162155
});
163156

164157
$(document).on('click', 'a.diff-load-button', (e) => {

web_src/js/modules/stores.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import {reactive} from 'vue';
22

3-
export const DiffTreeStore = reactive({
4-
selectedItem: '',
5-
});
3+
let diffTreeStoreReactive;
4+
export function diffTreeStore() {
5+
if (!diffTreeStoreReactive) {
6+
diffTreeStoreReactive = reactive(window.config.pageData.diffFileInfo);
7+
window.config.pageData.diffFileInfo = diffTreeStoreReactive;
8+
}
9+
return diffTreeStoreReactive;
10+
}

0 commit comments

Comments
 (0)