Skip to content

fix(android): Events batching & reordering overhaul - #869

Open
szydlovsky wants to merge 4 commits into
@kacperzolkiewski/fix-onlink-detectedfrom
@szydlovsky/batch-reorder-android-events
Open

szydlovsky wants to merge 4 commits into
@kacperzolkiewski/fix-onlink-detectedfrom
@szydlovsky/batch-reorder-android-events

Conversation

@szydlovsky

@szydlovsky szydlovsky commented Nov 19, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Motivation

Android sent JS events at the moment each change happened, often while an edit was still in progress. One keystroke could produce several onChangeHtml, onChangeState and onLinkDetected events with intermediate or stale values. The visible symptom was reported in #624: after pressing Enter inside a link, a stale onLinkDetected fires first and the correct one follows a few ms later.

The root cause is the order of Android's callbacks. onSelectionChanged runs only after every TextWatcher is done, so the styles were validated, and the events sent, against the caret from before the edit.

iOS doesn't have this problem: it applies all changes first and emits events once at the end. This PR makes Android work the same way and removes the isSelectionOutdated() workaround added in #624.

How it works

EnrichedTextInputView.batchEvents { } runs its block immediately and holds back these events while it runs:

  • onChangeText
  • onChangeHtml
  • onChangeState
  • onLinkDetected
  • onMentionDetected

Once the block finishes, each event that is due is sent once, in this order: text → link/mention → state → html. Links and mentions are detected again at that point, so they use the final caret and spans.

It doesn't queue or delay anything, and it doesn't collect unrelated events. It only merges nested calls, where one callback runs another inside it. A depth counter makes sure only the outermost scope sends events. Two separate edits still emit separately.

onChangeSelection and the mention-editing events (onStartMention, onChangeMention, onEndMention) aren't batched, same as on iOS.

Where batching was added

  • EnrichedEditable.replace (new, in EnrichedEditableFactory.kt): every text edit goes through it. It covers afterTextChanged and the onSelectionChanged that Android calls after it, so events use the final caret. This is the fix for fix(iOS, Android): split manual links when newline inserted in link #624's issue.
  • EnrichedTextWatcher.afterTextChanged: applying styles adds and removes many spans. onChangeText and onChangeHtml are now sent once, after styling, instead of before it plus once per span change.
  • EnrichedSelection.validateStyles: it calls about 20 state setters, which now produce one onChangeState instead of several intermediate ones.
  • runAsATransaction: multi-step programmatic edits (setValue, style toggles, mention insertion) emit once, when they're done.

Event order is closer to iOS

  • onChangeSelection now arrives before onChangeText and onChangeState for an edit, as on iOS.
  • onChangeText and onChangeHtml now describe the text with styles already applied.

Benefits

  • No stale or intermediate events: JS only sees final values.
  • Fewer events crossing the bridge, so fewer re-renders in JS.
  • Performance: EnrichedParser.toHtml() runs once per edit instead of once per span change. That's a noticeable saving on long documents and fast typing. The state payload is also built and compared once.
  • The isSelectionOutdated() workaround from fix(iOS, Android): split manual links when newline inserted in link #624 is no longer needed and has been removed.

Follow-up fixes: actions that are made of several steps

Some actions run several separate steps, each with its own transaction, so each step sent its own events. Two cases were found:

Toggling a code block over a link (found in manual testing):

  • Toggling off: the restored link's onLinkDetected only arrived after moving the caret. This was already happening on main.
  • Toggling back on: a stale link event arrived, followed by (0,0).

Both come from verifyStyle. It removes each conflicting style in its own transaction, and links are restored after the last validation.

Pasting (found in review):

  • handleTextPaste first calls setValue, which moves the caret to the end of the document. Only after that does it restore the caret and detect links in the pasted text.
  • Pasting mid-document could therefore report the document's trailing link, or briefly show the state at the end of the document. A pasted URL could also send onChangeHtml both before and after its link was added.
  • This was already happening on main.

The fix wraps each of these entry points in a single batchEvents, so all their steps emit once, with final values:

  • verifyAndToggleStyle
  • toggleCheckboxListItem
  • addLink
  • addImage
  • startMention
  • addMention
  • setStyle
  • handleTextPaste

Test Plan

