1// SPDX-License-Identifier: AGPL-3.0-or-later
2
3package web
4
5// Commit is a commit: the message and facts, the diff tools, the diff.
6templ Commit(l Layout, p CommitPage) {
7 @Shell(l, commitRail(p)) {
8 <header class="show">
9 <h1>{ p.Subject }</h1>
10 if p.Body != "" {
11 <pre class="msg">{ p.Body }</pre>
12 }
13 <dl class="kv show-kv">
14 <dt>commit</dt>
15 <dd>{ p.SHA }</dd>
16 <dt>author</dt>
17 <dd>@personView(p.Author)</dd>
18 if p.Committer != nil {
19 <dt>committer</dt>
20 <dd>@personView(*p.Committer)</dd>
21 }
22 <dt>
23 if p.Merge {
24 merge of
25 } else {
26 parent
27 }
28 </dt>
29 <dd>
30 if p.Root {
31 none (root commit)
32 }
33 for i, par := range p.Parents {
34 if i > 0 {
35 { " + " }
36 }
37 <a href={ templ.SafeURL(par.Href) }>{ par.Label }</a>
38 }
39 if p.Merge {
40 { " · diff against the first parent" }
41 }
42 </dd>
43 if len(p.Refs) > 0 {
44 <dt>refs</dt>
45 <dd>
46 for _, r := range p.Refs {
47 <span class="pill">{ r }</span>
48 }
49 </dd>
50 }
51 </dl>
52 </header>
53 <div class="diff-tools" role="toolbar" aria-label="Diff view">
54 <span class="dim">{ plural(p.NFiles, "file", "files") } · <span class="plus">+{ itoa(p.Adds) }</span> <span class="minus">-{ itoa(p.Dels) }</span></span>
55 <span class="grow"></span>
56 @viewLink(p.DiffView, "unified", "Unified", "u", p.UnifiedHref)
57 @viewLink(p.DiffView, "split", "Split", "d", p.SplitHref)
58 @collapseButton()
59 </div>
60 if p.Invisible {
61 <p class="notice">
62 this diff holds invisible characters (direction controls, zero-width characters). They are shown as
63 <span class="inv"><U+XXXX></span> markers, never applied.
64 </p>
65 }
66 if p.Note != "" {
67 <p class="notice">{ p.Note }</p>
68 }
69 <div
70 id="diffs"
71 if p.Both {
72 data-diff={ p.View }
73 } else {
74 data-view={ p.View }
75 }
76 if p.Both && !p.Pinned {
77 hx-live="this.dataset.diff = sessionStorage.getItem('diff') || this.dataset.diff"
78 }
79 >
80 for _, f := range p.Files {
81 @DiffFileView(f, p.DiffView)
82 }
83 if len(p.Files) == 0 {
84 <p class="note">no changed files{ " " }
85 if p.Merge {
86 against the first parent.
87 } else {
88 in this commit.
89 }
90 </p>
91 }
92 </div>
93 }
94}
95
96// viewLink is a button of the diff tools. It is a link, so that the split
97// layout works without JavaScript; with it, and when both layouts are in the
98// page, a click only flips the layout.
99templ viewLink(p DiffView, view, label, key, href string) {
100 <a
101 href={ templ.SafeURL(href) }
102 class="btn vbtn"
103 data-view={ view }
104 aria-keyshortcuts={ key }
105 if p.View == view {
106 aria-current="true"
107 }
108 if p.Both {
109 hx-on:click="event.preventDefault(); document.body.dispatchEvent(new CustomEvent('k-' + this.dataset.view))"
110 :aria-current={ "q('#diffs').attr['data-diff'] === '" + view + "' ? 'true' : false" }
111 }
112 >{ label }<kbd aria-hidden="true">{ key }</kbd></a>
113}
114
115// collapseButton folds every file of the diff, or opens them all again when
116// none is open; its label follows the state.
117templ collapseButton() {
118 <button type="button" aria-keyshortcuts="x" hx-on:click="document.body.dispatchEvent(new CustomEvent('k-collapse'))" class="btn">
119 <span hx-live="this.textContent = q('[data-file][open]').count || !q('[data-file]').count ? 'Collapse all' : 'Expand all'">Collapse all</span><kbd aria-hidden="true">x</kbd>
120 </button>
121}
122
123templ personView(pr Person) {
124 { pr.Name }
125 if pr.WhenAt != "" {
126 { " · " }<time datetime={ pr.WhenAt } title={ pr.Abs }>{ pr.When }</time>{ " · " + pr.Abs }
127 }
128}
129
130templ commitRail(p CommitPage) {
131 <section aria-labelledby="r-files">
132 <h2 id="r-files">Files <span class="count">{ itoa(len(p.Files)) }</span></h2>
133 @FileList(p.Files)
134 </section>
135}
136
137// FileList is the table of contents of a diff: status letter, path, +n -m
138// and an ASCII bar. The row of the file in view is marked.
139templ FileList(files []DiffFile) {
140 <ol class="files" aria-label="Changed files">
141 for _, f := range files {
142 <li>
143 <a
144 href={ templ.SafeURL("#" + f.ID) }
145 :aria-current={ "(() => { let all = q('.dfile').arr(); let cur = all.filter(f => f.getBoundingClientRect().top < innerHeight / 3).at(-1) || all[0]; return cur?.id === '" + f.ID + "' ? 'true' : false })()" }
146 hx-on:click="document.getElementById(this.hash.slice(1)).open = true"
147 >
148 <span class="st" title={ f.Status }>{ f.Status }</span>
149 <span class="p" title={ f.Path }>{ f.Path }</span>
150 <span class="n"><span class="plus">+{ itoa(f.Add) }</span> <span class="minus">-{ itoa(f.Del) }</span></span>
151 <span class="b" aria-hidden="true"><span class="plus">{ f.Bar.Add }</span><span class="minus">{ f.Bar.Del }</span>{ f.Bar.Rest }</span>
152 </a>
153 </li>
154 }
155 </ol>
156}
157
158// DiffFileView is one file of the diff: a <details> (per-file collapse). The
159// unified and the split tables are both in it; #diffs[data-diff] shows one.
160// Every changed line keeps its +/- marker, so a change is never conveyed by
161// colour alone.
162templ DiffFileView(f DiffFile, p DiffView) {
163 <details
164 class="dfile"
165 id={ f.ID }
166 data-file
167 if f.Open {
168 open
169 }
170 >
171 <summary>
172 <span class="st">{ f.Status }</span>{ " " }<span class="p">{ f.Path }</span>{ " " }<span class="plus">+{ itoa(f.Add) }</span>{ " " }<span class="minus">-{ itoa(f.Del) }</span>
173 if f.Note != "" {
174 { " " }<span class="note">{ f.Note }</span>
175 }
176 </summary>
177 if len(f.Hunks) > 0 {
178 <div class="dscroll">
179 if p.ShowUnified {
180 <table class="dt uni" aria-label={ f.Path + ", unified" }>
181 for _, h := range f.Hunks {
182 <tbody>
183 <tr class="hunk"><td colspan="4">{ h.Header }</td></tr>
184 for _, ln := range h.Lines {
185 <tr class={ ln.Kind }>
186 <td class="ln">@gutter(ln.Old, ln.OldHref)</td>
187 <td class="ln">@gutter(ln.New, ln.NewHref)</td>
188 <td class="m">{ ln.Mark }</td>
189 <td class="tx">@codeLine(ln)</td>
190 </tr>
191 if len(ln.Notes) > 0 || ln.Form != nil {
192 <tr class="fnote">
193 <td colspan="3"></td>
194 <td>@lineNotes(ln.Notes, ln.Form, "")</td>
195 </tr>
196 }
197 }
198 </tbody>
199 }
200 </table>
201 }
202 if p.ShowSplit {
203 <table class="dt spl" aria-label={ f.Path + ", split" }>
204 <colgroup><col class="c-ln"/><col class="c-m"/><col/><col class="c-ln"/><col class="c-m"/><col/></colgroup>
205 for _, h := range f.Hunks {
206 <tbody>
207 <tr class="hunk"><td colspan="6">{ h.Header }</td></tr>
208 for _, row := range h.Split {
209 <tr>
210 @splitSide(row.L, true)
211 @splitSide(row.R, false)
212 </tr>
213 if notes, form := row.Notes(), row.Form(); len(notes) > 0 || form != nil {
214 <tr class="fnote">
215 <td colspan="6">@lineNotes(notes, form, "-s")</td>
216 </tr>
217 }
218 }
219 </tbody>
220 }
221 </table>
222 }
223 </div>
224 } else {
225 <p class="note pad">
226 if f.Note != "" {
227 { f.Note }
228 } else {
229 no textual change.
230 }
231 </p>
232 }
233 </details>
234}
235
236// splitSide is one half of a row of the split view; a side without a line is
237// an empty cell.
238templ splitSide(ln *DiffLine, old bool) {
239 if ln == nil {
240 <td class="ln none"></td>
241 <td class="m none"></td>
242 <td class="tx none"></td>
243 } else {
244 <td class={ "ln", ln.Kind }>
245 if old {
246 @gutter(ln.Old, ln.OldHref)
247 } else {
248 @gutter(ln.New, ln.NewHref)
249 }
250 </td>
251 <td class={ "m", ln.Kind }>{ ln.Mark }</td>
252 <td class={ "tx", ln.Kind }>@codeLine(*ln)</td>
253 }
254}
255
256// gutter is a line number of the gutter. On a merge request that can be
257// commented on, it links to the form that comments on that line.
258templ gutter(n, href string) {
259 if href != "" {
260 <a href={ templ.SafeURL(href) } title="comment on this line">{ n }</a>
261 } else {
262 { n }
263 }
264}