Download this file
| 1 | import Foundation |
| 2 | |
| 3 | /// Reconcile against optimistic native storage, not the last published snapshot. |
| 4 | /// Equal visible text can still carry a new CRDT node mapping after publication. |
| 5 | @MainActor enum DocumentRenderReconciliation { |
| 6 | static func apply(_ content: NSAttributedString, to storage: NSMutableAttributedString) { |
| 7 | guard !storage.isEqual(to: content) else { return } |
| 8 | storage.beginEditing() |
| 9 | defer { storage.endEditing() } |
| 10 | if let (range, replacement) = DocumentComposition.replacement(before: storage.string, after: content.string) { |
| 11 | storage.replaceCharacters(in: range, with: replacement) |
| 12 | } |
| 13 | content.enumerateAttributes(in: NSRange(location: 0, length: content.length)) { attributes, range, _ in |
| 14 | var changed = false |
| 15 | storage.enumerateAttributes(in: range) { current, _, stop in |
| 16 | if !NSDictionary(dictionary: current).isEqual(to: attributes) { changed = true; stop.pointee = true } |
| 17 | } |
| 18 | if changed { storage.setAttributes(attributes, range: range) } |
| 19 | } |
| 20 | } |
| 21 | } |
| 22 | |