Skip to content

Commit

Permalink
Fixed issue where selectedEditorView may be unintentionally updated o…
Browse files Browse the repository at this point in the history
…n setting attributed text (#345)
  • Loading branch information
rajdeep authored Oct 4, 2024
1 parent b43ebb8 commit e374630
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Proton/Sources/Swift/Attachment/Attachment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ open class Attachment: NSTextAttachment, BoundsObserving {
containerTextView.textStorage.replaceCharacters(in: range, with: "")
// Set the selected range in container to show the cursor at deleted location
// after attachment is removed.
containerTextView.selectedRange = NSRange(location: range.location, length: 0)
containerTextView.updateSelectedRangeIgnoringCallback(NSRange(location: range.location, length: 0))
}

/// Range of this attachment in it's container
Expand Down
8 changes: 8 additions & 0 deletions Proton/Sources/Swift/Core/RichTextView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ class RichTextView: AutogrowingTextView {

private var delegateOverrides = [GestureRecognizerDelegateOverride]()

private(set) var ignoreSelectedRangeChangeCallback = false

private var _canBecomeFirstResponder = true
override var canBecomeFirstResponder: Bool {
return _canBecomeFirstResponder
Expand Down Expand Up @@ -203,6 +205,12 @@ class RichTextView: AutogrowingTextView {
draw(CGRect(origin: .zero, size: contentSize))
}

func updateSelectedRangeIgnoringCallback(_ selectedRange: NSRange) {
ignoreSelectedRangeChangeCallback = true
self.selectedRange = selectedRange
ignoreSelectedRangeChangeCallback = false
}

override var selectedTextRange: UITextRange? {
didSet{
let old = oldValue?.toNSRange(in: self)
Expand Down
15 changes: 10 additions & 5 deletions Proton/Sources/Swift/Core/RichTextViewContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,16 @@ class RichTextViewContext: NSObject, UITextViewDelegate {
}

func textViewDidChangeSelection(_ textView: UITextView) {
guard textView.delegate === self else { return }
if textView.selectedTextRange != nil {
selectedTextView = textView.asRichTextView
} else {
selectedTextView = nil
guard textView.delegate === self,
textView.asRichTextView?.ignoreSelectedRangeChangeCallback == false,
textView.asRichTextView?.editorView?.isSettingAttributedText != true else { return }

if textView.isEditable && textView.isFirstResponder || textView.isEditable == false {
if textView.selectedTextRange != nil {
selectedTextView = textView.asRichTextView
} else {
selectedTextView = nil
}
}

guard let richTextView = textView as? RichTextView else { return }
Expand Down
3 changes: 2 additions & 1 deletion Proton/Sources/Swift/Editor/EditorView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,7 @@ open class EditorView: UIView {
pendingAttributedText = newValue
return
}
isSettingAttributedText = true
attachmentRenderingScheduler.cancel()
renderedViewport = nil
// Clear text before setting new value to avoid issues with formatting/layout when
Expand All @@ -569,7 +570,7 @@ open class EditorView: UIView {
pendingAttributedText = nil

AggregateEditorViewDelegate.editor(self, willSetAttributedText: newValue, isDeferred: isDeferred)
isSettingAttributedText = true

richTextView.attributedText = newValue
isSettingAttributedText = false
AggregateEditorViewDelegate.editor(self, didSetAttributedText: newValue, isDeferred: isDeferred)
Expand Down
1 change: 1 addition & 0 deletions Proton/Tests/Core/RichTextViewContextTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@ class RichTextViewContextTests: XCTestCase {
let textView = RichTextView(context: context)
textView.richTextViewDelegate = mockTextViewDelegate

textView.isEditable = false
textView.text = "Sample text"
textView.selectedRange = .zero

Expand Down

0 comments on commit e374630

Please sign in to comment.