import Foundation /// Reconcile against optimistic native storage, not the last published snapshot. /// Equal visible text can still carry a new CRDT node mapping after publication. @MainActor enum DocumentRenderReconciliation { static func apply(_ content: NSAttributedString, to storage: NSMutableAttributedString) { guard !storage.isEqual(to: content) else { return } storage.beginEditing() defer { storage.endEditing() } if let (range, replacement) = DocumentComposition.replacement(before: storage.string, after: content.string) { storage.replaceCharacters(in: range, with: replacement) } content.enumerateAttributes(in: NSRange(location: 0, length: content.length)) { attributes, range, _ in var changed = false storage.enumerateAttributes(in: range) { current, _, stop in if !NSDictionary(dictionary: current).isEqual(to: attributes) { changed = true; stop.pointee = true } } if changed { storage.setAttributes(attributes, range: range) } } } }