diff --git a/Note/FormatNoteViewController.swift b/Note/FormatNoteViewController.swift index dfa2168..29509a0 100644 --- a/Note/FormatNoteViewController.swift +++ b/Note/FormatNoteViewController.swift @@ -16,6 +16,7 @@ class FormatNoteViewController: UIViewController { private var pasteButton: UIBarButtonItem! private var resetButton: UIBarButtonItem! private var rightBarButtonMode: RightBarButtonMode = .paste + private var isCloudFormatted = false private enum RightBarButtonMode { case paste @@ -104,6 +105,8 @@ class FormatNoteViewController: UIViewController { @objc private func didTapResetButton() { textView.text = "" rightBarButtonMode = .paste + isCloudFormatted = false + iCloudButton.setTitle("iCloud 格式化", for: .normal) updateRightBarButtonItem() } @@ -121,9 +124,15 @@ class FormatNoteViewController: UIViewController { } @objc private func didTapiCloudButton() { - textView.text = formatForCloud(textView.text) - rightBarButtonMode = .reset - updateRightBarButtonItem() + if isCloudFormatted { + saveToFiles() + } else { + textView.text = formatForCloud(textView.text) + isCloudFormatted = true + iCloudButton.setTitle("保存到 iCloud", for: .normal) + rightBarButtonMode = .reset + updateRightBarButtonItem() + } } @objc private func didTapNoteButton() { @@ -201,6 +210,33 @@ class FormatNoteViewController: UIViewController { return content } + private func saveToFiles() { + let fileName = generateFileName() + let fileURL = FileManager.default.temporaryDirectory.appendingPathComponent(fileName) + + do { + try textView.text.write(to: fileURL, atomically: true, encoding: .utf8) + let documentPicker = UIDocumentPickerViewController(forExporting: [fileURL], asCopy: true) + documentPicker.delegate = self + present(documentPicker, animated: true) + } catch { + print("Failed to write file: \(error)") + } + } + + private func generateFileName() -> String { + let text = textView.text ?? "" + let firstLine = text.components(separatedBy: .newlines).first ?? "" + let trimmed = firstLine.trimmingCharacters(in: .whitespaces) + let withoutHash = trimmed.replacingOccurrences(of: "^#\\s*", with: "", options: .regularExpression) + let cleanName = withoutHash.trimmingCharacters(in: .whitespaces) + + if cleanName.isEmpty { + return "formatted_note_\(Int(Date().timeIntervalSince1970)).md" + } + return cleanName + ".md" + } + private func removeEmptyLines(_ text: String) -> String { let lines = text.components(separatedBy: .newlines) let nonEmptyLines = lines.filter { !$0.trimmingCharacters(in: .whitespaces).isEmpty } @@ -216,6 +252,17 @@ class FormatNoteViewController: UIViewController { } } +extension FormatNoteViewController: UIDocumentPickerDelegate { + + func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) { + // File saved to selected location + } + + func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) { + // User cancelled + } +} + extension FormatNoteViewController: UITextViewDelegate { func textViewDidBeginEditing(_ textView: UITextView) {