From dc80a7c8cff8ab9aebf0a1c754e6eaf74290d41f Mon Sep 17 00:00:00 2001 From: Rajdeep Kwatra Date: Tue, 20 Aug 2024 15:21:50 +1000 Subject: [PATCH 1/2] Fixed context.selectedEditor to only be set if editor is focused when editable --- .../Swift/Core/RichTextViewContext.swift | 7 ++- .../Tests/Core/RichTextViewContextTests.swift | 46 ++++++++++++++++++- 2 files changed, 51 insertions(+), 2 deletions(-) diff --git a/Proton/Sources/Swift/Core/RichTextViewContext.swift b/Proton/Sources/Swift/Core/RichTextViewContext.swift index 0fef4dbb..7291e4bf 100644 --- a/Proton/Sources/Swift/Core/RichTextViewContext.swift +++ b/Proton/Sources/Swift/Core/RichTextViewContext.swift @@ -33,7 +33,12 @@ class RichTextViewContext: NSObject, UITextViewDelegate { func textViewDidChangeSelection(_ textView: UITextView) { guard textView.delegate === self else { return } if textView.selectedTextRange != nil { - selectedTextView = textView.asRichTextView + if (textView.isEditable && textView.isFirstResponder) || + textView.isEditable == false { + selectedTextView = textView.asRichTextView + } else { + selectedTextView = nil + } } else { selectedTextView = nil } diff --git a/Proton/Tests/Core/RichTextViewContextTests.swift b/Proton/Tests/Core/RichTextViewContextTests.swift index 9057dc60..837a63e8 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" From 62bd43f41b09a2ff867b72b9089ee30a83e88b23 Mon Sep 17 00:00:00 2001 From: Rajdeep Kwatra Date: Thu, 22 Aug 2024 13:29:09 +1000 Subject: [PATCH 2/2] Fixed logic to not nil out selectedEditorView --- Proton/Sources/Swift/Core/RichTextView.swift | 4 +++- Proton/Sources/Swift/Core/RichTextViewContext.swift | 9 ++------- Proton/Tests/Core/RichTextViewContextTests.swift | 4 ++-- 3 files changed, 7 insertions(+), 10 deletions(-) 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 7291e4bf..3472d62c 100644 --- a/Proton/Sources/Swift/Core/RichTextViewContext.swift +++ b/Proton/Sources/Swift/Core/RichTextViewContext.swift @@ -32,15 +32,10 @@ class RichTextViewContext: NSObject, UITextViewDelegate { func textViewDidChangeSelection(_ textView: UITextView) { guard textView.delegate === self else { return } - if textView.selectedTextRange != nil { - if (textView.isEditable && textView.isFirstResponder) || - textView.isEditable == false { + if (textView.isEditable && textView.isFirstResponder) || textView.isEditable == false { + if textView.selectedTextRange != nil { selectedTextView = textView.asRichTextView - } else { - selectedTextView = nil } - } else { - selectedTextView = nil } guard let richTextView = textView as? RichTextView else { return } diff --git a/Proton/Tests/Core/RichTextViewContextTests.swift b/Proton/Tests/Core/RichTextViewContextTests.swift index 837a63e8..5021323a 100644 --- a/Proton/Tests/Core/RichTextViewContextTests.swift +++ b/Proton/Tests/Core/RichTextViewContextTests.swift @@ -383,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