const noteContent = document.getElementById('note-content'); const formatPopup = document.getElementById('format-popup'); const paperDate = document.getElementById('paper-date'); const wordCountEl = document.getElementById('word-count'); const charCountEl = document.getElementById('char-count'); const lastSavedEl = document.getElementById('last-saved'); const syncStatus = document.getElementById('sync-status'); let autoSaveTimer = null; let settings = { autosave: true, calendar: 'default', tasklist: 'default' }; function init() { updateDate(); loadNote(); loadSettings(); setupEventListeners(); updateStats(); } function updateDate() { const now = new Date(); const options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' }; paperDate.textContent = now.toLocaleDateString(undefined, options); } function setupEventListeners() { document.addEventListener('selectionchange', handleSelectionChange); document.addEventListener('keydown', (e) => { if (e.key === 'Escape') { hideFormatPopup(); hideAllModals(); } if ((e.ctrlKey || e.metaKey) && e.key === 's') { e.preventDefault(); saveNote(); } if ((e.ctrlKey || e.metaKey) && e.key === 'b') { e.preventDefault(); formatText('bold'); } if ((e.ctrlKey || e.metaKey) && e.key === 'i') { e.preventDefault(); formatText('italic'); } if ((e.ctrlKey || e.metaKey) && e.key === 'u') { e.preventDefault(); formatText('underline'); } }); noteContent.addEventListener('blur', () => { setTimeout(hideFormatPopup, 200); }); } function handleNoteInput() { updateStats(); if (settings.autosave) { clearTimeout(autoSaveTimer); autoSaveTimer = setTimeout(saveNote, 2000); updateSyncStatus('syncing'); } } function handlePaste(e) { e.preventDefault(); const text = e.clipboardData.getData('text/plain'); document.execCommand('insertText', false, text); } function updateStats() { const text = noteContent.innerText || ''; const words = text.trim() ? text.trim().split(/\s+/).length : 0; const chars = text.length; wordCountEl.textContent = words; charCountEl.textContent = chars; } function handleSelectionChange() { const selection = window.getSelection(); if (!selection.rangeCount || selection.isCollapsed) { hideFormatPopup(); return; } const range = selection.getRangeAt(0); const selectedText = range.toString().trim(); if (!selectedText || !noteContent.contains(range.commonAncestorContainer)) { hideFormatPopup(); return; } showFormatPopup(range); } function showFormatPopup(range) { const rect = range.getBoundingClientRect(); const containerRect = noteContent.closest('.paper-container').getBoundingClientRect(); formatPopup.style.top = `${rect.top - containerRect.top - 50}px`; formatPopup.style.left = `${rect.left - containerRect.left + (rect.width / 2) - 100}px`; formatPopup.classList.remove('hidden'); } function hideFormatPopup() { formatPopup.classList.add('hidden'); } function formatText(command) { document.execCommand(command, false, null); noteContent.focus(); } function insertCheckbox() { const checkbox = document.createElement('div'); checkbox.className = 'checkbox-item'; checkbox.innerHTML = ` New task `; const selection = window.getSelection(); if (selection.rangeCount) { const range = selection.getRangeAt(0); range.deleteContents(); range.insertNode(checkbox); range.setStartAfter(checkbox); selection.removeAllRanges(); selection.addRange(range); } hideFormatPopup(); } function toggleCheckbox(checkbox) { const item = checkbox.closest('.checkbox-item'); if (checkbox.checked) { item.classList.add('checked'); } else { item.classList.remove('checked'); } handleNoteInput(); } function saveNote() { const content = noteContent.innerHTML; const timestamp = new Date().toISOString(); localStorage.setItem('paper-note', content); localStorage.setItem('paper-note-timestamp', timestamp); saveToHistory(content, timestamp); updateSyncStatus('synced'); updateLastSaved(); } function loadNote() { const content = localStorage.getItem('paper-note'); if (content) { noteContent.innerHTML = content; updateLastSaved(); } } function saveToHistory(content, timestamp) { const history = JSON.parse(localStorage.getItem('paper-history') || '[]'); const preview = noteContent.innerText.substring(0, 100); history.unshift({ content, timestamp, preview }); if (history.length > 50) { history.pop(); } localStorage.setItem('paper-history', JSON.stringify(history)); } function updateLastSaved() { const timestamp = localStorage.getItem('paper-note-timestamp'); if (timestamp) { const date = new Date(timestamp); const now = new Date(); const diff = now - date; let text; if (diff < 60000) { text = 'Saved just now'; } else if (diff < 3600000) { const mins = Math.floor(diff / 60000); text = `Saved ${mins} minute${mins > 1 ? 's' : ''} ago`; } else if (diff < 86400000) { const hours = Math.floor(diff / 3600000); text = `Saved ${hours} hour${hours > 1 ? 's' : ''} ago`; } else { text = `Saved ${date.toLocaleDateString()}`; } lastSavedEl.textContent = text; } } function updateSyncStatus(status) { syncStatus.className = 'sync-status'; const statusText = syncStatus.querySelector('.status-text'); if (status === 'syncing') { syncStatus.classList.add('syncing'); statusText.textContent = 'Saving...'; } else if (status === 'synced') { statusText.textContent = 'Synced'; } else if (status === 'error') { syncStatus.classList.add('error'); statusText.textContent = 'Error'; } } function clearNote() { if (noteContent.innerText.trim() && !confirm('Are you sure you want to clear all notes?')) { return; } noteContent.innerHTML = ''; updateStats(); saveNote(); } function exportNote() { const content = noteContent.innerText; const blob = new Blob([content], { type: 'text/plain' }); const url = URL.createObjectURL(blob); const a = document.createElement('a'); a.href = url; a.download = `paper-note-${new Date().toISOString().split('T')[0]}.txt`; a.click(); URL.revokeObjectURL(url); } function processNotes() { const content = noteContent.innerText.trim(); if (!content) { alert('Please write some notes first!'); return; } showModal('processing-modal'); const statusEl = document.getElementById('processing-status'); const statuses = [ 'Identifying actionable items...', 'Detecting tasks and deadlines...', 'Finding meeting requests...', 'Analyzing email drafts...', 'Preparing actions...' ]; let statusIndex = 0; const statusInterval = setInterval(() => { statusIndex = (statusIndex + 1) % statuses.length; statusEl.textContent = statuses[statusIndex]; }, 800); setTimeout(() => { clearInterval(statusInterval); hideModal('processing-modal'); showResults(analyzeContent(content)); }, 3000); } function analyzeContent(content) { const lines = content.split('\n').filter(line => line.trim()); const results = { tasks: [], events: [], emails: [], files: [] }; const taskPatterns = [ /(?:todo|task|need to|must|should|have to|don't forget to|remember to)\s*[:\-]?\s*(.+)/i, /\[\s*\]\s*(.+)/, /^[-•*]\s*(.+)/ ]; const eventPatterns = [ /(?:meeting|call|appointment|schedule|on)\s+(?:with\s+)?(.+?)(?:\s+(?:at|on)\s+(.+))?$/i, /(\d{1,2}[\/\-]\d{1,2}(?:[\/\-]\d{2,4})?)\s*[:\-]?\s*(.+)/ ]; const emailPatterns = [ /(?:email|send|write to|reply to|message)\s+(.+)/i ]; lines.forEach(line => { for (const pattern of taskPatterns) { const match = line.match(pattern); if (match) { results.tasks.push({ title: match[1] || line, original: line }); return; } } for (const pattern of eventPatterns) { const match = line.match(pattern); if (match) { results.events.push({ title: match[2] || match[1], time: match[2] ? match[1] : null, original: line }); return; } } for (const pattern of emailPatterns) { const match = line.match(pattern); if (match) { results.emails.push({ title: match[1], original: line }); return; } } }); if (results.tasks.length === 0 && results.events.length === 0 && results.emails.length === 0) { if (lines.length > 3) { results.files.push({ title: lines[0].substring(0, 50), type: 'document' }); } } return results; } function showResults(results) { const summaryEl = document.getElementById('results-summary'); const listEl = document.getElementById('results-list'); const total = results.tasks.length + results.events.length + results.emails.length + results.files.length; if (total === 0) { summaryEl.innerHTML = '