Skip to content

Commit a1cee9e

Browse files
committed
Merge branch 'add-file-tree-to-file-view-page' of github.com:kerwin612/gitea into kerwin612-add-file-tree-to-file-view-page
2 parents 09000c4 + 087f0d4 commit a1cee9e

File tree

6 files changed

+50
-28
lines changed

6 files changed

+50
-28
lines changed

routers/web/repo/blame.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,16 @@ func RefBlame(ctx *context.Context) {
9696
ctx.Data["FileSize"] = fileSize
9797
ctx.Data["FileName"] = blob.Name()
9898

99+
var tplName templates.TplName
100+
if ctx.FormBool("only_content") {
101+
tplName = tplRepoHomeContent
102+
} else {
103+
tplName = tplRepoHome
104+
}
105+
99106
if fileSize >= setting.UI.MaxDisplayFileSize {
100107
ctx.Data["IsFileTooLarge"] = true
101-
ctx.HTML(http.StatusOK, tplRepoHome)
108+
ctx.HTML(http.StatusOK, tplName)
102109
return
103110
}
104111

@@ -126,7 +133,7 @@ func RefBlame(ctx *context.Context) {
126133

127134
renderBlame(ctx, result.Parts, commitNames)
128135

129-
ctx.HTML(http.StatusOK, tplRepoHome)
136+
ctx.HTML(http.StatusOK, tplName)
130137
}
131138

132139
type blameResult struct {

routers/web/repo/view.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,13 @@ import (
4646
)
4747

4848
const (
49-
tplRepoEMPTY templates.TplName = "repo/empty"
50-
tplRepoHome templates.TplName = "repo/home"
51-
tplRepoViewList templates.TplName = "repo/view_list"
52-
tplWatchers templates.TplName = "repo/watchers"
53-
tplForks templates.TplName = "repo/forks"
54-
tplMigrating templates.TplName = "repo/migrate/migrating"
49+
tplRepoEMPTY templates.TplName = "repo/empty"
50+
tplRepoHome templates.TplName = "repo/home"
51+
tplRepoHomeContent templates.TplName = "repo/home_content"
52+
tplRepoViewList templates.TplName = "repo/view_list"
53+
tplWatchers templates.TplName = "repo/watchers"
54+
tplForks templates.TplName = "repo/forks"
55+
tplMigrating templates.TplName = "repo/migrate/migrating"
5556
)
5657

5758
type fileInfo struct {

routers/web/repo/view_home.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,5 +379,9 @@ func Home(ctx *context.Context) {
379379
}
380380
}
381381

382-
ctx.HTML(http.StatusOK, tplRepoHome)
382+
if ctx.FormBool("only_content") {
383+
ctx.HTML(http.StatusOK, tplRepoHomeContent)
384+
} else {
385+
ctx.HTML(http.StatusOK, tplRepoHome)
386+
}
383387
}

templates/repo/home.tmpl

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@
101101

102102
{{if not $isTreePathRoot}}
103103
{{$treeNameIdxLast := Eval $treeNamesLen "-" 1}}
104-
<span class="breadcrumb repo-path tw-ml-1">
104+
<span id="repo_path" class="breadcrumb repo-path tw-ml-1">
105105
<a class="section" href="{{.RepoLink}}/src/{{.BranchNameSubURL}}" title="{{.Repository.Name}}">{{StringUtils.EllipsisString .Repository.Name 30}}</a>
106106
{{- range $i, $v := .TreeNames -}}
107107
<span class="breadcrumb-divider">/</span>
@@ -122,25 +122,16 @@
122122
{{template "repo/clone_panel" .}}
123123
{{end}}
124124
{{if and (not $isTreePathRoot) (not .IsViewFile) (not .IsBlame)}}{{/* IsViewDirectory (not home), TODO: split the templates, avoid using "if" tricks */}}
125-
<a class="ui button" href="{{.RepoLink}}/commits/{{.BranchNameSubURL}}/{{.TreePath | PathEscapeSegments}}">
125+
<a id="path_history" class="ui button" href="{{.RepoLink}}/commits/{{.BranchNameSubURL}}/{{.TreePath | PathEscapeSegments}}">
126126
{{svg "octicon-history" 16 "tw-mr-2"}}{{ctx.Locale.Tr "repo.file_history"}}
127127
</a>
128128
{{end}}
129129
</div>
130130
</div>
131-
{{if .IsViewFile}}
132-
{{template "repo/view_file" .}}
133-
{{else if .IsBlame}}
134-
{{template "repo/blame" .}}
135-
{{else}}{{/* IsViewDirectory */}}
136-
{{if $isTreePathRoot}}
137-
{{template "repo/code/upstream_diverging_info" .}}
138-
{{end}}
139-
{{template "repo/view_list" .}}
140-
{{if and .ReadmeExist (or .IsMarkup .IsPlainText)}}
141-
{{template "repo/view_file" .}}
142-
{{end}}
143-
{{end}}
131+
132+
<div id="path_content">
133+
{{template "repo/home_content" .}}
134+
</div>
144135
</div>
145136

146137
{{if $showSidebar}}

templates/repo/home_content.tmpl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{{$treeNamesLen := len .TreeNames}}
2+
{{$isTreePathRoot := eq $treeNamesLen 0}}
3+
4+
{{if .IsViewFile}}
5+
{{template "repo/view_file" .}}
6+
{{else if .IsBlame}}
7+
{{template "repo/blame" .}}
8+
{{else}}{{/* IsViewDirectory */}}
9+
{{if $isTreePathRoot}}
10+
{{template "repo/code/upstream_diverging_info" .}}
11+
{{end}}
12+
{{template "repo/view_list" .}}
13+
{{if and .ReadmeExist (or .IsMarkup .IsPlainText)}}
14+
{{template "repo/view_file" .}}
15+
{{end}}
16+
{{end}}

web_src/js/features/repo-view-file-tree-sidebar.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,12 @@ async function toggleSidebar(visibility) {
3131
async function loadChildren(item, recursive?: boolean) {
3232
const el = document.querySelector('#view-file-tree');
3333
const apiBaseUrl = el.getAttribute('data-api-base-url');
34-
const response = await GET(`${apiBaseUrl}/tree/${item ? item.path : ''}?recursive=${recursive ?? false}`);
34+
const response = await GET(`${apiBaseUrl}/tree/${item ? item.path : ''}?ref=&recursive=${recursive ?? false}`);
3535
const json = await response.json();
3636
if (json instanceof Array) {
3737
return json.map((i) => ({
3838
name: i.name,
3939
isFile: i.isFile,
40-
htmlUrl: i.html_url,
4140
path: i.path,
4241
children: i.children,
4342
}));
@@ -46,7 +45,10 @@ async function loadChildren(item, recursive?: boolean) {
4645
}
4746

4847
async function loadContent(item) {
49-
document.querySelector('.repo-home-filelist').innerHTML = `load content of ${item.path}`;
48+
// todo: change path of `repo_path` `path_history`
49+
// load content by path (content based on home_content.tmpl)
50+
const response = await GET(`${window.location.href}?only_content=true`);
51+
document.querySelector('#path_content').innerHTML = await response.text();
5052
}
5153

5254
export async function initViewFileTreeSidebar() {
@@ -63,13 +65,14 @@ export async function initViewFileTreeSidebar() {
6365

6466
const fileTree = document.querySelector('#view-file-tree');
6567
const treePath = fileTree.getAttribute('data-tree-path');
68+
const basePath = window.location.href.replace(treePath, '');
6669
const selectedItem = ref(treePath);
6770

6871
const files = await loadChildren({path: treePath}, true);
6972

7073
fileTree.classList.remove('is-loading');
7174
const fileTreeView = createApp(ViewFileTree, {files, selectedItem, loadChildren, loadContent: (item) => {
72-
window.history.pushState(null, null, item.htmlUrl);
75+
window.history.pushState(null, null, `${basePath}${item.path}`);
7376
selectedItem.value = item.path;
7477
loadContent(item);
7578
}});

0 commit comments

Comments
 (0)