Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix WEBP image copying #24743

Merged
merged 6 commits into from
May 16, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 11 additions & 16 deletions web_src/js/features/copycontent.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,14 @@ import {convertImage} from '../utils.js';

const {i18n} = window.config;

async function doCopy(content, btn) {
const success = await clippie(content);
showTemporaryTooltip(btn, success ? i18n.copy_success : i18n.copy_error);
}

export function initCopyContent() {
const btn = document.getElementById('copy-content');
if (!btn || btn.classList.contains('disabled')) return;

btn.addEventListener('click', async () => {
if (btn.classList.contains('is-loading')) return;
let content, isImage;
let content;
let isRasterImage = false;
const link = btn.getAttribute('data-link');

// when data-link is present, we perform a fetch. this is either because
Expand All @@ -28,7 +24,7 @@ export function initCopyContent() {
const contentType = res.headers.get('content-type');

if (contentType.startsWith('image/') && !contentType.startsWith('image/svg')) {
isImage = true;
isRasterImage = true;
content = await res.blob();
} else {
content = await res.text();
Expand All @@ -43,15 +39,14 @@ export function initCopyContent() {
content = Array.from(lineEls).map((el) => el.textContent).join('');
}

try {
await doCopy(content, btn);
} catch {
if (isImage) { // convert image to png as last-resort as some browser only support png copy
try {
await doCopy(await convertImage(content, 'image/png'), btn);
} catch {
showTemporaryTooltip(btn, i18n.copy_error);
}
// 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);
} else {
showTemporaryTooltip(btn, i18n.copy_error);
}
Expand Down