Create New Journal Entry
View & Generate Journal
No journal entries found for the selected period.
${pwjEscapeHtml(entry.accomplishments)}
Challenges & Solutions:
${pwjEscapeHtml(entry.challenges)}
`; if(entry.learnings) html += `Learnings & Insights:
${pwjEscapeHtml(entry.learnings)}
`; if(entry.notes) html += `General Notes:
${pwjEscapeHtml(entry.notes)}
`; html += ``;
html += ``;
html += ``;
html += `
`;
entryDiv.innerHTML = html;
pwjJournalEntriesArea.appendChild(entryDiv);
});
}
function pwjEscapeHtml(unsafe) {
if (typeof unsafe !== 'string') return '';
return unsafe
.replace(/&/g, "&")
.replace(//g, ">")
.replace(/"/g, """)
.replace(/'/g, "'");
}
function pwjEditEntry(id) {
const entry = pwjJournalEntries.find(item => item.id === id);
if (entry) {
pwjEntryIdEl.value = entry.id;
pwjEntryDateEl.value = entry.date;
pwjEntryTitleEl.value = entry.title;
pwjAccomplishmentsEl.value = entry.accomplishments;
pwjChallengesEl.value = entry.challenges;
pwjLearningsEl.value = entry.learnings;
pwjMoodEl.value = entry.mood;
pwjNotesEl.value = entry.notes;
if (pwjTabButtons && pwjTabButtons[0]) {
pwjTabButtons[0].click();
}
window.scrollTo(0,0);
}
}
function pwjDeleteEntry(id) {
if (confirm('Are you sure you want to delete this journal entry?')) {
pwjJournalEntries = pwjJournalEntries.filter(item => item.id !== id);
pwjSaveEntries();
pwjLoadJournalEntries();
}
}
// PDF Download
function pwjDownloadPDF() {
if (!pwjFilterStartDateEl || !pwjFilterEndDateEl || !pwjJournalEntries) {
alert("Initialization error. Please refresh.");
return;
}
const startDate = pwjFilterStartDateEl.value;
const endDate = pwjFilterEndDateEl.value;
const userName = pwjJournalUserNameEl ? pwjJournalUserNameEl.value.trim() : "";
if (!startDate || !endDate) {
alert("Please select a date range to generate the journal.");
return;
}
const entriesToExport = pwjJournalEntries.filter(entry => {
return entry.date >= startDate && entry.date <= endDate;
}).sort((a, b) => new Date(a.date) - new Date(b.date));
if (entriesToExport.length === 0) {
alert("No entries found for the selected period to export.");
return;
}
const { jsPDF } = window.jspdf;
const doc = new jsPDF('p', 'pt', 'a4');
const primaryColorPDF = getComputedStyle(document.documentElement).getPropertyValue('--pwj-primary-color').trim();
const accentColorPDF = getComputedStyle(document.documentElement).getPropertyValue('--pwj-accent-color').trim();
const textColorPDF = getComputedStyle(document.documentElement).getPropertyValue('--pwj-text-color-dark').trim();
let yPos = 40;
const leftMargin = 40;
const contentWidth = doc.internal.pageSize.getWidth() - (2 * leftMargin);
const lineSpacing = 15;
doc.setFontSize(20);
doc.setTextColor(primaryColorPDF);
doc.text("Personal Work Journal", doc.internal.pageSize.getWidth() / 2, yPos, { align: 'center' });
yPos += 25;
if (userName) {
doc.setFontSize(12);
doc.setTextColor(textColorPDF);
doc.text(`Journal for: ${pwjEscapeHtml(userName)}`, doc.internal.pageSize.getWidth() / 2, yPos, { align: 'center' });
yPos += 20;
}
doc.setFontSize(10);
doc.setTextColor(textColorPDF);
doc.text(`Period: ${new Date(startDate+'T00:00:00').toLocaleDateString()} - ${new Date(endDate+'T00:00:00').toLocaleDateString()}`, doc.internal.pageSize.getWidth() / 2, yPos, { align: 'center' });
yPos += 30;
function checkPageBreak(currentY, spaceNeeded = lineSpacing) {
if (currentY + spaceNeeded > doc.internal.pageSize.getHeight() - 40) {
doc.addPage();
return 40;
}
return currentY;
}
function addSectionToPDF(title, textContent, titleSize = 11, textSize = 10, titleColor = accentColorPDF, textColor = textColorPDF) {
if (!textContent || textContent.trim() === "") return;
yPos = checkPageBreak(yPos, titleSize + 5);
doc.setFontSize(titleSize);
doc.setFont(undefined, 'bold');
doc.setTextColor(titleColor);
doc.text(title, leftMargin, yPos);
yPos += titleSize + 2;
doc.setFontSize(textSize);
doc.setFont(undefined, 'normal');
doc.setTextColor(textColor);
const textLines = doc.splitTextToSize(textContent, contentWidth);
for (let i = 0; i < textLines.length; i++) {
yPos = checkPageBreak(yPos, lineSpacing * 0.8);
doc.text(textLines[i], leftMargin, yPos);
yPos += lineSpacing * 0.8;
}
yPos += lineSpacing * 0.5;
}
entriesToExport.forEach((entry, index) => {
if (index > 0) {
yPos = checkPageBreak(yPos, 20);
doc.setDrawColor(220, 208, 255); // Hex --pwj-border-color to RGB approx
doc.line(leftMargin, yPos, leftMargin + contentWidth, yPos);
yPos += 20;
}
yPos = checkPageBreak(yPos, 25);
doc.setFontSize(14);
doc.setFont(undefined, 'bold');
doc.setTextColor(primaryColorPDF);
doc.text(pwjEscapeHtml(entry.title) || `Entry for ${new Date(entry.date+'T00:00:00').toLocaleDateString()}`, leftMargin, yPos);
yPos += 18;
doc.setFontSize(9);
doc.setFont(undefined, 'italic');
doc.setTextColor(textColorPDF);
let metaText = `Date: ${new Date(entry.date+'T00:00:00').toLocaleDateString()}`;
if (entry.mood) metaText += ` | Mood: ${pwjEscapeHtml(entry.mood)}`;
doc.text(metaText, leftMargin, yPos);
yPos += 20;
addSectionToPDF("Accomplishments:", entry.accomplishments);
addSectionToPDF("Challenges & Solutions:", entry.challenges);
addSectionToPDF("Learnings & Insights:", entry.learnings);
addSectionToPDF("General Notes/Reflections:", entry.notes);
});
doc.save(`PersonalWorkJournal_${userName ? userName.replace(/\s+/g, '_') + '_' : ''}${startDate}_to_${endDate}.pdf`);
}
