From 8000f994957f5307aa0ad43320da41e60b1dcf7b Mon Sep 17 00:00:00 2001 From: Allan Lasser Date: Wed, 4 Dec 2024 12:10:19 -0500 Subject: [PATCH] Guard against failures to access clipboard API Fixes #937 --- src/langs/json/en.json | 1 + src/lib/components/documents/Share.svelte | 31 ++++---- .../documents/stories/Share.stories.svelte | 2 + .../components/projects/ProjectShare.svelte | 25 +++---- src/lib/utils/copy.ts | 12 ++++ src/lib/utils/tests/copy.test.ts | 70 +++++++++++++++++++ 6 files changed, 108 insertions(+), 33 deletions(-) create mode 100644 src/lib/utils/copy.ts create mode 100644 src/lib/utils/tests/copy.test.ts diff --git a/src/langs/json/en.json b/src/langs/json/en.json index 484fbec88..d8d9017c8 100644 --- a/src/langs/json/en.json +++ b/src/langs/json/en.json @@ -544,6 +544,7 @@ "note": "Note", "copy": "Copy", "copiedToClipboard": "Copied to clipboard", + "couldNotCopyToClipboard": "Could not copy to clipboard", "permalink": "Permalink", "iframe": "Embed HTML iFrame", "preview": "Embed Preview", diff --git a/src/lib/components/documents/Share.svelte b/src/lib/components/documents/Share.svelte index 21f6e29e7..fbb9d54aa 100644 --- a/src/lib/components/documents/Share.svelte +++ b/src/lib/components/documents/Share.svelte @@ -20,22 +20,22 @@ Organization24, } from "svelte-octicons"; - import Button from "../common/Button.svelte"; + import Button from "$lib/components/common/Button.svelte"; import CustomizeEmbed, { embedSettings } from "./CustomizeEmbed.svelte"; - import Field from "../common/Field.svelte"; - import FieldLabel from "../common/FieldLabel.svelte"; - import Number from "../inputs/Number.svelte"; - import Select from "../inputs/Select.svelte"; - import Tab from "../common/Tab.svelte"; - import Text from "../inputs/Text.svelte"; - import TextArea from "../inputs/TextArea.svelte"; - import Tip from "../common/Tip.svelte"; + import Field from "$lib/components/common/Field.svelte"; + import FieldLabel from "$lib/components/common/FieldLabel.svelte"; + import Number from "$lib/components/inputs/Number.svelte"; + import Select from "$lib/components/inputs/Select.svelte"; + import Tab from "$lib/components/common/Tab.svelte"; + import Text from "$lib/components/inputs/Text.svelte"; + import TextArea from "$lib/components/inputs/TextArea.svelte"; + import Tip from "$lib/components/common/Tip.svelte"; - import Portal from "../layouts/Portal.svelte"; - import Modal from "../layouts/Modal.svelte"; - import Edit from "../forms/Edit.svelte"; + import Portal from "$lib/components/layouts/Portal.svelte"; + import Modal from "$lib/components/layouts/Modal.svelte"; + import Edit from "$lib/components/forms/Edit.svelte"; - import { toast } from "../layouts/Toaster.svelte"; + import copy from "$lib/utils/copy"; import { createEmbedSearchParams } from "$lib/utils/embed"; import { canonicalPageUrl, @@ -108,11 +108,6 @@ break; } } - - async function copy(text: string) { - await navigator.clipboard.writeText(text); - toast($_("share.copiedToClipboard")); - }
diff --git a/src/lib/components/documents/stories/Share.stories.svelte b/src/lib/components/documents/stories/Share.stories.svelte index 885ef933c..04ba88513 100644 --- a/src/lib/components/documents/stories/Share.stories.svelte +++ b/src/lib/components/documents/stories/Share.stories.svelte @@ -1,6 +1,7 @@