1
1
mirror of https://github.com/go-gitea/gitea synced 2024-12-23 00:54:28 +00:00

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
This commit is contained in:
mrsdizzie 2020-07-08 17:02:38 -04:00 committed by GitHub
parent cedbd3684f
commit a6168fa25d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 27 additions and 10 deletions

View File

@ -44,6 +44,9 @@ func NewContext() {
func Code(fileName, code string) string { func Code(fileName, code string) string {
NewContext() NewContext()
if code == "" {
return "\n"
}
if len(code) > sizeLimit { if len(code) > sizeLimit {
return code return code
} }
@ -133,7 +136,12 @@ func File(numLines int, fileName string, code []byte) map[int]string {
m := make(map[int]string, numLines) m := make(map[int]string, numLines)
for k, v := range strings.SplitN(htmlbuf.String(), "\n", numLines) { for k, v := range strings.SplitN(htmlbuf.String(), "\n", numLines) {
line := k + 1 line := k + 1
m[line] = string(v) content := string(v)
//need to keep lines that are only \n so copy/paste works properly in browser
if content == "" {
content = "\n"
}
m[line] = content
} }
return m return m
} }
@ -143,7 +151,12 @@ func plainText(code string, numLines int) map[int]string {
m := make(map[int]string, numLines) m := make(map[int]string, numLines)
for k, v := range strings.SplitN(string(code), "\n", numLines) { for k, v := range strings.SplitN(string(code), "\n", numLines) {
line := k + 1 line := k + 1
m[line] = string(v) content := string(v)
//need to keep lines that are only \n so copy/paste works properly in browser
if content == "" {
content = "\n"
}
m[line] = content
} }
return m return m
} }

View File

@ -269,20 +269,20 @@ func (diffSection *DiffSection) GetComputedInlineDiffFor(diffLine *DiffLine) tem
case DiffLineAdd: case DiffLineAdd:
compareDiffLine = diffSection.GetLine(DiffLineDel, diffLine.RightIdx) compareDiffLine = diffSection.GetLine(DiffLineDel, diffLine.RightIdx)
if compareDiffLine == nil { if compareDiffLine == nil {
return template.HTML(highlight.Code(diffSection.FileName, diffLine.Content[1:]+"\n")) return template.HTML(highlight.Code(diffSection.FileName, diffLine.Content[1:]))
} }
diff1 = compareDiffLine.Content diff1 = compareDiffLine.Content
diff2 = diffLine.Content diff2 = diffLine.Content
case DiffLineDel: case DiffLineDel:
compareDiffLine = diffSection.GetLine(DiffLineAdd, diffLine.LeftIdx) compareDiffLine = diffSection.GetLine(DiffLineAdd, diffLine.LeftIdx)
if compareDiffLine == nil { if compareDiffLine == nil {
return template.HTML(highlight.Code(diffSection.FileName, diffLine.Content[1:]+"\n")) return template.HTML(highlight.Code(diffSection.FileName, diffLine.Content[1:]))
} }
diff1 = diffLine.Content diff1 = diffLine.Content
diff2 = compareDiffLine.Content diff2 = compareDiffLine.Content
default: default:
if strings.IndexByte(" +-", diffLine.Content[0]) > -1 { if strings.IndexByte(" +-", diffLine.Content[0]) > -1 {
return template.HTML(highlight.Code(diffSection.FileName, diffLine.Content[1:]+"\n")) return template.HTML(highlight.Code(diffSection.FileName, diffLine.Content[1:]))
} }
return template.HTML(highlight.Code(diffSection.FileName, diffLine.Content)) return template.HTML(highlight.Code(diffSection.FileName, diffLine.Content))
} }

View File

@ -139,10 +139,10 @@
{{else}} {{else}}
<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> <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>
<td class="lines-type-marker lines-type-marker-old">{{if $line.LeftIdx}}<span class="mono" data-type-marker="{{$line.GetLineTypeMarker}}"></span>{{end}}</td> <td class="lines-type-marker lines-type-marker-old">{{if $line.LeftIdx}}<span class="mono" data-type-marker="{{$line.GetLineTypeMarker}}"></span>{{end}}</td>
<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> <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>
<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> <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>
<td class="lines-type-marker lines-type-marker-new">{{if $line.RightIdx}}<span class="mono" data-type-marker="{{$line.GetLineTypeMarker}}"></span>{{end}}</td> <td class="lines-type-marker lines-type-marker-new">{{if $line.RightIdx}}<span class="mono" data-type-marker="{{$line.GetLineTypeMarker}}"></span>{{end}}</td>
<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> <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>
{{end}} {{end}}
</tr> </tr>
{{if gt (len $line.Comments) 0}} {{if gt (len $line.Comments) 0}}

View File

@ -22,7 +22,7 @@
{{if eq .GetType 4}} {{if eq .GetType 4}}
<td class="chroma lines-code blob-hunk"><span class="mono wrap">{{$section.GetComputedInlineDiffFor $line}}</span></td> <td class="chroma lines-code blob-hunk"><span class="mono wrap">{{$section.GetComputedInlineDiffFor $line}}</span></td>
{{else}} {{else}}
<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> <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>
{{end}} {{end}}
</tr> </tr>
{{if gt (len $line.Comments) 0}} {{if gt (len $line.Comments) 0}}

View File

@ -1,5 +1,5 @@
{{template "base/head" .}} {{template "base/head" .}}
<div class="repository file list"> <div class="repository file list {{if .IsBlame}}blame{{end}}">
{{template "repo/header" .}} {{template "repo/header" .}}
<div class="ui container {{if .IsBlame}}fluid padded{{end}}"> <div class="ui container {{if .IsBlame}}fluid padded{{end}}">
{{template "base/alert" .}} {{template "base/alert" .}}

View File

@ -105,7 +105,7 @@
<span id="L{{$line}}" data-line-number="{{$line}}"></span> <span id="L{{$line}}" data-line-number="{{$line}}"></span>
</td> </td>
<td rel="L{{$line}}" class="lines-code chroma"> <td rel="L{{$line}}" class="lines-code chroma">
<pre><code>{{$code | Safe}}</code></pre> <code>{{$code | Safe}}</code>
</td> </td>
</tr> </tr>
{{end}} {{end}}

View File

@ -1039,6 +1039,10 @@ i.icon.centerlock {
} }
} }
.lines-code code {
white-space: pre;
}
.blame .lines-num { .blame .lines-num {
padding: 0 !important; padding: 0 !important;
background-color: #f5f5f5; background-color: #f5f5f5;