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 issue where selectedEditorView may be unintentionally updated o… #345

Merged
merged 1 commit into from
Oct 4, 2024
Merged
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
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
Loading