1
0
Fork 0

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>
This commit is contained in:
wxiaoguang 2023-05-30 18:53:15 +08:00 committed by GitHub
parent 32185efc14
commit ee99cf6313
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 76 additions and 100 deletions

View file

@ -45,33 +45,31 @@
{{end}} {{end}}
</div> </div>
</div> </div>
<script id="diff-data-script"> <script id="diff-data-script" type="module">
(() => { 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}}];
const diffData = { const diffData = {
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}}], isIncomplete: {{.Diff.IsIncomplete}},
isIncomplete: {{.Diff.IsIncomplete}}, tooManyFilesMessage: "{{$.locale.Tr "repo.diff.too_many_files"}}",
tooManyFilesMessage: "{{$.locale.Tr "repo.diff.too_many_files"}}", binaryFileMessage: "{{$.locale.Tr "repo.diff.bin"}}",
binaryFileMessage: "{{$.locale.Tr "repo.diff.bin"}}", showMoreMessage: "{{.locale.Tr "repo.diff.show_more"}}",
showMoreMessage: "{{.locale.Tr "repo.diff.show_more"}}", statisticsMessage: "{{.locale.Tr "repo.diff.stats_desc_file"}}",
statisticsMessage: "{{.locale.Tr "repo.diff.stats_desc_file"}}", linkLoadMore: "{{$.Link}}?skip-to={{.Diff.End}}&file-only=true",
fileTreeIsVisible: false, };
fileListIsVisible: false,
isLoadingNewData: false, // for first time loading, the diffFileInfo is a plain object
diffEnd: {{.Diff.End}}, // after the Vue component is mounted, the diffFileInfo is a reactive object
link: "{{$.Link}}" // keep in mind that this script block would be executed many times when loading more files, by "loadMoreFiles"
}; let diffFileInfo = window.config.pageData.diffFileInfo || {
if(window.config.pageData.diffFileInfo) { files:[],
// Page is already loaded - add the data to our existing data fileTreeIsVisible: false,
window.config.pageData.diffFileInfo.files.push(...diffData.files); fileListIsVisible: false,
window.config.pageData.diffFileInfo.isIncomplete = diffData.isIncomplete; isLoadingNewData: false,
window.config.pageData.diffFileInfo.diffEnd = diffData.diffEnd; selectedItem: '',
window.config.pageData.diffFileInfo.link = diffData.link; };
} else { diffFileInfo = Object.assign(diffFileInfo, diffData);
// new load of page - populate initial data diffFileInfo.files.push(...diffDataFiles);
window.config.pageData.diffFileInfo = diffData; window.config.pageData.diffFileInfo = diffFileInfo;
} </script>
})();
</script>
<div id="diff-file-list"></div> <div id="diff-file-list"></div>
<div id="diff-container"> <div id="diff-container">
<div id="diff-file-tree" class="gt-hidden"></div> <div id="diff-file-tree" class="gt-hidden"></div>

View file

