Jotley / app / macOS / Views / NoteDetailPlaceholderView+ClipboardExport.swift
| 1 | import SwiftUI |
| 2 | import AppKit |
| 3 | import UniformTypeIdentifiers |
| 4 | |
| 5 | extension NoteDetailPlaceholderView { |
| 6 | // Helper function to copy content based on current tab |
| 7 | func copyCurrentTabContent() { |
| 8 | let contentToCopy: String |
| 9 | switch DetailTab(rawValue: selectedTab) { |
| 10 | case .summary: contentToCopy = note.summary ?? "" |
| 11 | case .transcript: contentToCopy = editableTranscript |
| 12 | case .none: contentToCopy = "" |
| 13 | } |
| 14 | if !contentToCopy.isEmpty { |
| 15 | NSPasteboard.general.clearContents() |
| 16 | NSPasteboard.general.setString(contentToCopy, forType: .string) |
| 17 | } |
| 18 | } |
| 19 | |
| 20 | // MARK: - Export Functions |
| 21 | |
| 22 | func exportAsMarkdown() { |
| 23 | let content = generateMarkdownContent() |
| 24 | saveExportFile(content: content, extension: "md") |
| 25 | } |
| 26 | |
| 27 | func exportAsPlainText() { |
| 28 | let content = generatePlainTextContent() |
| 29 | saveExportFile(content: content, extension: "txt") |
| 30 | } |
| 31 | |
| 32 | func exportSummary() { |
| 33 | let content = generateSectionContent(section: "Summary", text: note.summary) |
| 34 | saveExportFile(content: content, extension: "md", filenameSuffix: " - Summary") |
| 35 | } |
| 36 | |
| 37 | /// Saves the summary in Jotley's structured layout as a PDF. |
| 38 | func exportSummaryPDF() { |
| 39 | let savePanel = NSSavePanel() |
| 40 | savePanel.allowedContentTypes = [.pdf] |
| 41 | savePanel.nameFieldStringValue = "\(safeFilename) - Summary.pdf" |
| 42 | savePanel.title = "Export Summary as PDF" |
| 43 | savePanel.canCreateDirectories = true |
| 44 | let title = note.title?.trimmingCharacters(in: .whitespacesAndNewlines) ?? "" |
| 45 | let date = note.timestamp, markdown = note.summary ?? "" |
| 46 | savePanel.begin { response in |
| 47 | guard response == .OK, let url = savePanel.url else { return } |
| 48 | do { try SummaryPDF.write(title: title.isEmpty ? "Untitled Note" : title, date: date, markdown: markdown, to: url) } |
| 49 | catch { |
| 50 | obsidianAlertTitle = "Export Failed" |
| 51 | obsidianAlertMessage = "The PDF could not be created: \(error.localizedDescription). Your note remains saved in Jotley." |
| 52 | showObsidianSaveAlert = true |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | func exportTranscript() { |
| 58 | let content = generateSectionContent(section: "Transcript", text: note.transcription) |
| 59 | saveExportFile(content: content, extension: "md", filenameSuffix: " - Transcript") |
| 60 | } |
| 61 | |
| 62 | private func generateSectionContent(section: String, text: String?) -> String { |
| 63 | var content = "" |
| 64 | |
| 65 | // Title |
| 66 | let title = note.title?.trimmingCharacters(in: .whitespacesAndNewlines) ?? "Untitled Note" |
| 67 | content += "# \(title) - \(section)\n\n" |
| 68 | |
| 69 | // Metadata |
| 70 | if let timestamp = note.timestamp { |
| 71 | let formatter = DateFormatter() |
| 72 | formatter.dateStyle = .long |
| 73 | formatter.timeStyle = .short |
| 74 | content += "*Exported: \(formatter.string(from: timestamp))*\n\n" |
| 75 | } |
| 76 | |
| 77 | // Section content |
| 78 | if let text = text?.trimmingCharacters(in: .whitespacesAndNewlines), !text.isEmpty { |
| 79 | content += "\(text)\n" |
| 80 | } else { |
| 81 | content += "*No \(section.lowercased()) available*\n" |
| 82 | } |
| 83 | |
| 84 | return content |
| 85 | } |
| 86 | |
| 87 | private var safeFilename: String { |
| 88 | let title = note.title?.trimmingCharacters(in: .whitespacesAndNewlines) ?? "Untitled" |
| 89 | // Remove characters that are invalid in filenames |
| 90 | let invalidChars = CharacterSet(charactersIn: "/\\?%*|\"<>:") |
| 91 | return title.components(separatedBy: invalidChars).joined(separator: "-") |
| 92 | } |
| 93 | |
| 94 | private func generateMarkdownContent() -> String { |
| 95 | var content = "" |
| 96 | |
| 97 | // Title |
| 98 | let title = note.title?.trimmingCharacters(in: .whitespacesAndNewlines) ?? "Untitled Note" |
| 99 | content += "# \(title)\n\n" |
| 100 | |
| 101 | // Metadata |
| 102 | if let timestamp = note.timestamp { |
| 103 | let formatter = DateFormatter() |
| 104 | formatter.dateStyle = .long |
| 105 | formatter.timeStyle = .short |
| 106 | content += "*Created: \(formatter.string(from: timestamp))*\n\n" |
| 107 | } |
| 108 | |
| 109 | // Tags |
| 110 | let tags = note.tagNames |
| 111 | if !tags.isEmpty { |
| 112 | content += "**Tags:** \(tags.map { "#\($0)" }.joined(separator: " "))\n\n" |
| 113 | } |
| 114 | |
| 115 | // Summary |
| 116 | if let summary = note.summary?.trimmingCharacters(in: .whitespacesAndNewlines), !summary.isEmpty { |
| 117 | content += "## Summary\n\n\(summary)\n\n" |
| 118 | } |
| 119 | |
| 120 | // Transcript |
| 121 | if let transcription = note.transcription?.trimmingCharacters(in: .whitespacesAndNewlines), !transcription.isEmpty { |
| 122 | content += "## Transcript\n\n\(transcription)\n" |
| 123 | } |
| 124 | |
| 125 | return content |
| 126 | } |
| 127 | |
| 128 | private func generatePlainTextContent() -> String { |
| 129 | var content = "" |
| 130 | |
| 131 | // Title |
| 132 | let title = note.title?.trimmingCharacters(in: .whitespacesAndNewlines) ?? "Untitled Note" |
| 133 | content += "\(title)\n" |
| 134 | content += String(repeating: "=", count: title.count) + "\n\n" |
| 135 | |
| 136 | // Metadata |
| 137 | if let timestamp = note.timestamp { |
| 138 | let formatter = DateFormatter() |
| 139 | formatter.dateStyle = .long |
| 140 | formatter.timeStyle = .short |
| 141 | content += "Created: \(formatter.string(from: timestamp))\n\n" |
| 142 | } |
| 143 | |
| 144 | // Tags |
| 145 | let tags = note.tagNames |
| 146 | if !tags.isEmpty { |
| 147 | content += "Tags: \(tags.joined(separator: ", "))\n\n" |
| 148 | } |
| 149 | |
| 150 | // Summary |
| 151 | if let summary = note.summary?.trimmingCharacters(in: .whitespacesAndNewlines), !summary.isEmpty { |
| 152 | content += "SUMMARY\n-------\n\(summary)\n\n" |
| 153 | } |
| 154 | |
| 155 | // Transcript |
| 156 | if let transcription = note.transcription?.trimmingCharacters(in: .whitespacesAndNewlines), !transcription.isEmpty { |
| 157 | content += "TRANSCRIPT\n----------\n\(transcription)\n" |
| 158 | } |
| 159 | |
| 160 | return content |
| 161 | } |
| 162 | |
| 163 | private func saveExportFile(content: String, extension fileExtension: String, filenameSuffix: String = "") { |
| 164 | let savePanel = NSSavePanel() |
| 165 | savePanel.allowedContentTypes = fileExtension == "md" ? [.init(filenameExtension: "md")!] : [.plainText] |
| 166 | savePanel.nameFieldStringValue = "\(safeFilename)\(filenameSuffix).\(fileExtension)" |
| 167 | savePanel.title = "Export Note" |
| 168 | savePanel.message = "Choose where to save your note" |
| 169 | savePanel.canCreateDirectories = true |
| 170 | |
| 171 | savePanel.begin { response in |
| 172 | if response == .OK, let url = savePanel.url { |
| 173 | do { |
| 174 | try content.write(to: url, atomically: true, encoding: .utf8) |
| 175 | } catch { |
| 176 | obsidianAlertTitle = "Export Failed" |
| 177 | obsidianAlertMessage = "The note could not be exported: \(error.localizedDescription). Your note remains saved in Jotley." |
| 178 | showObsidianSaveAlert = true |
| 179 | } |
| 180 | } |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | } |