Skip to content

Commit

Permalink
Send selection change event when typing
Browse files Browse the repository at this point in the history
  • Loading branch information
hypest committed Feb 7, 2020
1 parent 2657a54 commit 24edfbc
Showing 1 changed file with 21 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,8 @@ private class AztecTextWatcher implements TextWatcher {
private EventDispatcher mEventDispatcher;
private ReactAztecText mEditText;
private String mPreviousText;
private int mPreviousSelectionStart;
private int mPreviousSelectionEnd;

public AztecTextWatcher(final ReactContext reactContext, final ReactAztecText aztecText) {
mEventDispatcher = reactContext.getNativeModule(UIManagerModule.class).getEventDispatcher();
Expand All @@ -555,6 +557,8 @@ public AztecTextWatcher(final ReactContext reactContext, final ReactAztecText az
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// Incoming charSequence gets mutated before onTextChanged() is invoked
mPreviousText = s.toString();
mPreviousSelectionStart = mEditText.getSelectionStart();
mPreviousSelectionEnd = mEditText.getSelectionEnd();
}

@Override
Expand All @@ -576,14 +580,14 @@ public void onTextChanged(CharSequence s, int start, int before, int count) {
// if the "Enter" handling is underway, don't sent text change events. The ReactAztecEnterEvent will have
// the text (minus the Enter char itself).
if (!mEditText.isEnterPressedUnderway()) {
int currentEventCount = mEditText.incrementAndGetEventCounter();
final String content = mEditText.toHtml(mEditText.getText(), false);
// The event that contains the event counter and updates it must be sent first.
// TODO: t7936714 merge these events
mEventDispatcher.dispatchEvent(
new ReactTextChangedEvent(
mEditText.getId(),
mEditText.toHtml(mEditText.getText(), false),
currentEventCount));
content,
mEditText.incrementAndGetEventCounter()));

mEventDispatcher.dispatchEvent(
new ReactTextInputEvent(
Expand All @@ -592,6 +596,20 @@ public void onTextChanged(CharSequence s, int start, int before, int count) {
oldText,
start,
start + before));


final int selectionStart = mEditText.getSelectionStart();
final int selectionEnd = mEditText.getSelectionEnd();
if (selectionStart != mPreviousSelectionStart
|| selectionEnd != mPreviousSelectionEnd) {
mEventDispatcher.dispatchEvent(
new ReactAztecSelectionChangeEvent(
mEditText.getId(),
content,
selectionStart,
selectionEnd,
mEditText.incrementAndGetEventCounter()));
}
}


Expand Down

0 comments on commit 24edfbc

Please sign in to comment.