Skip to content

Commit

Permalink
[RNMobile] Fix dictation regression on iOS (WordPress#49452)
Browse files Browse the repository at this point in the history
These changes address an issue with dictation not working as expected on devices running iOS 16 or later.
  • Loading branch information
Siobhan Bamber authored and sethrubenstein committed Jul 13, 2023
1 parent bd16365 commit 40ceda7
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 20 deletions.
39 changes: 19 additions & 20 deletions packages/react-native-aztec/ios/RNTAztecView/RCTAztecView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,6 @@ class RCTAztecView: Aztec.TextView {
let placeholderWidthInset = 2 * leftTextInset
return placeholderLabel.widthAnchor.constraint(equalTo: widthAnchor, constant: -placeholderWidthInset)
}()

/// If a dictation start with an empty UITextView,
/// the dictation engine refreshes the TextView with an empty string when the dictation finishes.
/// This helps to avoid propagating that unwanted empty string to RN. (Solving #606)
/// on `textViewDidChange` and `textViewDidChangeSelection`
private var isInsertingDictationResult = false

// MARK: - Font

Expand Down Expand Up @@ -355,16 +349,18 @@ class RCTAztecView: Aztec.TextView {
}

// MARK: - Dictation

override func dictationRecordingDidEnd() {
isInsertingDictationResult = true
}

public override func insertDictationResult(_ dictationResult: [UIDictationPhrase]) {
let objectPlaceholder = "\u{FFFC}"
let dictationText = dictationResult.reduce("") { $0 + $1.text }
isInsertingDictationResult = false
self.text = self.text?.replacingOccurrences(of: objectPlaceholder, with: dictationText)

func removeUnicodeAndRestoreCursor(from textView: UITextView) {
// Capture current cursor position
let originalPosition = textView.offset(from: textView.beginningOfDocument, to: textView.selectedTextRange?.start ?? textView.beginningOfDocument)

// Replace occurrences of the obj symbol ("\u{FFFC}")
textView.text = textView.text?.replacingOccurrences(of: "\u{FFFC}", with: "")

if let newPosition = textView.position(from: textView.beginningOfDocument, offset: originalPosition) {
// Move the cursor to the correct, new position following dictation
textView.selectedTextRange = textView.textRange(from: newPosition, to: newPosition)
}
}

// MARK: - Custom Edit Intercepts
Expand Down Expand Up @@ -771,7 +767,7 @@ class RCTAztecView: Aztec.TextView {
extension RCTAztecView: UITextViewDelegate {

func textViewDidChangeSelection(_ textView: UITextView) {
guard isFirstResponder, isInsertingDictationResult == false else {
guard isFirstResponder else {
return
}

Expand All @@ -784,10 +780,13 @@ extension RCTAztecView: UITextViewDelegate {
}

func textViewDidChange(_ textView: UITextView) {
guard isInsertingDictationResult == false else {
return
// Workaround for RN dictation bug that adds obj symbol.
// Ref: https://github.com/facebook/react-native/issues/36521
// TODO: Remove workaround when RN issue is fixed
if textView.text?.contains("\u{FFFC}") == true {
removeUnicodeAndRestoreCursor(from: textView)
}

propagateContentChanges()
updatePlaceholderVisibility()
//Necessary to send height information to JS after pasting text.
Expand Down
2 changes: 2 additions & 0 deletions packages/react-native-editor/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ For each user feature we should also add a importance categorization label to i
- [**] Fix undo/redo history when inserting a link configured to open in a new tab [#50460]
- [*] [List block] Fix an issue when merging a list item into a Paragraph would remove its nested list items. [#50701]

- [**] [iOS] Fix dictation regression, in which typing/dictating at the same time caused content loss. [#49452]

## 1.95.0
- [*] Fix crash when trying to convert to regular blocks an undefined/deleted reusable block [#50475]
- [**] Tapping on nested text blocks gets focus directly instead of having to tap multiple times depeding on the nesting levels. [#50108]
Expand Down

0 comments on commit 40ceda7

Please sign in to comment.