Skip to content

Commit 59e46d4

Browse files
committed
Merge branch 'add-tree-sidebar-2-file-view' into add-file-tree-to-file-view-page
2 parents 887928e + 181645f commit 59e46d4

File tree

12 files changed

+436
-3
lines changed

12 files changed

+436
-3
lines changed

routers/web/repo/blame.go

+5
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ func RefBlame(ctx *context.Context) {
4646
return
4747
}
4848

49+
// ctx.Data["RepoPreferences"] = ctx.Session.Get("repoPreferences")
50+
ctx.Data["RepoPreferences"] = &preferencesForm{
51+
ShowFileViewTreeSidebar: true,
52+
}
53+
4954
branchLink := ctx.Repo.RepoLink + "/src/" + ctx.Repo.BranchNameSubURL()
5055
treeLink := branchLink
5156
rawLink := ctx.Repo.RepoLink + "/raw/" + ctx.Repo.BranchNameSubURL()

routers/web/repo/file.go

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright 2014 The Gogs Authors. All rights reserved.
2+
// Copyright 2018 The Gitea Authors. All rights reserved.
3+
// SPDX-License-Identifier: MIT
4+
5+
package repo
6+
7+
import (
8+
"net/http"
9+
10+
"code.gitea.io/gitea/models/unit"
11+
"code.gitea.io/gitea/modules/git"
12+
"code.gitea.io/gitea/services/context"
13+
files_service "code.gitea.io/gitea/services/repository/files"
14+
)
15+
16+
// canReadFiles returns true if repository is readable and user has proper access level.
17+
func canReadFiles(r *context.Repository) bool {
18+
return r.Permission.CanRead(unit.TypeCode)
19+
}
20+
21+
// GetContents Get the metadata and contents (if a file) of an entry in a repository, or a list of entries if a dir
22+
func GetContents(ctx *context.Context) {
23+
if !canReadFiles(ctx.Repo) {
24+
ctx.NotFound("Invalid FilePath", nil)
25+
return
26+
}
27+
28+
treePath := ctx.PathParam("*")
29+
ref := ctx.FormTrim("ref")
30+
31+
if fileList, err := files_service.GetContentsOrList(ctx, ctx.Repo.Repository, treePath, ref); err != nil {
32+
if git.IsErrNotExist(err) {
33+
ctx.NotFound("GetContentsOrList", err)
34+
return
35+
}
36+
ctx.ServerError("Repo.GitRepo.GetCommit", err)
37+
} else {
38+
ctx.JSON(http.StatusOK, fileList)
39+
}
40+
}
41+
42+
// GetContentsList Get the metadata of all the entries of the root dir
43+
func GetContentsList(ctx *context.Context) {
44+
GetContents(ctx)
45+
}

routers/web/repo/repo.go

+18
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"code.gitea.io/gitea/modules/base"
2323
"code.gitea.io/gitea/modules/cache"
2424
"code.gitea.io/gitea/modules/git"
25+
"code.gitea.io/gitea/modules/json"
2526
"code.gitea.io/gitea/modules/log"
2627
"code.gitea.io/gitea/modules/optional"
2728
repo_module "code.gitea.io/gitea/modules/repository"
@@ -758,3 +759,20 @@ func PrepareBranchList(ctx *context.Context) {
758759
}
759760
ctx.Data["Branches"] = brs
760761
}
762+
763+
type preferencesForm struct {
764+
ShowFileViewTreeSidebar bool `json:"show_file_view_tree_sidebar"`
765+
}
766+
767+
func UpdatePreferences(ctx *context.Context) {
768+
form := &preferencesForm{}
769+
if err := json.NewDecoder(ctx.Req.Body).Decode(&form); err != nil {
770+
ctx.ServerError("DecodePreferencesForm", err)
771+
return
772+
}
773+
// if err := ctx.Session.Set("repoPreferences", form); err != nil {
774+
// ctx.ServerError("Session.Set", err)
775+
// return
776+
// }
777+
ctx.JSONOK()
778+
}

routers/web/repo/view_home.go

+5
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,11 @@ func Home(ctx *context.Context) {
305305
return
306306
}
307307

308+
// ctx.Data["RepoPreferences"] = ctx.Session.Get("repoPreferences")
309+
ctx.Data["RepoPreferences"] = &preferencesForm{
310+
ShowFileViewTreeSidebar: true,
311+
}
312+
308313
title := ctx.Repo.Repository.Owner.Name + "/" + ctx.Repo.Repository.Name
309314
if len(ctx.Repo.Repository.Description) > 0 {
310315
title += ": " + ctx.Repo.Repository.Description

routers/web/web.go

+5
Original file line numberDiff line numberDiff line change
@@ -987,6 +987,7 @@ func registerRoutes(m *web.Router) {
987987
m.Get("/migrate", repo.Migrate)
988988
m.Post("/migrate", web.Bind(forms.MigrateRepoForm{}), repo.MigratePost)
989989
m.Get("/search", repo.SearchRepo)
990+
m.Put("/preferences", repo.UpdatePreferences)
990991
}, reqSignIn)
991992
// end "/repo": create, migrate, search
992993

@@ -1161,6 +1162,10 @@ func registerRoutes(m *web.Router) {
11611162
m.Get("/tag/*", context.RepoRefByType(context.RepoRefTag), repo.TreeList)
11621163
m.Get("/commit/*", context.RepoRefByType(context.RepoRefCommit), repo.TreeList)
11631164
})
1165+
m.Group("/contents", func() {
1166+
m.Get("", repo.GetContentsList)
1167+
m.Get("/*", repo.GetContents)
1168+
})
11641169
m.Get("/compare", repo.MustBeNotEmpty, repo.SetEditorconfigIfExists, repo.SetDiffViewStyle, repo.SetWhitespaceBehavior, repo.CompareDiff)
11651170
m.Combo("/compare/*", repo.MustBeNotEmpty, repo.SetEditorconfigIfExists).
11661171
Get(repo.SetDiffViewStyle, repo.SetWhitespaceBehavior, repo.CompareDiff).

templates/repo/home.tmpl

+17-3
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,24 @@
1919
{{$treeNamesLen := len .TreeNames}}
2020
{{$isTreePathRoot := eq $treeNamesLen 0}}
2121
{{$showSidebar := $isTreePathRoot}}
22-
<div class="{{Iif $showSidebar "repo-grid-filelist-sidebar" "repo-grid-filelist-only"}}">
22+
{{$hasTreeSidebar := not $isTreePathRoot}}
23+
{{$showTreeSidebar := .RepoPreferences.ShowFileViewTreeSidebar}}
24+
{{$hideTreeSidebar := not $showTreeSidebar}}
25+
{{$hasAndShowTreeSidebar := and $hasTreeSidebar $showTreeSidebar}}
26+
<div class="{{Iif $showSidebar "repo-grid-filelist-sidebar" (Iif $showTreeSidebar "repo-grid-tree-sidebar" "repo-grid-filelist-only")}}">
27+
{{if $hasTreeSidebar}}
28+
<div class="repo-view-file-tree-sidebar not-mobile {{if $hideTreeSidebar}}tw-hidden{{end}}">{{template "repo/view_file_tree_sidebar" .}}</div>
29+
{{end}}
30+
2331
<div class="repo-home-filelist">
2432
{{template "repo/sub_menu" .}}
2533
<div class="repo-button-row">
2634
<div class="repo-button-row-left">
35+
{{if $hasTreeSidebar}}
36+
<button class="show-tree-sidebar-button ui compact basic button icon not-mobile {{if $showTreeSidebar}}tw-hidden{{end}}" title="{{ctx.Locale.Tr "repo.diff.show_file_tree"}}">
37+
{{svg "octicon-sidebar-collapse" 20 "icon"}}
38+
</button>
39+
{{end}}
2740
{{$branchDropdownCurrentRefType := "branch"}}
2841
{{$branchDropdownCurrentRefShortName := .BranchName}}
2942
{{if .IsViewTag}}
@@ -40,6 +53,7 @@
4053
"RefLinkTemplate" "{RepoLink}/src/{RefType}/{RefShortName}/{TreePath}"
4154
"AllowCreateNewRef" .CanCreateBranch
4255
"ShowViewAllRefsEntry" true
56+
"ContainerClasses" (Iif $hasAndShowTreeSidebar "tw-hidden" "")
4357
}}
4458
{{if and .CanCompareOrPull .IsViewBranch (not .Repository.IsArchived)}}
4559
{{$cmpBranch := ""}}
@@ -48,7 +62,7 @@
4862
{{end}}
4963
{{$cmpBranch = print $cmpBranch (.BranchName|PathEscapeSegments)}}
5064
{{$compareLink := printf "%s/compare/%s...%s" .BaseRepo.Link (.BaseRepo.DefaultBranch|PathEscapeSegments) $cmpBranch}}
51-
<a id="new-pull-request" role="button" class="ui compact basic button" href="{{$compareLink}}"
65+
<a id="new-pull-request" role="button" class="ui compact basic button {{if $hasAndShowTreeSidebar}}tw-hidden{{end}}" href="{{$compareLink}}"
5266
data-tooltip-content="{{if .PullRequestCtx.Allowed}}{{ctx.Locale.Tr "repo.pulls.compare_changes"}}{{else}}{{ctx.Locale.Tr "action.compare_branch"}}{{end}}">
5367
{{svg "octicon-git-pull-request"}}
5468
</a>
@@ -60,7 +74,7 @@
6074
{{end}}
6175

