From 28993e9a68d83a4f1e6ba2fa9fc679797c458ca5 Mon Sep 17 00:00:00 2001 From: Yarden Shoham Date: Sat, 29 Oct 2022 10:18:59 +0000 Subject: [PATCH 01/13] Add "Copy" button to file view The clipboard functionality is now extended with the ability to fetch text before copying it to the clipboard. Signed-off-by: Yarden Shoham --- templates/repo/view_file.tmpl | 5 ++++- web_src/js/features/clipboard.js | 22 +++++++++++++++++++--- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/templates/repo/view_file.tmpl b/templates/repo/view_file.tmpl index 2b4f279cb6e36..55d391ed72bf4 100644 --- a/templates/repo/view_file.tmpl +++ b/templates/repo/view_file.tmpl @@ -46,7 +46,10 @@ {{end}} {{if not .ReadmeInList}}
- {{.locale.Tr "repo.file_raw"}} + + {{.locale.Tr "repo.file_raw"}} {{if not .IsViewCommit}} {{.locale.Tr "repo.file_permalink"}} {{end}} diff --git a/web_src/js/features/clipboard.js b/web_src/js/features/clipboard.js index 85324303e347e..8ca6612e73962 100644 --- a/web_src/js/features/clipboard.js +++ b/web_src/js/features/clipboard.js @@ -2,7 +2,15 @@ import {showTemporaryTooltip} from '../modules/tippy.js'; const {copy_success, copy_error} = window.config.i18n; -export async function copyToClipboard(text) { +export async function copyToClipboard(text, shouldFetchForText) { + if (shouldFetchForText) { + try { + const response = await fetch(text, {method: 'GET', redirect: 'follow'}); + text = await response.text(); + } catch { + console.error(`failed to fetch text from ${text}`); + } + } try { await navigator.clipboard.writeText(text); } catch { @@ -39,19 +47,27 @@ function fallbackCopyToClipboard(text) { // For all DOM elements with [data-clipboard-target] or [data-clipboard-text], // this copy-to-clipboard will work for them +// If the target has the attribute [data-fetch-text] set to true, a fetch call happens to get the content export default function initGlobalCopyToClipboardListener() { document.addEventListener('click', (e) => { let target = e.target; // in case , so we just search // up to 3 levels for performance for (let i = 0; i < 3 && target; i++) { - const text = target.getAttribute('data-clipboard-text') || document.querySelector(target.getAttribute('data-clipboard-target'))?.value; + const shouldFetchForText = target.getAttribute('data-fetch-text') === 'true'; + let text; + const {clipboardTarget} = target.dataset; + if (shouldFetchForText) { + text = document.querySelector(clipboardTarget)?.href; + } else { + text = target.getAttribute('data-clipboard-text') || document.querySelector(clipboardTarget)?.value; + } if (text) { e.preventDefault(); (async() => { - const success = await copyToClipboard(text); + const success = await copyToClipboard(text, shouldFetchForText); showTemporaryTooltip(target, success ? copy_success : copy_error); })(); From 342b8742f00b6d40508478f6203fb7dee662813f Mon Sep 17 00:00:00 2001 From: Yarden Shoham Date: Sun, 30 Oct 2022 19:03:49 +0000 Subject: [PATCH 02/13] Revert `clipboard.js` changes --- web_src/js/features/clipboard.js | 22 +++------------------- 1 file changed, 3 insertions(+), 19 deletions(-) diff --git a/web_src/js/features/clipboard.js b/web_src/js/features/clipboard.js index 8ca6612e73962..85324303e347e 100644 --- a/web_src/js/features/clipboard.js +++ b/web_src/js/features/clipboard.js @@ -2,15 +2,7 @@ import {showTemporaryTooltip} from '../modules/tippy.js'; const {copy_success, copy_error} = window.config.i18n; -export async function copyToClipboard(text, shouldFetchForText) { - if (shouldFetchForText) { - try { - const response = await fetch(text, {method: 'GET', redirect: 'follow'}); - text = await response.text(); - } catch { - console.error(`failed to fetch text from ${text}`); - } - } +export async function copyToClipboard(text) { try { await navigator.clipboard.writeText(text); } catch { @@ -47,27 +39,19 @@ function fallbackCopyToClipboard(text) { // For all DOM elements with [data-clipboard-target] or [data-clipboard-text], // this copy-to-clipboard will work for them -// If the target has the attribute [data-fetch-text] set to true, a fetch call happens to get the content export default function initGlobalCopyToClipboardListener() { document.addEventListener('click', (e) => { let target = e.target; // in case , so we just search // up to 3 levels for performance for (let i = 0; i < 3 && target; i++) { - const shouldFetchForText = target.getAttribute('data-fetch-text') === 'true'; - let text; - const {clipboardTarget} = target.dataset; - if (shouldFetchForText) { - text = document.querySelector(clipboardTarget)?.href; - } else { - text = target.getAttribute('data-clipboard-text') || document.querySelector(clipboardTarget)?.value; - } + const text = target.getAttribute('data-clipboard-text') || document.querySelector(target.getAttribute('data-clipboard-target'))?.value; if (text) { e.preventDefault(); (async() => { - const success = await copyToClipboard(text, shouldFetchForText); + const success = await copyToClipboard(text); showTemporaryTooltip(target, success ? copy_success : copy_error); })(); From b36d2d385689f5e47ef688182bf61c294303af07 Mon Sep 17 00:00:00 2001 From: Yarden Shoham Date: Sun, 30 Oct 2022 19:21:30 +0000 Subject: [PATCH 03/13] Use "Copy content" instead of "Copy" --- options/locale/locale_en-US.ini | 1 + templates/repo/view_file.tmpl | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index 1566dfc97d422..77982372fd2e4 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -88,6 +88,7 @@ edit = Edit copy = Copy copy_url = Copy URL +copy_content = Copy content copy_branch = Copy branch name copy_success = Copied! copy_error = Copy failed diff --git a/templates/repo/view_file.tmpl b/templates/repo/view_file.tmpl index 55d391ed72bf4..62946cf56e75f 100644 --- a/templates/repo/view_file.tmpl +++ b/templates/repo/view_file.tmpl @@ -46,10 +46,10 @@ {{end}} {{if not .ReadmeInList}}
- - {{.locale.Tr "repo.file_raw"}} + {{.locale.Tr "repo.file_raw"}} {{if not .IsViewCommit}} {{.locale.Tr "repo.file_permalink"}} {{end}} From 4fd9ebad17a69b0a47ff9c1d67b6988b25b31fb8 Mon Sep 17 00:00:00 2001 From: Yarden Shoham Date: Sun, 30 Oct 2022 19:39:20 +0000 Subject: [PATCH 04/13] Grab text from DOM Co-authored-by: silverwind --- web_src/js/features/common-global.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/web_src/js/features/common-global.js b/web_src/js/features/common-global.js index a3aebc024625c..8e5d36ea1c5f2 100644 --- a/web_src/js/features/common-global.js +++ b/web_src/js/features/common-global.js @@ -343,6 +343,12 @@ export function initGlobalButtons() { window.location.href = $this.attr('data-done-url'); }); }); + + // get raw text for copy content button + const text = Array.from(document.querySelectorAll('.lines-code')).map((el) => el.textContent).join(''); + for (const copyContentButton of document.querySelectorAll('button.copy-content')) { + copyContentButton.setAttribute('data-clipboard-text', text); + } } /** From a2fd11077b6197ac5a60601d4c35a2eacd818f28 Mon Sep 17 00:00:00 2001 From: Yarden Shoham Date: Sun, 30 Oct 2022 19:39:58 +0000 Subject: [PATCH 05/13] Render button only on raw text --- templates/repo/view_file.tmpl | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/templates/repo/view_file.tmpl b/templates/repo/view_file.tmpl index 62946cf56e75f..224b1415d42e8 100644 --- a/templates/repo/view_file.tmpl +++ b/templates/repo/view_file.tmpl @@ -46,7 +46,11 @@ {{end}} {{if not .ReadmeInList}}
- {{.locale.Tr "repo.file_raw"}} From 1bce5cee7994e0303187b2d04bf43faf1a2bec7b Mon Sep 17 00:00:00 2001 From: Yarden Shoham Date: Sun, 30 Oct 2022 19:42:47 +0000 Subject: [PATCH 06/13] Move button to the end --- templates/repo/view_file.tmpl | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/templates/repo/view_file.tmpl b/templates/repo/view_file.tmpl index 224b1415d42e8..3af566e4b8275 100644 --- a/templates/repo/view_file.tmpl +++ b/templates/repo/view_file.tmpl @@ -46,13 +46,6 @@ {{end}} {{if not .ReadmeInList}}
- {{.locale.Tr "repo.file_raw"}} {{if not .IsViewCommit}} {{.locale.Tr "repo.file_permalink"}} @@ -65,6 +58,13 @@ {{.locale.Tr "repo.escape_control_characters"}} {{end}} +
{{svg "octicon-download"}} {{if .Repository.CanEnableEditor}} From 7cd16ae1f4f2b1fe433a60fac27b43a75ded8639 Mon Sep 17 00:00:00 2001 From: Yarden Shoham Date: Sun, 30 Oct 2022 23:39:00 +0200 Subject: [PATCH 07/13] Update web_src/js/features/common-global.js Co-authored-by: delvh --- web_src/js/features/common-global.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/web_src/js/features/common-global.js b/web_src/js/features/common-global.js index 8e5d36ea1c5f2..66a1588bea530 100644 --- a/web_src/js/features/common-global.js +++ b/web_src/js/features/common-global.js @@ -345,8 +345,10 @@ export function initGlobalButtons() { }); // get raw text for copy content button + const copyContentButtons = document.querySelectorAll('button.copy-content'); + if (copyContentButtons.length == 0) return; const text = Array.from(document.querySelectorAll('.lines-code')).map((el) => el.textContent).join(''); - for (const copyContentButton of document.querySelectorAll('button.copy-content')) { + for (const copyContentButton of copyContentButtons) { copyContentButton.setAttribute('data-clipboard-text', text); } } From 83a41f08fedd9b3bed065e3cec6bf93cdb8c6ce9 Mon Sep 17 00:00:00 2001 From: Yarden Shoham Date: Sun, 30 Oct 2022 23:11:25 +0000 Subject: [PATCH 08/13] Fkx lint Signed-off-by: Yarden Shoham --- web_src/js/features/common-global.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/web_src/js/features/common-global.js b/web_src/js/features/common-global.js index 66a1588bea530..13cf995057ce0 100644 --- a/web_src/js/features/common-global.js +++ b/web_src/js/features/common-global.js @@ -346,10 +346,11 @@ export function initGlobalButtons() { // get raw text for copy content button const copyContentButtons = document.querySelectorAll('button.copy-content'); - if (copyContentButtons.length == 0) return; - const text = Array.from(document.querySelectorAll('.lines-code')).map((el) => el.textContent).join(''); - for (const copyContentButton of copyContentButtons) { - copyContentButton.setAttribute('data-clipboard-text', text); + if (copyContentButtons.length !== 0) { + const text = Array.from(document.querySelectorAll('.lines-code')).map((el) => el.textContent).join(''); + for (const copyContentButton of copyContentButtons) { + copyContentButton.setAttribute('data-clipboard-text', text); + } } } From 16746bd135fe9e346fb8acd61e6d1c3276f1c3be Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Thu, 3 Nov 2022 10:23:19 +0800 Subject: [PATCH 09/13] fix --- templates/repo/view_file.tmpl | 2 +- web_src/js/features/common-global.js | 9 --------- web_src/js/features/repo-code.js | 17 ++++++++++++++++- 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/templates/repo/view_file.tmpl b/templates/repo/view_file.tmpl index 3af566e4b8275..04b68f2016658 100644 --- a/templates/repo/view_file.tmpl +++ b/templates/repo/view_file.tmpl @@ -58,7 +58,7 @@ {{.locale.Tr "repo.escape_control_characters"}} {{end}} - From a0c1ff229077eb99eb19e63dc53b66ab29958c9c Mon Sep 17 00:00:00 2001 From: Yarden Shoham Date: Fri, 4 Nov 2022 15:26:01 +0200 Subject: [PATCH 11/13] Update templates/repo/view_file.tmpl Co-authored-by: silverwind --- templates/repo/view_file.tmpl | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/templates/repo/view_file.tmpl b/templates/repo/view_file.tmpl index 8edbfed3ce611..c3ec2d859377e 100644 --- a/templates/repo/view_file.tmpl +++ b/templates/repo/view_file.tmpl @@ -58,11 +58,7 @@ {{.locale.Tr "repo.escape_control_characters"}} {{end}} -
From 6a73e1f5af23578c436f2222cd617d8e2aa443e5 Mon Sep 17 00:00:00 2001 From: Yarden Shoham Date: Fri, 4 Nov 2022 13:31:56 +0000 Subject: [PATCH 12/13] Remove parentheses --- templates/repo/view_file.tmpl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/repo/view_file.tmpl b/templates/repo/view_file.tmpl index c3ec2d859377e..adb7800d5a38f 100644 --- a/templates/repo/view_file.tmpl +++ b/templates/repo/view_file.tmpl @@ -58,7 +58,7 @@ {{.locale.Tr "repo.escape_control_characters"}} {{end}} -
From 1d6c1a1d75469cf4ce554015d25ba6c7e69fb5f6 Mon Sep 17 00:00:00 2001 From: Yarden Shoham Date: Fri, 4 Nov 2022 13:45:55 +0000 Subject: [PATCH 13/13] Render as tag --- options/locale/locale_en-US.ini | 1 + templates/repo/view_file.tmpl | 8 +++++--- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index 79764506679f0..3a83941d56ff4 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -1090,6 +1090,7 @@ editor.cannot_edit_non_text_files = Binary files cannot be edited in the web int editor.edit_this_file = Edit File editor.this_file_locked = File is locked editor.must_be_on_a_branch = You must be on a branch to make or propose changes to this file. +editor.only_copy_raw = You may only copy raw text files. editor.fork_before_edit = You must fork this repository to make or propose changes to this file. editor.delete_this_file = Delete File editor.must_have_write_access = You must have write access to make or propose changes to this file. diff --git a/templates/repo/view_file.tmpl b/templates/repo/view_file.tmpl index adb7800d5a38f..60d2a812defd0 100644 --- a/templates/repo/view_file.tmpl +++ b/templates/repo/view_file.tmpl @@ -58,11 +58,13 @@ {{.locale.Tr "repo.escape_control_characters"}} {{end}} -
{{svg "octicon-download"}} + {{if or .IsMarkup .IsRenderedHTML (not .IsTextSource)}} + {{svg "octicon-copy" 14}} + {{else}} + {{svg "octicon-copy" 14}} + {{end}} {{if .Repository.CanEnableEditor}} {{if .CanEditFile}} {{svg "octicon-pencil"}}