Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add button to change width of diff view #22824

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions models/user/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ type User struct {

// Preferences
DiffViewStyle string `xorm:"NOT NULL DEFAULT ''"`
DiffViewWidth string `xorm:"NOT NULL DEFAULT ''"`
Theme string `xorm:"NOT NULL DEFAULT ''"`
KeepActivityPrivate bool `xorm:"NOT NULL DEFAULT false"`
}
Expand Down Expand Up @@ -211,6 +212,12 @@ func UpdateUserDiffViewStyle(u *User, style string) error {
return UpdateUserCols(db.DefaultContext, u, "diff_view_style")
}

// UpdateUserDiffViewWidth updates the users diff view width
func UpdateUserDiffViewWidth(u *User, width string) error {
u.DiffViewWidth = width
return UpdateUserCols(db.DefaultContext, u, "diff_view_width")
}

// UpdateUserTheme updates a users' theme irrespective of the site wide theme
func UpdateUserTheme(u *User, themeName string) error {
u.Theme = themeName
Expand Down
2 changes: 2 additions & 0 deletions modules/structs/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ type UserSettings struct {
Language string `json:"language"`
Theme string `json:"theme"`
DiffViewStyle string `json:"diff_view_style"`
DiffViewWidth string `json:"diff_view_width"`
// Privacy
HideEmail bool `json:"hide_email"`
HideActivity bool `json:"hide_activity"`
Expand All @@ -89,6 +90,7 @@ type UserSettingsOptions struct {
Language *string `json:"language"`
Theme *string `json:"theme"`
DiffViewStyle *string `json:"diff_view_style"`
DiffViewWidth *string `json:"diff_view_width"`
// Privacy
HideEmail *bool `json:"hide_email"`
HideActivity *bool `json:"hide_activity"`
Expand Down
2 changes: 2 additions & 0 deletions options/locale/locale_en-US.ini
Original file line number Diff line number Diff line change
Expand Up @@ -2222,6 +2222,8 @@ diff.download_patch = Download Patch File
diff.download_diff = Download Diff File
diff.show_split_view = Split View
diff.show_unified_view = Unified View
diff.show_full_width = Full Width
diff.show_compact_width = Compact Width
diff.whitespace_button = Whitespace
diff.whitespace_show_everything = Show all changes
diff.whitespace_ignore_all_whitespace = Ignore whitespace when comparing lines
Expand Down
28 changes: 28 additions & 0 deletions routers/web/repo/middlewares.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,34 @@ func SetDiffViewStyle(ctx *context.Context) {
}
}

// SetDiffViewWidth set the width of the diff view
func SetDiffViewWidth(ctx *context.Context) {
queryWidth := ctx.FormString("width")

if !ctx.IsSigned {
ctx.Data["IsWidthFull"] = queryWidth == "full"
return
}

var (
userWidth = ctx.Doer.DiffViewWidth
width string
)

if queryWidth == "full" || queryWidth == "compact" {
width = queryWidth
} else if userWidth == "full" || userWidth == "compact" {
width = userWidth
} else {
width = "compact"
}

ctx.Data["IsWidthFull"] = width == "full"
if err := user_model.UpdateUserDiffViewWidth(ctx.Doer, width); err != nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be a temporary change, so we shouldn't change the user's preference.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, this should be permanent. Just like split/unified view. Personally I want it always to be full.

ctx.ServerError("ErrUpdateDiffViewWidth", err)
}
}

// SetWhitespaceBehavior set whitespace behavior as render variable
func SetWhitespaceBehavior(ctx *context.Context) {
const defaultWhitespaceBehavior = "show-all"
Expand Down
12 changes: 6 additions & 6 deletions routers/web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -1044,9 +1044,9 @@ func RegisterRoutes(m *web.Route) {
m.Get("/tag/*", context.RepoRefByType(context.RepoRefTag), repo.TreeList)
m.Get("/commit/*", context.RepoRefByType(context.RepoRefCommit), repo.TreeList)
})
m.Get("/compare", repo.MustBeNotEmpty, reqRepoCodeReader, repo.SetEditorconfigIfExists, ignSignIn, repo.SetDiffViewStyle, repo.SetWhitespaceBehavior, repo.CompareDiff)
m.Get("/compare", repo.MustBeNotEmpty, reqRepoCodeReader, repo.SetEditorconfigIfExists, ignSignIn, repo.SetDiffViewStyle, repo.SetDiffViewWidth, repo.SetWhitespaceBehavior, repo.CompareDiff)
m.Combo("/compare/*", repo.MustBeNotEmpty, reqRepoCodeReader, repo.SetEditorconfigIfExists).
Get(ignSignIn, repo.SetDiffViewStyle, repo.SetWhitespaceBehavior, repo.CompareDiff).
Get(ignSignIn, repo.SetDiffViewStyle, repo.SetDiffViewWidth, repo.SetWhitespaceBehavior, repo.CompareDiff).
Post(reqSignIn, context.RepoMustNotBeArchived(), reqRepoPullsReader, repo.MustAllowPulls, web.Bind(forms.CreateIssueForm{}), repo.SetWhitespaceBehavior, repo.CompareAndPullRequestPost)
m.Group("/{type:issues|pulls}", func() {
m.Group("/{index}", func() {
Expand Down Expand Up @@ -1304,7 +1304,7 @@ func RegisterRoutes(m *web.Route) {
reqRepoWikiWriter,
web.Bind(forms.NewWikiForm{}),
repo.WikiPost)
m.Get("/commit/{sha:[a-f0-9]{7,40}}", repo.SetEditorconfigIfExists, repo.SetDiffViewStyle, repo.SetWhitespaceBehavior, repo.Diff)
m.Get("/commit/{sha:[a-f0-9]{7,40}}", repo.SetEditorconfigIfExists, repo.SetDiffViewStyle, repo.SetDiffViewWidth, repo.SetWhitespaceBehavior, repo.Diff)
m.Get("/commit/{sha:[a-f0-9]{7,40}}.{ext:patch|diff}", repo.RawDiff)
}, repo.MustEnableWiki, func(ctx *context.Context) {
ctx.Data["PageIsWiki"] = true
Expand Down Expand Up @@ -1335,7 +1335,7 @@ func RegisterRoutes(m *web.Route) {
}, repo.MustBeNotEmpty, context.RepoRef(), reqRepoCodeReader)

m.Group("/blob_excerpt", func() {
m.Get("/{sha}", repo.SetEditorconfigIfExists, repo.SetDiffViewStyle, repo.ExcerptBlob)
m.Get("/{sha}", repo.SetEditorconfigIfExists, repo.SetDiffViewStyle, repo.SetDiffViewWidth, repo.ExcerptBlob)
}, func(ctx *context.Context) (cancel gocontext.CancelFunc) {
if ctx.FormBool("wiki") {
ctx.Data["PageIsWiki"] = true
Expand Down Expand Up @@ -1366,7 +1366,7 @@ func RegisterRoutes(m *web.Route) {
m.Post("/set_allow_maintainer_edit", web.Bind(forms.UpdateAllowEditsForm{}), repo.SetAllowEdits)
m.Post("/cleanup", context.RepoMustNotBeArchived(), context.RepoRef(), repo.CleanUpPullRequest)
m.Group("/files", func() {
m.Get("", context.RepoRef(), repo.SetEditorconfigIfExists, repo.SetDiffViewStyle, repo.SetWhitespaceBehavior, repo.ViewPullFiles)
m.Get("", context.RepoRef(), repo.SetEditorconfigIfExists, repo.SetDiffViewStyle, repo.SetDiffViewWidth, repo.SetWhitespaceBehavior, repo.ViewPullFiles)
m.Group("/reviews", func() {
m.Get("/new_comment", repo.RenderNewCodeCommentForm)
m.Post("/comments", web.Bind(forms.CodeCommentForm{}), repo.CreateCodeComment)
Expand Down Expand Up @@ -1416,7 +1416,7 @@ func RegisterRoutes(m *web.Route) {

m.Group("", func() {
m.Get("/graph", repo.Graph)
m.Get("/commit/{sha:([a-f0-9]{7,40})$}", repo.SetEditorconfigIfExists, repo.SetDiffViewStyle, repo.SetWhitespaceBehavior, repo.Diff)
m.Get("/commit/{sha:([a-f0-9]{7,40})$}", repo.SetEditorconfigIfExists, repo.SetDiffViewStyle, repo.SetDiffViewWidth, repo.SetWhitespaceBehavior, repo.Diff)
m.Get("/cherry-pick/{sha:([a-f0-9]{7,40})$}", repo.SetEditorconfigIfExists, repo.CherryPick)
}, repo.MustBeNotEmpty, context.RepoRef(), reqRepoCodeReader)

Expand Down
1 change: 1 addition & 0 deletions templates/repo/diff/whitespace_dropdown.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@
</div>
</div>
<a class="ui tiny basic toggle button icon-button tooltip" href="?style={{if .IsSplitStyle}}unified{{else}}split{{end}}&whitespace={{$.WhitespaceBehavior}}" data-content="{{if .IsSplitStyle}}{{.locale.Tr "repo.diff.show_unified_view"}}{{else}}{{.locale.Tr "repo.diff.show_split_view"}}{{end}}">{{if .IsSplitStyle}}{{svg "gitea-join"}}{{else}}{{svg "gitea-split"}}{{end}}</a>
<a class="ui tiny basic toggle button icon-button tooltip" href="?width={{if .IsWidthFull}}compact{{else}}full{{end}}&whitespace={{$.WhitespaceBehavior}}" data-content="{{if .IsWidthFull}}{{.locale.Tr "repo.diff.show_compact_width"}}{{else}}{{.locale.Tr "repo.diff.show_full_width"}}{{end}}">{{if .IsWidthFull}}{{svg "gitea-lock"}}{{else}}{{svg "gitea-unlock"}}{{end}}</a>
2 changes: 1 addition & 1 deletion templates/repo/pulls/files.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<div role="main" aria-label="{{.Title}}" class="page-content repository view issue pull files diff">
{{template "repo/header" .}}
<div class="ui container {{if .IsSplitStyle}}fluid padded{{end}}">
<div class="ui container {{if .IsWidthFull}}fluid padded{{end}}">
<div class="navbar">
{{template "repo/issue/navbar" .}}
<div class="ui right">
Expand Down
8 changes: 8 additions & 0 deletions templates/swagger/v1_json.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -19838,6 +19838,10 @@
"type": "string",
"x-go-name": "DiffViewStyle"
},
"diff_view_width": {
"type": "string",
"x-go-name": "DiffViewWidth"
},
"full_name": {
"type": "string",
"x-go-name": "FullName"
Expand Down Expand Up @@ -19882,6 +19886,10 @@
"type": "string",
"x-go-name": "DiffViewStyle"
},
"diff_view_width": {
"type": "string",
"x-go-name": "DiffViewWidth"
},
"full_name": {
"type": "string",
"x-go-name": "FullName"
Expand Down