feat: export notes as portable Markdown
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import AppKit
|
||||
import ConjureKit
|
||||
import Highlighter
|
||||
import UniformTypeIdentifiers
|
||||
|
||||
@MainActor
|
||||
enum NotesAppearance {
|
||||
@@ -77,6 +78,8 @@ final class NotesController: NSViewController, NSTextViewDelegate {
|
||||
actions.newNote = { [weak self] in self?.newNote() }
|
||||
actions.browseNotes = { [weak self] in self?.browseNotes() }
|
||||
actions.deleteNote = { [weak self] in self?.deleteNote() }
|
||||
actions.copyNoteMarkdown = { [weak self] in self?.copyAsMarkdown() }
|
||||
actions.exportNoteMarkdown = { [weak self] in self?.exportAsMarkdown() }
|
||||
let tools = [
|
||||
("textformat.size", "Heading"), ("bold", "Bold"), ("italic", "Italic"),
|
||||
("strikethrough", "Strikethrough"), ("chevron.left.forwardslash.chevron.right", "Inline code"),
|
||||
@@ -534,6 +537,51 @@ final class NotesController: NSViewController, NSTextViewDelegate {
|
||||
catch { showError(error) }
|
||||
}
|
||||
|
||||
private func copyAsMarkdown() {
|
||||
actions.close(animated: false)
|
||||
do {
|
||||
let export = try NoteMarkdownExport(contents: editor.markdownContents)
|
||||
let clipboard = NSPasteboard.general
|
||||
clipboard.clearContents()
|
||||
guard clipboard.setString(export.clipboardText, forType: .string) else {
|
||||
throw CocoaError(.fileWriteUnknown)
|
||||
}
|
||||
} catch { showExportError(error) }
|
||||
}
|
||||
|
||||
private func exportAsMarkdown() {
|
||||
guard let window = view.window, window.attachedSheet == nil else { return }
|
||||
actions.close(animated: false)
|
||||
do {
|
||||
let export = try NoteMarkdownExport(contents: editor.markdownContents)
|
||||
let panel = NSSavePanel()
|
||||
panel.title = "Export as Markdown"
|
||||
panel.prompt = "Export"
|
||||
panel.canCreateDirectories = true
|
||||
panel.nameFieldStringValue = export.hasImages ? "Note" : "Note.md"
|
||||
if export.hasImages {
|
||||
panel.message = "Create a new folder containing note.md and its images. Choose a folder name that does not already exist."
|
||||
} else {
|
||||
panel.allowedContentTypes = [UTType(filenameExtension: "md") ?? .plainText]
|
||||
}
|
||||
panel.beginSheetModal(for: window) { [weak self] response in
|
||||
guard let self else { return }
|
||||
defer { self.focusEditor() }
|
||||
guard response == .OK, let url = panel.url else { return }
|
||||
do { try export.write(to: url) }
|
||||
catch { self.showExportError(error) }
|
||||
}
|
||||
} catch { showExportError(error) }
|
||||
}
|
||||
|
||||
private func showExportError(_ error: Error) {
|
||||
status.isHidden = false
|
||||
status.stringValue = "Couldn’t export note: \(error.localizedDescription)"
|
||||
status.toolTip = status.stringValue
|
||||
status.textColor = .systemRed
|
||||
}
|
||||
|
||||
|
||||
private func deleteNote() {
|
||||
guard let window = view.window, window.attachedSheet == nil, let id = selectedID else { return }
|
||||
actions.close(animated: false)
|
||||
|
||||
Reference in New Issue
Block a user