2021-07-31 15:07:19 -08:00
|
|
|
<template>
|
2025-07-05 03:37:42 +02:00
|
|
|
<div v-if="model.length > 0 || edit">
|
2023-11-24 10:40:35 +01:00
|
|
|
<v-card class="mt-4">
|
2026-01-31 15:48:42 +01:00
|
|
|
<v-list-item class="pr-2 pl-0">
|
|
|
|
|
<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>
|
2025-06-20 00:09:12 +07:00
|
|
|
<v-divider class="mx-2" />
|
|
|
|
|
<v-list
|
2025-07-05 03:37:42 +02:00
|
|
|
v-if="model.length > 0"
|
2026-01-31 15:48:42 +01:00
|
|
|
lines="two"
|
2025-06-20 00:09:12 +07:00
|
|
|
:flat="!edit"
|
|
|
|
|
>
|
|
|
|
|
<v-list-item
|
2025-07-05 03:37:42 +02:00
|
|
|
v-for="(item, i) in model"
|
2025-06-20 00:09:12 +07:00
|
|
|
:key="i"
|
2026-01-31 15:48:42 +01:00
|
|
|
:href="!edit ? assetURL(item.fileName ?? '') : ''"
|
|
|
|
|
target="_blank"
|
|
|
|
|
class="pr-2"
|
2025-06-20 00:09:12 +07:00
|
|
|
>
|
|
|
|
|
<template #prepend>
|
2026-01-31 15:48:42 +01:00
|
|
|
<v-avatar size="48" rounded="lg" class="elevation-1">
|
|
|
|
|
<v-img
|
|
|
|
|
v-if="isImage(item.fileName)"
|
|
|
|
|
:src="assetURL(item.fileName ?? '')"
|
|
|
|
|
:alt="item.name"
|
|
|
|
|
loading="lazy"
|
|
|
|
|
cover
|
|
|
|
|
/>
|
|
|
|
|
<v-icon v-else size="large">
|
|
|
|
|
{{ getIconDefinition(item.icon).icon }}
|
|
|
|
|
</v-icon>
|
|
|
|
|
</v-avatar>
|
2025-06-20 00:09:12 +07:00
|
|
|
</template>
|
2026-01-31 15:48:42 +01:00
|
|
|
|
|
|
|
|
<v-list-item-title>
|
2025-06-20 00:09:12 +07:00
|
|
|
{{ item.name }}
|
|
|
|
|
</v-list-item-title>
|
2025-11-29 10:13:26 -06:00
|
|
|
<template #append>
|
2026-01-31 15:48:42 +01:00
|
|
|
<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>
|
2025-06-20 00:09:12 +07:00
|
|
|
<v-btn
|
|
|
|
|
v-if="!edit"
|
|
|
|
|
icon
|
2026-01-31 15:48:42 +01:00
|
|
|
variant="plain"
|
2025-06-20 00:09:12 +07:00
|
|
|
:href="assetURL(item.fileName ?? '')"
|
2026-01-31 15:48:42 +01:00
|
|
|
download
|
2025-06-20 00:09:12 +07:00
|
|
|
>
|
2021-07-31 15:07:19 -08:00
|
|
|
<v-icon> {{ $globals.icons.download }} </v-icon>
|
|
|
|
|
</v-btn>
|
2025-11-29 10:13:26 -06:00
|
|
|
</template>
|
2021-07-31 15:07:19 -08:00
|
|
|
</v-list-item>
|
|
|
|
|
</v-list>
|
|
|
|
|
</v-card>
|
|
|
|
|
<div class="d-flex ml-auto mt-2">
|
2025-06-20 00:09:12 +07:00
|
|
|
<v-spacer />
|
2021-11-25 14:17:02 -09:00
|
|
|
<BaseDialog
|
2022-05-25 11:50:45 -08:00
|
|
|
v-model="state.newAssetDialog"
|
2025-06-20 00:09:12 +07:00
|
|
|
:title="$t('asset.new-asset')"
|
2022-05-25 11:50:45 -08:00
|
|
|
:icon="getIconDefinition(state.newAsset.icon).icon"
|
2025-06-20 00:09:12 +07:00
|
|
|
can-submit
|
2021-11-25 14:17:02 -09:00
|
|
|
@submit="addAsset"
|
|
|
|
|
>
|
2021-11-22 20:10:48 -09:00
|
|
|
<v-card-text class="pt-4">
|
2025-06-20 00:09:12 +07:00
|
|
|
<v-text-field
|
|
|
|
|
v-model="state.newAsset.name"
|
|
|
|
|
:label="$t('general.name')"
|
|
|
|
|
/>
|
2021-07-31 15:07:19 -08:00
|
|
|
<div class="d-flex justify-space-between">
|
|
|
|
|
<v-select
|
2022-05-25 11:50:45 -08:00
|
|
|
v-model="state.newAsset.icon"
|
2025-06-20 00:09:12 +07:00
|
|
|
density="compact"
|
2022-05-25 11:50:45 -08:00
|
|
|
:prepend-icon="getIconDefinition(state.newAsset.icon).icon"
|
2021-07-31 15:07:19 -08:00
|
|
|
:items="iconOptions"
|
2025-06-20 00:09:12 +07:00
|
|
|
item-title="title"
|
2021-07-31 15:07:19 -08:00
|
|
|
item-value="name"
|
|
|
|
|
class="mr-2"
|
|
|
|
|
>
|
2026-01-31 15:48:42 +01:00
|
|
|
<template #item="{ props: itemProps, item }">
|
2025-11-29 11:33:17 -06:00
|
|
|
<v-list-item v-bind="itemProps">
|
2025-11-29 10:13:26 -06:00
|
|
|
<template #prepend>
|
2026-01-31 15:48:42 +01:00
|
|
|
<v-avatar>
|
|
|
|
|
<v-icon>
|
|
|
|
|
{{ item.raw.icon }}
|
|
|
|
|
</v-icon>
|
|
|
|
|
</v-avatar>
|
2025-11-29 10:13:26 -06:00
|
|
|
</template>
|
|
|
|
|
</v-list-item>
|
2021-07-31 15:07:19 -08:00
|
|
|
</template>
|
|
|
|
|
</v-select>
|
2025-06-20 00:09:12 +07:00
|
|
|
<AppButtonUpload
|
|
|
|
|
:post="false"
|
|
|
|
|
file-name="file"
|
|
|
|
|
:text-btn="false"
|
|
|
|
|
@uploaded="setFileObject"
|
|
|
|
|
/>
|
2021-07-31 15:07:19 -08:00
|
|
|
</div>
|
|
|
|
|
</v-card-text>
|
2021-08-01 19:24:47 -08:00
|
|
|
</BaseDialog>
|
2021-07-31 15:07:19 -08:00
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</template>
|
|
|
|
|
|
2025-07-05 03:37:42 +02:00
|
|
|
<script setup lang="ts">
|
2022-01-09 07:15:23 +01:00
|
|
|
import { useStaticRoutes, useUserApi } from "~/composables/api";
|
2021-11-22 20:10:48 -09:00
|
|
|
import { alert } from "~/composables/use-toast";
|
2025-06-20 00:09:12 +07:00
|
|
|
import type { RecipeAsset } from "~/lib/api/types/recipe";
|
2026-01-31 15:48:42 +01:00
|
|
|
import { useCopy } from "~/composables/use-copy";
|
2021-11-22 20:10:48 -09:00
|
|
|
|
2025-07-05 03:37:42 +02:00
|
|
|
const props = defineProps({
|
|
|
|
|
slug: {
|
|
|
|
|
type: String,
|
|
|
|
|
required: true,
|
2021-07-31 15:07:19 -08:00
|
|
|
},
|
2025-07-05 03:37:42 +02:00
|
|
|
recipeId: {
|
|
|
|
|
type: String,
|
|
|
|
|
required: true,
|
|
|
|
|
},
|
|
|
|
|
edit: {
|
|
|
|
|
type: Boolean,
|
|
|
|
|
default: true,
|
|
|
|
|
},
|
|
|
|
|
});
|
2021-08-02 22:15:11 -08:00
|
|
|
|
2025-07-05 03:37:42 +02:00
|
|
|
const model = defineModel<RecipeAsset[]>({ required: true });
|
2021-11-22 20:10:48 -09:00
|
|
|
|
2025-07-05 03:37:42 +02:00
|
|
|
const api = useUserApi();
|
2021-11-22 20:10:48 -09:00
|
|
|
|
2025-07-05 03:37:42 +02:00
|
|
|
const state = reactive({
|
|
|
|
|
newAssetDialog: false,
|
|
|
|
|
fileObject: {} as File,
|
|
|
|
|
newAsset: {
|
|
|
|
|
name: "",
|
|
|
|
|
icon: "mdi-file",
|
|
|
|
|
},
|
|
|
|
|
});
|
2021-11-22 20:10:48 -09:00
|
|
|
|
2025-07-05 03:37:42 +02:00
|
|
|
const i18n = useI18n();
|
|
|
|
|
const { $globals } = useNuxtApp();
|
2026-01-31 15:48:42 +01:00
|
|
|
const { copyText } = useCopy();
|
2022-08-28 13:54:32 -08:00
|
|
|
|
2025-07-05 03:37:42 +02:00
|
|
|
const iconOptions = [
|
|
|
|
|
{
|
|
|
|
|
name: "mdi-file",
|
|
|
|
|
title: i18n.t("asset.file"),
|
|
|
|
|
icon: $globals.icons.file,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "mdi-file-pdf-box",
|
|
|
|
|
title: i18n.t("asset.pdf"),
|
|
|
|
|
icon: $globals.icons.filePDF,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "mdi-file-image",
|
|
|
|
|
title: i18n.t("asset.image"),
|
|
|
|
|
icon: $globals.icons.fileImage,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "mdi-code-json",
|
|
|
|
|
title: i18n.t("asset.code"),
|
|
|
|
|
icon: $globals.icons.codeJson,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "mdi-silverware-fork-knife",
|
|
|
|
|
title: i18n.t("asset.recipe"),
|
|
|
|
|
icon: $globals.icons.primary,
|
|
|
|
|
},
|
|
|
|
|
];
|
2021-11-22 20:10:48 -09:00
|
|
|
|
2025-07-05 03:37:42 +02:00
|
|
|
const serverBase = useRequestURL().origin;
|
2021-11-22 20:10:48 -09:00
|
|
|
|
2025-07-05 03:37:42 +02:00
|
|
|
function getIconDefinition(icon: string) {
|
|
|
|
|
return iconOptions.find(item => item.name === icon) || iconOptions[0];
|
|
|
|
|
}
|
2021-11-22 20:10:48 -09:00
|
|
|
|
2026-01-31 15:48:42 +01:00
|
|
|
function isImage(fileName?: string | null) {
|
|
|
|
|
if (!fileName) return false;
|
|
|
|
|
return /\.(png|jpe?g|gif|webp|bmp|avif)$/i.test(fileName);
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-05 03:37:42 +02:00
|
|
|
const { recipeAssetPath } = useStaticRoutes();
|
|
|
|
|
function assetURL(assetName: string) {
|
|
|
|
|
return recipeAssetPath(props.recipeId, assetName);
|
|
|
|
|
}
|
2021-11-22 20:10:48 -09:00
|
|
|
|
2025-07-05 03:37:42 +02:00
|
|
|
function assetEmbed(name: string) {
|
2026-01-31 15:48:42 +01:00
|
|
|
return `<img src="${serverBase}${assetURL(name)}" height="100%" width="100%" />`;
|
2025-07-05 03:37:42 +02:00
|
|
|
}
|
2021-11-22 20:10:48 -09:00
|
|
|
|
2025-07-05 03:37:42 +02:00
|
|
|
function setFileObject(fileObject: File) {
|
|
|
|
|
state.fileObject = fileObject;
|
2026-01-31 15:48:42 +01:00
|
|
|
// 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("."));
|
|
|
|
|
}
|
2025-07-05 03:37:42 +02:00
|
|
|
}
|
2021-11-22 20:10:48 -09:00
|
|
|
|
2025-07-05 03:37:42 +02:00
|
|
|
function validFields() {
|
2026-01-31 15:48:42 +01:00
|
|
|
// Only require a file; name will fall back to the file name if empty
|
|
|
|
|
return Boolean(state.fileObject?.name);
|
2025-07-05 03:37:42 +02:00
|
|
|
}
|
2021-11-22 20:10:48 -09:00
|
|
|
|
2025-07-05 03:37:42 +02:00
|
|
|
async function addAsset() {
|
|
|
|
|
if (!validFields()) {
|
|
|
|
|
alert.error(i18n.t("asset.error-submitting-form") as string);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-11-22 20:10:48 -09:00
|
|
|
|
2026-01-31 15:48:42 +01:00
|
|
|
const nameToUse = state.newAsset.name?.trim() || state.fileObject.name;
|
|
|
|
|
|
2025-07-05 03:37:42 +02:00
|
|
|
const { data } = await api.recipes.createAsset(props.slug, {
|
2026-01-31 15:48:42 +01:00
|
|
|
name: nameToUse,
|
2025-07-05 03:37:42 +02:00
|
|
|
icon: state.newAsset.icon,
|
|
|
|
|
file: state.fileObject,
|
|
|
|
|
extension: state.fileObject.name.split(".").pop() || "",
|
|
|
|
|
});
|
|
|
|
|
if (data) {
|
|
|
|
|
model.value = [...model.value, data];
|
|
|
|
|
}
|
|
|
|
|
state.newAsset = { name: "", icon: "mdi-file" };
|
|
|
|
|
state.fileObject = {} as File;
|
|
|
|
|
}
|
2021-07-31 15:07:19 -08:00
|
|
|
</script>
|