From 121b62b4fcd75a6e24ed2f9809171be106f5ccbf Mon Sep 17 00:00:00 2001 From: silverwind Date: Tue, 16 May 2023 01:33:14 +0200 Subject: [PATCH 1/4] Fix webp copying --- web_src/js/features/copycontent.js | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/web_src/js/features/copycontent.js b/web_src/js/features/copycontent.js index e51953625d4da..c364383a009d8 100644 --- a/web_src/js/features/copycontent.js +++ b/web_src/js/features/copycontent.js @@ -4,11 +4,6 @@ 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; @@ -43,15 +38,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 (isImage) { + const success = await clippie(await convertImage(content, 'image/png')); + showTemporaryTooltip(btn, success ? i18n.copy_success : i18n.copy_error); } else { showTemporaryTooltip(btn, i18n.copy_error); } From 689337ff44d940edc935c5d38dd24c741bc737d4 Mon Sep 17 00:00:00 2001 From: silverwind Date: Tue, 16 May 2023 01:38:54 +0200 Subject: [PATCH 2/4] rename variable --- web_src/js/features/copycontent.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/web_src/js/features/copycontent.js b/web_src/js/features/copycontent.js index c364383a009d8..a276176284863 100644 --- a/web_src/js/features/copycontent.js +++ b/web_src/js/features/copycontent.js @@ -10,7 +10,7 @@ export function initCopyContent() { btn.addEventListener('click', async () => { if (btn.classList.contains('is-loading')) return; - let content, isImage; + let content, isRasterImage; const link = btn.getAttribute('data-link'); // when data-link is present, we perform a fetch. this is either because @@ -23,7 +23,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(); @@ -43,7 +43,7 @@ export function initCopyContent() { if (success) { showTemporaryTooltip(btn, i18n.copy_success); } else { - if (isImage) { + if (isRasterImage) { const success = await clippie(await convertImage(content, 'image/png')); showTemporaryTooltip(btn, success ? i18n.copy_success : i18n.copy_error); } else { From 67353b0fe22cabe118ad0b46a50c37481c042bcf Mon Sep 17 00:00:00 2001 From: silverwind Date: Tue, 16 May 2023 01:43:02 +0200 Subject: [PATCH 3/4] init with bool --- web_src/js/features/copycontent.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/web_src/js/features/copycontent.js b/web_src/js/features/copycontent.js index a276176284863..4d5509768fe7e 100644 --- a/web_src/js/features/copycontent.js +++ b/web_src/js/features/copycontent.js @@ -10,7 +10,7 @@ export function initCopyContent() { btn.addEventListener('click', async () => { if (btn.classList.contains('is-loading')) return; - let content, isRasterImage; + let content, isRasterImage = false; const link = btn.getAttribute('data-link'); // when data-link is present, we perform a fetch. this is either because From 07bfb33d68288296742ddf4ff41d86c82e76c763 Mon Sep 17 00:00:00 2001 From: silverwind Date: Tue, 16 May 2023 01:43:34 +0200 Subject: [PATCH 4/4] move to separate line --- web_src/js/features/copycontent.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/web_src/js/features/copycontent.js b/web_src/js/features/copycontent.js index 4d5509768fe7e..646fafeb0f4eb 100644 --- a/web_src/js/features/copycontent.js +++ b/web_src/js/features/copycontent.js @@ -10,7 +10,8 @@ export function initCopyContent() { btn.addEventListener('click', async () => { if (btn.classList.contains('is-loading')) return; - let content, isRasterImage = false; + let content; + let isRasterImage = false; const link = btn.getAttribute('data-link'); // when data-link is present, we perform a fetch. this is either because