From fc1a63ae6133794330ad5c5b952f0897ee913aed Mon Sep 17 00:00:00 2001 From: gempir Date: Wed, 8 Feb 2023 22:32:27 +0100 Subject: [PATCH 1/5] add button to change width of diff view --- models/user/user.go | 7 +++++ modules/structs/user.go | 2 ++ options/locale/locale_de-DE.ini | 2 ++ options/locale/locale_en-US.ini | 2 ++ routers/web/repo/middlewares.go | 28 ++++++++++++++++++++ routers/web/web.go | 12 ++++----- templates/repo/diff/whitespace_dropdown.tmpl | 1 + templates/repo/pulls/files.tmpl | 2 +- templates/swagger/v1_json.tmpl | 8 ++++++ 9 files changed, 57 insertions(+), 7 deletions(-) diff --git a/models/user/user.go b/models/user/user.go index 0917bea7540df..219ccb3e56f84 100644 --- a/models/user/user.go +++ b/models/user/user.go @@ -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"` } @@ -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 diff --git a/modules/structs/user.go b/modules/structs/user.go index c5e96f335690a..4404c600d15d2 100644 --- a/modules/structs/user.go +++ b/modules/structs/user.go @@ -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"` @@ -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"` diff --git a/options/locale/locale_de-DE.ini b/options/locale/locale_de-DE.ini index d8874fd5fa82e..c3226793f86e5 100644 --- a/options/locale/locale_de-DE.ini +++ b/options/locale/locale_de-DE.ini @@ -2152,6 +2152,8 @@ diff.download_patch=Patch-Datei herunterladen diff.download_diff=Vergleichs-Datei herunterladen diff.show_split_view=Geteilte Ansicht diff.show_unified_view=Gesamtansicht +diff.show_full_width = Volle Breite +diff.show_compact_width = Kompakte Breite diff.whitespace_button=Leerzeichen diff.whitespace_show_everything=Alle Änderungen anzeigen diff.whitespace_ignore_all_whitespace=Ignoriere Leerzeichen beim Zeilen vergleichen diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index bc2e8cb91cfba..70caa22273689 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -2221,6 +2221,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 diff --git a/routers/web/repo/middlewares.go b/routers/web/repo/middlewares.go index 9a4aa3382cbba..26f9bcbbcedfd 100644 --- a/routers/web/repo/middlewares.go +++ b/routers/web/repo/middlewares.go @@ -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 { + ctx.ServerError("ErrUpdateDiffViewWidth", err) + } +} + // SetWhitespaceBehavior set whitespace behavior as render variable func SetWhitespaceBehavior(ctx *context.Context) { const defaultWhitespaceBehavior = "show-all" diff --git a/routers/web/web.go b/routers/web/web.go index a024c0ac37ebd..aa36736182371 100644 --- a/routers/web/web.go +++ b/routers/web/web.go @@ -1043,9 +1043,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() { @@ -1303,7 +1303,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 @@ -1334,7 +1334,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 @@ -1365,7 +1365,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) @@ -1415,7 +1415,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) diff --git a/templates/repo/diff/whitespace_dropdown.tmpl b/templates/repo/diff/whitespace_dropdown.tmpl index 47a5ab5533c34..bcd1998287458 100644 --- a/templates/repo/diff/whitespace_dropdown.tmpl +++ b/templates/repo/diff/whitespace_dropdown.tmpl @@ -29,3 +29,4 @@ {{if .IsSplitStyle}}{{svg "gitea-join"}}{{else}}{{svg "gitea-split"}}{{end}} +{{if .IsWidthFull}}{{svg "gitea-lock"}}{{else}}{{svg "gitea-unlock"}}{{end}} diff --git a/templates/repo/pulls/files.tmpl b/templates/repo/pulls/files.tmpl index e5e91bf5c33f9..ad093272c5d12 100644 --- a/templates/repo/pulls/files.tmpl +++ b/templates/repo/pulls/files.tmpl @@ -5,7 +5,7 @@
{{template "repo/header" .}} -
+