SamNet • Notepad
Words: 0, Characters: 0
Saved
Back to Tools ";
const sourceHTML = header + content + " "; const source = 'data:application/vnd.ms-word;charset=utf-8,' + encodeURIComponent(sourceHTML); const link = document.createElement("a"); link.href = source; link.download = `${note.title}.doc`; link.click(); }; // FIXED: Rewrote the PDF export function to use html2canvas for reliability const exportToPdf = () => { const note = getActiveNote(); if (!note) return; const content = note.isMarkdown ? markdownPreview : editor; // Use html2canvas to capture the content as an image html2canvas(content, { scale: 2, // Improve resolution // Set background color for the canvas to match the editor theme backgroundColor: content.classList.contains('yellow-bg') ? '#fffbe9' : (document.documentElement.getAttribute('data-theme') === 'dark' ? '#0b1020' : '#ffffff') }).then(canvas => { const imgData = canvas.toDataURL('image/png'); const pdf = new jsPDF({ orientation: 'p', unit: 'px', // Set PDF dimensions to match the captured image format: [canvas.width, canvas.height] }); pdf.addImage(imgData, 'PNG', 0, 0, canvas.width, canvas.height); pdf.save(`${note.title}.pdf`); }).catch(err => { console.error("Error exporting to PDF:", err); // Use a simple alert for error feedback, as custom modals are complex alert("Sorry, there was an error exporting to PDF."); }); }; // NEW: Function to export the current note as a .txt file const exportToTxt = () => { const note = getActiveNote(); if (!note) return; // Get plain text content from either editor const textContent = note.isMarkdown ? markdownEditor.value : editor.innerText; // Create a blob with the text content const blob = new Blob([textContent], { type: 'text/plain;charset=utf-8' }); // Create a link to trigger the download const link = document.createElement("a"); link.href = URL.createObjectURL(blob); link.download = `${note.title}.txt`; link.click(); // Clean up the created URL URL.revokeObjectURL(link.href); }; const importFromWord = (event) => { const file = event.target.files[0]; if (!file) return; const reader = new FileReader(); reader.onload = (e) => { mammoth.convertToHtml({ arrayBuffer: e.target.result }) .then(result => { const note = getActiveNote(); if (note) { note.content = result.value; note.isMarkdown = false; loadNoteContent(); saveState(); } }).catch(err => alert("Could not import .docx file.")); }; reader.readAsArrayBuffer(file); }; toolbar.addEventListener('click', (e) => { const button = e.target.closest('button'); if (!button || !button.dataset.command) return; const { command, value } = button.dataset; if (command === 'createLink') { const url = prompt('Enter URL:', 'https://'); if (url) document.execCommand(command, false, url); } else { document.execCommand(command, false, value || null); } editor.focus(); }); fontSelector.addEventListener('change', (e) => { document.execCommand('fontName', false, e.target.value); editor.focus(); }); bgToggleButton.addEventListener('click', toggleBackground); exportButton.addEventListener('click', exportToWord); exportPdfButton.addEventListener('click', exportToPdf); // NEW: Added event listener for the new TXT export button exportTxtButton.addEventListener('click', exportToTxt); importButton.addEventListener('click', () => fileInput.click()); fileInput.addEventListener('change', importFromWord); // --- Help Modal --- helpButton.addEventListener('click', () => helpModal.classList.add('visible')); helpModal.addEventListener('click', (e) => { if (e.target === helpModal || e.target.closest('.close-modal')) { helpModal.classList.remove('visible'); } }); // --- Theme Toggle --- (function(){ const root = document.documentElement; const btn = document.getElementById('themeToggle'); const iconSun = document.getElementById('iconSun'); const iconMoon = document.getElementById('iconMoon'); function setTheme(mode){ root.setAttribute('data-theme', mode); localStorage.setItem('samnet-theme', mode); iconSun.classList.toggle('hidden', mode === 'dark'); iconMoon.classList.toggle('hidden', mode !== 'dark'); } const savedTheme = localStorage.getItem('samnet-theme') || 'dark'; setTheme(savedTheme); btn.addEventListener('click', () => setTheme(root.getAttribute('data-theme') === 'dark' ? 'light' : 'dark')); })(); // --- Find and Replace --- findBtn.addEventListener('click', () => { findReplacePanel.style.display = findReplacePanel.style.display === 'flex' ? 'none' : 'flex'; }); replaceBtn.addEventListener('click', () => { const findText = findInput.value; const replaceText = replaceInput.value; if (findText) { const content = editor.innerHTML; const newContent = content.replace(findText, replaceText); if (content !== newContent) { editor.innerHTML = newContent; updateNoteContent(); } } }); replaceAllBtn.addEventListener('click', () => { const findText = findInput.value; const replaceText = replaceInput.value; if (findText) { const content = editor.innerHTML; const newContent = content.replace(new RegExp(findText, 'g'), replaceText); if (content !== newContent) { editor.innerHTML = newContent; updateNoteContent(); } } }); // --- To-Do List --- todoBtn.addEventListener('click', () => { document.execCommand('insertHTML', false, '
  • '); }); editor.addEventListener('click', (e) => { if (e.target.matches('input[type="checkbox"]')) { e.target.parentElement.classList.toggle('checked'); updateNoteContent(); } }); // --- Syntax Highlighting --- highlightCodeBtn.addEventListener('click', () => { const selection = window.getSelection(); if (!selection.rangeCount) return; const range = selection.getRangeAt(0); const selectedText = range.toString(); if (selectedText) { const pre = document.createElement('pre'); const code = document.createElement('code'); code.textContent = selectedText; hljs.highlightElement(code); pre.appendChild(code); range.deleteContents(); range.insertNode(pre); updateNoteContent(); } }); // --- Tooltip Listeners --- document.addEventListener('mouseover', showTooltip); document.addEventListener('mouseout', hideTooltip); tooltipToggle.addEventListener('change', (e) => { tooltipsEnabled = e.target.checked; localStorage.setItem('samnet_tooltips_enabled', tooltipsEnabled); }); // Initial Load loadState(); });