refactor: use structured rich text for Notes editing

This commit is contained in:
2026-10-22 13:13:31 +02:00
parent 5803a00944
commit 751f22f6b1
20 changed files with 2155 additions and 2583 deletions
@@ -7,7 +7,7 @@ func markdownExportPreservesWholeTextAndSnapshotsBeforeWriting() throws {
let directory = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString)
defer { try? FileManager.default.removeItem(at: directory) }
try FileManager.default.createDirectory(at: directory, withIntermediateDirectories: false)
let text = "\n # 日本語 🦊 \r\n**bold** and `code`\n- [ ] task\n\u{fffc}\n"
let text = "\n日本語 🦊 \r\nplain and code\nordinary task\n\u{fffc}\n"
let source = NSMutableAttributedString(string: text)
source.addAttribute(.attachment, value: NSTextAttachment(), range: NSRange(location: 0, length: 1))
let export = try NoteMarkdownExport(contents: source)
@@ -32,13 +32,13 @@ func markdownExportWritesOriginalImageBytesAndEscapedLinksWithoutChangingSource(
wrapper.preferredFilename = name
let originalFilename = wrapper.filename
let attachment = NSTextAttachment(fileWrapper: wrapper)
let source = NSMutableAttributedString(string: "\n # 🦊\n")
let source = NSMutableAttributedString(attributedString: NoteDocument.importMarkdown(NSAttributedString(string: "\n# 🦊\n")))
source.append(NSAttributedString(attachment: attachment))
source.append(NSAttributedString(string: "\n- [ ] todo \n"))
source.append(NoteDocument.importMarkdown(NSAttributedString(string: "\n- [ ] todo \n")))
let before = NSAttributedString(attributedString: source)
let export = try NoteMarkdownExport(contents: source)
#expect(export.hasImages)
#expect(export.clipboardText == "\n # 🦊\n``<pixel [x] `1` #%.png>``\n- [ ] todo \n")
#expect(export.clipboardText == "\n# 🦊\n``<pixel [x] `1` #%.png>``\n- [ ] todo \n")
#expect(source.isEqual(to: before))
#expect(wrapper.preferredFilename == name)
#expect(wrapper.filename == originalFilename)
@@ -46,7 +46,7 @@ func markdownExportWritesOriginalImageBytesAndEscapedLinksWithoutChangingSource(
source.mutableString.setString("Changed")
wrapper.preferredFilename = "changed.png"
try export.write(to: directory)
let markdown = "\n # 🦊\n![pixel \\[x\\] \\`1\\` #%.png](pixel%20%5Bx%5D%20%601%60%20%23%25.png)\n- [ ] todo \n"
let markdown = "\n# 🦊\n![pixel \\[x\\] \\`1\\` #%.png](pixel%20%5Bx%5D%20%601%60%20%23%25.png)\n- [ ] todo \n"
#expect(try String(contentsOf: directory.appendingPathComponent("note.md"), encoding: .utf8) == markdown)
#expect(try Data(contentsOf: directory.appendingPathComponent(name)) == png)
#expect(Set(try FileManager.default.contentsOfDirectory(atPath: directory.path)) == ["note.md", name])