6276
{{if and .CanWriteCode .IsViewBranch (not .Repository.IsMirror) (not .Repository.IsArchived) (not .IsViewFile)}}
63-
<button class="ui dropdown basic compact jump button"{{if not .Repository.CanEnableEditor}} disabled{{end}}>
77+
<button class="add-file-dropdown ui dropdown basic compact jump button {{if $hasAndShowTreeSidebar}}tw-hidden{{end}}"{{if not .Repository.CanEnableEditor}} disabled{{end}}>
6478
{{ctx.Locale.Tr "repo.editor.add_file"}}
6579
{{svg "octicon-triangle-down" 14 "dropdown icon"}}
6680
<div class="menu">
+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<div class="view-file-tree-sidebar-top">
2+
<div class="sidebar-header">
3+
<button class="hide-tree-sidebar-button ui compact basic button icon" title="{{ctx.Locale.Tr "repo.diff.hide_file_tree"}}">
4+
{{svg "octicon-sidebar-expand" 20 "icon"}}
5+
</button>
6+
<b> Files</b>
7+
</div>
8+
<div class="sidebar-ref">
9+
{{$branchDropdownCurrentRefType := "branch"}}
10+
{{$branchDropdownCurrentRefShortName := .BranchName}}
11+
{{if .IsViewTag}}
12+
{{$branchDropdownCurrentRefType = "tag"}}
13+
{{$branchDropdownCurrentRefShortName = .TagName}}
14+
{{end}}
15+
{{template "repo/branch_dropdown" dict
16+
"Repository" .Repository
17+
"ShowTabBranches" true
18+
"ShowTabTags" true
19+
"CurrentRefType" $branchDropdownCurrentRefType
20+
"CurrentRefShortName" $branchDropdownCurrentRefShortName
21+
"CurrentTreePath" .TreePath
22+
"RefLinkTemplate" "{RepoLink}/src/{RefType}/{RefShortName}/{TreePath}"
23+
"AllowCreateNewRef" .CanCreateBranch
24+
"ShowViewAllRefsEntry" true
25+
}}
26+
27+
{{if and .CanCompareOrPull .IsViewBranch (not .Repository.IsArchived)}}
28+
{{$cmpBranch := ""}}
29+
{{if ne .Repository.ID .BaseRepo.ID}}
30+
{{$cmpBranch = printf "%s/%s:" (.Repository.OwnerName|PathEscape) (.Repository.Name|PathEscape)}}
31+
{{end}}
32+
{{$cmpBranch = print $cmpBranch (.BranchName|PathEscapeSegments)}}
33+
{{$compareLink := printf "%s/compare/%s...%s" .BaseRepo.Link (.BaseRepo.DefaultBranch|PathEscapeSegments) $cmpBranch}}
34+
<a role="button" class="ui compact basic button" href="{{$compareLink}}"
35+
data-tooltip-content="{{if .PullRequestCtx.Allowed}}{{ctx.Locale.Tr "repo.pulls.compare_changes"}}{{else}}{{ctx.Locale.Tr "action.compare_branch"}}{{end}}">
36+
{{svg "octicon-git-pull-request"}}
37+
</a>
38+
{{end}}
39+
40+
{{if and .CanWriteCode .IsViewBranch (not .Repository.IsMirror) (not .Repository.IsArchived) (not .IsViewFile)}}
41+
<button class="ui dropdown basic compact jump button"{{if not .Repository.CanEnableEditor}} disabled{{end}}>
42+
{{ctx.Locale.Tr "repo.editor.add_file"}}
43+
{{svg "octicon-triangle-down" 14 "dropdown icon"}}
44+
<div class="menu">
45+
<a class="item" href="{{.RepoLink}}/_new/{{.BranchName | PathEscapeSegments}}/{{.TreePath | PathEscapeSegments}}">
46+
{{ctx.Locale.Tr "repo.editor.new_file"}}
47+
</a>
48+
{{if .RepositoryUploadEnabled}}
49+
<a class="item" href="{{.RepoLink}}/_upload/{{.BranchName | PathEscapeSegments}}/{{.TreePath | PathEscapeSegments}}">
50+
{{ctx.Locale.Tr "repo.editor.upload_file"}}
51+
</a>
52+
{{end}}
53+
<a class="item" href="{{.RepoLink}}/_diffpatch/{{.BranchName | PathEscapeSegments}}/{{.TreePath | PathEscapeSegments}}">
54+
{{ctx.Locale.Tr "repo.editor.patch"}}
55+
</a>
56+
</div>
57+
</button>
58+
{{end}}
59+
</div>
60+
</div>
61+
<div class="view-file-tree-sidebar-bottom">
62+
<div id="view-file-tree" class="center" data-api-base-url="{{.RepoLink}}" data-tree-path="{{$.TreePath}}">
63+
{{svg "octicon-sync" 16 "job-status-rotate"}}
64+
</div>
65+
</div>

