Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
wxiaoguang committed Nov 3, 2022
1 parent 83a41f0 commit 5b584a9
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 11 deletions.
2 changes: 1 addition & 1 deletion templates/repo/view_file.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
<a class="ui mini basic button unescape-button" style="display: none;">{{.locale.Tr "repo.unescape_control_characters"}}</a>
<a class="ui mini basic button escape-button">{{.locale.Tr "repo.escape_control_characters"}}</a>
{{end}}
<button class="ui mini basic button small compact tooltip copy-content" id="clipboard-btn" data-content="{{.locale.Tr "copy_content"}}" aria-label="{{.locale.Tr "copy_content"}}"
<button class="ui mini basic button small compact tooltip" id="copy-file-content" data-content="{{.locale.Tr "copy_content"}}" aria-label="{{.locale.Tr "copy_content"}}"
{{if or (.IsMarkup) (.IsRenderedHTML) (not .IsTextSource)}}
disabled
{{end}}
Expand Down
9 changes: 0 additions & 9 deletions web_src/js/features/common-global.js
Original file line number Diff line number Diff line change
Expand Up @@ -343,15 +343,6 @@ export function initGlobalButtons() {
window.location.href = $this.attr('data-done-url');
});
});

// get raw text for copy content button
const copyContentButtons = document.querySelectorAll('button.copy-content');
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);
}
}
}

/**
Expand Down
17 changes: 16 additions & 1 deletion web_src/js/features/repo-code.js
Original file line number Diff line number Diff line change
@@ -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);
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -185,4 +199,5 @@ export function initRepoCodeView() {
if (!success) return;
document.querySelector('.code-line-button')?._tippy?.hide();
});
initCopyFileContent();
}

0 comments on commit 5b584a9

Please sign in to comment.