From 527e9575606152ac227592efbf297f8951056ca6 Mon Sep 17 00:00:00 2001 From: Dominic Date: Mon, 5 Sep 2022 12:52:30 +0200 Subject: [PATCH] Add 'copy' button, use translations for buttons --- src/components/code.vue | 68 ++++++++++++++++++++++++++++++-------- src/utils/use-clipboard.ts | 67 +++++++++++++++++++++++++++++++++++++ 2 files changed, 122 insertions(+), 13 deletions(-) create mode 100644 src/utils/use-clipboard.ts diff --git a/src/components/code.vue b/src/components/code.vue index 9d0dfcd..6a1ccf3 100644 --- a/src/components/code.vue +++ b/src/components/code.vue @@ -8,27 +8,51 @@ class="inline-progress" /> +
       
     
- - Download + + +
+ + + {{ t('copy') }} + + + + + {{ t('download') }} + +
+ + @@ -85,11 +121,17 @@ export default { border-color: var(--primary); } -.downloadBtn { - align-self: flex-end; +.buttonRow { + display: flex; + flex-direction: row; + justify-content: flex-end; margin-top: 15px; } +.buttonRow>div { + margin-left: 15px; +} + .inline-progress { --v-progress-circular-size: 1em; display: inline; diff --git a/src/utils/use-clipboard.ts b/src/utils/use-clipboard.ts new file mode 100644 index 0000000..f812e2e --- /dev/null +++ b/src/utils/use-clipboard.ts @@ -0,0 +1,67 @@ +/** + * Composable to access the clipboard api and check if the api is available + * + * Based on the apps own composable + * @see https://github.com/directus/directus/blob/main/app/src/composables/use-clipboard.ts + * + * @NOTE in contrast to the original app's solution we need to pass the notification store instance. + * @NOTE in the future the app components might get etracted into their own package. Once this is happened it's possible that we could use the app's default composable + */ + + + import { computed } from 'vue'; + import { useI18n } from 'vue-i18n'; + + + type Message = { + success?: string; + fail?: string; + }; + + + + export function useClipboard() { + const { t } = useI18n(); + + const isCopySupported = computed(() => { + return !!navigator?.clipboard?.writeText; + }); + + const isPasteSupported = computed(() => { + return !!navigator?.clipboard?.readText; + }); + + return { isCopySupported, isPasteSupported, copyToClipboard, pasteFromClipboard }; + + async function copyToClipboard(value: string, notificationStore: any, message?: Message): Promise { + try { + await navigator?.clipboard?.writeText(value); + notificationStore.add({ + title: message?.success ?? t('copy_raw_value_success'), + }); + return true; + } catch (err: any) { + notificationStore.add({ + type: 'error', + title: message?.fail ?? t('copy_raw_value_fail'), + }); + return false; + } + } + + async function pasteFromClipboard(notificationStore: any, message?: Message): Promise { + try { + const pasteValue = await navigator?.clipboard?.readText(); + notificationStore.add({ + title: message?.success ?? t('paste_raw_value_success'), + }); + return pasteValue; + } catch (err: any) { + notificationStore.add({ + type: 'error', + title: message?.fail ?? t('paste_raw_value_fail'), + }); + return null; + } + } + } \ No newline at end of file