格式化笔记 iCloud 按钮支持二阶保存到系统文件
This commit is contained in:
@@ -16,6 +16,7 @@ class FormatNoteViewController: UIViewController {
|
|||||||
private var pasteButton: UIBarButtonItem!
|
private var pasteButton: UIBarButtonItem!
|
||||||
private var resetButton: UIBarButtonItem!
|
private var resetButton: UIBarButtonItem!
|
||||||
private var rightBarButtonMode: RightBarButtonMode = .paste
|
private var rightBarButtonMode: RightBarButtonMode = .paste
|
||||||
|
private var isCloudFormatted = false
|
||||||
|
|
||||||
private enum RightBarButtonMode {
|
private enum RightBarButtonMode {
|
||||||
case paste
|
case paste
|
||||||
@@ -104,6 +105,8 @@ class FormatNoteViewController: UIViewController {
|
|||||||
@objc private func didTapResetButton() {
|
@objc private func didTapResetButton() {
|
||||||
textView.text = ""
|
textView.text = ""
|
||||||
rightBarButtonMode = .paste
|
rightBarButtonMode = .paste
|
||||||
|
isCloudFormatted = false
|
||||||
|
iCloudButton.setTitle("iCloud 格式化", for: .normal)
|
||||||
updateRightBarButtonItem()
|
updateRightBarButtonItem()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -121,10 +124,16 @@ class FormatNoteViewController: UIViewController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@objc private func didTapiCloudButton() {
|
@objc private func didTapiCloudButton() {
|
||||||
|
if isCloudFormatted {
|
||||||
|
saveToFiles()
|
||||||
|
} else {
|
||||||
textView.text = formatForCloud(textView.text)
|
textView.text = formatForCloud(textView.text)
|
||||||
|
isCloudFormatted = true
|
||||||
|
iCloudButton.setTitle("保存到 iCloud", for: .normal)
|
||||||
rightBarButtonMode = .reset
|
rightBarButtonMode = .reset
|
||||||
updateRightBarButtonItem()
|
updateRightBarButtonItem()
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@objc private func didTapNoteButton() {
|
@objc private func didTapNoteButton() {
|
||||||
textView.text = formatForNote(textView.text)
|
textView.text = formatForNote(textView.text)
|
||||||
@@ -201,6 +210,33 @@ class FormatNoteViewController: UIViewController {
|
|||||||
return content
|
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 {
|
private func removeEmptyLines(_ text: String) -> String {
|
||||||
let lines = text.components(separatedBy: .newlines)
|
let lines = text.components(separatedBy: .newlines)
|
||||||
let nonEmptyLines = lines.filter { !$0.trimmingCharacters(in: .whitespaces).isEmpty }
|
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 {
|
extension FormatNoteViewController: UITextViewDelegate {
|
||||||
|
|
||||||
func textViewDidBeginEditing(_ textView: UITextView) {
|
func textViewDidBeginEditing(_ textView: UITextView) {
|
||||||
|
|||||||
Reference in New Issue
Block a user