Your deck is empty. Add some cards to get started!
';
return;
}
deck.forEach((card, index) => {
const cardEl = document.createElement('div');
cardEl.className = 'p-4 bg-gray-50 rounded-lg border border-gray-200 flex justify-between items-center';
cardEl.innerHTML = `
${card.front}
${card.back}
`;
cardListEl.appendChild(cardEl);
});
}
// Add a new card
window.addCard = function() {
const frontEl = document.getElementById('card-front');
const backEl = document.getElementById('card-back');
if (frontEl.value.trim() === '' || backEl.value.trim() === '') {
alert('Please fill out both the front and back of the card.');
return;
}
const newCard = {
front: frontEl.value,
back: backEl.value,
level: 0, // Start at level 0
dueDate: new Date().toISOString() // Due immediately
};
deck.push(newCard);
saveDeck();
renderCardList();
frontEl.value = '';
backEl.value = '';
};
// Delete a card
window.deleteCard = function(index) {
if (confirm('Are you sure you want to delete this card?')) {
deck.splice(index, 1);
saveDeck();
renderCardList();
}
};
// --- Study Session Logic ---
function startStudySession() {
const today = new Date().setHours(0,0,0,0);
dueCards = deck.filter(card => new Date(card.dueDate).setHours(0,0,0,0) <= today)
.sort(() => Math.random() - 0.5); // Shuffle due cards
showNextCard();
}
function showNextCard() {
const flashcardArea = document.getElementById('flashcard-area');
const studyControls = document.getElementById('study-controls');
if (dueCards.length > 0) {
currentCard = dueCards.pop();
flashcardArea.innerHTML = `
${currentCard.front}
${currentCard.back}
`;
studyControls.style.display = 'block';
document.getElementById('flip-button').style.display = 'block';
document.getElementById('recall-buttons').classList.add('hidden');
} else {
flashcardArea.innerHTML = `
All Done!
You've reviewed all due cards for today. Come back tomorrow for more!
`;
studyControls.style.display = 'none';
}
}
window.flipCard = function() {
document.getElementById('current-flashcard').classList.add('is-flipped');
document.getElementById('flip-button').style.display = 'none';
document.getElementById('recall-buttons').classList.remove('hidden');
};
window.handleRecall = function(rating) {
// rating: 0=Hard, 1=Good, 2=Easy
if (!currentCard) return;
if (rating === 0) { // Hard
currentCard.level = 0;
} else {
currentCard.level++;
}
let intervalDays;
if (rating === 0) { // Hard, review again in 10 minutes (for simplicity, we'll make it due tomorrow)
intervalDays = 1;
} else if (rating === 1) { // Good
intervalDays = Math.pow(2, currentCard.level);
} else { // Easy
intervalDays = Math.pow(2, currentCard.level + 1);
}
const newDueDate = new Date();
newDueDate.setDate(newDueDate.getDate() + intervalDays);
currentCard.dueDate = newDueDate.toISOString();
saveDeck();
showNextCard();
};
// --- Tab Logic ---
window.changeTab = function(tabIndex) {
const tabs = document.querySelectorAll('.tab');
const tabContents = document.querySelectorAll('.tab-content');
tabs[currentTabIndex].classList.remove('active');
tabContents[currentTabIndex].classList.remove('active');
currentTabIndex = tabIndex;
tabs[currentTabIndex].classList.add('active');
tabContents[currentTabIndex].classList.add('active');
if (tabIndex === 0) { // Study Session
startStudySession();
} else { // Manage Deck
renderCardList();
}
};
// --- PDF Download ---
window.downloadPDF = function() {
if (deck.length === 0) {
alert("Your deck is empty. Add some cards before downloading.");
return;
}
const { jsPDF } = window.jspdf;
const doc = new jsPDF({ unit: 'pt', format: 'a4' });
let yPosition = 40;
const leftMargin = 40;
const usableWidth = doc.internal.pageSize.getWidth() - (2 * leftMargin);
doc.setFontSize(18);
doc.setFont('helvetica', 'bold');
doc.text("Flashcard Deck", doc.internal.pageSize.getWidth() / 2, yPosition, { align: 'center' });
yPosition += 40;
deck.forEach((card, index) => {
if (yPosition > doc.internal.pageSize.getHeight() - 100) {
doc.addPage();
yPosition = 40;
}
// Card Front
doc.setFontSize(12);
doc.setFont('helvetica', 'bold');
let frontText = doc.splitTextToSize(`Front: ${card.front}`, usableWidth);
doc.text(frontText, leftMargin, yPosition);
yPosition += frontText.length * 15;
// Card Back
doc.setFontSize(11);
doc.setFont('helvetica', 'normal');
let backText = doc.splitTextToSize(`Back: ${card.back}`, usableWidth);
doc.text(backText, leftMargin + 10, yPosition);
yPosition += backText.length * 14;
// Separator
if (index < deck.length - 1) {
yPosition += 10;
doc.setDrawColor(200);
doc.line(leftMargin, yPosition, leftMargin + usableWidth, yPosition);
yPosition += 20;
}
});
doc.save('memory_booster_deck.pdf');
};
// Initial Load
loadDeck();
startStudySession();
});