Skip to content

Commit ded3cc1

Browse files
authored
Added misc helper functions (#235)
1 parent 5b6d0ac commit ded3cc1

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

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

+17
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,11 @@ open class EditorView: UIView {
152152
/// mat be more than when synchronous mode, ie default, is used. The perceived performance/TTI will almost always be better with asynchronous rendering.
153153
public weak var asyncAttachmentRenderingDelegate: AsyncAttachmentRenderingDelegate?
154154

155+
/// Returns `UITextInput` of current instance
156+
public var textInput: UITextInput {
157+
richTextView
158+
}
159+
155160
public var textInteractions: [UITextInteraction] {
156161
richTextView.interactions.compactMap({ $0 as? UITextInteraction })
157162
}
@@ -740,6 +745,18 @@ open class EditorView: UIView {
740745
return richTextView.becomeFirstResponder()
741746
}
742747

748+
/// Denotes of the Editor is first responder
749+
/// - Returns: true, if is first responder
750+
public func isFirstResponder() -> Bool {
751+
richTextView.isFirstResponder
752+
}
753+
754+
/// Resets typing attributes back to default text color, font and paragraph style.
755+
///All other attributes are dropped.
756+
public func resetTypingAttributes() {
757+
richTextView.resetTypingAttributes()
758+
}
759+
743760
/// Converts given range to `UITextRange`, if valid
744761
/// - Parameter range: Range to convert
745762
/// - Returns: `UITextRange` representation of provided NSRange, if valid.

Diff for: Proton/Sources/Swift/Helpers/NSRangeExtensions.swift

+13-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,11 @@ public extension NSRange {
3333
}
3434

3535
var lastCharacterRange: NSRange {
36-
return NSRange(location: location + length, length: 1)
36+
return NSRange(location: max(location + length - 1, 0), length: 1)
37+
}
38+
39+
var previousPosition: NSRange {
40+
return NSRange(location: max(location - 1, 0), length: 0)
3741
}
3842

3943
var nextPosition: NSRange {
@@ -44,6 +48,14 @@ public extension NSRange {
4448
return location + length
4549
}
4650

51+
var nextCharacterRange: NSRange {
52+
NSRange(location: location + length, length: 1)
53+
}
54+
55+
var previousCharacterRange: NSRange {
56+
NSRange(location: location - 1, length: 1)
57+
}
58+
4759
/// Converts the range to `UITextRange` in given `UITextInput`. Returns nil if the range is invalid in the `UITextInput`.
4860
/// - Parameter textInput: UITextInput to convert the range in.
4961
func toTextRange(textInput: UITextInput) -> UITextRange? {

0 commit comments

Comments
 (0)