From 537796e1a01009e8873673b091e2b47cfa73d4bc Mon Sep 17 00:00:00 2001 From: tglide <26071571+TGlide@users.noreply.github.com> Date: Mon, 5 Dec 2022 17:41:39 +0000 Subject: [PATCH 1/3] fix: copy not working on http --- src/lib/components/copy.svelte | 51 +++++++++++++++++++++++++++------- 1 file changed, 41 insertions(+), 10 deletions(-) diff --git a/src/lib/components/copy.svelte b/src/lib/components/copy.svelte index 1f0e5dfd8..e9272af34 100644 --- a/src/lib/components/copy.svelte +++ b/src/lib/components/copy.svelte @@ -9,23 +9,54 @@ let content = 'Click to copy'; - const copy = async () => { + async function securedCopy() { try { await navigator.clipboard.writeText(value); + } catch { + return false; + } + + return true; + } + + function unsecuredCopy() { + const textArea = document.createElement('textarea'); + textArea.value = value; + document.body.appendChild(textArea); + textArea.focus(); + textArea.select(); + + let success = true; + try { + document.execCommand('copy'); + } catch { + success = false; + } finally { + document.body.removeChild(textArea); + } + + return success; + } + + async function copy() { + // Because of how JS works, unsecuredCopy only runs if securedCopy fails + const success = (await securedCopy()) || unsecuredCopy(); + + if (success) { content = 'Copied'; - } catch (error) { + } else { addNotification({ - message: error.message, + message: 'Unable to copy to clipboard', type: 'error' }); - } finally { - if (event) { - trackEvent('click_id_tag', { - name: event - }); - } } - }; + + if (event) { + trackEvent('click_id_tag', { + name: event + }); + } + } Date: Tue, 6 Dec 2022 16:44:28 +0000 Subject: [PATCH 2/3] refactor: update copy comment --- src/lib/components/copy.svelte | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/lib/components/copy.svelte b/src/lib/components/copy.svelte index e9272af34..2bfa61ddd 100644 --- a/src/lib/components/copy.svelte +++ b/src/lib/components/copy.svelte @@ -39,7 +39,8 @@ } async function copy() { - // Because of how JS works, unsecuredCopy only runs if securedCopy fails + // securedCopy works only in HTTPS environment. + // unsecuredCopy works in HTTP and only runs if securedCopy fails. const success = (await securedCopy()) || unsecuredCopy(); if (success) { From 9b7895c11de4772b8ccd1847198482ce4e4e020d Mon Sep 17 00:00:00 2001 From: tglide <26071571+TGlide@users.noreply.github.com> Date: Fri, 9 Dec 2022 11:44:38 +0000 Subject: [PATCH 3/3] refactor: add copy helper & fix copyInput --- src/lib/components/copy.svelte | 38 +++-------------------------- src/lib/components/copyInput.svelte | 14 ++++++----- src/lib/helpers/copy.ts | 36 +++++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 40 deletions(-) create mode 100644 src/lib/helpers/copy.ts diff --git a/src/lib/components/copy.svelte b/src/lib/components/copy.svelte index 2bfa61ddd..0e859e79c 100644 --- a/src/lib/components/copy.svelte +++ b/src/lib/components/copy.svelte @@ -2,6 +2,7 @@ import { trackEvent } from '$lib/actions/analytics'; import { tooltip } from '$lib/actions/tooltip'; import { clickOnEnter } from '$lib/helpers/a11y'; + import { copy } from '$lib/helpers/copy'; import { addNotification } from '$lib/stores/notifications'; export let value: string; @@ -9,39 +10,8 @@ let content = 'Click to copy'; - async function securedCopy() { - try { - await navigator.clipboard.writeText(value); - } catch { - return false; - } - - return true; - } - - function unsecuredCopy() { - const textArea = document.createElement('textarea'); - textArea.value = value; - document.body.appendChild(textArea); - textArea.focus(); - textArea.select(); - - let success = true; - try { - document.execCommand('copy'); - } catch { - success = false; - } finally { - document.body.removeChild(textArea); - } - - return success; - } - - async function copy() { - // securedCopy works only in HTTPS environment. - // unsecuredCopy works in HTTP and only runs if securedCopy fails. - const success = (await securedCopy()) || unsecuredCopy(); + async function handleClick() { + const success = await copy(value); if (success) { content = 'Copied'; @@ -61,7 +31,7 @@ setTimeout(() => (content = 'Click to copy'))} use:tooltip={{ diff --git a/src/lib/components/copyInput.svelte b/src/lib/components/copyInput.svelte index 65bd6bb81..b06f1ae36 100644 --- a/src/lib/components/copyInput.svelte +++ b/src/lib/components/copyInput.svelte @@ -1,5 +1,6 @@