fix: show copy to clipboard failure (#2886)

* fix for AppButtonCopy

* add some logging

* fix if statement

* refactor, use .then

* check for copied

* Fix recipe share link

* refactor AppButtonCopy

* update tooltip text

* update use-copy

* logging

* fix is supported check

* more fixes for use-copy.ts

---------

Co-authored-by: Michael Genson <71845777+michael-genson@users.noreply.github.com>
This commit is contained in:
Kuchenpirat 2023-12-29 16:48:28 +01:00 committed by GitHub
parent f6f9a1c532
commit 6a71af98bd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 55 additions and 18 deletions

View File

@ -143,7 +143,7 @@ export default defineComponent({
} }
const { share, isSupported: shareIsSupported } = useShare(); const { share, isSupported: shareIsSupported } = useShare();
const { copy } = useClipboard(); const { copy, copied, isSupported } = useClipboard();
function getRecipeText() { function getRecipeText() {
return i18n.t("recipe.share-recipe-message", [props.name]); return i18n.t("recipe.share-recipe-message", [props.name]);
@ -154,8 +154,18 @@ export default defineComponent({
} }
async function copyTokenLink(token: string) { async function copyTokenLink(token: string) {
await copy(getTokenLink(token)); if (isSupported.value) {
alert.success(i18n.t("recipe-share.recipe-link-copied-message") as string); await copy(getTokenLink(token));
if (copied.value) {
alert.success(i18n.t("recipe-share.recipe-link-copied-message") as string);
}
else {
alert.error(i18n.t("general.clipboard-copy-failure") as string);
}
}
else {
alert.error(i18n.t("general.clipboard-not-supported") as string);
}
} }
async function shareRecipe(token: string) { async function shareRecipe(token: string) {

View File

@ -2,7 +2,7 @@
<v-tooltip <v-tooltip
ref="copyToolTip" ref="copyToolTip"
v-model="show" v-model="show"
color="success lighten-1" :color="copied? 'success lighten-1' : 'red lighten-1'"
top top
:open-on-hover="false" :open-on-hover="false"
:open-on-click="true" :open-on-click="true"
@ -29,12 +29,14 @@
<v-icon left dark> <v-icon left dark>
{{ $globals.icons.clipboardCheck }} {{ $globals.icons.clipboardCheck }}
</v-icon> </v-icon>
<slot> {{ $t("general.copied_message") }} </slot> <slot v-if="!isSupported"> {{ $t("general.your-browser-does-not-support-clipboard") }} </slot>
<slot v-else> {{ copied ? $t("general.copied_message") : $t("general.clipboard-copy-failure") }} </slot>
</span> </span>
</v-tooltip> </v-tooltip>
</template> </template>
<script lang="ts"> <script lang="ts">
import { useClipboard } from "@vueuse/core"
import { defineComponent, ref } from "@nuxtjs/composition-api"; import { defineComponent, ref } from "@nuxtjs/composition-api";
import { VTooltip } from "~/types/vuetify"; import { VTooltip } from "~/types/vuetify";
@ -58,6 +60,7 @@ export default defineComponent({
}, },
}, },
setup(props) { setup(props) {
const { copy, copied, isSupported } = useClipboard()
const show = ref(false); const show = ref(false);
const copyToolTip = ref<VTooltip | null>(null); const copyToolTip = ref<VTooltip | null>(null);
@ -65,13 +68,21 @@ export default defineComponent({
copyToolTip.value?.deactivate(); copyToolTip.value?.deactivate();
} }
function textToClipboard() { async function textToClipboard() {
if (isSupported.value) {
await copy(props.copyText);
if (copied.value) {
console.log(`Copied\n${props.copyText}`)
}
else {
console.warn("Copy failed: ", copied.value);
}
}
else {
console.warn("Clipboard is currently not supported by your browser. Ensure you're on a secure (https) site.");
}
show.value = true; show.value = true;
const copyText = props.copyText;
navigator.clipboard.writeText(copyText).then(
() => console.log(`Copied\n${copyText}`),
() => console.log(`Copied Failed\n${copyText}`)
);
setTimeout(() => { setTimeout(() => {
toggleBlur(); toggleBlur();
}, 500); }, 500);
@ -81,6 +92,8 @@ export default defineComponent({
show, show,
copyToolTip, copyToolTip,
textToClipboard, textToClipboard,
copied,
isSupported,
}; };
}, },
}); });

View File

@ -7,23 +7,30 @@ export function useCopy() {
const { i18n } = useContext(); const { i18n } = useContext();
function copyText(text: string) { function copyText(text: string) {
if (!isSupported) { if (!isSupported.value) {
alert.error(i18n.tc("general.clipboard-not-supported")); alert.error(i18n.tc("general.clipboard-not-supported"));
return; return;
} }
copy(text); copy(text).then(() => {
alert.success(i18n.tc("general.copied-to-clipboard")); // Verify copy success as no error is thrown on failure.
if (copied.value) {
alert.success(i18n.tc("general.copied-to-clipboard"));
}
else {
alert.error(i18n.tc("general.clipboard-copy-failure"));
}
});
} }
return { copyText, copied }; return { copyText, copied };
} }
export function useCopyList() { export function useCopyList() {
const { copy, isSupported } = useClipboard(); const { copy, isSupported, copied } = useClipboard();
const { i18n } = useContext(); const { i18n } = useContext();
function checkClipboard() { function checkClipboard() {
if (!isSupported) { if (!isSupported.value) {
alert.error(i18n.tc("general.your-browser-does-not-support-clipboard")); alert.error(i18n.tc("general.your-browser-does-not-support-clipboard"));
return false; return false;
} }
@ -54,7 +61,13 @@ export function useCopyList() {
function copyText(text: string, len: number) { function copyText(text: string, len: number) {
copy(text).then(() => { copy(text).then(() => {
alert.success(i18n.tc("general.copied-items-to-clipboard", len)); // Verify copy success as no error is thrown on failure.
if (copied.value) {
alert.success(i18n.tc("general.copied-items-to-clipboard", len));
}
else {
alert.error(i18n.tc("general.clipboard-copy-failure"));
}
}); });
} }

View File

@ -198,7 +198,8 @@
"refresh": "Refresh", "refresh": "Refresh",
"upload-file": "Upload File", "upload-file": "Upload File",
"created-on-date": "Created on: {0}", "created-on-date": "Created on: {0}",
"unsaved-changes": "You have unsaved changes. Do you want to save before leaving? Okay to save, Cancel to discard changes." "unsaved-changes": "You have unsaved changes. Do you want to save before leaving? Okay to save, Cancel to discard changes.",
"clipboard-copy-failure": "Failed to copy to the clipboard."
}, },
"group": { "group": {
"are-you-sure-you-want-to-delete-the-group": "Are you sure you want to delete <b>{groupName}<b/>?", "are-you-sure-you-want-to-delete-the-group": "Are you sure you want to delete <b>{groupName}<b/>?",