|
| 1 | +import {copyToClipboard} from './clipboard.js'; |
| 2 | +import {showTemporaryTooltip} from '../modules/tippy.js'; |
| 3 | +import {convertImage} from '../utils.js'; |
| 4 | +const {i18n} = window.config; |
| 5 | + |
| 6 | +async function doCopy(content, btn) { |
| 7 | + const success = await copyToClipboard(content); |
| 8 | + showTemporaryTooltip(btn, success ? i18n.copy_success : i18n.copy_error); |
| 9 | +} |
| 10 | + |
| 11 | +export function initCopyContent() { |
| 12 | + const btn = document.getElementById('copy-content'); |
| 13 | + if (!btn || btn.classList.contains('disabled')) return; |
| 14 | + |
| 15 | + btn.addEventListener('click', async () => { |
| 16 | + if (btn.classList.contains('is-loading')) return; |
| 17 | + let content, isImage; |
| 18 | + const link = btn.getAttribute('data-link'); |
| 19 | + |
| 20 | + // when data-link is present, we perform a fetch. this is either because |
| 21 | + // the text to copy is not in the DOM or it is an image which should be |
| 22 | + // fetched to copy in full resolution |
| 23 | + if (link) { |
| 24 | + btn.classList.add('is-loading'); |
| 25 | + try { |
| 26 | + const res = await fetch(link, {credentials: 'include', redirect: 'follow'}); |
| 27 | + const contentType = res.headers.get('content-type'); |
| 28 | + |
| 29 | + if (contentType.startsWith('image/') && !contentType.startsWith('image/svg')) { |
| 30 | + isImage = true; |
| 31 | + content = await res.blob(); |
| 32 | + } else { |
| 33 | + content = await res.text(); |
| 34 | + } |
| 35 | + } catch { |
| 36 | + return showTemporaryTooltip(btn, i18n.copy_error); |
| 37 | + } finally { |
| 38 | + btn.classList.remove('is-loading'); |
| 39 | + } |
| 40 | + } else { // text, read from DOM |
| 41 | + const lineEls = document.querySelectorAll('.file-view .lines-code'); |
| 42 | + content = Array.from(lineEls).map((el) => el.textContent).join(''); |
| 43 | + } |
| 44 | + |
| 45 | + try { |
| 46 | + await doCopy(content, btn); |
| 47 | + } catch { |
| 48 | + if (isImage) { // convert image to png as last-resort as some browser only support png copy |
| 49 | + try { |
| 50 | + await doCopy(await convertImage(content, 'image/png'), btn); |
| 51 | + } catch { |
| 52 | + showTemporaryTooltip(btn, i18n.copy_error); |
| 53 | + } |
| 54 | + } else { |
| 55 | + showTemporaryTooltip(btn, i18n.copy_error); |
| 56 | + } |
| 57 | + } |
| 58 | + }); |
| 59 | +} |
0 commit comments