From 76c35091186aa7d208eb17c54be94221f5e93fc6 Mon Sep 17 00:00:00 2001 From: fish Date: Sun, 12 Jul 2026 21:10:34 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=9D=E5=AD=98=E6=96=87=E4=BB=B6=E6=97=B6?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=E7=AC=AC=E4=B8=80=E8=A1=8C=E5=86=85=E5=AE=B9?= =?UTF-8?q?=E4=BD=9C=E4=B8=BA=E6=96=87=E4=BB=B6=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Note/FormatLogViewController.swift | 47 +++++++++++++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) diff --git a/Note/FormatLogViewController.swift b/Note/FormatLogViewController.swift index 9e6448e..adaed68 100644 --- a/Note/FormatLogViewController.swift +++ b/Note/FormatLogViewController.swift @@ -12,6 +12,7 @@ class FormatLogViewController: UIViewController { private var textView: UITextView! private var formatButton: UIButton! private var doneButton: UIBarButtonItem! + private var isFormatted = false override func viewDidLoad() { super.viewDidLoad() @@ -61,7 +62,40 @@ class FormatLogViewController: UIViewController { } @objc private func didTapFormatButton() { - textView.text = formatLog(textView.text) + if isFormatted { + saveToFiles() + } else { + textView.text = formatLog(textView.text) + isFormatted = true + formatButton.setTitle("保存到 iCloud", for: .normal) + } + } + + 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_log_\(Int(Date().timeIntervalSince1970)).md" + } + return cleanName + ".md" } private func formatLog(_ text: String) -> String { @@ -97,3 +131,14 @@ extension FormatLogViewController: UITextViewDelegate { navigationItem.rightBarButtonItem = nil } } + +extension FormatLogViewController: UIDocumentPickerDelegate { + + func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) { + // File saved to selected location + } + + func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) { + // User cancelled + } +}