`;
pdfContent.classList.remove('hidden');
try {
const canvas = await html2canvas(pdfContent, { scale: 2 });
const imgData = canvas.toDataURL('image/png');
const pdf = new jsPDF('p', 'pt', 'a4');
const imgProps = pdf.getImageProperties(imgData);
const pdfWidth = pdf.internal.pageSize.getWidth();
const pdfHeight = (imgProps.height * pdfWidth) / imgProps.width;
pdf.addImage(imgData, 'PNG', 0, 0, pdfWidth, pdfHeight);
const safeTitle = entry.title.replace(/[^a-z0-9]/gi, '_').toLowerCase();
pdf.save(`${safeTitle}.pdf`);
} catch (error) {
console.error("PDF Generation Error:", error);
} finally {
pdfContent.classList.add('hidden');
}
}
// --- EVENT LISTENERS ---
setPinBtn.addEventListener('click', () => {
const pin = pinSetupInput.value;
if (pin.length === 4 && /^\d{4}$/.test(pin)) {
localStorage.setItem(PIN_KEY, pin);
unlockApp();
} else {
pinSetupError.textContent = 'Please enter a valid 4-digit PIN.';
}
});
unlockBtn.addEventListener('click', () => {
const storedPin = localStorage.getItem(PIN_KEY);
if (pinEntryInput.value === storedPin) {
unlockApp();
} else {
pinEntryError.textContent = 'Incorrect PIN. Please try again.';
pinEntryInput.value = '';
}
});
// Allow Enter key to submit PIN
pinSetupInput.addEventListener('keyup', (e) => { if (e.key === 'Enter') setPinBtn.click(); });
pinEntryInput.addEventListener('keyup', (e) => { if (e.key === 'Enter') unlockBtn.click(); });
lockBtn.addEventListener('click', lockApp);
newEntryBtn.addEventListener('click', () => {
currentEntryId = 'new';
entryTitleEdit.value = '';
entryBodyEdit.value = '';
switchContentPanel('edit');
});
entryList.addEventListener('click', (e) => {
const item = e.target.closest('.entry-item');
if (item) {
viewEntry(parseInt(item.dataset.id, 10));
}
});
saveEntryBtn.addEventListener('click', saveEntry);
cancelEditBtn.addEventListener('click', () => {
if (currentEntryId === 'new' || !currentEntryId) {
switchContentPanel('welcome');
} else {
viewEntry(currentEntryId);
}
});
editEntryBtn.addEventListener('click', () => editEntry(currentEntryId));
deleteEntryBtn.addEventListener('click', deleteEntry);
downloadPdfBtn.addEventListener('click', downloadPDF);
// --- INITIALIZATION ---
function init() {
const storedPin = localStorage.getItem(PIN_KEY);
if (storedPin) {
switchView('lockScreen');
} else {
switchView('pinSetup');
}
}
init();
});
