Skip to content

Commit 86ee41e

Browse files
sondr3lunny
authored andcommitted
Expandable commit bodies (#2980)
* Initial working state of expandable commit bodies * Fix all commits having showing button for multiline commits * Refactor checking multiline messages method * Force newlines with <br> in commit body * Show multiple lines in the list view of repositories * Fixed proper newlines and minor refactor Use <pre> instead of <p>, this is so we can use \n instead of having to manually place <br> into the HTML. Makes it easier to display commit bodies. * Fix commit list messages jumping around * Fix indentation in view_list.tmpl * Use vertical-align: baseline instead of top * Refactor commit button toggle function * Remove RenderCommitBodyLink function * Add comments * Add newline at the end of _repository.less * Fix long commit bodies not properly wrapping inside <pre> * Don't split on double newlines * Show the commit body in commit view * Update stylesheets * Add/fix comments and run make fmt * Fix spaces not being tabs
1 parent 4cf90aa commit 86ee41e

File tree

7 files changed

+53
-5
lines changed

7 files changed

+53
-5
lines changed

modules/templates/helper.go

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,10 @@ func NewFuncMap() []template.FuncMap {
110110
"EscapePound": func(str string) string {
111111
return strings.NewReplacer("%", "%25", "#", "%23", " ", "%20", "?", "%3F").Replace(str)
112112
},
113-
"RenderCommitMessage": RenderCommitMessage,
114-
"RenderCommitMessageLink": RenderCommitMessageLink,
113+
"RenderCommitMessage": RenderCommitMessage,
114+
"RenderCommitMessageLink": RenderCommitMessageLink,
115+
"RenderCommitBody": RenderCommitBody,
116+
"IsMultilineCommitMessage": IsMultilineCommitMessage,
115117
"ThemeColorMetaTag": func() string {
116118
return setting.UI.ThemeColorMetaTag
117119
},
@@ -280,6 +282,29 @@ func renderCommitMessage(msg string, opts markup.RenderIssueIndexPatternOptions)
280282
return template.HTML(msgLines[0])
281283
}
282284

285+
// RenderCommitBody extracts the body of a commit message without its title.
286+
func RenderCommitBody(msg, urlPrefix string, metas map[string]string) template.HTML {
287+
return renderCommitBody(msg, markup.RenderIssueIndexPatternOptions{
288+
URLPrefix: urlPrefix,
289+
Metas: metas,
290+
})
291+
}
292+
293+
func renderCommitBody(msg string, opts markup.RenderIssueIndexPatternOptions) template.HTML {
294+
cleanMsg := template.HTMLEscapeString(msg)
295+
fullMessage := string(markup.RenderIssueIndexPattern([]byte(cleanMsg), opts))
296+
body := strings.Split(strings.TrimSpace(fullMessage), "\n")
297+
if len(body) == 0 {
298+
return template.HTML("")
299+
}
300+
return template.HTML(strings.Join(body[1:], "\n"))
301+
}
302+
303+
// IsMultilineCommitMessage checks to see if a commit message contains multiple lines.
304+
func IsMultilineCommitMessage(msg string) bool {
305+
return strings.Count(strings.TrimSpace(msg), "\n") > 1
306+
}
307+
283308
// Actioner describes an action
284309
type Actioner interface {
285310
GetOpType() models.ActionType

public/css/index.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public/js/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2016,3 +2016,7 @@ function initFilterBranchTagDropdown(selector) {
20162016
});
20172017
});
20182018
}
2019+
2020+
$(".commit-button").click(function() {
2021+
$(this).parent().find('.commit-body').toggle();
2022+
});

public/less/_repository.less

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1606,3 +1606,11 @@
16061606
}
16071607
}
16081608
}
1609+
1610+
.commit-list {
1611+
vertical-align: baseline;
1612+
}
1613+
1614+
.commit-body {
1615+
white-space: pre-wrap;
1616+
}

templates/repo/commits_table.tmpl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
<th class="three wide right aligned">{{.i18n.Tr "repo.commits.date"}}</th>
3131
</tr>
3232
</thead>
33-
<tbody>
33+
<tbody class="commit-list">
3434
{{ $r:= List .Commits}}
3535
{{range $r}}
3636
<tr>
@@ -61,6 +61,10 @@
6161
</td>
6262
<td class="message collapsing">
6363
<span class="has-emoji{{if gt .ParentCount 1}} grey text{{end}}">{{RenderCommitMessage .Summary $.RepoLink $.Repository.ComposeMetas}}</span>
64+
{{if IsMultilineCommitMessage .Message}}
65+
<button class="basic compact mini ui icon button commit-button"><i class="ellipsis horizontal icon"></i></button>
66+
<pre class="commit-body" style="display: none;">{{RenderCommitBody .Message $.RepoLink $.Repository.ComposeMetas}}</pre>
67+
{{end}}
6468
{{template "repo/commit_status" .Status}}
6569
</td>
6670
<td class="grey text right aligned">{{TimeSince .Author.When $.Lang}}</td>

templates/repo/diff/page.tmpl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
{{.i18n.Tr "repo.diff.browse_source"}}
1111
</a>
1212
<h3>{{RenderCommitMessage .Commit.Message $.RepoLink $.Repository.ComposeMetas}}{{template "repo/commit_status" .CommitStatus}}</h3>
13+
{{if IsMultilineCommitMessage .Commit.Message}}
14+
<pre class="commit-body">{{RenderCommitBody .Commit.Message $.RepoLink $.Repository.ComposeMetas}}</pre>
15+
{{end}}
1316
</div>
1417
<div class="ui attached info segment {{if .Commit.Signature}} isSigned {{if .Verification.Verified }} isVerified {{end}}{{end}}">
1518
{{if .Author}}

templates/repo/view_list.tmpl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<table id="repo-files-table" class="ui fixed single line table">
22
<thead>
3-
<tr>
3+
<tr class="commit-list">
44
<th class="four wide">
55
{{if .LatestCommitUser}}
66
<img class="ui avatar image img-12" src="{{.LatestCommitUser.RelAvatarLink}}" />
@@ -28,6 +28,10 @@
2828
{{end}}
2929
</a>
3030
<span class="grey has-emoji">{{RenderCommitMessage .LatestCommit.Summary .RepoLink $.Repository.ComposeMetas}}
31+
{{if IsMultilineCommitMessage .LatestCommit.Message}}
32+
<button class="basic compact mini ui icon button commit-button"><i class="ellipsis horizontal icon"></i></button>
33+
<pre class="commit-body" style="display: none;">{{RenderCommitBody .LatestCommit.Message $.RepoLink $.Repository.ComposeMetas}}</pre>
34+
{{end}}
3135
{{template "repo/commit_status" .LatestCommitStatus}}</span>
3236
</th>
3337
<th class="nine wide">

0 commit comments

Comments
 (0)