mirror of
				https://github.com/go-gitea/gitea.git
				synced 2025-11-03 20:36:07 +01:00 
			
		
		
		
	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  ### After  #### Rendered files and binaries have their button disabled   Signed-off-by: Yarden Shoham <hrsi88@gmail.com> Co-authored-by: silverwind <me@silverwind.io> Co-authored-by: delvh <dev.lh@web.de> Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
This commit is contained in:
		@@ -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();
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user