feat(ui): copy button
This commit is contained in:
37
app.vue
37
app.vue
@@ -9,6 +9,10 @@ const content = ref("");
|
|||||||
const canvas = ref(null);
|
const canvas = ref(null);
|
||||||
const qrCode = ref("");
|
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 () => {
|
const updateQRCode = async () => {
|
||||||
await nextTick();
|
await nextTick();
|
||||||
|
|
||||||
@@ -29,6 +33,26 @@ const downloadQRCode = () => {
|
|||||||
link.click();
|
link.click();
|
||||||
document.body.removeChild(link);
|
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);
|
||||||
|
};
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
@@ -38,30 +62,35 @@ const downloadQRCode = () => {
|
|||||||
<USelectMenu
|
<USelectMenu
|
||||||
v-model="format"
|
v-model="format"
|
||||||
:options="IMAGE_FORMATS"
|
:options="IMAGE_FORMATS"
|
||||||
@input="updateQRCode"
|
@change="updateQRCode"
|
||||||
/>
|
/>
|
||||||
<USelectMenu v-model="logo" :options="LOGOS" @input="updateQRCode" />
|
<USelectMenu v-model="logo" :options="LOGOS" @change="updateQRCode" />
|
||||||
<UInput v-model="content" @input="updateQRCode" />
|
<UInput v-model="content" @input="updateQRCode" />
|
||||||
|
|
||||||
<canvas ref="canvas" class="hidden" />
|
<canvas ref="canvas" class="hidden" />
|
||||||
<img :src="qrCode" />
|
<img :src="qrCode" />
|
||||||
|
|
||||||
<UButton
|
<UButton
|
||||||
|
block
|
||||||
icon="i-heroicons-arrow-down-tray"
|
icon="i-heroicons-arrow-down-tray"
|
||||||
size="sm"
|
size="sm"
|
||||||
color="primary"
|
color="primary"
|
||||||
variant="solid"
|
variant="solid"
|
||||||
label="Download"
|
label="Download"
|
||||||
:trailing="false"
|
:trailing="false"
|
||||||
|
:disabled="isQRCodeEmpty"
|
||||||
@click="downloadQRCode"
|
@click="downloadQRCode"
|
||||||
/>
|
/>
|
||||||
<UButton
|
<UButton
|
||||||
icon="i-heroicons-clipboard-document"
|
block
|
||||||
|
:icon="copyIcon"
|
||||||
size="sm"
|
size="sm"
|
||||||
color="primary"
|
color="primary"
|
||||||
variant="solid"
|
variant="solid"
|
||||||
label="Copy to clipboard"
|
:label="copyLabel"
|
||||||
:trailing="false"
|
:trailing="false"
|
||||||
|
:disabled="isQRCodeEmpty"
|
||||||
|
@click="copyQRCode"
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import { z } from "zod";
|
|||||||
|
|
||||||
export const LOGOS = ["session", "instagram"] as const;
|
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];
|
export type ImageFormat = (typeof IMAGE_FORMATS)[number];
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user