use note title as default Save As filename

This commit is contained in:
jess 2026-04-06 02:40:08 -07:00
parent 358e30f613
commit 5ebf308104
1 changed files with 14 additions and 1 deletions

View File

@ -186,13 +186,26 @@ class AppDelegate: NSObject, NSApplicationDelegate {
@objc private func saveNoteAs() {
let panel = NSSavePanel()
panel.allowedContentTypes = [UTType(filenameExtension: "md")!]
panel.nameFieldStringValue = "note.md"
panel.nameFieldStringValue = defaultFilename()
panel.beginSheetModal(for: window) { [weak self] response in
guard response == .OK, let url = panel.url else { return }
self?.appState.saveNoteToFile(url)
}
}
private func defaultFilename() -> String {
let firstLine = appState.documentText
.components(separatedBy: "\n").first?
.trimmingCharacters(in: .whitespaces) ?? ""
let stripped = firstLine.replacingOccurrences(
of: "^#+\\s*", with: "", options: .regularExpression
)
let trimmed = stripped.trimmingCharacters(in: .whitespaces)
guard !trimmed.isEmpty, trimmed != "Untitled" else { return "note.md" }
let sanitized = trimmed.map { "/:\\\\".contains($0) ? "-" : String($0) }.joined()
return sanitized.prefix(80) + ".md"
}
@objc private func openSettings() {
SettingsWindowController.show()
}