Skip to content

Commit

Permalink
[RNMobile] Fix dictation regression on iOS (#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 Jun 5, 2023
1 parent 27b78f2 commit f630a68
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

1 comment on commit f630a68

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flaky tests detected in f630a68.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/5175290529
📝 Reported issues:

Please sign in to comment.