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

Send backspace event to JS in more scenarios #1517

Merged
merged 2 commits into from
Nov 5, 2019
Merged
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
31 changes: 21 additions & 10 deletions react-native-aztec/ios/RNTAztecView/RCTAztecView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -319,12 +319,19 @@ class RCTAztecView: Aztec.TextView {
}

private func interceptBackspace() -> Bool {
guard (isNewLineBeforeSelectionAndNotEndOfContent() && selectedRange.length == 0) || (selectedRange.location == 0 && selectedRange.length == 0),
guard (isNewLineBeforeSelectionAndNotEndOfContent() && selectedRange.length == 0)
|| (selectedRange.location == 0 && selectedRange.length == 0)
|| text.count == 1 // send backspace event when cleaning all characters
|| selectedRange == NSRange(location: 0, length: textStorage.length), // send backspace event when deleting all the text
let onBackspace = onBackspace else {
return false
}

let caretData = packCaretDataForRN()
var range = selectedRange
if text.count == 1 {
range = NSRange(location: 0, length: textStorage.length)
}
let caretData = packCaretDataForRN(overrideRange: range)
onSelectionChange?(caretData)
onBackspace(caretData)
return true
}
Expand Down Expand Up @@ -360,10 +367,14 @@ class RCTAztecView: Aztec.TextView {

return [name: size]
}

func packCaretDataForRN() -> [AnyHashable: Any] {
var start = selectedRange.location
var end = selectedRange.location + selectedRange.length

func packCaretDataForRN(overrideRange: NSRange? = nil) -> [AnyHashable: Any] {
var range = selectedRange
if let overrideRange = overrideRange {
range = overrideRange
}
var start = range.location
var end = range.location + range.length
if selectionAffinity == .backward {
(start, end) = (end, start)
}
Expand All @@ -372,9 +383,9 @@ class RCTAztecView: Aztec.TextView {

result["selectionStart"] = start
result["selectionEnd"] = end

if let selectedTextRange = selectedTextRange {
let caretEndRect = caretRect(for: selectedTextRange.end)
if let range = selectedTextRange {
let caretEndRect = caretRect(for: range.end)
// Sergio Estevao: Sometimes the carectRect can be invalid so we need to check before sending this to JS.
if !(caretEndRect.isInfinite || caretEndRect.isNull) {
result["selectionEndCaretX"] = caretEndRect.origin.x
Expand Down