|
| 1 | +// Export functionality for NoteKeeper |
| 2 | +const exportEls = { |
| 3 | + exportButton: document.getElementById("export-button"), |
| 4 | + notes: document.getElementById("notes"), |
| 5 | + status: document.getElementById("save-status"), |
| 6 | +}; |
| 7 | + |
| 8 | +// Generate timestamped filename: YYYYMMDDThhmm-notekeeper.txt |
| 9 | +const generateFilename = () => { |
| 10 | + const now = new Date(); |
| 11 | + const year = now.getFullYear(); |
| 12 | + const month = String(now.getMonth() + 1).padStart(2, "0"); |
| 13 | + const day = String(now.getDate()).padStart(2, "0"); |
| 14 | + const hours = String(now.getHours()).padStart(2, "0"); |
| 15 | + const minutes = String(now.getMinutes()).padStart(2, "0"); |
| 16 | + |
| 17 | + return `${year}${month}${day}T${hours}${minutes}-notekeeper.txt`; |
| 18 | +}; |
| 19 | + |
| 20 | +// Export notes to local file |
| 21 | +const exportNotes = () => { |
| 22 | + const content = exportEls.notes.value; |
| 23 | + |
| 24 | + if (!content.trim()) { |
| 25 | + exportEls.status.textContent = "Nothing to export"; |
| 26 | + setTimeout(() => { |
| 27 | + exportEls.status.textContent = "Ready"; |
| 28 | + }, 2000); |
| 29 | + return; |
| 30 | + } |
| 31 | + |
| 32 | + try { |
| 33 | + const filename = generateFilename(); |
| 34 | + const blob = new Blob([content], { type: "text/plain;charset=utf-8" }); |
| 35 | + const url = URL.createObjectURL(blob); |
| 36 | + |
| 37 | + const a = document.createElement("a"); |
| 38 | + a.href = url; |
| 39 | + a.download = filename; |
| 40 | + document.body.appendChild(a); |
| 41 | + a.click(); |
| 42 | + document.body.removeChild(a); |
| 43 | + URL.revokeObjectURL(url); |
| 44 | + |
| 45 | + exportEls.status.textContent = "Exported"; |
| 46 | + setTimeout(() => { |
| 47 | + exportEls.status.textContent = "Ready"; |
| 48 | + }, 2000); |
| 49 | + } catch (error) { |
| 50 | + console.error("Export error:", error); |
| 51 | + exportEls.status.textContent = "Export failed"; |
| 52 | + setTimeout(() => { |
| 53 | + exportEls.status.textContent = "Ready"; |
| 54 | + }, 2000); |
| 55 | + } |
| 56 | +}; |
| 57 | + |
| 58 | +// Initialize export functionality |
| 59 | +if (exportEls.exportButton && exportEls.notes && exportEls.status) { |
| 60 | + exportEls.exportButton.addEventListener("click", exportNotes); |
| 61 | +} |
0 commit comments