Skip to content

Commit

Permalink
Added misc helper functions
Browse files Browse the repository at this point in the history
  • Loading branch information
rajdeep committed Oct 16, 2023
1 parent c273191 commit 12844c7
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
17 changes: 17 additions & 0 deletions Proton/Sources/Swift/Editor/EditorView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,11 @@ open class EditorView: UIView {
/// mat be more than when synchronous mode, ie default, is used. The perceived performance/TTI will almost always be better with asynchronous rendering.
public weak var asyncAttachmentRenderingDelegate: AsyncAttachmentRenderingDelegate?

/// Returns `UITextInput` of current instance
public var textInput: UITextInput {
richTextView
}

public var textInteractions: [UITextInteraction] {
richTextView.interactions.compactMap({ $0 as? UITextInteraction })
}
Expand Down Expand Up @@ -740,6 +745,18 @@ open class EditorView: UIView {
return richTextView.becomeFirstResponder()
}

/// Denotes of the Editor is first responder
/// - Returns: true, if is first responder
public func isFirstResponder() -> Bool {
richTextView.isFirstResponder
}

/// Resets typing attributes back to default text color, font and paragraph style.
///All other attributes are dropped.
public func resetTypingAttributes() {
richTextView.resetTypingAttributes()
}

/// Converts given range to `UITextRange`, if valid
/// - Parameter range: Range to convert
/// - Returns: `UITextRange` representation of provided NSRange, if valid.
Expand Down
14 changes: 13 additions & 1 deletion Proton/Sources/Swift/Helpers/NSRangeExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ public extension NSRange {
}

var lastCharacterRange: NSRange {
return NSRange(location: location + length, length: 1)
return NSRange(location: max(location + length - 1, 0), length: 1)
}

var previousPosition: NSRange {
return NSRange(location: max(location - 1, 0), length: 0)
}

var nextPosition: NSRange {
Expand All @@ -44,6 +48,14 @@ public extension NSRange {
return location + length
}

var nextCharacterRange: NSRange {
NSRange(location: location + length, length: 1)
}

var previousCharacterRange: NSRange {
NSRange(location: location - 1, length: 1)
}

/// Converts the range to `UITextRange` in given `UITextInput`. Returns nil if the range is invalid in the `UITextInput`.
/// - Parameter textInput: UITextInput to convert the range in.
func toTextRange(textInput: UITextInput) -> UITextRange? {
Expand Down

0 comments on commit 12844c7

Please sign in to comment.