Tested manually on Android, with an event log open:

  • Enter inside, before and after a link: one correct onLinkDetected
  • Caret into, within and out of a link: no event spam, one empty event when leaving
  • Typing a URL is auto-detected and fires once
  • setLink, editing and removing a link: one event each
  • Backspace into a link and deleting a whole link
  • URL inside a code block isn't detected
  • Code block on and off over a URL: link event right away, then a single (0,0)
  • Same for inline code, with the caret outside the URL, and over a multi-line selection
  • @ and query typing: mention events stay live
  • Picking a mention, moving the caret into and out of it, backspacing it
  • Toggling bold at a collapsed caret, then typing: the style persists
  • Inline styles on a selection: one state update
  • Caret across differently styled runs: no toolbar flicker
  • Headings and blockquote: toggling, Enter at the end, Enter on an empty one
  • Lists: continuing, exiting on an empty item, backspace at the start of an item
  • Switching between list types, and between headings and lists or quotes
  • Checkbox tap and checkbox toggle from JS
  • Alignment change
  • Code block over bold, italic, links and mentions: one state and one html event
  • Styles blocked inside a code block or inline code: nothing applied, toolbar correct
  • addLink with a selection and with a collapsed caret
  • addImage in plain text, in a list and at the start of a line
  • startMention / addMention from JS
  • Custom setStyle with and without a selection
  • Markdown shortcuts (- , 1. )
  • Select all + backspace, then typing
  • One onChangeText and at most one onChangeHtml per keystroke
  • Typing inside a bold run updates the html
  • Pasting multi-line plain and rich text
  • setValue, clearing, and setting html from JS
  • No onChangeHtml passed
  • Fast typing in a long document
  • ZWS caret correction in empty list items and headings, and around images
  • Empty input, first line, last line, quick repeated toggles
  • Pasting mid-document doesn't report the document's trailing link or flicker the toolbar
  • Pasting a plain-text URL sends one onChangeHtml with the link included

Screenshots / Videos

--

Compatibility

OS Implemented
iOS ✅ (already behaves this way)
Android ✅
Web ❌

Checklist

  • E2E tests are passing
  • Required E2E tests have been added (if applicable)

@szydlovsky
szydlovsky requested review from exploIF and a balanced review from Copilot November 19, 2026 16:55
@szydlovsky
szydlovsky changed the base branch from main to @kacperzolkiewski/fix-onlink-detected November 19, 2026 17:00
@szydlovsky
szydlovsky force-pushed the @szydlovsky/batch-reorder-android-events branch from 111c7d6 to 606c177 Compare November 19, 2026 17:02

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Android batching can emit out-of-order state events and incorrect paste updates, while iOS marked-text handling can leave toolbar and detected-link state stale.

2 open issues
What changed in this PR

This PR batches Android editor events around completed edits to reduce duplicate notifications and stale link detection, bringing event delivery closer to iOS behavior.

Changes:

  • Adds nested event batching across text edits, style validation, and programmatic operations.
  • Updates link detection, manual-link newline handling, and iOS active-link reporting.
  • Initializes the example editor’s selection to an empty range.
File Description
ios/​EnrichedTextInputView.mm Refreshes detected-link text and adjusts active-style update timing.
apps/​example/​src/​hooks/​useEditorState.ts Initializes selection at position zero.
android/​src/​main/​java/​com/​swmansion/​enriched/​textinput/​watchers/​EnrichedTextWatcher.kt Defers text notifications until styling completes.
android/​src/​main/​java/​com/​swmansion/​enriched/​textinput/​watchers/​EnrichedSpanWatcher.kt Defers HTML generation during batches.
android/​src/​main/​java/​com/​swmansion/​enriched/​textinput/​utils/​EnrichedSpanState.kt Suppresses state notifications during batching.
android/​src/​main/​java/​com/​swmansion/​enriched/​textinput/​utils/​EnrichedSelection.kt Batches validation and includes link ranges in deduplication.
android/​src/​main/​java/​com/​swmansion/​enriched/​textinput/​utils/​EnrichedEditableFactory.kt Wraps editable replacements in event batches.
android/​src/​main/​java/​com/​swmansion/​enriched/​textinput/​styles/​ParametrizedStyles.kt Splits manual links around inserted newlines.
android/​src/​main/​java/​com/​swmansion/​enriched/​textinput/​EnrichedTextInputView.kt Coordinates nested batches and event delivery order.

Review effort: Balanced

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants