Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed context.selectedEditor to only be set if editor is focused when… #337

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Proton/Sources/Swift/Core/RichTextView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
8 changes: 4 additions & 4 deletions Proton/Sources/Swift/Core/RichTextViewContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
50 changes: 47 additions & 3 deletions Proton/Tests/Core/RichTextViewContextTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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

Expand Down
Loading