Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom scaling option #1345

Merged
merged 14 commits into from
Jun 9, 2022
102 changes: 102 additions & 0 deletions frontend/components/Domain/Recipe/RecipeScaleEditButton.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<template>
<div>
<div class="text-center d-flex align-center">
<div>
<v-menu v-model="menu" offset-y top nudge-top="6" :close-on-content-click="false">
<template #activator="{ on, attrs }">
<v-card class="pa-1 px-2" dark color="secondary darken-1" small v-bind="attrs" v-on="on">
<span v-if="recipeYield"> {{ scaledYield }} </span>
<span v-if="!recipeYield"> x {{ scale }} </span>
</v-card>
</template>
<v-card min-width="300px">
<v-card-title class="mb-0">
{{ $t("recipe.edit-scale") }}
</v-card-title>
<v-card-text class="mt-n5">
<div class="mt-4 d-flex align-center">
<v-text-field v-model.number="scale" type="number" :min="0" :label="$t('recipe.edit-scale')" />
<v-tooltip right color="secondary darken-1">
<template #activator="{ on, attrs }">
<v-btn v-bind="attrs" icon class="mx-1" small v-on="on" @click="scale = 1">
<v-icon>
{{ $globals.icons.undo }}
</v-icon>
</v-btn>
</template>
<span> Reset Scale </span>
</v-tooltip>
</div>
</v-card-text>
</v-card>
</v-menu>
</div>
<BaseButtonGroup
class="pl-2"
:large="false"
:buttons="[
{
icon: $globals.icons.minus,
text: 'Decrease Scale by 1',
event: 'decrement',
},
{
icon: $globals.icons.createAlt,
text: 'Increase Scale by 1',
event: 'increment',
},
]"
@decrement="scale > 1 ? scale-- : null"
@increment="scale++"
/>
</div>
</div>
</template>

<script lang="ts">
import { defineComponent, reactive, toRefs, computed } from "@nuxtjs/composition-api";

