Skip to content

Commit e3a7f15

Browse files
yardenshohamsilverwinddelvhwxiaoguang
authored
Add "Copy" button to file view of raw text (#21629)
If a raw text file is displayed, a copy button of the text is enabled. * Closes #12866 ### Before ![image](https://user-images.githubusercontent.com/20454870/198898628-df1bcb0c-79d7-4ffb-95e4-441d77430827.png) ### After ![image](https://user-images.githubusercontent.com/20454870/199988152-ea1099ad-29e1-4765-a9ca-4c03c1737453.png) #### Rendered files and binaries have their button disabled ![image](https://user-images.githubusercontent.com/20454870/199988408-73de6327-5e9e-462b-b2b6-8c3f5b878386.png) ![image](https://user-images.githubusercontent.com/20454870/199988563-844f8656-f48d-4929-880e-b6558c1c054a.png) Signed-off-by: Yarden Shoham <[email protected]> Co-authored-by: silverwind <[email protected]> Co-authored-by: delvh <[email protected]> Co-authored-by: wxiaoguang <[email protected]>
1 parent 2900dc9 commit e3a7f15

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

options/locale/locale_en-US.ini

+2
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ edit = Edit
8888

8989
copy = Copy
9090
copy_url = Copy URL
91+
copy_content = Copy content
9192
copy_branch = Copy branch name
9293
copy_success = Copied!
9394
copy_error = Copy failed
@@ -1090,6 +1091,7 @@ editor.cannot_edit_non_text_files = Binary files cannot be edited in the web int
10901091
editor.edit_this_file = Edit File
10911092
editor.this_file_locked = File is locked
10921093
editor.must_be_on_a_branch = You must be on a branch to make or propose changes to this file.
1094+
editor.only_copy_raw = You may only copy raw text files.
10931095
editor.fork_before_edit = You must fork this repository to make or propose changes to this file.
10941096
editor.delete_this_file = Delete File
10951097
editor.must_have_write_access = You must have write access to make or propose changes to this file.

templates/repo/view_file.tmpl

+5
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@
6060
{{end}}
6161
</div>
6262
<a download href="{{$.RawFileLink}}"><span class="btn-octicon tooltip" data-content="{{.locale.Tr "repo.download_file"}}" data-position="bottom center">{{svg "octicon-download"}}</span></a>
63+
{{if or .IsMarkup .IsRenderedHTML (not .IsTextSource)}}
64+
<span class="btn-octicon tooltip disabled" id="copy-file-content" data-content="{{.locale.Tr "repo.editor.only_copy_raw"}}" aria-label="{{.locale.Tr "repo.editor.only_copy_raw"}}">{{svg "octicon-copy" 14}}</span>
65+
{{else}}
66+
<a class="btn-octicon tooltip" id="copy-file-content" data-content="{{.locale.Tr "copy_content"}}" aria-label="{{.locale.Tr "copy_content"}}">{{svg "octicon-copy" 14}}</a>
67+
{{end}}
6368
{{if .Repository.CanEnableEditor}}
6469
{{if .CanEditFile}}
6570
<a href="{{.RepoLink}}/_edit/{{PathEscapeSegments .BranchName}}/{{PathEscapeSegments .TreePath}}"><span class="btn-octicon tooltip" data-content="{{.EditFileTooltip}}" data-position="bottom center">{{svg "octicon-pencil"}}</span></a>

web_src/js/features/repo-code.js

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import $ from 'jquery';
22
import {svg} from '../svg.js';
33
import {invertFileFolding} from './file-fold.js';
4-
import {createTippy} from '../modules/tippy.js';
4+
import {createTippy, showTemporaryTooltip} from '../modules/tippy.js';
55
import {copyToClipboard} from './clipboard.js';
66

7+
const {i18n} = window.config;
8+
79
function changeHash(hash) {
810
if (window.history.pushState) {
911
window.history.pushState(null, null, hash);
@@ -110,6 +112,18 @@ function showLineButton() {
110112
});
111113
}
112114

115+
function initCopyFileContent() {
116+
// get raw text for copy content button, at the moment, only one button (and one related file content) is supported.
117+
const copyFileContent = document.querySelector('#copy-file-content');
118+
if (!copyFileContent) return;
119+
120+
copyFileContent.addEventListener('click', async () => {
121+
const text = Array.from(document.querySelectorAll('.file-view .lines-code')).map((el) => el.textContent).join('');
122+
const success = await copyToClipboard(text);
123+
showTemporaryTooltip(copyFileContent, success ? i18n.copy_success : i18n.copy_error);
124+
});
125+
}
126+
113127
export function initRepoCodeView() {
114128
if ($('.code-view .lines-num').length > 0) {
115129
$(document).on('click', '.lines-num span', function (e) {
@@ -185,4 +199,5 @@ export function initRepoCodeView() {
185199
if (!success) return;
186200
document.querySelector('.code-line-button')?._tippy?.hide();
187201
});
202+
initCopyFileContent();
188203
}

0 commit comments

Comments
 (0)