Compare commits

..

10 Commits

2 changed files with 145 additions and 25 deletions

View File

@@ -42,7 +42,7 @@ async function submitRecipe() {
<div class="flex flex-row align-middle items-center"> <div class="flex flex-row align-middle items-center">
<p class="mt-4 text-[22pt] font-bold 'mt-4'">Ingredients</p> <p class="mt-4 text-[22pt] font-bold 'mt-4'">Ingredients</p>
<button class="hidden ml-auto mt-5 text-white bg-white/10 rounded-lg px-3 py-1 " id="add-ingredient-btn" >Add</button> <button disabled class="disabled:text-white/20 transition-colors ml-auto mt-5 text-white bg-white/10 rounded-lg px-3 py-1 " id="add-ingredient-btn" >Add</button>
</div> </div>
<table class={`table-fixed text-left bg-[#2a2b2c] rounded-lg w-full`}> <table class={`table-fixed text-left bg-[#2a2b2c] rounded-lg w-full`}>
<thead> <thead>
@@ -52,10 +52,10 @@ async function submitRecipe() {
<th class="px-4 py-2">Ingredient</th> <th class="px-4 py-2">Ingredient</th>
</tr> </tr>
</thead> </thead>
<tbody id="ingredient-table" class="w-full border-t px-4 py-2 border-gray-600"> <tbody id="ingredient-table" class="w-full border-t px-4 py-2 border-white/10">
<tr id="ingredient-input" class=""> <tr id="ingredient-input" class="">
<td class="px-2 py-1"> <td class="px-2 py-1">
<input id="ing-qty" class="w-full h-9 bg-white/10 rounded-lg px-2 py-2" type="text" placeholder="Qty"> <input id="ing-qty" class="w-full h-9 my-1 bg-white/10 rounded-lg px-2 py-2" type="text" placeholder="Qty">
</td> </td>
<td class="px-2 py-1"> <td class="px-2 py-1">
<input id="ing-unit" class="w-full h-9 bg-white/10 rounded-lg px-2 py-2" type="text" placeholder="Unit"> <input id="ing-unit" class="w-full h-9 bg-white/10 rounded-lg px-2 py-2" type="text" placeholder="Unit">
@@ -69,22 +69,26 @@ async function submitRecipe() {
</table> </table>
</div> </div>
<ul class="flex mt-4 md:flex-2/3 w-full flex-col"> <div class="flex mt-4 md:flex-2/3 w-full flex-col md:ml-3">
<!-- <p class="hidden md:block text-[28pt] font-bold pl-5">Helloi</p> --> <!-- <p class="hidden md:block text-[28pt] font-bold pl-5">Helloi</p> -->
<!-- Steps --> <!-- Steps -->
<p class="text-[22pt] font-bold md:hidden">Steps</p> <p class="text-[22pt] font-bold md:hidden">Steps</p>
<div class="bg-[#2a2b2c] md:ml-3 rounded-lg mb-2 p-2"> <div class="bg-[#2a2b2c] rounded-lg mb-2 p-2">
<!-- <p class="text-bold text-[10pt]">Step</p> --> <!-- <p class="text-bold text-[10pt]">Step</p> -->
<textarea <textarea
id="new-instruction" id="new-instruction"
class="bg-white/10 w-full h-full rounded-lg resize-none px-3 py-1" class="block bg-white/10 w-full h-full rounded-lg resize-none px-3 py-1"
placeholder="New Step" placeholder="New Step"
oninput="this.style.height = ''; this.style.height = this.scrollHeight + 'px'" oninput="this.style.height = ''; this.style.height = this.scrollHeight + 'px'"
/> />
</div> </div>
</ul>
<button disabled class="disabled:text-white/20 ml-auto mb-2 transition-colors text-white bg-white/10 rounded-lg px-3 py-1 " id="add-step-btn" >Add</button>
<ul id="step-list"></ul>
</div>
</div> </div>
</SiteLayout> </SiteLayout>

View File

@@ -2,17 +2,17 @@ let ingredientFields: HTMLInputElement[] = []
let ingredientTable: HTMLTableSectionElement = document.querySelector('#ingredient-table')! let ingredientTable: HTMLTableSectionElement = document.querySelector('#ingredient-table')!
let ingredientAddButton: HTMLButtonElement = document.querySelector('#add-ingredient-btn')! let ingredientAddButton: HTMLButtonElement = document.querySelector('#add-ingredient-btn')!
let stepInput: HTMLTextAreaElement = document.querySelector('#new-instruction')!
let stepAddButton: HTMLButtonElement = document.querySelector('#add-step-btn')!
let stepList: HTMLUListElement = document.querySelector('#step-list')!
let currentStepIndex = 0
// - VARS // - VARS
let ingredients: {qty: string, unit: string, name: string}[] = [] let ingredients: {qty: string, unit: string, name: string}[] = []
let steps: { let steps: {
name: string, index: number,
description: string, instruction: string,
servings: number, ingredients: string[], // IDs of ingredient fields
rating: number,
worktime: number,
waittime: number,
images: string[], // record ID of uploaded image
}[] = [] }[] = []
// - INIT // - INIT
@@ -23,13 +23,40 @@ document.addEventListener('DOMContentLoaded', function() {
document.querySelector('#ing-unit')!, document.querySelector('#ing-unit')!,
document.querySelector('#ing-name')! document.querySelector('#ing-name')!
) )
stepInput.addEventListener('input', showAddStepButton)
// show plus button once the user clicks off one of the text fields // show plus button once the user types in the text fields
ingredientFields.forEach(f => f.addEventListener('beforeinput', showAddIngredientButton)) ingredientFields.forEach(f => {
f.addEventListener('input', showAddIngredientButton)
f.addEventListener('keydown', showAddIngredientButton)
})
// onclick for add button // onclick for add button
document.querySelector('#add-ingredient-btn')?.addEventListener('click', addIngredient); document.querySelector('#add-ingredient-btn')?.addEventListener('click', addIngredient);
// for pressing enter to reset cursor
ingredientFields[2].addEventListener('keyup', e => {if (e.key === 'Enter') addIngredient()} ) // Enter key navigation for ingredient fields
ingredientFields[0].addEventListener('keydown', e => {
if (e.key === 'Enter') {
ingredientFields[1].focus() // Move from qty to unit
}
})
ingredientFields[1].addEventListener('keydown', e => {
if (e.key === 'Enter') {
ingredientFields[2].focus() // Move from unit to name
}
})
// for pressing enter to add ingredient (on the last field)
ingredientFields[2].addEventListener('keydown', e => {if (e.key === 'Enter') addIngredient()} )
// Initial check for button state
showAddIngredientButton()
showAddStepButton()
// Steps
stepInput.addEventListener('keyup', e => { if (e.key === 'Enter' && e.shiftKey) addStep() } )
stepAddButton.addEventListener('click', addStep)
}); });
// - ADD // - ADD
@@ -52,18 +79,107 @@ function addIngredient() {
// Add row to table and clear fields // Add row to table and clear fields
ingredientTable.appendChild(newRow) ingredientTable.appendChild(newRow)
ingredientFields.forEach(f => f.value = '') ingredientFields.forEach(f => f.value = '')
ingredientAddButton.style.display = 'none' // Hide Add Ingredient button ingredientAddButton.disabled = true // Hide Add Ingredient button
// move cursor to Qty field again // move cursor to Qty field again
ingredientFields[0].focus() ingredientFields[0].focus()
} }
function addStep() {
const step = {
index: currentStepIndex++,
instruction: stepInput.value,
ingredients: []
}
steps.push(step)
renderSteps()
stepInput.value = ''
stepAddButton.disabled = true
stepInput.focus()
}
function renderSteps() {
// clear the step list
stepList.innerHTML = ''
// re-render all steps in their current order
steps.forEach((step, displayIndex) => {
const newStep = document.createElement('div')
// times like this i regret using astro
newStep.innerHTML = `
<div class="flex justify-between items-start ">
<div class="flex flex-col">
<p class="text-bold text-[10pt]">Step ${displayIndex + 1}</p>
<div class="flex flex-col md:flex-row md:items-stretch">
<p class="w-full md:flex-2/3 pr-1 text-left">${step.instruction}</p>
</div>
</div>
<div class="flex flex-col gap-1">
<button id="move-up-btn" class="px-2 py-1 bg-white/10 hover:bg-white/30 transition-colors rounded text-xs" data-step-index="${step.index}" ${displayIndex === 0 ? 'disabled' : ''}>↑</button>
<button id="move-down-btn" class="px-2 py-1 bg-white/10 hover:bg-white/30 transition-colors rounded text-xs" data-step-index="${step.index}" ${displayIndex === steps.length - 1 ? 'disabled' : ''}>↓</button>
</div>
</div>
`
newStep.id = `step-${step.index}`
newStep.className = "bg-[#2a2b2c] rounded-lg mb-2 p-3"
stepList.appendChild(newStep)
})
// event listeners to reorder buttons
document.querySelectorAll('#move-up-btn').forEach(btn => {
btn.addEventListener('click', (e) => {
const stepIndex = parseInt((e.target as HTMLButtonElement).dataset.stepIndex!)
moveStep(stepIndex, -1)
})
})
document.querySelectorAll('#move-down-btn').forEach(btn => {
btn.addEventListener('click', (e) => {
const stepIndex = parseInt((e.target as HTMLButtonElement).dataset.stepIndex!)
moveStep(stepIndex, 1)
})
})
}
// - UTILS // - UTILS
function showAddIngredientButton() { function showAddIngredientButton() {
// only show if there is text in the field const hasQty = ingredientFields[0].value.trim().length > 0
const hasName = ingredientFields[2].value.trim().length > 0
if (ingredientFields[0].value && ingredientFields[2].value) {
ingredientAddButton.style.display = 'block' if (hasQty && hasName) {
ingredientAddButton.disabled = false
} else { } else {
ingredientAddButton.style.display = 'hidden' ingredientAddButton.disabled = true
} }
}
function showAddStepButton() {
if (stepInput.value.trim().length > 0) {
stepAddButton.disabled = false
} else {
stepAddButton.disabled = true
}
}
// shift: the direction to move. should be +1 to move down or -1 to move up the list
function moveStep(stepIndex: number, shift: number) {
// Find the step in the array
const currentStepArrayIndex = steps.findIndex(step => step.index === stepIndex)
if (currentStepArrayIndex === -1) return // step not found
const newIndex = currentStepArrayIndex + shift
// check bounds
if (newIndex < 0 || newIndex >= steps.length) return
// swap the steps in the array
const temp = steps[currentStepArrayIndex]
steps[currentStepArrayIndex] = steps[newIndex]
steps[newIndex] = temp
// re-render the steps
renderSteps()
} }