diff --git a/Proton/Sources/Swift/Core/RichTextView.swift b/Proton/Sources/Swift/Core/RichTextView.swift index fe1e08a1..caebb5b0 100644 --- a/Proton/Sources/Swift/Core/RichTextView.swift +++ b/Proton/Sources/Swift/Core/RichTextView.swift @@ -695,7 +695,9 @@ class RichTextView: AutogrowingTextView { } func didTap(at location: CGPoint) { - context?.selectedTextView = self + if isEditable == false { + context?.selectedTextView = self + } let characterRange = rangeOfCharacter(at: location) richTextViewDelegate?.richTextView(self, didTapAtLocation: location, characterRange: characterRange) } diff --git a/Proton/Sources/Swift/Core/RichTextViewContext.swift b/Proton/Sources/Swift/Core/RichTextViewContext.swift index 0fef4dbb..3472d62c 100644 --- a/Proton/Sources/Swift/Core/RichTextViewContext.swift +++ b/Proton/Sources/Swift/Core/RichTextViewContext.swift @@ -32,10 +32,10 @@ class RichTextViewContext: NSObject, UITextViewDelegate { func textViewDidChangeSelection(_ textView: UITextView) { guard textView.delegate === self else { return } - if textView.selectedTextRange != nil { - selectedTextView = textView.asRichTextView - } else { - selectedTextView = nil + if (textView.isEditable && textView.isFirstResponder) || textView.isEditable == false { + if textView.selectedTextRange != nil { + selectedTextView = textView.asRichTextView + } } guard let richTextView = textView as? RichTextView else { return } diff --git a/Proton/Tests/Core/RichTextViewContextTests.swift b/Proton/Tests/Core/RichTextViewContextTests.swift index 9057dc60..5021323a 100644 --- a/Proton/Tests/Core/RichTextViewContextTests.swift +++ b/Proton/Tests/Core/RichTextViewContextTests.swift @@ -25,6 +25,25 @@ import XCTest class RichTextViewContextTests: XCTestCase { + var window: UIWindow! + var viewController: UIViewController! + + + override func setUp() { + super.setUp() + window = UIWindow(frame: UIScreen.main.bounds) + viewController = UIViewController() + window.rootViewController = viewController + + window.makeKeyAndVisible() + } + + override func tearDown() { + viewController = nil + window = nil + super.tearDown() + } + func testInvokesSelectionChange() { let testExpectation = expectation(description: #function) let mockTextViewDelegate = MockRichTextViewDelegate() @@ -297,12 +316,37 @@ class RichTextViewContextTests: XCTestCase { waitForExpectations(timeout: 1.0) } - func testSetsSelectedEditorOnTextRangeChange() { + func testSetsSelectedEditorOnTextRangeChangeWhenReadOnly() { let testExpectation = expectation(description: #function) let mockTextViewDelegate = MockRichTextViewDelegate() let context = RichTextEditorContext.default let textView = RichTextView(context: context) + textView.isEditable = false + textView.richTextViewDelegate = mockTextViewDelegate + + textView.text = "Sample text" + textView.selectedRange = .zero + + mockTextViewDelegate.onSelectionChanged = { _, _, _, _ in + XCTAssertEqual(context.selectedTextView, textView) + testExpectation.fulfill() + } + + context.textViewDidChangeSelection(textView) + + waitForExpectations(timeout: 1.0) + } + + func testSetsSelectedEditorOnTextRangeChangeWhenIsFirstResponder() { + let testExpectation = expectation(description: #function) + let mockTextViewDelegate = MockRichTextViewDelegate() + + let context = RichTextEditorContext.default + let textView = RichTextView(context: context) + viewController.view.addSubview(textView) + + XCTAssertTrue(textView.becomeFirstResponder()) textView.richTextViewDelegate = mockTextViewDelegate textView.text = "Sample text" @@ -339,14 +383,14 @@ class RichTextViewContextTests: XCTestCase { waitForExpectations(timeout: 1.0) } - func testSetsSelectedEditorOnTap() { + func testSetsSelectedReadOnlyEditorOnTap() { let testExpectation = expectation(description: #function) let mockTextViewDelegate = MockRichTextViewDelegate() let context = RichTextEditorContext.default let textView = RichTextView(context: context) textView.richTextViewDelegate = mockTextViewDelegate - + textView.isEditable = false textView.text = "Sample text" textView.selectedTextRange = nil