From 24edfbcdebd761bc0682fd184a8f86bbde802d00 Mon Sep 17 00:00:00 2001 From: Stefanos Togkoulidis Date: Fri, 7 Feb 2020 12:48:46 +0200 Subject: [PATCH] Send selection change event when typing --- .../ReactNativeAztec/ReactAztecManager.java | 24 ++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/react-native-aztec/android/src/main/java/org/wordpress/mobile/ReactNativeAztec/ReactAztecManager.java b/react-native-aztec/android/src/main/java/org/wordpress/mobile/ReactNativeAztec/ReactAztecManager.java index 8f9df39fd2..7caa9808cb 100644 --- a/react-native-aztec/android/src/main/java/org/wordpress/mobile/ReactNativeAztec/ReactAztecManager.java +++ b/react-native-aztec/android/src/main/java/org/wordpress/mobile/ReactNativeAztec/ReactAztecManager.java @@ -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(); @@ -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 @@ -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( @@ -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())); + } }