Skip to content

Commit a6168fa

Browse files
authored
Make copy/paste work for source code (#12191)
* Make copy/paste work for source code Fix regression casued by #12047 so copy/paste works properly in all browsers. Fixes #12184 Also while looking at this I saw a small display issue for blame view. I think #12023 was merged into original PR through an update branch before #12047 was merged and made one of the css ruules not apply anymore. * use pseudo-element to prevent copying of comment + symbol even when not visually selected * remove added newline here should not be necessary anymore * make sure empty line is newline so there is something to select and copy
1 parent cedbd36 commit a6168fa

File tree

7 files changed

+27
-10
lines changed

7 files changed

+27
-10
lines changed

modules/highlight/highlight.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ func NewContext() {
4444
func Code(fileName, code string) string {
4545
NewContext()
4646

47+
if code == "" {
48+
return "\n"
49+
}
4750
if len(code) > sizeLimit {
4851
return code
4952
}
@@ -133,7 +136,12 @@ func File(numLines int, fileName string, code []byte) map[int]string {
133136
m := make(map[int]string, numLines)
134137
for k, v := range strings.SplitN(htmlbuf.String(), "\n", numLines) {
135138
line := k + 1
136-
m[line] = string(v)
139+
content := string(v)
140+
//need to keep lines that are only \n so copy/paste works properly in browser
141+
if content == "" {
142+
content = "\n"
143+
}
144+
m[line] = content
137145
}
138146
return m
139147
}
@@ -143,7 +151,12 @@ func plainText(code string, numLines int) map[int]string {
143151
m := make(map[int]string, numLines)
144152
for k, v := range strings.SplitN(string(code), "\n", numLines) {
145153
line := k + 1
146-
m[line] = string(v)
154+
content := string(v)
155+
//need to keep lines that are only \n so copy/paste works properly in browser
156+
if content == "" {
157+
content = "\n"
158+
}
159+
m[line] = content
147160
}
148161
return m
149162
}

services/gitdiff/gitdiff.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -269,20 +269,20 @@ func (diffSection *DiffSection) GetComputedInlineDiffFor(diffLine *DiffLine) tem
269269
case DiffLineAdd:
270270
compareDiffLine = diffSection.GetLine(DiffLineDel, diffLine.RightIdx)
271271
if compareDiffLine == nil {
272-
return template.HTML(highlight.Code(diffSection.FileName, diffLine.Content[1:]+"\n"))
272+
return template.HTML(highlight.Code(diffSection.FileName, diffLine.Content[1:]))
273273
}
274274
diff1 = compareDiffLine.Content
275275
diff2 = diffLine.Content
276276
case DiffLineDel:
277277
compareDiffLine = diffSection.GetLine(DiffLineAdd, diffLine.LeftIdx)
278278
if compareDiffLine == nil {
279-
return template.HTML(highlight.Code(diffSection.FileName, diffLine.Content[1:]+"\n"))
279+
return template.HTML(highlight.Code(diffSection.FileName, diffLine.Content[1:]))
280280
}
281281
diff1 = diffLine.Content
282282
diff2 = compareDiffLine.Content
283283
default:
284284
if strings.IndexByte(" +-", diffLine.Content[0]) > -1 {
285-
return template.HTML(highlight.Code(diffSection.FileName, diffLine.Content[1:]+"\n"))
285+
return template.HTML(highlight.Code(diffSection.FileName, diffLine.Content[1:]))
286286
}
287287
return template.HTML(highlight.Code(diffSection.FileName, diffLine.Content))
288288
}

templates/repo/diff/box.tmpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,10 @@
139139
{{else}}
140140
<td class="lines-num lines-num-old" data-line-num="{{if $line.LeftIdx}}{{$line.LeftIdx}}{{end}}"><span rel="{{if $line.LeftIdx}}diff-{{Sha1 $file.Name}}L{{$line.LeftIdx}}{{end}}"></span></td>
141141
<td class="lines-type-marker lines-type-marker-old">{{if $line.LeftIdx}}<span class="mono" data-type-marker="{{$line.GetLineTypeMarker}}"></span>{{end}}</td>
142-
<td class="lines-code lines-code-old halfwidth">{{if and $.SignedUserID $line.CanComment $.PageIsPullFiles (not (eq .GetType 2))}}<a class="ui green button add-code-comment add-code-comment-left" data-path="{{$file.Name}}" data-side="left" data-idx="{{$line.LeftIdx}}">+</a>{{end}}<span class="mono wrap">{{if $line.LeftIdx}}{{$section.GetComputedInlineDiffFor $line}}{{end}}</span></td>
142+
<td class="lines-code lines-code-old halfwidth">{{if and $.SignedUserID $line.CanComment $.PageIsPullFiles (not (eq .GetType 2))}}<a class="ui green button add-code-comment add-code-comment-left" data-path="{{$file.Name}}" data-side="left" data-idx="{{$line.LeftIdx}}" data-type-marker="+"></a>{{end}}<span class="mono wrap">{{if $line.LeftIdx}}{{$section.GetComputedInlineDiffFor $line}}{{end}}</span></td>
143143
<td class="lines-num lines-num-new" data-line-num="{{if $line.RightIdx}}{{$line.RightIdx}}{{end}}"><span rel="{{if $line.RightIdx}}diff-{{Sha1 $file.Name}}R{{$line.RightIdx}}{{end}}"></span></td>
144144
<td class="lines-type-marker lines-type-marker-new">{{if $line.RightIdx}}<span class="mono" data-type-marker="{{$line.GetLineTypeMarker}}"></span>{{end}}</td>
145-
<td class="lines-code lines-code-new halfwidth">{{if and $.SignedUserID $line.CanComment $.PageIsPullFiles (not (eq .GetType 3))}}<a class="ui green button add-code-comment add-code-comment-right" data-path="{{$file.Name}}" data-side="right" data-idx="{{$line.RightIdx}}">+</a>{{end}}<span class="mono wrap">{{if $line.RightIdx}}{{$section.GetComputedInlineDiffFor $line}}{{end}}</span></td>
145+
<td class="lines-code lines-code-new halfwidth">{{if and $.SignedUserID $line.CanComment $.PageIsPullFiles (not (eq .GetType 3))}}<a class="ui green button add-code-comment add-code-comment-right" data-path="{{$file.Name}}" data-side="right" data-idx="{{$line.RightIdx}}" data-type-marker="+"></a>{{end}}<span class="mono wrap">{{if $line.RightIdx}}{{$section.GetComputedInlineDiffFor $line}}{{end}}</span></td>
146146
{{end}}
147147
</tr>
148148
{{if gt (len $line.Comments) 0}}

templates/repo/diff/section_unified.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
{{if eq .GetType 4}}
2323
<td class="chroma lines-code blob-hunk"><span class="mono wrap">{{$section.GetComputedInlineDiffFor $line}}</span></td>
2424
{{else}}
25-
<td class="chroma lines-code{{if (not $line.RightIdx)}} lines-code-old{{end}}">{{if and $.root.SignedUserID $line.CanComment $.root.PageIsPullFiles}}<a class="ui green button add-code-comment add-code-comment-{{if $line.RightIdx}}right{{else}}left{{end}}" data-path="{{$file.Name}}" data-side="{{if $line.RightIdx}}right{{else}}left{{end}}" data-idx="{{if $line.RightIdx}}{{$line.RightIdx}}{{else}}{{$line.LeftIdx}}{{end}}">+</a>{{end}}<span class="mono wrap">{{$section.GetComputedInlineDiffFor $line}}</span></td>
25+
<td class="chroma lines-code{{if (not $line.RightIdx)}} lines-code-old{{end}}">{{if and $.root.SignedUserID $line.CanComment $.root.PageIsPullFiles}}<a class="ui green button add-code-comment add-code-comment-{{if $line.RightIdx}}right{{else}}left{{end}}" data-path="{{$file.Name}}" data-side="{{if $line.RightIdx}}right{{else}}left{{end}}" data-idx="{{if $line.RightIdx}}{{$line.RightIdx}}{{else}}{{$line.LeftIdx}}{{end}}" data-type-marker="+"></a>{{end}}<span class="mono wrap">{{$section.GetComputedInlineDiffFor $line}}</span></td>
2626
{{end}}
2727
</tr>
2828
{{if gt (len $line.Comments) 0}}

templates/repo/home.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{{template "base/head" .}}
2-
<div class="repository file list">
2+
<div class="repository file list {{if .IsBlame}}blame{{end}}">
33
{{template "repo/header" .}}
44
<div class="ui container {{if .IsBlame}}fluid padded{{end}}">
55
{{template "base/alert" .}}

templates/repo/view_file.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@
105105
<span id="L{{$line}}" data-line-number="{{$line}}"></span>
106106
</td>
107107
<td rel="L{{$line}}" class="lines-code chroma">
108-
<pre><code>{{$code | Safe}}</code></pre>
108+
<code>{{$code | Safe}}</code>
109109
</td>
110110
</tr>
111111
{{end}}

web_src/less/_base.less

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1039,6 +1039,10 @@ i.icon.centerlock {
10391039
}
10401040
}
10411041

1042+
.lines-code code {
1043+
white-space: pre;
1044+
}
1045+
10421046
.blame .lines-num {
10431047
padding: 0 !important;
10441048
background-color: #f5f5f5;

0 commit comments

Comments
 (0)