web_src/css/repo/home.css

+43
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,49 @@
4848
}
4949
}
5050

51+
.repo-grid-tree-sidebar {
52+
display: grid;
53+
grid-template-columns: 300px auto;
54+
grid-template-rows: auto auto 1fr;
55+
}
56+
57+
.repo-grid-tree-sidebar .repo-home-filelist {
58+
min-width: 0;
59+
grid-column: 2;
60+
grid-row: 1 / 4;
61+
}
62+
63+
.repo-grid-tree-sidebar .repo-view-file-tree-sidebar {
64+
display: flex;
65+
flex-direction: column;
66+
gap: 0.25em;
67+
}
68+
69+
.repo-grid-tree-sidebar .view-file-tree-sidebar-top {
70+
display: flex;
71+
flex-direction: column;
72+
gap: 0.25em;
73+
}
74+
75+
.repo-grid-tree-sidebar .view-file-tree-sidebar-top .button {
76+
padding: 6px 10px !important;
77+
height: 30px;
78+
flex-shrink: 0;
79+
margin: 0;
80+
}
81+
82+
.repo-grid-tree-sidebar .view-file-tree-sidebar-top .sidebar-ref {
83+
display: flex;
84+
gap: 0.25em;
85+
}
86+
87+
@media (max-width: 767.98px) {
88+
.repo-grid-tree-sidebar {
89+
grid-template-columns: auto;
90+
grid-template-rows: auto auto auto;
91+
}
92+
}
93+
5194
.language-stats {
5295
display: flex;
5396
gap: 2px;
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<script lang="ts" setup>
2+
import ViewFileTreeItem from './ViewFileTreeItem.vue';
3+
4+
defineProps<{
5+
files: any,
6+
selectedItem: string,
7+
loadChildren: any,
8+
}>();
9+
</script>
10+
11+
<template>
12+
<div class="view-file-tree-items">
13+
<!-- only render the tree if we're visible. in many cases this is something that doesn't change very often -->
14+
<ViewFileTreeItem v-for="item in files" :key="item.name" :item="item" :selected-item="selectedItem" :load-children="loadChildren"/>
15+
</div>
16+
</template>
17+
18+
<style scoped>
19+
.view-file-tree-items {
20+
display: flex;
21+
flex-direction: column;
22+
gap: 1px;
23+
margin-right: .5rem;
24+
}
25+
</style>

0 commit comments

Comments
 (0)