From e3a7f15791db3c98bb80136a133748223fc26e05 Mon Sep 17 00:00:00 2001 From: Yarden Shoham Date: Fri, 4 Nov 2022 21:33:50 +0200 Subject: 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 Co-authored-by: silverwind Co-authored-by: delvh Co-authored-by: wxiaoguang --- web_src/js/features/repo-code.js | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) (limited to 'web_src') diff --git a/web_src/js/features/repo-code.js b/web_src/js/features/repo-code.js index 002a25f6ed..ad27036372 100644 --- a/web_src/js/features/repo-code.js +++ b/web_src/js/features/repo-code.js @@ -1,9 +1,11 @@ import $ from 'jquery'; import {svg} from '../svg.js'; import {invertFileFolding} from './file-fold.js'; -import {createTippy} from '../modules/tippy.js'; +import {createTippy, showTemporaryTooltip} from '../modules/tippy.js'; import {copyToClipboard} from './clipboard.js'; +const {i18n} = window.config; + function changeHash(hash) { if (window.history.pushState) { window.history.pushState(null, null, hash); @@ -110,6 +112,18 @@ function showLineButton() { }); } +function initCopyFileContent() { + // get raw text for copy content button, at the moment, only one button (and one related file content) is supported. + const copyFileContent = document.querySelector('#copy-file-content'); + if (!copyFileContent) return; + + copyFileContent.addEventListener('click', async () => { + const text = Array.from(document.querySelectorAll('.file-view .lines-code')).map((el) => el.textContent).join(''); + const success = await copyToClipboard(text); + showTemporaryTooltip(copyFileContent, success ? i18n.copy_success : i18n.copy_error); + }); +} + export function initRepoCodeView() { if ($('.code-view .lines-num').length > 0) { $(document).on('click', '.lines-num span', function (e) { @@ -185,4 +199,5 @@ export function initRepoCodeView() { if (!success) return; document.querySelector('.code-line-button')?._tippy?.hide(); }); + initCopyFileContent(); } -- cgit v1.2.3