From f1a5cd9bc0e54e35397e32037a54b186c7daeba0 Mon Sep 17 00:00:00 2001 From: Pihkaal Date: Mon, 30 Sep 2024 21:40:37 +0200 Subject: [PATCH] feat(ui): copy button --- app.vue | 37 +++++++++++++++++++++++++++++++++---- utils/settings.ts | 2 +- 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/app.vue b/app.vue index 4fcf8c0..89676a5 100644 --- a/app.vue +++ b/app.vue @@ -9,6 +9,10 @@ const content = ref(""); const canvas = ref(null); const qrCode = ref(""); +const copyIcon = ref("i-heroicons-clipboard-document"); +const copyLabel = ref("Copy to clipboard"); +const isQRCodeEmpty = computed(() => qrCode.value.length === 0); + const updateQRCode = async () => { await nextTick(); @@ -29,6 +33,26 @@ const downloadQRCode = () => { link.click(); document.body.removeChild(link); }; + +const copyQRCode = async () => { + if (content.value.length === 0) return; + + const logoUrl = `/${logo.value}.png`; + await renderQRCodeToCanvas(canvas.value, content.value, logoUrl); + + const qrCode = canvas.value.toDataURL(`image/png`); + + const blob = await (await fetch(qrCode)).blob(); + const item = new ClipboardItem({ "image/png": blob }); + await navigator.clipboard.write([item]); + + copyIcon.value = "i-heroicons-clipboard-document-check"; + copyLabel.value = "Copied!"; + setTimeout(() => { + copyIcon.value = "i-heroicons-clipboard-document"; + copyLabel.value = "Copy to clipboard"; + }, 3000); +}; diff --git a/utils/settings.ts b/utils/settings.ts index 1719cf0..3615f43 100644 --- a/utils/settings.ts +++ b/utils/settings.ts @@ -2,7 +2,7 @@ import { z } from "zod"; export const LOGOS = ["session", "instagram"] as const; -export const IMAGE_FORMATS = ["jpeg", "png", "webp"] as const; +export const IMAGE_FORMATS = ["png", "jpeg", "webp"] as const; export type ImageFormat = (typeof IMAGE_FORMATS)[number];