feat(ui): form validation and improved ui
This commit is contained in:
107
app.vue
107
app.vue
@@ -1,33 +1,44 @@
|
|||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { renderQRCodeToCanvas } from "@/utils/renderer";
|
// TODO: default image or smth when no content
|
||||||
import { IMAGE_FORMATS, LOGOS } from "@/utils/settings";
|
|
||||||
|
|
||||||
const format = ref(IMAGE_FORMATS[0]);
|
import { renderQRCodeToCanvas } from "@/utils/renderer";
|
||||||
const logo = ref(LOGOS[0]);
|
import {
|
||||||
const content = ref("");
|
IMAGE_FORMATS,
|
||||||
|
LOGOS,
|
||||||
|
settingsSchema,
|
||||||
|
type Settings,
|
||||||
|
} from "@/utils/settings";
|
||||||
|
|
||||||
|
import type { FormSubmitEvent } from "#ui/types";
|
||||||
|
|
||||||
const canvas = ref(null);
|
const canvas = ref(null);
|
||||||
const qrCode = ref("");
|
const qrCode = ref("");
|
||||||
|
|
||||||
const copyIcon = ref("i-heroicons-clipboard-document");
|
const copyIcon = ref("i-heroicons-clipboard-document");
|
||||||
const copyLabel = ref("Copy to clipboard");
|
const copyLabel = ref("Copy");
|
||||||
const isQRCodeEmpty = computed(() => qrCode.value.length === 0);
|
const isQRCodeEmpty = computed(() => qrCode.value.length === 0);
|
||||||
|
|
||||||
|
const state = reactive({
|
||||||
|
logo: LOGOS[0],
|
||||||
|
format: IMAGE_FORMATS[0],
|
||||||
|
content: undefined,
|
||||||
|
});
|
||||||
|
|
||||||
const updateQRCode = async () => {
|
const updateQRCode = async () => {
|
||||||
await nextTick();
|
if (!state.content) return;
|
||||||
|
|
||||||
if (content.value.length === 0) return;
|
const logoUrl = `/${state.logo}.png`;
|
||||||
|
await renderQRCodeToCanvas(canvas.value, state.content, logoUrl);
|
||||||
|
|
||||||
const logoUrl = `/${logo.value}.png`;
|
console.log(state.format);
|
||||||
await renderQRCodeToCanvas(canvas.value, content.value, logoUrl);
|
|
||||||
|
|
||||||
qrCode.value = canvas.value.toDataURL(`image/${format.value}`);
|
qrCode.value = canvas.value.toDataURL(`image/${state.format}`);
|
||||||
};
|
};
|
||||||
|
|
||||||
const downloadQRCode = () => {
|
const downloadQRCode = () => {
|
||||||
const link = document.createElement("a");
|
const link = document.createElement("a");
|
||||||
link.href = qrCode.value;
|
link.href = qrCode.value;
|
||||||
link.download = "qrcode.png";
|
link.download = `qrcode.${state.format}`;
|
||||||
|
|
||||||
document.body.appendChild(link);
|
document.body.appendChild(link);
|
||||||
link.click();
|
link.click();
|
||||||
@@ -35,10 +46,10 @@ const downloadQRCode = () => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const copyQRCode = async () => {
|
const copyQRCode = async () => {
|
||||||
if (content.value.length === 0) return;
|
if (state.content.length === 0) return;
|
||||||
|
|
||||||
const logoUrl = `/${logo.value}.png`;
|
const logoUrl = `/${state.format}.png`;
|
||||||
await renderQRCodeToCanvas(canvas.value, content.value, logoUrl);
|
await renderQRCodeToCanvas(canvas.value, state.content, logoUrl);
|
||||||
|
|
||||||
const qrCode = canvas.value.toDataURL(`image/png`);
|
const qrCode = canvas.value.toDataURL(`image/png`);
|
||||||
|
|
||||||
@@ -50,41 +61,52 @@ const copyQRCode = async () => {
|
|||||||
copyLabel.value = "Copied!";
|
copyLabel.value = "Copied!";
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
copyIcon.value = "i-heroicons-clipboard-document";
|
copyIcon.value = "i-heroicons-clipboard-document";
|
||||||
copyLabel.value = "Copy to clipboard";
|
copyLabel.value = "Copy";
|
||||||
}, 3000);
|
}, 3000);
|
||||||
};
|
};
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
<div>
|
<div role="main" class="flex h-[100vh] items-center justify-center">
|
||||||
<NuxtRouteAnnouncer />
|
<NuxtRouteAnnouncer />
|
||||||
|
<canvas ref="canvas" class="hidden" />
|
||||||
|
|
||||||
|
<div
|
||||||
|
class="w-full h-full max-w-[850px] max-h-[375px] flex justify-between gap-8"
|
||||||
|
>
|
||||||
|
<img :src="qrCode" class="h-full aspect-square" />
|
||||||
|
|
||||||
|
<div class="flex-1 flex flex-col justify-center">
|
||||||
|
<UForm :schema="settingsSchema" :state="state" class="space-y-4">
|
||||||
|
<UFormGroup
|
||||||
|
label="Username or link"
|
||||||
|
name="content"
|
||||||
|
@input="updateQRCode"
|
||||||
|
>
|
||||||
|
<UInput v-model="state.content" />
|
||||||
|
</UFormGroup>
|
||||||
|
|
||||||
|
<UFormGroup label="Logo" name="logo">
|
||||||
<USelectMenu
|
<USelectMenu
|
||||||
v-model="format"
|
v-model="state.logo"
|
||||||
|
:options="LOGOS"
|
||||||
|
searchable
|
||||||
|
@change="updateQRCode"
|
||||||
|
/>
|
||||||
|
</UFormGroup>
|
||||||
|
|
||||||
|
<UFormGroup label="Format" name="format">
|
||||||
|
<USelectMenu
|
||||||
|
v-model="state.format"
|
||||||
:options="IMAGE_FORMATS"
|
:options="IMAGE_FORMATS"
|
||||||
@change="updateQRCode"
|
@change="updateQRCode"
|
||||||
/>
|
/>
|
||||||
<USelectMenu v-model="logo" :options="LOGOS" @change="updateQRCode" />
|
</UFormGroup>
|
||||||
<UInput v-model="content" @input="updateQRCode" />
|
|
||||||
|
|
||||||
<canvas ref="canvas" class="hidden" />
|
|
||||||
<img :src="qrCode" />
|
|
||||||
|
|
||||||
<UButton
|
|
||||||
block
|
|
||||||
icon="i-heroicons-arrow-down-tray"
|
|
||||||
size="sm"
|
|
||||||
color="primary"
|
|
||||||
variant="solid"
|
|
||||||
label="Download"
|
|
||||||
:trailing="false"
|
|
||||||
:disabled="isQRCodeEmpty"
|
|
||||||
@click="downloadQRCode"
|
|
||||||
/>
|
|
||||||
<UButton
|
<UButton
|
||||||
block
|
block
|
||||||
:icon="copyIcon"
|
:icon="copyIcon"
|
||||||
size="sm"
|
size="md"
|
||||||
color="primary"
|
color="primary"
|
||||||
variant="solid"
|
variant="solid"
|
||||||
:label="copyLabel"
|
:label="copyLabel"
|
||||||
@@ -92,5 +114,20 @@ const copyQRCode = async () => {
|
|||||||
:disabled="isQRCodeEmpty"
|
:disabled="isQRCodeEmpty"
|
||||||
@click="copyQRCode"
|
@click="copyQRCode"
|
||||||
/>
|
/>
|
||||||
|
|
||||||
|
<UButton
|
||||||
|
block
|
||||||
|
icon="i-heroicons-arrow-down-tray"
|
||||||
|
size="md"
|
||||||
|
color="primary"
|
||||||
|
variant="solid"
|
||||||
|
label="Download"
|
||||||
|
:trailing="false"
|
||||||
|
:disabled="isQRCodeEmpty"
|
||||||
|
@click="downloadQRCode"
|
||||||
|
/>
|
||||||
|
</UForm>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|||||||
@@ -9,5 +9,7 @@ export type ImageFormat = (typeof IMAGE_FORMATS)[number];
|
|||||||
export const settingsSchema = z.object({
|
export const settingsSchema = z.object({
|
||||||
format: z.enum(IMAGE_FORMATS).default("png"),
|
format: z.enum(IMAGE_FORMATS).default("png"),
|
||||||
logo: z.enum(LOGOS),
|
logo: z.enum(LOGOS),
|
||||||
content: z.string().min(1),
|
content: z.string().min(1, "Required"),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
export type Settings = z.infer<typeof settingsSchema>;
|
||||||
|
|||||||
Reference in New Issue
Block a user