upgade create from image visuals

This commit is contained in:
Kuchenpirat 2025-06-28 15:50:57 +00:00
commit 88d44a1fe7
3 changed files with 107 additions and 134 deletions

View file

@ -64,3 +64,7 @@ a {
.fill-height { .fill-height {
min-height: 100vh; min-height: 100vh;
} }
.vue-simple-handler {
background-color: rgb(var(--v-theme-primary)) !important;
}

View file

@ -1,48 +1,51 @@
<template> <template>
<v-container class="pa-0"> <v-card class="ma-0 pt-2" :elevation="4">
<v-row no-gutters> <v-card-text>
<v-col <!-- Controls Row (Menu) -->
cols="8" <v-row class="mb-2 mx-1">
align-self="center" <v-btn
> color="error"
<Cropper :icon="$globals.icons.delete"
ref="cropper" @click="$emit('delete')"
class="cropper"
:src="img"
:default-size="defaultSize"
:style="`height: ${cropperHeight}; width: ${cropperWidth};`"
/> />
</v-col> <v-spacer />
<v-spacer /> <v-menu offset-y>
<v-col <template #activator="{ props }">
cols="2" <v-btn color="info" v-bind="props" :icon="$globals.icons.edit" />
align-self="center" </template>
> <v-list>
<v-container class="pa-0 mx-0"> <template v-for="(row, keyRow) in controls" :key="keyRow">
<v-row <v-list-item-group>
v-for="(row, keyRow) in controls" <v-list-item
:key="keyRow" v-for="(control, keyControl) in row"
> :key="keyControl"
<v-col @click="control.callback()"
v-for="(control, keyControl) in row" >
:key="keyControl" <v-list-item-icon>
:cols="12 / row.length" <v-icon :color="control.color">
class="py-2 mx-0" {{ control.icon }}
style="display: flex; align-items: center; justify-content: center;" </v-icon>
> </v-list-item-icon>
<v-btn <v-list-item-content>
icon <!-- Optionally, you can add a label here if you want -->
:color="control.color" </v-list-item-content>
@click="control.callback()" </v-list-item>
> </v-list-item-group>
<v-icon> {{ control.icon }} </v-icon> </template>
</v-btn> </v-list>
</v-col> </v-menu>
</v-row> </v-row>
</v-container>
</v-col> <!-- Image Row -->
</v-row> <Cropper
</v-container> ref="cropper"
class="cropper"
:src="img"
:default-size="defaultSize"
:style="`height: ${cropperHeight}; width: ${cropperWidth};`"
/>
</v-card-text>
</v-card>
</template> </template>
<script lang="ts"> <script lang="ts">
@ -65,9 +68,9 @@ export default defineNuxtComponent({
default: undefined, default: undefined,
}, },
}, },
emits: ["save"], emits: ["save", "delete"],
setup(_, context) { setup(_, context) {
const cropper = ref<Cropper>(); const cropper = ref<any>();
const { $globals } = useNuxtApp(); const { $globals } = useNuxtApp();
interface Control { interface Control {
@ -153,7 +156,6 @@ export default defineNuxtComponent({
}, },
methods: { methods: {
// @ts-expect-error https://advanced-cropper.github.io/vue-advanced-cropper/guides/advanced-recipes.html
defaultSize({ imageSize, visibleArea }) { defaultSize({ imageSize, visibleArea }) {
return { return {
width: (visibleArea || imageSize).width, width: (visibleArea || imageSize).width,

View file

@ -1,86 +1,55 @@
<template> <template>
<div> <div>
<v-form <v-form ref="domUrlForm" @submit.prevent="createRecipe">
ref="domUrlForm"
@submit.prevent="createRecipe"
>
<div> <div>
<v-card-title class="headline"> <v-card-title class="headline">
{{ $t('recipe.create-recipe-from-an-image') }} {{ $t("recipe.create-recipe-from-an-image") }}
</v-card-title> </v-card-title>
<v-card-text> <v-card-text>
<p>{{ $t('recipe.create-recipe-from-an-image-description') }}</p> <p>{{ $t("recipe.create-recipe-from-an-image-description") }}</p>
<v-container class="pa-0"> <v-container class="px-0">
<v-row> <AppButtonUpload
<v-col class="ml-auto"
cols="auto" url="none"
align-self="center" file-name="images"
> accept="image/*"
<AppButtonUpload :text="$t('recipe.upload-image')"
v-if="!uploadedImage" :text-btn="false"
class="ml-auto" :post="false"
url="none" @uploaded="uploadImage"
file-name="image" />
accept="image/*"
:text="$t('recipe.upload-image')" <div v-if="uploadedImages.length > 0" class="mt-3">
:text-btn="false" <p class="my-2">
:post="false" {{ $t("recipe.crop-and-rotate-the-image") }}
@uploaded="uploadImage" </p>
/>
<v-btn
v-if="!!uploadedImage"
color="error"
@click="clearImage"
>
<v-icon start>
{{ $globals.icons.close }}
</v-icon>
{{ $t('recipe.remove-image') }}
</v-btn>
</v-col>
<v-spacer />
</v-row>
<div
v-if="uploadedImage && uploadedImagePreviewUrl"
class="mt-3"
>
<v-row> <v-row>
<v-col <v-col
v-for="(imageUrl, index) in uploadedImagesPreviewUrls"
:key="index"
cols="12" cols="12"
class="pb-0" sm="6"
lg="4"
xl="3"
> >
<v-card-text class="pa-0">
<p class="mb-0">
{{ $t('recipe.crop-and-rotate-the-image') }}
</p>
</v-card-text>
</v-col>
</v-row>
<v-row style="max-width: 600px;">
<v-spacer />
<v-col cols="12">
<ImageCropper <ImageCropper
:img="uploadedImagePreviewUrl" :img="imageUrl"
cropper-height="50vh" cropper-height="100%"
cropper-width="100%" cropper-width="100%"
@save="updateUploadedImage" class="mt-4"
@save="(croppedImage) => updateUploadedImage(index, croppedImage)"
@delete="clearImage(index)"
/> />
</v-col> </v-col>
<v-spacer />
</v-row> </v-row>
</div> </div>
</v-container> </v-container>
</v-card-text> </v-card-text>
<v-card-actions v-if="uploadedImage"> <v-card-actions v-if="uploadedImages.length > 0">
<div> <div class="w-100 d-flex flex-column align-center">
<p style="width: 250px"> <p style="width: 250px">
<BaseButton <BaseButton rounded block type="submit" :loading="loading" />
rounded
block
type="submit"
:loading="loading"
/>
</p> </p>
<p> <p>
<v-checkbox <v-checkbox
@ -90,11 +59,8 @@
:disabled="loading" :disabled="loading"
/> />
</p> </p>
<p <p v-if="loading" class="mb-0">
v-if="loading" {{ $t("recipe.please-wait-image-procesing") }}
class="mb-0"
>
{{ $t('recipe.please-wait-image-procesing') }}
</p> </p>
</div> </div>
</v-card-actions> </v-card-actions>
@ -121,55 +87,56 @@ export default defineNuxtComponent({
const groupSlug = computed(() => route.params.groupSlug || ""); const groupSlug = computed(() => route.params.groupSlug || "");
const domUrlForm = ref<VForm | null>(null); const domUrlForm = ref<VForm | null>(null);
const uploadedImage = ref<Blob | File>(); const uploadedImages = ref<(Blob | File)[]>([]);
const uploadedImageName = ref<string>(""); const uploadedImageNames = ref<string[]>([]);
const uploadedImagePreviewUrl = ref<string>(); const uploadedImagesPreviewUrls = ref<string[]>([]);
const shouldTranslate = ref(true); const shouldTranslate = ref(true);
function uploadImage(fileObject: File) { function uploadImage(fileObject: File) {
uploadedImage.value = fileObject; uploadedImages.value = [...uploadedImages.value, fileObject];
uploadedImageName.value = fileObject.name; uploadedImageNames.value = [...uploadedImageNames.value, fileObject.name];
uploadedImagePreviewUrl.value = URL.createObjectURL(fileObject); uploadedImagesPreviewUrls.value = [...uploadedImagesPreviewUrls.value, URL.createObjectURL(fileObject)];
} }
function updateUploadedImage(fileObject: Blob) { function clearImage(index: number) {
uploadedImage.value = fileObject; URL.revokeObjectURL(uploadedImagesPreviewUrls.value[index]);
uploadedImagePreviewUrl.value = URL.createObjectURL(fileObject);
}
function clearImage() { uploadedImages.value = uploadedImages.value.filter((_, i) => i !== index);
uploadedImage.value = undefined; uploadedImageNames.value = uploadedImageNames.value.filter((_, i) => i !== index);
uploadedImageName.value = ""; uploadedImagesPreviewUrls.value = uploadedImagesPreviewUrls.value.filter((_, i) => i !== index);
uploadedImagePreviewUrl.value = undefined;
} }
async function createRecipe() { async function createRecipe() {
if (!uploadedImage.value) { if (uploadedImages.value.length === 0) {
return; return;
} }
state.loading = true; state.loading = true;
const translateLanguage = shouldTranslate.value ? i18n.locale : undefined; const translateLanguage = shouldTranslate.value ? i18n.locale : undefined;
const { data, error } = await api.recipes.createOneFromImage(uploadedImage.value, uploadedImageName.value, translateLanguage?.value); const { data, error } = await api.recipes.createOneFromImages(uploadedImages.value, translateLanguage?.value);
if (error || !data) { if (error || !data) {
alert.error(i18n.t("events.something-went-wrong")); alert.error(i18n.t("events.something-went-wrong"));
state.loading = false; state.loading = false;
} }
else { else {
router.push(`/g/${groupSlug.value}/r/${data}`); router.push(`/g/${groupSlug.value}/r/${data}`);
}; }
}
function updateUploadedImage(index: number, croppedImage: Blob) {
uploadedImages.value[index] = croppedImage;
} }
return { return {
...toRefs(state), ...toRefs(state),
domUrlForm, domUrlForm,
uploadedImage, uploadedImages,
uploadedImagePreviewUrl, uploadedImagesPreviewUrls,
shouldTranslate, shouldTranslate,
uploadImage, uploadImage,
updateUploadedImage,
clearImage, clearImage,
createRecipe, createRecipe,
updateUploadedImage,
}; };
}, },
}); });