|
1 | 1 | import $ from 'jquery';
|
2 | 2 | import {svg} from '../svg.js';
|
3 | 3 | import {invertFileFolding} from './file-fold.js';
|
4 |
| -import {createTippy} from '../modules/tippy.js'; |
| 4 | +import {createTippy, showTemporaryTooltip} from '../modules/tippy.js'; |
5 | 5 | import {copyToClipboard} from './clipboard.js';
|
6 | 6 |
|
| 7 | +const {i18n} = window.config; |
| 8 | + |
7 | 9 | function changeHash(hash) {
|
8 | 10 | if (window.history.pushState) {
|
9 | 11 | window.history.pushState(null, null, hash);
|
@@ -110,6 +112,18 @@ function showLineButton() {
|
110 | 112 | });
|
111 | 113 | }
|
112 | 114 |
|
| 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 | + |
113 | 127 | export function initRepoCodeView() {
|
114 | 128 | if ($('.code-view .lines-num').length > 0) {
|
115 | 129 | $(document).on('click', '.lines-num span', function (e) {
|
@@ -185,4 +199,5 @@ export function initRepoCodeView() {
|
185 | 199 | if (!success) return;
|
186 | 200 | document.querySelector('.code-line-button')?._tippy?.hide();
|
187 | 201 | });
|
| 202 | + initCopyFileContent(); |
188 | 203 | }
|
0 commit comments