Compare commits

..

2 Commits

+135 -3
View File
@@ -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,13 +124,142 @@ class FormatNoteViewController: UIViewController {
}
@objc private func didTapiCloudButton() {
if isCloudFormatted {
saveToFiles()
} else {
textView.text = formatForCloud(textView.text)
isCloudFormatted = true
iCloudButton.setTitle("保存到 iCloud", for: .normal)
rightBarButtonMode = .reset
updateRightBarButtonItem()
}
}
@objc private func didTapNoteButton() {
textView.text = formatForNote(textView.text)
rightBarButtonMode = .reset
updateRightBarButtonItem()
}
@objc private func didTapNoteButton() {
rightBarButtonMode = .reset
updateRightBarButtonItem()
private func formatForCloud(_ text: String) -> String {
var content = text
content = content.replacingOccurrences(of: " ", with: "")
content = content.replacingOccurrences(of: " ", with: "")
content = content.replacingOccurrences(of: "\u{00A0}", with: "")
content = content.replacingOccurrences(of: "# ", with: "")
content = content.replacingOccurrences(of: "#", with: "")
content = content.replacingOccurrences(of: "", with: "")
content = content.replacingOccurrences(of: "!", with: "")
content = content.replacingOccurrences(of: ";", with: "")
content = content.replacingOccurrences(of: "", with: "")
content = content.replacingOccurrences(of: ".", with: "")
content = content.replacingOccurrences(of: "", with: "(")
content = content.replacingOccurrences(of: "", with: ")")
content = content.replacingOccurrences(of: "?", with: "")
content = content.replacingOccurrences(of: ",", with: "")
content = content.replacingOccurrences(of: ":", with: "")
let numberReplacements: [(String, String)] = [
("20、", "二十、"), ("19、", "十九、"), ("18、", "十八、"), ("17、", "十七、"),
("16、", "十六、"), ("15、", "十五、"), ("14、", "十四、"), ("13、", "十三、"),
("12、", "十二、"), ("11、", "十一、"), ("10、", "十、"),
("9、", "九、"), ("8、", "八、"), ("7、", "七、"), ("6、", "六、"),
("5、", "五、"), ("4、", "四、"), ("3、", "三、"), ("2、", "二、"), ("1、", "一、")
]
for (old, new) in numberReplacements {
content = content.replacingOccurrences(of: old, with: new)
}
content = content.replacingOccurrences(of: "", with: "\n")
content = content.replacingOccurrences(of: "", with: "\n")
content = removeEmptyLines(content)
content = content.replacingOccurrences(of: "\\s*", with: "\n\n", options: .regularExpression)
content = content.replacingOccurrences(of: "", with: "\n")
content = content.replacingOccurrences(of: "", with: "\n")
content = content.replacingOccurrences(of: "\n\n", with: "")
content = content.replacingOccurrences(of: ":一、", with: "\n\n一、")
content = content.replacingOccurrences(of: " ", with: "")
content = content.replacingOccurrences(of: "---", with: "---\n")
content = content.replacingOccurrences(of: "日星期", with: "日 星期")
content = "# " + content
content = insertEmptyLineAfterFirstLine(content)
return content
}
private func formatForNote(_ text: String) -> String {
var content = text
content = content.replacingOccurrences(of: " ", with: "")
content = content.replacingOccurrences(of: "# ", with: "")
content = content.replacingOccurrences(of: "#", with: "")
content = content.replacingOccurrences(of: "", with: "")
content = content.replacingOccurrences(of: "!", with: "")
content = content.replacingOccurrences(of: ";", with: "")
content = content.replacingOccurrences(of: "", with: "")
content = content.replacingOccurrences(of: ".", with: "")
content = content.replacingOccurrences(of: "", with: "(")
content = content.replacingOccurrences(of: "", with: ")")
content = content.replacingOccurrences(of: "?", with: "")
content = content.replacingOccurrences(of: ",", with: "")
content = content.replacingOccurrences(of: ":", with: "")
content = content.replacingOccurrences(of: "", with: "\n")
content = content.replacingOccurrences(of: "", with: "\n")
content = removeEmptyLines(content)
content = content.replacingOccurrences(of: "---", with: "")
content = content.replacingOccurrences(of: "日星期", with: "日 星期")
content = insertEmptyLineAfterFirstLine(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 {
let lines = text.components(separatedBy: .newlines)
let nonEmptyLines = lines.filter { !$0.trimmingCharacters(in: .whitespaces).isEmpty }
return nonEmptyLines.joined(separator: "\n")
}
private func insertEmptyLineAfterFirstLine(_ text: String) -> String {
var lines = text.components(separatedBy: .newlines)
if !lines.isEmpty {
lines.insert("", at: 1)
}
return lines.joined(separator: "\n")
}
}
extension FormatNoteViewController: UIDocumentPickerDelegate {
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
// File saved to selected location
}
func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) {
// User cancelled
}
}