mirror of
https://github.com/mealie-recipes/mealie.git
synced 2026-01-31 21:13:15 -05:00
feat: Improve recipe assets preview (#6602)
Co-authored-by: Michael Genson <71845777+michael-genson@users.noreply.github.com>
This commit is contained in:
@@ -1,60 +1,97 @@
|
|||||||
<template>
|
<template>
|
||||||
<div v-if="model.length > 0 || edit">
|
<div v-if="model.length > 0 || edit">
|
||||||
<v-card class="mt-4">
|
<v-card class="mt-4">
|
||||||
<v-card-title class="py-2">
|
<v-list-item class="pr-2 pl-0">
|
||||||
{{ $t("asset.assets") }}
|
<v-card-title>
|
||||||
</v-card-title>
|
{{ $t("asset.assets") }}
|
||||||
|
</v-card-title>
|
||||||
|
<template #append>
|
||||||
|
<v-btn
|
||||||
|
v-if="edit"
|
||||||
|
variant="plain"
|
||||||
|
:icon="$globals.icons.create"
|
||||||
|
@click="state.newAssetDialog = true"
|
||||||
|
/>
|
||||||
|
</template>
|
||||||
|
</v-list-item>
|
||||||
<v-divider class="mx-2" />
|
<v-divider class="mx-2" />
|
||||||
<v-list
|
<v-list
|
||||||
v-if="model.length > 0"
|
v-if="model.length > 0"
|
||||||
|
lines="two"
|
||||||
:flat="!edit"
|
:flat="!edit"
|
||||||
>
|
>
|
||||||
<v-list-item
|
<v-list-item
|
||||||
v-for="(item, i) in model"
|
v-for="(item, i) in model"
|
||||||
:key="i"
|
:key="i"
|
||||||
|
:href="!edit ? assetURL(item.fileName ?? '') : ''"
|
||||||
|
target="_blank"
|
||||||
|
class="pr-2"
|
||||||
>
|
>
|
||||||
<template #prepend>
|
<template #prepend>
|
||||||
<div class="ma-auto">
|
<v-avatar size="48" rounded="lg" class="elevation-1">
|
||||||
<v-tooltip location="bottom">
|
<v-img
|
||||||
<template #activator="{ props: tooltipProps }">
|
v-if="isImage(item.fileName)"
|
||||||
<v-icon v-bind="tooltipProps">
|
:src="assetURL(item.fileName ?? '')"
|
||||||
{{ getIconDefinition(item.icon).icon }}
|
:alt="item.name"
|
||||||
</v-icon>
|
loading="lazy"
|
||||||
</template>
|
cover
|
||||||
<span>{{ getIconDefinition(item.icon).title }}</span>
|
/>
|
||||||
</v-tooltip>
|
<v-icon v-else size="large">
|
||||||
</div>
|
{{ getIconDefinition(item.icon).icon }}
|
||||||
|
</v-icon>
|
||||||
|
</v-avatar>
|
||||||
</template>
|
</template>
|
||||||
<v-list-item-title class="pl-2">
|
|
||||||
|
<v-list-item-title>
|
||||||
{{ item.name }}
|
{{ item.name }}
|
||||||
</v-list-item-title>
|
</v-list-item-title>
|
||||||
<template #append>
|
<template #append>
|
||||||
|
<v-menu v-if="edit" location="bottom end">
|
||||||
|
<template #activator="{ props: menuProps }">
|
||||||
|
<v-btn
|
||||||
|
v-bind="menuProps"
|
||||||
|
icon
|
||||||
|
variant="plain"
|
||||||
|
>
|
||||||
|
<v-icon :icon="$globals.icons.dotsVertical" />
|
||||||
|
</v-btn>
|
||||||
|
</template>
|
||||||
|
<v-list density="compact" min-width="220">
|
||||||
|
<v-list-item
|
||||||
|
:href="assetURL(item.fileName ?? '')"
|
||||||
|
:prepend-icon="$globals.icons.eye"
|
||||||
|
:title="$t('general.view')"
|
||||||
|
target="_blank"
|
||||||
|
/>
|
||||||
|
<v-list-item
|
||||||
|
:href="assetURL(item.fileName ?? '')"
|
||||||
|
:prepend-icon="$globals.icons.download"
|
||||||
|
:title="$t('general.download')"
|
||||||
|
download
|
||||||
|
/>
|
||||||
|
<v-list-item
|
||||||
|
v-if="edit"
|
||||||
|
:prepend-icon="$globals.icons.contentCopy"
|
||||||
|
:title="$t('general.copy')"
|
||||||
|
@click="copyText(assetEmbed(item.fileName ?? ''))"
|
||||||
|
/>
|
||||||
|
<v-list-item
|
||||||
|
v-if="edit"
|
||||||
|
:prepend-icon="$globals.icons.delete"
|
||||||
|
:title="$t('general.delete')"
|
||||||
|
@click="model.splice(i, 1)"
|
||||||
|
/>
|
||||||
|
</v-list>
|
||||||
|
</v-menu>
|
||||||
<v-btn
|
<v-btn
|
||||||
v-if="!edit"
|
v-if="!edit"
|
||||||
color="primary"
|
|
||||||
icon
|
icon
|
||||||
size="small"
|
variant="plain"
|
||||||
:href="assetURL(item.fileName ?? '')"
|
:href="assetURL(item.fileName ?? '')"
|
||||||
target="_blank"
|
download
|
||||||
top
|
|
||||||
>
|
>
|
||||||
<v-icon> {{ $globals.icons.download }} </v-icon>
|
<v-icon> {{ $globals.icons.download }} </v-icon>
|
||||||
</v-btn>
|
</v-btn>
|
||||||
<div v-else>
|
|
||||||
<v-btn
|
|
||||||
color="error"
|
|
||||||
icon
|
|
||||||
size="small"
|
|
||||||
top
|
|
||||||
@click="model.splice(i, 1)"
|
|
||||||
>
|
|
||||||
<v-icon>{{ $globals.icons.delete }}</v-icon>
|
|
||||||
</v-btn>
|
|
||||||
<AppButtonCopy
|
|
||||||
color=""
|
|
||||||
:copy-text="assetEmbed(item.fileName ?? '')"
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
</template>
|
</template>
|
||||||
</v-list-item>
|
</v-list-item>
|
||||||
</v-list>
|
</v-list>
|
||||||
@@ -68,18 +105,9 @@
|
|||||||
can-submit
|
can-submit
|
||||||
@submit="addAsset"
|
@submit="addAsset"
|
||||||
>
|
>
|
||||||
<template #activator>
|
|
||||||
<BaseButton
|
|
||||||
v-if="edit"
|
|
||||||
size="small"
|
|
||||||
create
|
|
||||||
@click="state.newAssetDialog = true"
|
|
||||||
/>
|
|
||||||
</template>
|
|
||||||
<v-card-text class="pt-4">
|
<v-card-text class="pt-4">
|
||||||
<v-text-field
|
<v-text-field
|
||||||
v-model="state.newAsset.name"
|
v-model="state.newAsset.name"
|
||||||
density="compact"
|
|
||||||
:label="$t('general.name')"
|
:label="$t('general.name')"
|
||||||
/>
|
/>
|
||||||
<div class="d-flex justify-space-between">
|
<div class="d-flex justify-space-between">
|
||||||
@@ -92,10 +120,14 @@
|
|||||||
item-value="name"
|
item-value="name"
|
||||||
class="mr-2"
|
class="mr-2"
|
||||||
>
|
>
|
||||||
<template #item="{ item, props: itemProps }">
|
<template #item="{ props: itemProps, item }">
|
||||||
<v-list-item v-bind="itemProps">
|
<v-list-item v-bind="itemProps">
|
||||||
<template #prepend>
|
<template #prepend>
|
||||||
<v-icon>{{ item.raw.icon }}</v-icon>
|
<v-avatar>
|
||||||
|
<v-icon>
|
||||||
|
{{ item.raw.icon }}
|
||||||
|
</v-icon>
|
||||||
|
</v-avatar>
|
||||||
</template>
|
</template>
|
||||||
</v-list-item>
|
</v-list-item>
|
||||||
</template>
|
</template>
|
||||||
@@ -107,7 +139,6 @@
|
|||||||
@uploaded="setFileObject"
|
@uploaded="setFileObject"
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
{{ state.fileObject.name }}
|
|
||||||
</v-card-text>
|
</v-card-text>
|
||||||
</BaseDialog>
|
</BaseDialog>
|
||||||
</div>
|
</div>
|
||||||
@@ -118,6 +149,7 @@
|
|||||||
import { useStaticRoutes, useUserApi } from "~/composables/api";
|
import { useStaticRoutes, useUserApi } from "~/composables/api";
|
||||||
import { alert } from "~/composables/use-toast";
|
import { alert } from "~/composables/use-toast";
|
||||||
import type { RecipeAsset } from "~/lib/api/types/recipe";
|
import type { RecipeAsset } from "~/lib/api/types/recipe";
|
||||||
|
import { useCopy } from "~/composables/use-copy";
|
||||||
|
|
||||||
const props = defineProps({
|
const props = defineProps({
|
||||||
slug: {
|
slug: {
|
||||||
@@ -149,6 +181,7 @@ const state = reactive({
|
|||||||
|
|
||||||
const i18n = useI18n();
|
const i18n = useI18n();
|
||||||
const { $globals } = useNuxtApp();
|
const { $globals } = useNuxtApp();
|
||||||
|
const { copyText } = useCopy();
|
||||||
|
|
||||||
const iconOptions = [
|
const iconOptions = [
|
||||||
{
|
{
|
||||||
@@ -184,21 +217,31 @@ function getIconDefinition(icon: string) {
|
|||||||
return iconOptions.find(item => item.name === icon) || iconOptions[0];
|
return iconOptions.find(item => item.name === icon) || iconOptions[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function isImage(fileName?: string | null) {
|
||||||
|
if (!fileName) return false;
|
||||||
|
return /\.(png|jpe?g|gif|webp|bmp|avif)$/i.test(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
const { recipeAssetPath } = useStaticRoutes();
|
const { recipeAssetPath } = useStaticRoutes();
|
||||||
function assetURL(assetName: string) {
|
function assetURL(assetName: string) {
|
||||||
return recipeAssetPath(props.recipeId, assetName);
|
return recipeAssetPath(props.recipeId, assetName);
|
||||||
}
|
}
|
||||||
|
|
||||||
function assetEmbed(name: string) {
|
function assetEmbed(name: string) {
|
||||||
return `<img src="${serverBase}${assetURL(name)}" height="100%" width="100%"> </img>`;
|
return `<img src="${serverBase}${assetURL(name)}" height="100%" width="100%" />`;
|
||||||
}
|
}
|
||||||
|
|
||||||
function setFileObject(fileObject: File) {
|
function setFileObject(fileObject: File) {
|
||||||
state.fileObject = fileObject;
|
state.fileObject = fileObject;
|
||||||
|
// If the user didn't provide a name, default to the file base name
|
||||||
|
if (!state.newAsset.name?.trim()) {
|
||||||
|
state.newAsset.name = fileObject.name.substring(0, fileObject.name.lastIndexOf("."));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function validFields() {
|
function validFields() {
|
||||||
return state.newAsset.name.length > 0 && state.fileObject.name.length > 0;
|
// Only require a file; name will fall back to the file name if empty
|
||||||
|
return Boolean(state.fileObject?.name);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function addAsset() {
|
async function addAsset() {
|
||||||
@@ -207,8 +250,10 @@ async function addAsset() {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const nameToUse = state.newAsset.name?.trim() || state.fileObject.name;
|
||||||
|
|
||||||
const { data } = await api.recipes.createAsset(props.slug, {
|
const { data } = await api.recipes.createAsset(props.slug, {
|
||||||
name: state.newAsset.name,
|
name: nameToUse,
|
||||||
icon: state.newAsset.icon,
|
icon: state.newAsset.icon,
|
||||||
file: state.fileObject,
|
file: state.fileObject,
|
||||||
extension: state.fileObject.name.split(".").pop() || "",
|
extension: state.fileObject.name.split(".").pop() || "",
|
||||||
|
|||||||
@@ -53,7 +53,7 @@ async def get_recipe_asset(recipe_id: UUID4, file_name: str):
|
|||||||
"""Returns a recipe asset"""
|
"""Returns a recipe asset"""
|
||||||
file = Recipe.directory_from_id(recipe_id).joinpath("assets", file_name)
|
file = Recipe.directory_from_id(recipe_id).joinpath("assets", file_name)
|
||||||
|
|
||||||
try:
|
if file.exists():
|
||||||
return FileResponse(file, content_disposition_type="attachment", filename=file_name)
|
return FileResponse(file)
|
||||||
except Exception as e:
|
else:
|
||||||
raise HTTPException(status.HTTP_404_NOT_FOUND) from e
|
raise HTTPException(status.HTTP_404_NOT_FOUND)
|
||||||
|
|||||||
Reference in New Issue
Block a user