Skip to content

Commit 638bed4

Browse files
authored
Substring from range fix (#271)
* Fixed selected range on replace characters * Fixed a crash related to attachmentRanges when the text is changed in editor and the editor is not already on a Window
1 parent a6ec0f1 commit 638bed4

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

Diff for: Proton/Sources/Swift/Editor/EditorView.swift

+13-1
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,12 @@ open class EditorView: UIView {
153153
/// Context for the current Editor
154154
public let editorViewContext: EditorViewContext
155155

156+
/// Returns if `attributedText` change is pending. `AttributedText` may not have been applied if the `EditorView` is not already on
157+
/// `window` and `forceApplyAttributedText` is not set to `true`.
158+
public var isAttributedTextPending: Bool {
159+
pendingAttributedText != nil
160+
}
161+
156162
/// Enables asynchronous rendering of attachments.
157163
/// - Note:
158164
/// Since attachments must me rendered on main thread, the rendering only continues when there is no user interaction. By default, rendering starts
@@ -341,6 +347,7 @@ open class EditorView: UIView {
341347
/// An attachment is only counted as a single character. Content length does not include
342348
/// length of content within the Attachment that is hosting another `EditorView`.
343349
public var contentLength: Int {
350+
guard pendingAttributedText == nil else { return attributedText.length }
344351
return richTextView.contentLength
345352
}
346353

@@ -461,6 +468,11 @@ open class EditorView: UIView {
461468
public var forceApplyAttributedText = false
462469

463470
/// Text to be set in the `EditorView`
471+
/// - Important: `attributedText` is not set for rendering in `EditorView` if the `EditorView` is not already in a `Window`. Value of `true`
472+
/// for `isAttributedTextPending` confirms that the text has not yet been rendered even though it is set in the `EditorView`.
473+
/// Notification of text being set can be observed by subscribing to `didSetAttributedText` in `EditorViewDelegate`.
474+
/// Alternatively, `forceApplyAttributedText` may be set to `true` to always apply `attributedText` irrespective of `EditorView` being
475+
/// in a `Window` or not.
464476
public var attributedText: NSAttributedString {
465477
get {
466478
pendingAttributedText ?? richTextView.attributedText
@@ -787,7 +799,7 @@ open class EditorView: UIView {
787799
}
788800

789801
public func attachmentsInRange(_ range: NSRange) -> [AttachmentRange] {
790-
guard range.endLocation < contentLength else { return [] }
802+
guard range.endLocation < attributedText.length else { return [] }
791803
let substring = attributedText.attributedSubstring(from: range)
792804
return substring.attachmentRanges
793805
}

Diff for: Proton/Tests/Editor/EditorViewTests.swift

+8
Original file line numberDiff line numberDiff line change
@@ -823,6 +823,14 @@ class EditorViewTests: XCTestCase {
823823
XCTAssertTrue(editor.richTextView.selectedRange.isValidIn(editor.richTextView))
824824
XCTAssertEqual(editor.selectedRange, NSRange(location: 3, length: 0))
825825
}
826+
827+
func testAttachmentsInRangeWithInvalidRange() {
828+
let editor = EditorView()
829+
editor.replaceCharacters(in: .zero, with: NSAttributedString(string: "Test"))
830+
editor.attributedText = NSAttributedString(string: "In")
831+
_ = editor.attachmentsInRange(NSRange(location: 2, length: 1))
832+
XCTAssertEqual(editor.attributedText.length, 2)
833+
}
826834
}
827835

828836
class DummyMultiEditorAttachment: Attachment {

0 commit comments

Comments
 (0)