@ -1,10 +1,10 @@
<template> <template>
<ol class="diff-detail-box diff-stats gt-m-0" ref="root" v-if="fileListIsVisible"> <ol class="diff-detail-box diff-stats gt-m-0" ref="root" v-if="store.fileListIsVisible">
<li v-for="file in files" :key="file.NameHash"> <li v-for="file in store.files" :key="file.NameHash">
<div class="gt-font-semibold gt-df gt-ac pull-right"> <div class="gt-font-semibold gt-df gt-ac pull-right">
<span v-if="file.IsBin" class="gt-ml-1 gt-mr-3">{{ binaryFileMessage }}</span> <span v-if="file.IsBin" class="gt-ml-1 gt-mr-3">{{ store.binaryFileMessage }}</span>
{{ file.IsBin ? '' : file.Addition + file.Deletion }} {{ file.IsBin ? '' : file.Addition + file.Deletion }}
<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)"> <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)">
<div class="diff-stats-add-bar" :style="{ 'width': diffStatsWidth(file.Addition, file.Deletion) }"/> <div class="diff-stats-add-bar" :style="{ 'width': diffStatsWidth(file.Addition, file.Deletion) }"/>
</span> </span>
</div> </div>
@ -12,22 +12,21 @@
<span :class="['status', diffTypeToString(file.Type)]" :data-tooltip-content="diffTypeToString(file.Type)">&nbsp;</span> <span :class="['status', diffTypeToString(file.Type)]" :data-tooltip-content="diffTypeToString(file.Type)">&nbsp;</span>
<a class="file gt-mono" :href="'#diff-' + file.NameHash">{{ file.Name }}</a> <a class="file gt-mono" :href="'#diff-' + file.NameHash">{{ file.Name }}</a>
</li> </li>
<li v-if="isIncomplete" id="diff-too-many-files-stats" class="gt-pt-2"> <li v-if="store.isIncomplete" class="gt-pt-2">
<span class="file gt-df gt-ac gt-sb">{{ tooManyFilesMessage }} <span class="file gt-df gt-ac gt-sb">{{ store.tooManyFilesMessage }}
<a :class="['ui', 'basic', 'tiny', 'button', isLoadingNewData === true ? 'disabled' : '']" id="diff-show-more-files-stats" @click.stop="loadMoreData">{{ showMoreMessage }}</a> <a :class="['ui', 'basic', 'tiny', 'button', store.isLoadingNewData ? 'disabled' : '']" @click.stop="loadMoreData">{{ store.showMoreMessage }}</a>
</span> </span>
</li> </li>
</ol> </ol>
</template> </template>
<script> <script>
import {doLoadMoreFiles} from '../features/repo-diff.js'; import {loadMoreFiles} from '../features/repo-diff.js';
import {diffTreeStore} from '../modules/stores.js';
const {pageData} = window.config;
export default { export default {
data: () => { data: () => {
return pageData.diffFileInfo; return {store: diffTreeStore()};
}, },
mounted() { mounted() {
document.getElementById('show-file-list-btn').addEventListener('click', this.toggleFileList); document.getElementById('show-file-list-btn').addEventListener('click', this.toggleFileList);
@ -37,7 +36,7 @@ export default {
}, },
methods: { methods: {
toggleFileList() { toggleFileList() {
this.fileListIsVisible = !this.fileListIsVisible; this.store.fileListIsVisible = !this.store.fileListIsVisible;
}, },
diffTypeToString(pType) { diffTypeToString(pType) {
const diffTypes = { const diffTypes = {
@ -53,10 +52,7 @@ export default {
return `${adds / (adds + dels) * 100}%`; return `${adds / (adds + dels) * 100}%`;
}, },
loadMoreData() { loadMoreData() {
this.isLoadingNewData = true; loadMoreFiles(this.store.linkLoadMore);
doLoadMoreFiles(this.link, this.diffEnd, () => {
this.isLoadingNewData = false;
});
} }
}, },
}; };

View file

@ -1,42 +1,33 @@
<template> <template>
<div <div v-if="store.fileTreeIsVisible" class="gt-mr-3 gt-mt-3 diff-detail-box">
v-if="fileTreeIsVisible"
class="gt-mr-3 gt-mt-3 diff-detail-box"
>
<!-- only render the tree if we're visible. in many cases this is something that doesn't change very often --> <!-- only render the tree if we're visible. in many cases this is something that doesn't change very often -->
<div class="ui list"> <div class="ui list">
<DiffFileTreeItem v-for="item in fileTree" :key="item.name" :item="item"/> <DiffFileTreeItem v-for="item in fileTree" :key="item.name" :item="item"/>
</div> </div>
<div v-if="isIncomplete" id="diff-too-many-files-stats" class="gt-pt-2"> <div v-if="store.isIncomplete" class="gt-pt-2">
<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> <a :class="['ui', 'basic', 'tiny', 'button', store.isLoadingNewData ? 'disabled' : '']" @click.stop="loadMoreData">{{ store.showMoreMessage }}</a>
</div> </div>
</div> </div>
</template> </template>
<script> <script>
import DiffFileTreeItem from './DiffFileTreeItem.vue'; import DiffFileTreeItem from './DiffFileTreeItem.vue';
import {doLoadMoreFiles} from '../features/repo-diff.js'; import {loadMoreFiles} from '../features/repo-diff.js';
import {toggleElem} from '../utils/dom.js'; import {toggleElem} from '../utils/dom.js';
import {DiffTreeStore} from '../modules/stores.js'; import {diffTreeStore} from '../modules/stores.js';
import {setFileFolding} from '../features/file-fold.js'; import {setFileFolding} from '../features/file-fold.js';
const {pageData} = window.config;
const LOCAL_STORAGE_KEY = 'diff_file_tree_visible'; const LOCAL_STORAGE_KEY = 'diff_file_tree_visible';
export default { export default {
components: {DiffFileTreeItem}, components: {DiffFileTreeItem},
data: () => { data: () => {
const fileTreeIsVisible = localStorage.getItem(LOCAL_STORAGE_KEY) === 'true'; return {store: diffTreeStore()};
pageData.diffFileInfo.fileTreeIsVisible = fileTreeIsVisible;
return {
...pageData.diffFileInfo,
store: DiffTreeStore,
};
}, },
computed: { computed: {
fileTree() { fileTree() {
const result = []; const result = [];
for (const file of this.files) { for (const file of this.store.files) {
// Split file into directories // Split file into directories
const splits = file.Name.split('/'); const splits = file.Name.split('/');
let index = 0; let index = 0;
@ -98,9 +89,7 @@ export default {
} }
}, },
mounted() { mounted() {
// replace the pageData.diffFileInfo.files with our watched data so we get updates this.store.fileTreeIsVisible = localStorage.getItem(LOCAL_STORAGE_KEY) === 'true';
pageData.diffFileInfo.files = this.files;
document.querySelector('.diff-toggle-file-tree-button').addEventListener('click', this.toggleVisibility); document.querySelector('.diff-toggle-file-tree-button').addEventListener('click', this.toggleVisibility);
this.hashChangeListener = () => { this.hashChangeListener = () => {
@ -124,12 +113,12 @@ export default {
} }
}, },
toggleVisibility() { toggleVisibility() {
this.updateVisibility(!this.fileTreeIsVisible); this.updateVisibility(!this.store.fileTreeIsVisible);
}, },
updateVisibility(visible) { updateVisibility(visible) {
this.fileTreeIsVisible = visible; this.store.fileTreeIsVisible = visible;
localStorage.setItem(LOCAL_STORAGE_KEY, this.fileTreeIsVisible); localStorage.setItem(LOCAL_STORAGE_KEY, this.store.fileTreeIsVisible);
this.updateState(this.fileTreeIsVisible); this.updateState(this.store.fileTreeIsVisible);
}, },
updateState(visible) { updateState(visible) {
const btn = document.querySelector('.diff-toggle-file-tree-button'); const btn = document.querySelector('.diff-toggle-file-tree-button');
@ -142,12 +131,7 @@ export default {
toggleElem(toHide, visible); toggleElem(toHide, visible);
}, },
loadMoreData() { loadMoreData() {
this.isLoadingNewData = true; loadMoreFiles(this.store.linkLoadMore);
doLoadMoreFiles(this.link, this.diffEnd, () => {
this.isLoadingNewData = false;
const {pageData} = window.config;
this.diffEnd = pageData.diffFileInfo.diffEnd;
});
}, },
}, },
}; };

View file

@ -40,7 +40,7 @@
<script> <script>
import {SvgIcon} from '../svg.js'; import {SvgIcon} from '../svg.js';
import {DiffTreeStore} from '../modules/stores.js'; import {diffTreeStore} from '../modules/stores.js';
export default { export default {
components: {SvgIcon}, components: {SvgIcon},
@ -56,7 +56,7 @@ export default {
}, },
}, },
data: () => ({ data: () => ({
store: DiffTreeStore, store: diffTreeStore(),
collapsed: false, collapsed: false,
}), }),
methods: { methods: {

View file

@ -5,7 +5,7 @@ import {initDiffFileTree} from './repo-diff-filetree.js';
import {validateTextareaNonEmpty} from './comp/ComboMarkdownEditor.js'; import {validateTextareaNonEmpty} from './comp/ComboMarkdownEditor.js';
import {initViewedCheckboxListenerFor, countAndUpdateViewedFiles, initExpandAndCollapseFilesButton} from './pull-view-file.js'; import {initViewedCheckboxListenerFor, countAndUpdateViewedFiles, initExpandAndCollapseFilesButton} from './pull-view-file.js';
const {csrfToken} = window.config; const {csrfToken, pageData} = window.config;
function initRepoDiffReviewButton() { function initRepoDiffReviewButton() {
const $reviewBox = $('#review-box'); const $reviewBox = $('#review-box');
@ -119,37 +119,29 @@ function onShowMoreFiles() {
countAndUpdateViewedFiles(); countAndUpdateViewedFiles();
} }
export function doLoadMoreFiles(link, diffEnd, callback) { export function loadMoreFiles(url) {
const url = `${link}?skip-to=${diffEnd}&file-only=true`;
loadMoreFiles(url, callback);
}
function loadMoreFiles(url, callback) {
const $target = $('a#diff-show-more-files'); const $target = $('a#diff-show-more-files');
if ($target.hasClass('disabled')) { if ($target.hasClass('disabled') || pageData.diffFileInfo.isLoadingNewData) {
callback();
return; return;
} }
pageData.diffFileInfo.isLoadingNewData = true;
$target.addClass('disabled'); $target.addClass('disabled');
$.ajax({ $.ajax({
type: 'GET', type: 'GET',
url, url,
}).done((resp) => { }).done((resp) => {
if (!resp) { const $resp = $(resp);
$target.removeClass('disabled'); // the response is a full HTML page, we need to extract the relevant contents:
callback(resp); // 1. append the newly loaded file list items to the existing list
return; $('#diff-incomplete').replaceWith($resp.find('#diff-file-boxes').children());
} // 2. re-execute the script to append the newly loaded items to the JS variables to refresh the DiffFileTree
$('#diff-incomplete').replaceWith($(resp).find('#diff-file-boxes').children()); $('body').append($resp.find('script#diff-data-script'));
// By simply rerunning the script we add the new data to our existing
// pagedata object. this triggers vue and the filetree and filelist will
// render the new elements.
$('body').append($(resp).find('script#diff-data-script'));
onShowMoreFiles(); onShowMoreFiles();
callback(resp); }).always(() => {
}).fail(() => {
$target.removeClass('disabled'); $target.removeClass('disabled');
callback(); pageData.diffFileInfo.isLoadingNewData = false;
}); });
} }
@ -158,7 +150,8 @@ function initRepoDiffShowMore() {
e.preventDefault(); e.preventDefault();
const $target = $(e.target); const $target = $(e.target);
loadMoreFiles($target.data('href'), () => {}); const linkLoadMore = $target.attr('data-href');
loadMoreFiles(linkLoadMore);
}); });
$(document).on('click', 'a.diff-load-button', (e) => { $(document).on('click', 'a.diff-load-button', (e) => {

View file

@ -1,5 +1,10 @@
import {reactive} from 'vue'; import {reactive} from 'vue';
export const DiffTreeStore = reactive({ let diffTreeStoreReactive;
selectedItem: '', export function diffTreeStore() {
}); if (!diffTreeStoreReactive) {
diffTreeStoreReactive = reactive(window.config.pageData.diffFileInfo);
window.config.pageData.diffFileInfo = diffTreeStoreReactive;
}
return diffTreeStoreReactive;
}