Skip to content

Commit

Permalink
Reset containedEditor when attachment is removed from container (#274)
Browse files Browse the repository at this point in the history
  • Loading branch information
rajdeep authored Feb 9, 2024
1 parent 14c352f commit f04eb43
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
1 change: 1 addition & 0 deletions Proton/Sources/Swift/Attachment/Attachment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,7 @@ open class Attachment: NSTextAttachment, BoundsObserving {
@objc
func removeFromSuperview() {
view?.removeFromSuperview()
containerEditorView = nil
}

/// Causes invalidation of layout of the attachment when the containing view bounds are changed
Expand Down
22 changes: 22 additions & 0 deletions Proton/Tests/Editor/EditorViewTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -831,6 +831,28 @@ class EditorViewTests: XCTestCase {
_ = editor.attachmentsInRange(NSRange(location: 2, length: 1))
XCTAssertEqual(editor.attributedText.length, 2)
}

func testRemovesContainerEditorOnDeletingAttachment() {
let viewController = EditorTestViewController()
let editor = viewController.editor

let dummyAttachment1 = DummyMultiEditorAttachment(numberOfEditors: 1)
editor.insertAttachment(in: .zero, attachment: dummyAttachment1)

let dummyAttachment2 = DummyMultiEditorAttachment(numberOfEditors: 2)
editor.insertAttachment(in: editor.textEndRange, attachment: dummyAttachment2)

viewController.render()

XCTAssertEqual(dummyAttachment1.containerEditorView, editor)
XCTAssertEqual(dummyAttachment2.containerEditorView, editor)

editor.replaceCharacters(in: NSRange(location: 0, length: 1), with: NSAttributedString())
dummyAttachment2.removeFromContainer()

XCTAssertNil(dummyAttachment1.containerEditorView)
XCTAssertNil(dummyAttachment2.containerEditorView)
}
}

class DummyMultiEditorAttachment: Attachment {
Expand Down

0 comments on commit f04eb43

Please sign in to comment.