export default defineComponent({
props: {
recipeYield: {
type: String,
default: null,
},
basicYield: {
type: String,
default: null,
},
scaledYield: {
type: String,
default: null,
},
editScale: {
type: Boolean,
default: false,
},
value: {
type: Number,
required: true,
},
},
setup(props, { emit }) {
const state = reactive({
tempScale: 1,
menu: false,
});

const scale = computed({
get: () => props.value,
set: (value) => {
const newScaleNumber = parseFloat(`${value}`);
emit("input", isNaN(newScaleNumber) ? 0 : newScaleNumber);
},
});

return {
scale,
...toRefs(state),
};
},
});
</script>
1 change: 0 additions & 1 deletion frontend/composables/recipes/use-recipe-ingredients.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ export function parseIngredientText(ingredient: RecipeIngredient, disableAmount:

// casting to number is required as sometimes quantity is a string
if (quantity && Number(quantity) !== 0) {
console.log("Using Quantity", quantity, typeof quantity);
if (unit?.fraction) {
const fraction = frac(quantity * scale, 10, true);
if (fraction[0] !== undefined && fraction[0] > 0) {
Expand Down
1 change: 1 addition & 0 deletions frontend/lang/messages/de-DE.json
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@
"description": "Beschreibung",
"disable-amount": "Zutatenmenge deaktivieren",
"disable-comments": "Kommentare deaktivieren",
"edit-scale": "Skalierung bearbeiten",
"fat-content": "Fett",
"fiber-content": "Ballaststoffe",
"grams": "g",
Expand Down
1 change: 1 addition & 0 deletions frontend/lang/messages/en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@
"description": "Description",
"disable-amount": "Disable Ingredient Amounts",
"disable-comments": "Disable Comments",
"edit-scale": "Edit Scale",
"fat-content": "Fat",
"fiber-content": "Fiber",
"grams": "grams",
Expand Down
62 changes: 34 additions & 28 deletions frontend/pages/recipe/_slug/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -178,38 +178,18 @@
<div class="d-flex justify-space-between align-center pt-2 pb-3">
<v-tooltip v-if="!form" small top color="secondary darken-1">
<template #activator="{ on, attrs }">
<v-btn
v-if="recipe.recipeYield"
dense
small
:hover="false"
type="label"
:ripple="false"
elevation="0"
color="secondary darken-1"
class="rounded-sm static"
<RecipeScaleEditButton
v-model.number="scale"
v-bind="attrs"
@click="scale = 1"
:recipe-yield="recipe.recipeYield"
:basic-yield="basicYield"
:scaled-yield="scaledYield"
:edit-scale="!recipe.settings.disableAmount && !form"
v-on="on"
>
{{ scaledYield }}
</v-btn>
/>
</template>
<span> Reset Scale </span>
<span> {{ $t("recipe.edit-scale") }} </span>
</v-tooltip>

<template v-if="!recipe.settings.disableAmount && !form">
<v-btn color="secondary darken-1" class="mx-1" small @click="scale > 1 ? scale-- : null">
<v-icon>
{{ $globals.icons.minus }}
</v-icon>
</v-btn>
<v-btn color="secondary darken-1" small @click="scale++">
<v-icon>
{{ $globals.icons.createAlt }}
</v-icon>
</v-btn>
</template>
<v-spacer></v-spacer>

<RecipeRating
Expand Down Expand Up @@ -497,6 +477,7 @@ import RecipeNutrition from "~/components/Domain/Recipe/RecipeNutrition.vue";
import RecipeInstructions from "~/components/Domain/Recipe/RecipeInstructions.vue";
import RecipeNotes from "~/components/Domain/Recipe/RecipeNotes.vue";
import RecipeImageUploadBtn from "~/components/Domain/Recipe/RecipeImageUploadBtn.vue";
import RecipeScaleEditButton from "~/components/Domain/Recipe/RecipeScaleEditButton.vue";
import RecipeSettingsMenu from "~/components/Domain/Recipe/RecipeSettingsMenu.vue";
import RecipeIngredientEditor from "~/components/Domain/Recipe/RecipeIngredientEditor.vue";
import RecipePrintView from "~/components/Domain/Recipe/RecipePrintView.vue";
Expand Down Expand Up @@ -531,6 +512,7 @@ export default defineComponent({
RecipeSettingsMenu,
RecipeTimeCard,
RecipeTools,
RecipeScaleEditButton,
VueMarkdown,
},
async beforeRouteLeave(_to, _from, next) {
Expand Down Expand Up @@ -607,6 +589,8 @@ export default defineComponent({
const state = reactive({
form: false,
scale: 1,
scaleTemp: 1,
scaleDialog: false,
hideImage: false,
imageKey: 1,
skeleton: false,
Expand Down Expand Up @@ -698,6 +682,19 @@ export default defineComponent({
return recipe.value?.recipeYield;
});

const basicYield = computed(() => {
const regMatchNum = /\d+/;
const yieldString = recipe.value?.recipeYield;
const num = yieldString?.match(regMatchNum);

if (num && num?.length > 0) {
const yieldAsInt = parseInt(num[0]);
return yieldString?.replace(num[0], String(yieldAsInt));
}

return recipe.value?.recipeYield;
});

async function uploadImage(fileObject: File) {
if (!recipe.value || !recipe.value.slug) {
return;
Expand Down Expand Up @@ -827,6 +824,13 @@ export default defineComponent({

const drag = ref(false);

// ===============================================================
// Scale

const setScale = (newScale: number) => {
state.scale = newScale;
};

return {
// Wake Lock
drag,
Expand All @@ -844,12 +848,14 @@ export default defineComponent({
enableLandscape,
imageHeight,
scaledYield,
basicYield,
toggleJson,
...toRefs(state),
recipe,
api,
loading,
addStep,
setScale,
deleteRecipe,
printRecipe,
closeEditor,
Expand Down
1 change: 1 addition & 0 deletions frontend/utils/icons/icon-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ export interface Icon {
webhook: string;
windowClose: string;
zip: string;
undo: string;

// Crud
backArrow: string;
Expand Down
2 changes: 2 additions & 0 deletions frontend/utils/icons/icons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ import {
mdiChartLine,
mdiHelpCircleOutline,
mdiDocker,
mdiUndo,
} from "@mdi/js";

export const icons = {
Expand Down Expand Up @@ -206,6 +207,7 @@ export const icons = {
webhook: mdiWebhook,
windowClose: mdiWindowClose,
zip: mdiFolderZipOutline,
undo: mdiUndo,

// Crud
backArrow: mdiArrowLeftBoldOutline,
Expand Down