| index 01fbed6..a6406f1 100644 |
| --- a/native/apple/Sources/DocsKit/DocumentSurface.swift |
| +++ b/native/apple/Sources/DocsKit/DocumentSurface.swift |
| @@ -77,6 +77,10 @@ typealias DocsStorageEditActions = NSTextStorage.EditActions |
| /// Its first paragraph's top margin when not first of the document. |
| var firstBefore: CGFloat? |
| var sources: Set<String> |
| + /// Its length as drawn. A block whose run in the storage is any |
| + /// other length has been edited there (or lost characters to an |
| + /// edit across blocks) and is never kept as it stands. |
| + var length: Int |
| } |
| private var entries: [String: Entry] = [:] |
| private var order: [String] = [] |
| @@ -195,6 +199,7 @@ typealias DocsStorageEditActions = NSTextStorage.EditActions |
| applying = true |
| if entries.isEmpty { drawWhole(newest.projection, into: text, storage: storage) } |
| else { drawChanged(newest.projection, storage: storage, textOnly: !advancing) } |
| + report(sources: sourceCounts.keys.sorted()) |
| let length = storage.length |
| let range = NSRange(location: min(selected.location, length), length: min(selected.length, max(0, length - selected.location))) |
| if selectedRange != range { Self.select(range, in: text) } |
| @@ -220,12 +225,17 @@ typealias DocsStorageEditActions = NSTextStorage.EditActions |
| #endif |
| #endif |
| index.rebuild(from: storage) |
| + rememberWhole(projection.children) |
| + } |
| + /// After a whole draw: every block as the storage now holds it. |
| + private func rememberWhole(_ nodes: [CRDTNode]) { |
| entries = [:]; sourceCounts = [:] |
| - let contexts = DocumentBlockSpacing.contexts(projection.children) |
| - for (node, context) in zip(projection.children, contexts) { remember(node, context: context) } |
| - order = projection.children.map(\.id) |
| + var lengths: [String: Int] = [:] |
| + for run in index.runs { if let id = run.id { lengths[id, default: 0] += run.length } } |
| + let contexts = DocumentBlockSpacing.contexts(nodes) |
| + for (node, context) in zip(nodes, contexts) { remember(node, context: context, length: lengths[node.id] ?? 0) } |
| + order = nodes.map(\.id) |
| pendingDirty = []; fullDiff = false |
| - report(sources: sourceCounts.keys.sorted()) |
| } |
| |
| /// Everything since: the blocks that changed (or whose neighbours |
| @@ -240,11 +250,7 @@ typealias DocsStorageEditActions = NSTextStorage.EditActions |
| // Two blocks under one id: no per-block identity to go by. |
| DocumentRenderReconciliation.apply(NativeTextProjection.render(projection, images: images), to: storage) |
| index.rebuild(from: storage) |
| - let contexts = DocumentBlockSpacing.contexts(nodes) |
| - entries = [:]; sourceCounts = [:] |
| - for (node, context) in zip(nodes, contexts) { remember(node, context: context) } |
| - order = ids; pendingDirty = []; fullDiff = false |
| - report(sources: sourceCounts.keys.sorted()) |
| + rememberWhole(nodes) |
| return |
| } |
| // What changed, by id. |
| @@ -282,7 +288,7 @@ typealias DocsStorageEditActions = NSTextStorage.EditActions |
| for (block, id) in ids.enumerated() where !draw.contains(id) { |
| guard let runs = positions[id], runs.count == 1 else { draw.insert(id); continue } |
| let run = index.runs[runs[0]] |
| - guard run.location >= lastEnd else { draw.insert(id); continue } |
| + guard run.location >= lastEnd, run.length == entries[id]?.length else { draw.insert(id); continue } |
| anchors.append((block, run)); lastEnd = run.end |
| } |
| // Gaps, last first, so earlier ranges stay where they are. |
| @@ -296,29 +302,31 @@ typealias DocsStorageEditActions = NSTextStorage.EditActions |
| var waiting = Set<String>() |
| for gap in gaps.reversed() { |
| let rendered = NSMutableAttributedString() |
| + var lengths: [Int] = [] |
| for block in gap.blocks { |
| - rendered.append(NativeTextProjection.renderBlock(nodes[block], context: contexts[block], images: images)) |
| + let drawn = NativeTextProjection.renderBlock(nodes[block], context: contexts[block], images: images) |
| + lengths.append(drawn.length) |
| + rendered.append(drawn) |
| } |
| if textOnly, (storage.string as NSString).substring(with: gap.range) != rendered.string { |
| for block in gap.blocks { waiting.insert(nodes[block].id) } |
| continue |
| } |
| DocumentRenderReconciliation.apply(rendered, to: storage, in: gap.range) |
| - for block in gap.blocks { remember(nodes[block], context: contexts[block]) } |
| + for (block, length) in zip(gap.blocks, lengths) { remember(nodes[block], context: contexts[block], length: length) } |
| } |
| for (block, _) in anchors { entries[nodes[block].id]?.context = contexts[block] } |
| let kept = Set(ids) |
| for id in order where !kept.contains(id) { forget(id) } |
| order = ids |
| pendingDirty = waiting; fullDiff = false |
| - report(sources: sourceCounts.keys.sorted()) |
| } |
| |
| - private func remember(_ node: CRDTNode, context: DocumentBlockSpacing.Context) { |
| + private func remember(_ node: CRDTNode, context: DocumentBlockSpacing.Context, length: Int) { |
| forget(node.id) |
| let sources = DocsImageStore.sources(in: node) |
| for source in sources { sourceCounts[source, default: 0] += 1 } |
| - entries[node.id] = Entry(node: node, context: context, firstBefore: DocumentBlockSpacing.firstBefore(node), sources: sources) |
| + entries[node.id] = Entry(node: node, context: context, firstBefore: DocumentBlockSpacing.firstBefore(node), sources: sources, length: length) |
| } |
| private func forget(_ id: String) { |
| guard let old = entries.removeValue(forKey: id) else { return } |
| @@ -341,9 +349,12 @@ typealias DocsStorageEditActions = NSTextStorage.EditActions |
| /// edit it refused (or one a dropped publication carried) would |
| /// otherwise stay on screen until something else touched the block. |
| private func typed(around edited: NSRange) { |
| - // The characters inserted, or the two either side of a deletion. |
| - let first = edited.length > 0 ? edited.location : max(0, edited.location - 1) |
| - let last = edited.length > 0 ? NSMaxRange(edited) - 1 : edited.location |
| + // From the character before the edit through the one after it: an |
| + // edit across blocks leaves the head of one and the tail of |
| + // another, and both must be drawn again even if the replica |
| + // changes neither (it refused the edit, or clamped it). |
| + let first = max(0, edited.location - 1) |
| + let last = NSMaxRange(edited) |
| let touched = index.runs(holding: first, through: last) |
| for run in touched { if let id = run.id { pendingDirty.insert(id) } } |
| // Text typed with no block (UIKit's typing attributes may not carry |