mirror of https://github.com/go-gitea/gitea
Git with a cup of tea, painless self-hosted git service
Mirror for internal git.with.parts use
https://git.with.parts
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
57 lines
2.0 KiB
57 lines
2.0 KiB
2 years ago
|
import {clippie} from 'clippie';
|
||
5 months ago
|
import {showTemporaryTooltip} from '../modules/tippy.ts';
|
||
|
import {convertImage} from '../utils.ts';
|
||
|
import {GET} from '../modules/fetch.ts';
|
||
2 years ago
|
|
||
2 years ago
|
const {i18n} = window.config;
|
||
|
|
||
|
export function initCopyContent() {
|
||
6 months ago
|
const btn = document.querySelector('#copy-content');
|
||
2 years ago
|
if (!btn || btn.classList.contains('disabled')) return;
|
||
|
|
||
|
btn.addEventListener('click', async () => {
|
||
|
if (btn.classList.contains('is-loading')) return;
|
||
2 years ago
|
let content;
|
||
|
let isRasterImage = false;
|
||
2 years ago
|
const link = btn.getAttribute('data-link');
|
||
|
|
||
|
// when data-link is present, we perform a fetch. this is either because
|
||
|
// the text to copy is not in the DOM or it is an image which should be
|
||
|
// fetched to copy in full resolution
|
||
|
if (link) {
|
||
8 months ago
|
btn.classList.add('is-loading', 'loading-icon-2px');
|
||
2 years ago
|
try {
|
||
1 year ago
|
const res = await GET(link, {credentials: 'include', redirect: 'follow'});
|
||
2 years ago
|
const contentType = res.headers.get('content-type');
|
||
|
|
||
|
if (contentType.startsWith('image/') && !contentType.startsWith('image/svg')) {
|
||
2 years ago
|
isRasterImage = true;
|
||
2 years ago
|
content = await res.blob();
|
||
|
} else {
|
||
|
content = await res.text();
|
||
|
}
|
||
|
} catch {
|
||
|
return showTemporaryTooltip(btn, i18n.copy_error);
|
||
|
} finally {
|
||
8 months ago
|
btn.classList.remove('is-loading', 'loading-icon-2px');
|
||
2 years ago
|
}
|
||
|
} else { // text, read from DOM
|
||
|
const lineEls = document.querySelectorAll('.file-view .lines-code');
|
||
2 years ago
|
content = Array.from(lineEls, (el) => el.textContent).join('');
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
// try copy original first, if that fails and it's an image, convert it to png
|
||
|
const success = await clippie(content);
|
||
|
if (success) {
|
||
|
showTemporaryTooltip(btn, i18n.copy_success);
|
||
|
} else {
|
||
|
if (isRasterImage) {
|
||
|
const success = await clippie(await convertImage(content, 'image/png'));
|
||
|
showTemporaryTooltip(btn, success ? i18n.copy_success : i18n.copy_error);
|
||
2 years ago
|
} else {
|
||
|
showTemporaryTooltip(btn, i18n.copy_error);
|
||
|
}
|
||
|
}
|
||
|
});
|
||
|
}
|