Spaces:
Running
Running
File size: 12,206 Bytes
2fff0e4 8f2894d 2fff0e4 8f2894d 2fff0e4 8f2894d 2fff0e4 8f2894d 24bdc07 8f2894d 2fff0e4 24bdc07 2fff0e4 8f2894d 2fff0e4 8f2894d 2fff0e4 8f2894d 2fff0e4 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 |
document.addEventListener('DOMContentLoaded', function() {
// Initialize variables
let entries = JSON.parse(localStorage.getItem('journalEntries')) || [];
const journalContainer = document.getElementById('journal-container');
const addEntryBtn = document.getElementById('add-entry-btn');
// Render existing entries
renderEntries();
// Add new entry
addEntryBtn.addEventListener('click', () => {
const newEntry = {
id: Date.now(),
date: new Date().toISOString().split('T')[0],
title: '',
blocks: [
{ id: Date.now() + 1, type: 'paragraph', content: '' }
]
};
entries.unshift(newEntry);
saveEntries();
renderEntries();
});
// Render all entries
function renderEntries() {
journalContainer.innerHTML = '';
if (entries.length === 0) {
journalContainer.innerHTML = `
<div class="text-center py-12">
<i data-feather="book-open" class="w-16 h-16 mx-auto text-gray-300 mb-4"></i>
<h3 class="text-xl font-medium text-gray-500 mb-2">No journal entries yet</h3>
<p class="text-gray-400">Create your first entry to get started</p>
</div>
`;
feather.replace();
return;
}
entries.forEach(entry => {
const entryElement = createEntryElement(entry);
journalContainer.appendChild(entryElement);
});
}
// Create entry element
function createEntryElement(entry) {
const entryDiv = document.createElement('div');
entryDiv.className = 'entry-card bg-white rounded-xl shadow-sm border border-gray-200 overflow-hidden';
entryDiv.innerHTML = `
<div class="p-6">
<div class="flex justify-between items-start mb-4">
<div>
<h2 class="text-xl font-semibold text-gray-800 mb-1">${entry.title || 'Untitled Entry'}</h2>
<div class="date-badge text-white text-sm px-3 py-1 rounded-full inline-block">
${formatDate(entry.date)}
</div>
</div>
<button class="delete-entry text-gray-400 hover:text-red-500" data-id="${entry.id}">
<i data-feather="trash-2" class="w-5 h-5"></i>
</button>
</div>
<div class="editor-container space-y-3" data-id="${entry.id}">
${entry.blocks.map(block => createBlockElement(block)).join('')}
</div>
<div class="mt-4 flex space-x-2">
<button class="add-block text-gray-500 hover:text-gray-700 flex items-center text-sm" data-id="${entry.id}">
<i data-feather="plus" class="w-4 h-4 mr-1"></i> Add block
</button>
</div>
</div>
`;
// Add event listeners
const deleteBtn = entryDiv.querySelector('.delete-entry');
deleteBtn.addEventListener('click', () => {
entries = entries.filter(e => e.id !== entry.id);
saveEntries();
renderEntries();
});
const addBlockBtn = entryDiv.querySelector('.add-block');
addBlockBtn.addEventListener('click', () => {
const entryId = parseInt(addBlockBtn.getAttribute('data-id'));
const entry = entries.find(e => e.id === entryId);
const newBlock = { id: Date.now(), type: 'paragraph', content: '' };
entry.blocks.push(newBlock);
saveEntries();
renderEntries();
});
// Add block event listeners
const editorContainer = entryDiv.querySelector('.editor-container');
editorContainer.querySelectorAll('[contenteditable]').forEach(element => {
element.addEventListener('input', handleContentChange);
element.addEventListener('keydown', handleKeyDown);
element.addEventListener('blur', handleBlur);
});
// Title input listener
const titleInput = entryDiv.querySelector('.entry-title');
if (titleInput) {
titleInput.addEventListener('input', handleTitleChange);
}
feather.replace();
return entryDiv;
}
// Create block element
function createBlockElement(block) {
switch (block.type) {
case 'heading':
return `
<div class="editor-block relative group pl-8 pr-4 py-2 rounded-lg hover:bg-gray-50" data-block-id="${block.id}">
<div class="absolute left-2 top-3 drag-handle text-gray-400 cursor-move group-hover:opacity-100">
<i data-feather="grip-vertical" class="w-4 h-4"></i>
</div>
<h3
class="text-2xl font-bold outline-none"
contenteditable="true"
data-type="heading"
data-block-id="${block.id}"
>${block.content}</h3>
<button class="delete-button absolute right-2 top-3 text-gray-400 hover:text-red-500">
<i data-feather="x" class="w-4 h-4"></i>
</button>
</div>
`;
case 'paragraph':
return `
<div class="editor-block relative group pl-8 pr-4 py-2 rounded-lg hover:bg-gray-50" data-block-id="${block.id}">
<div class="absolute left-2 top-3 drag-handle text-gray-400 cursor-move group-hover:opacity-100">
<i data-feather="grip-vertical" class="w-4 h-4"></i>
</div>
<p
class="outline-none text-gray-700 leading-relaxed"
contenteditable="true"
data-type="paragraph"
data-block-id="${block.id}"
data-placeholder="Type something..."
>${block.content}</p>
<button class="delete-button absolute right-2 top-3 text-gray-400 hover:text-red-500">
<i data-feather="x" class="w-4 h-4"></i>
</button>
</div>
`;
case 'divider':
return `
<div class="editor-block relative group py-4" data-block-id="${block.id}">
<div class="absolute left-2 top-5 drag-handle text-gray-400 cursor-move group-hover:opacity-100">
<i data-feather="grip-vertical" class="w-4 h-4"></i>
</div>
<hr class="border-gray-200">
<button class="delete-button absolute right-2 top-3 text-gray-400 hover:text-red-500">
<i data-feather="x" class="w-4 h-4"></i>
</button>
</div>
`;
default:
return '';
}
}
// Handle content change
function handleContentChange(e) {
const blockId = parseInt(e.target.getAttribute('data-block-id'));
const entryId = parseInt(e.target.closest('.editor-container').getAttribute('data-id'));
const content = e.target.textContent;
const entry = entries.find(e => e.id === entryId);
if (entry) {
const block = entry.blocks.find(b => b.id === blockId);
if (block) {
block.content = content;
saveEntries();
}
}
}
// Handle title change
function handleTitleChange(e) {
const entryId = parseInt(e.target.closest('.entry-card').querySelector('.editor-container').getAttribute('data-id'));
const title = e.target.textContent;
const entry = entries.find(e => e.id === entryId);
if (entry) {
entry.title = title;
saveEntries();
}
}
// Handle keydown events
function handleKeyDown(e) {
if (e.key === 'Enter') {
e.preventDefault();
insertBlockBelow(e.target);
} else if (e.key === 'Backspace' && e.target.textContent === '') {
e.preventDefault();
deleteBlock(e.target);
}
}
// Handle blur event
function handleBlur(e) {
if (e.target.textContent === '') {
const placeholder = e.target.getAttribute('data-placeholder');
if (placeholder) {
e.target.textContent = '';
}
}
}
// Insert block below current block
function insertBlockBelow(target) {
const blockId = parseInt(target.getAttribute('data-block-id'));
const entryId = parseInt(target.closest('.editor-container').getAttribute('data-id'));
const entry = entries.find(e => e.id === entryId);
if (entry) {
const blockIndex = entry.blocks.findIndex(b => b.id === blockId);
const newBlock = { id: Date.now(), type: 'paragraph', content: '' };
entry.blocks.splice(blockIndex + 1, 0, newBlock);
saveEntries();
renderEntries();
// Focus the new block after rendering
setTimeout(() => {
const newBlockElement = document.querySelector(`[data-block-id="${newBlock.id}"]`);
if (newBlockElement) {
const editableElement = newBlockElement.querySelector('[contenteditable]');
if (editableElement) {
editableElement.focus();
// Place cursor at the beginning
const range = document.createRange();
const sel = window.getSelection();
range.selectNodeContents(editableElement);
range.collapse(true);
sel.removeAllRanges();
sel.addRange(range);
}
}
}, 0);
}
}
// Delete block
function deleteBlock(target) {
const blockId = parseInt(target.getAttribute('data-block-id'));
const entryId = parseInt(target.closest('.editor-container').getAttribute('data-id'));
const entry = entries.find(e => e.id === entryId);
if (entry && entry.blocks.length > 1) {
const blockIndex = entry.blocks.findIndex(b => b.id === blockId);
entry.blocks = entry.blocks.filter(b => b.id !== blockId);
saveEntries();
renderEntries();
// Focus the previous block after rendering
setTimeout(() => {
if (blockIndex > 0) {
const prevBlock = entry.blocks[blockIndex - 1];
const prevBlockElement = document.querySelector(`[data-block-id="${prevBlock.id}"]`);
if (prevBlockElement) {
const range = document.createRange();
const sel = window.getSelection();
range.selectNodeContents(prevBlockElement);
range.collapse(false);
sel.removeAllRanges();
sel.addRange(range);
}
}
}, 0);
}
}
// Save entries to localStorage
function saveEntries() {
localStorage.setItem('journalEntries', JSON.stringify(entries));
}
// Format date
function formatDate(dateString) {
const options = { year: 'numeric', month: 'short', day: 'numeric' };
return new Date(dateString).toLocaleDateString(undefined, options);
}
}); |