Docs native S1a: a second text view on the same surface is drawn whole (#170, 2)
Fable's finding 2. When the text view's host is rebuilt without the model opening again (an iPad split collapsing, S2a's window structure), the new text view was attached to a surface that still remembered the old storage's blocks, nothing was waiting, and it stayed empty. Attaching a different view now lets the old one go (delegates and callbacks) and draws the new one whole from the newest publication. reset() keeps the attached view on purpose: a new draft or a discard keeps the same host, which is not rebuilt to attach again; its entries are cleared, so the next publication is drawn whole. SurfaceLifecycleTests.testASecondTextViewIsDrawnWhole failed before (the second view stayed empty) and passes now, including a later publication.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
3 files changed,
+56
−0
native/apple/Sources/DocsKit/DocumentSurface.swift+24 −0native/apple/Tests/DocsKitTests/SurfaceLifecycleTests.swift+31 −0native/ios/project.yml+1 −0
diff --git a/native/apple/Sources/DocsKit/DocumentSurface.swift b/native/apple/Sources/DocsKit/DocumentSurface.swift| index a6406f1..8fec62d 100644 |
| --- a/native/apple/Sources/DocsKit/DocumentSurface.swift |
| +++ b/native/apple/Sources/DocsKit/DocumentSurface.swift |
| @@ -120,6 +120,9 @@ typealias DocsStorageEditActions = NSTextStorage.EditActions |
| /// Forget the document drawn: a new one (or a new replica) follows, |
| /// whose revisions start again. The next publication is drawn whole. |
| + /// The text view stays attached: a new draft (iPhone) or a discard |
| + /// keeps the same host, which is not rebuilt to attach it again; a |
| + /// rebuilt host's new view is drawn whole by `attach`. |
| func reset() { |
| appliedRevision = 0; generation = 0; newest = nil |
| pendingDirty = []; fullDiff = false |
| @@ -131,6 +134,15 @@ typealias DocsStorageEditActions = NSTextStorage.EditActions |
| // MARK: - The text view |
| func attach(_ text: DocsTextView) { |
| + if let old = textView, old !== text { |
| + // Another text view, without the model opening again (the host |
| + // rebuilt: an iPad split collapsing, a window restructured). |
| + // Let the old one go, and draw the new one whole: what the |
| + // surface remembers is the old storage's, not this one's. |
| + Self.release(old) |
| + entries = [:]; order = []; sourceCounts = [:] |
| + pendingDirty = []; fullDiff = newest != nil |
| + } |
| textView = text |
| text.delegate = self |
| storage?.delegate = self |
| @@ -150,6 +162,18 @@ typealias DocsStorageEditActions = NSTextStorage.EditActions |
| flushIfDeferred(notifyLater: true) |
| } |
| + private static func release(_ text: DocsTextView) { |
| + if text.delegate === nil { return } |
| + text.delegate = nil |
| + #if os(macOS) |
| + text.textStorage?.delegate = nil |
| + #else |
| + text.textStorage.delegate = nil |
| + #endif |
| + text.ruleCommand = nil; text.clipboardEdit = nil; text.compositionChanged = nil |
| + text.compositionCommitted = nil; text.action = nil; text.canAction = nil |
| + } |
| + |
| /// An explicit command (not typing) went to the model. |
| func explicitCommand() { textView?.lastInputRule = nil } |
diff --git a/native/apple/Tests/DocsKitTests/SurfaceLifecycleTests.swift b/native/apple/Tests/DocsKitTests/SurfaceLifecycleTests.swift| new file mode 100644 |
| index 0000000..9a6db75 |
| --- /dev/null |
| +++ b/native/apple/Tests/DocsKitTests/SurfaceLifecycleTests.swift |
| @@ -0,0 +1,31 @@ |
| +import Foundation |
| +import XCTest |
| +@testable import DocsKit |
| + |
| +/// The surface across text views and model updates (Fable on #170, |
| +/// findings 2 and 3). |
| +@MainActor final class SurfaceLifecycleTests: XCTestCase { |
| + private func projection() -> CRDTProjection { |
| + SurfaceFixture.projection([SurfaceFixture.heading("h", "Title"), SurfaceFixture.paragraph("p", "Body text."), |
| + SurfaceFixture.list("l", "bullet_list", ["one", "two"])]) |
| + } |
| + |
| + /// The host is rebuilt without the model opening again (an iPad |
| + /// split collapsing, a restructured window): the new text view shows |
| + /// the document at once, with no further publication, and the old one |
| + /// no longer edits through the surface. |
| + func testASecondTextViewIsDrawnWhole() throws { |
| + let state = SurfaceState() |
| + state.publish(projection(), generation: 1, dirty: nil) |
| + let second = DocsTextView(frame: CGRect(x: 0, y: 0, width: 500, height: 700)) |
| + state.surface.attach(second) |
| + XCTAssertTrue(state.surface.textView === second) |
| + SurfaceFixture.assertMatchesWholeRender(Keyboard.storage(of: second), projection(), "the second text view") |
| + XCTAssertNil(state.text.delegate, "the first text view is let go") |
| + // And it keeps drawing: a later publication reaches the second view. |
| + var changed = projection() |
| + changed.children[1] = SurfaceFixture.paragraph("p", "Body text, changed.") |
| + state.publish(changed, generation: 1, dirty: ["p"]) |
| + SurfaceFixture.assertMatchesWholeRender(Keyboard.storage(of: second), changed, "a publication after the second attach") |
| + } |
| +} |
diff --git a/native/ios/project.yml b/native/ios/project.yml| index b14acc2..c3cb173 100644 |
| --- a/native/ios/project.yml |
| +++ b/native/ios/project.yml |
| @@ -141,6 +141,7 @@ targets: |
| - path: ../apple/Tests/DocsKitTests/BlockRenderEquivalenceTests.swift |
| - path: ../apple/Tests/DocsKitTests/RenderIsolationTests.swift |
| - path: ../apple/Tests/DocsKitTests/SurfaceTimingTests.swift |
| + - path: ../apple/Tests/DocsKitTests/SurfaceLifecycleTests.swift |
| dependencies: |
| - package: DocsNative |
| product: DocsKit |