| 1 | import Foundation |
| 2 | #if os(macOS) |
| 3 | import AppKit |
| 4 | #else |
| 5 | import UIKit |
| 6 | #endif |
| 7 | |
| 8 | /// Reconcile against optimistic native storage, not the last published snapshot. |
| 9 | /// Equal visible text can still carry a new CRDT node mapping after publication. |
| 10 | @MainActor enum DocumentRenderReconciliation { |
| 11 | static func apply(_ content: NSAttributedString, to storage: NSMutableAttributedString) { |
| 12 | apply(content, to: storage, in: NSRange(location: 0, length: storage.length)) |
| 13 | } |
| 14 | /// The same, for one stretch of the storage: `range` becomes `content`. |
| 15 | /// The text view's per-block drawing (DocumentSurface) reconciles only |
| 16 | /// the characters of the blocks that changed. |
| 17 | static func apply(_ content: NSAttributedString, to storage: NSMutableAttributedString, in range: NSRange) { |
| 18 | #if DOCS_PROBES |
| 19 | let probeStart = DocsProbes.now() |
| 20 | defer { DocsProbes.reconciles += 1; DocsProbes.reconcileMs += DocsProbes.ms(since: probeStart) } |
| 21 | #endif |
| 22 | let whole = range.location == 0 && range.length == storage.length |
| 23 | if whole { guard !storage.isEqual(to: content) else { return } } |
| 24 | else { guard !storage.attributedSubstring(from: range).isEqual(to: content) else { return } } |
| 25 | storage.beginEditing() |
| 26 | defer { storage.endEditing() } |
| 27 | // Only this stretch: `storage.string` would copy the whole text. |
| 28 | let before = storage.mutableString.substring(with: range) |
| 29 | if let (changed, replacement) = DocumentComposition.replacement(before: before, after: content.string) { |
| 30 | storage.replaceCharacters(in: NSRange(location: range.location + changed.location, length: changed.length), with: replacement) |
| 31 | } |
| 32 | content.enumerateAttributes(in: NSRange(location: 0, length: content.length)) { attributes, found, _ in |
| 33 | let target = NSRange(location: range.location + found.location, length: found.length) |
| 34 | var changed = false |
| 35 | storage.enumerateAttributes(in: target) { current, _, stop in |
| 36 | if !NSDictionary(dictionary: current).isEqual(to: attributes) { changed = true; stop.pointee = true } |
| 37 | } |
| 38 | if changed { storage.setAttributes(attributes, range: target) } |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | /// No production caller since S1a (the surface draws by block identity, |
| 43 | /// not by comparing renders): kept as the tests' oracle for "the storage |
| 44 | /// built block by block draws what a whole render draws". |
| 45 | /// |
| 46 | /// Whether two renders draw the same thing: the same text and attributes, |
| 47 | /// where a list compares by its marker and first number and an attachment |
| 48 | /// by its bounds and image size. Every render makes new `NSTextList`s and |
| 49 | /// `NSTextAttachment`s, and `isEqual` compares those by identity, so two |
| 50 | /// renders of one projection holding a list, an image or a rule never |
| 51 | /// compared equal; the editor then redrew the whole text on every update |
| 52 | /// (on iOS, without end: TestFlight 0.1.10 (12)). |
| 53 | /// |
| 54 | /// Only for deciding whether to draw. `apply` keeps its exact comparison: |
| 55 | /// a list's paragraphs share one `NSTextList`, which is what numbers them |
| 56 | /// as one list, so it must not keep some old lists beside new ones. |
| 57 | static func isEquivalent(_ a: NSAttributedString, _ b: NSAttributedString) -> Bool { |
| 58 | if a.isEqual(to: b) { return true } |
| 59 | guard a.length == b.length, a.string == b.string else { return false } |
| 60 | var equivalent = true |
| 61 | a.enumerateAttributes(in: NSRange(location: 0, length: a.length)) { attributes, range, stop in |
| 62 | var location = range.location |
| 63 | while location < NSMaxRange(range) { |
| 64 | var effective = NSRange() |
| 65 | let other = b.attributes(at: location, effectiveRange: &effective) |
| 66 | guard sameAttributes(attributes, other) else { equivalent = false; stop.pointee = true; return } |
| 67 | location = NSMaxRange(effective) |
| 68 | } |
| 69 | } |
| 70 | return equivalent |
| 71 | } |
| 72 | private static func sameAttributes(_ a: [NSAttributedString.Key: Any], _ b: [NSAttributedString.Key: Any]) -> Bool { |
| 73 | var plainA = a, plainB = b |
| 74 | for (key, value) in a { |
| 75 | if let x = value as? NSParagraphStyle, let y = b[key] as? NSParagraphStyle { |
| 76 | guard sameParagraphStyle(x, y) else { return false } |
| 77 | } else if let x = value as? NSTextAttachment, let y = b[key] as? NSTextAttachment { |
| 78 | guard sameAttachment(x, y) else { return false } |
| 79 | } else { continue } |
| 80 | plainA[key] = nil; plainB[key] = nil |
| 81 | } |
| 82 | return NSDictionary(dictionary: plainA).isEqual(to: plainB) |
| 83 | } |
| 84 | private static func sameParagraphStyle(_ a: NSParagraphStyle, _ b: NSParagraphStyle) -> Bool { |
| 85 | if a.isEqual(b) { return true } |
| 86 | guard a.textLists.count == b.textLists.count, |
| 87 | zip(a.textLists, b.textLists).allSatisfy({ $0.markerFormat == $1.markerFormat && $0.startingItemNumber == $1.startingItemNumber }), |
| 88 | let x = a.mutableCopy() as? NSMutableParagraphStyle, let y = b.mutableCopy() as? NSMutableParagraphStyle else { return false } |
| 89 | x.textLists = []; y.textLists = [] |
| 90 | return x.isEqual(y) |
| 91 | } |
| 92 | /// A rule's image is drawn afresh on each render, so images compare by |
| 93 | /// size, not identity. A document image is a placeholder symbol in 24 × |
| 94 | /// 24 bounds until the store loads it, so a load changes the bounds or |
| 95 | /// the image's size. |
| 96 | private static func sameAttachment(_ a: NSTextAttachment, _ b: NSTextAttachment) -> Bool { |
| 97 | a === b || (a.bounds == b.bounds && a.image?.size == b.image?.size && a.contents == b.contents && a.fileType == b.fileType) |
| 98 | } |
| 99 | } |
| 100 | |