Skip to content

Commit bdd3007

Browse files
authored
Fix WEBP image copying (#24743)
Fix regression from #23801, where I forgot that the new module will not throw, so the `catch` handlers were never triggered and in turn, the WEBP was not converted to PNG.
1 parent b926f96 commit bdd3007

File tree

1 file changed

+11
-16
lines changed

1 file changed

+11
-16
lines changed

web_src/js/features/copycontent.js

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,14 @@ import {convertImage} from '../utils.js';
44

55
const {i18n} = window.config;
66

7-
async function doCopy(content, btn) {
8-
const success = await clippie(content);
9-
showTemporaryTooltip(btn, success ? i18n.copy_success : i18n.copy_error);
10-
}
11-
127
export function initCopyContent() {
138
const btn = document.getElementById('copy-content');
149
if (!btn || btn.classList.contains('disabled')) return;
1510

1611
btn.addEventListener('click', async () => {
1712
if (btn.classList.contains('is-loading')) return;
18-
let content, isImage;
13+
let content;
14+
let isRasterImage = false;
1915
const link = btn.getAttribute('data-link');
2016

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

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

46-
try {
47-
await doCopy(content, btn);
48-
} catch {
49-
if (isImage) { // convert image to png as last-resort as some browser only support png copy
50-
try {
51-
await doCopy(await convertImage(content, 'image/png'), btn);
52-
} catch {
53-
showTemporaryTooltip(btn, i18n.copy_error);
54-
}
42+
// try copy original first, if that fails and it's an image, convert it to png
43+
const success = await clippie(content);
44+
if (success) {
45+
showTemporaryTooltip(btn, i18n.copy_success);
46+
} else {
47+
if (isRasterImage) {
48+
const success = await clippie(await convertImage(content, 'image/png'));
49+
showTemporaryTooltip(btn, success ? i18n.copy_success : i18n.copy_error);
5550
} else {
5651
showTemporaryTooltip(btn, i18n.copy_error);
5752
}

0 commit comments

Comments
 (0)