From 374e2a0e4918687b5fbc4044ab205b3a78ba9978 Mon Sep 17 00:00:00 2001 From: Martin Purita Date: Fri, 27 Sep 2024 15:31:03 -0700 Subject: [PATCH] allowing to text input spec to be able to listen to text pasted event Summary: Allowing ```TextInputSpec``` to listen to ```OnTextMenuItem``` for paste id. This allow to identify when the text in the input was pasted instead of written letter by letter so we can do some processing after that like replacing Meta AI with actual mention instead of plain text Reviewed By: zielinskimz Differential Revision: D63465179 fbshipit-source-id: 7cc96480db657cc6530b44328c11338bf2df98bc --- .../litho/widget/MaterialTextInputSpec.java | 5 +++- .../facebook/litho/widget/TextInputSpec.java | 29 +++++++++++++++++-- .../litho/widget/TextPastedEvent.java | 28 ++++++++++++++++++ 3 files changed, 59 insertions(+), 3 deletions(-) create mode 100644 litho-widget/src/main/java/com/facebook/litho/widget/TextPastedEvent.java diff --git a/litho-widget-material/src/main/java/com/facebook/litho/widget/MaterialTextInputSpec.java b/litho-widget-material/src/main/java/com/facebook/litho/widget/MaterialTextInputSpec.java index 640be069b4f..8e12ae73335 100644 --- a/litho-widget-material/src/main/java/com/facebook/litho/widget/MaterialTextInputSpec.java +++ b/litho-widget-material/src/main/java/com/facebook/litho/widget/MaterialTextInputSpec.java @@ -82,6 +82,7 @@ EditorActionEvent.class, SetTextEvent.class, InputConnectionEvent.class, + TextPastedEvent.class, }) class MaterialTextInputSpec { @@ -519,7 +520,9 @@ static void onBind( // NULLSAFE_FIXME[Parameter Not Nullable] MaterialTextInput.getEditorActionEventHandler(c), // NULLSAFE_FIXME[Parameter Not Nullable] - MaterialTextInput.getInputConnectionEventHandler(c)); + MaterialTextInput.getInputConnectionEventHandler(c), + // NULLSAFE_FIXME[Parameter Not Nullable] + MaterialTextInput.getTextPastedEventHandler(c)); } @OnUnmount diff --git a/litho-widget/src/main/java/com/facebook/litho/widget/TextInputSpec.java b/litho-widget/src/main/java/com/facebook/litho/widget/TextInputSpec.java index 11af834a7d9..23a54d7496a 100644 --- a/litho-widget/src/main/java/com/facebook/litho/widget/TextInputSpec.java +++ b/litho-widget/src/main/java/com/facebook/litho/widget/TextInputSpec.java @@ -197,6 +197,7 @@ EditorActionEvent.class, SetTextEvent.class, InputConnectionEvent.class, + TextPastedEvent.class, }) class TextInputSpec { @@ -899,7 +900,8 @@ static void onBind( TextInput.getKeyUpEventHandler(c), TextInput.getKeyPreImeEventHandler(c), TextInput.getEditorActionEventHandler(c), - TextInput.getInputConnectionEventHandler(c)); + TextInput.getInputConnectionEventHandler(c), + TextInput.getTextPastedEventHandler(c)); } static void onBindEditText( @@ -914,7 +916,8 @@ static void onBindEditText( EventHandler keyUpEventHandler, EventHandler keyPreImeEventHandler, EventHandler EditorActionEventHandler, - EventHandler inputConnectionEventHandler) { + EventHandler inputConnectionEventHandler, + EventHandler textPastedEventHandler) { editText.attachWatchers(textWatchers); editText.setCustomSelectionActionModeCallback(selectionActionModeCallback); if (SDK_INT >= M) { @@ -929,6 +932,7 @@ static void onBindEditText( editText.setKeyPreImeEventEventHandler(keyPreImeEventHandler); editText.setEditorActionEventHandler(EditorActionEventHandler); editText.setInputConnectionEventHandler(inputConnectionEventHandler); + editText.setTextPastedEventHandler(textPastedEventHandler); } @OnUnmount @@ -963,6 +967,7 @@ static void onUnbind(final ComponentContext c, EditTextWithEventHandlers editTex if (SDK_INT >= M) { editText.setCustomInsertionActionModeCallback(null); } + editText.setTextPastedEventHandler(null); } @Nullable @@ -1184,6 +1189,7 @@ static class EditTextWithEventHandlers extends EditText private static final int UNMEASURED_LINE_COUNT = -1; + @Nullable private EventHandler mTextPastedEventHandler; @Nullable private EventHandler mTextChangedEventHandler; @Nullable private EventHandler mSelectionChangedEventHandler; @Nullable private EventHandler mInputFocusChangedEventHandler; @@ -1198,6 +1204,7 @@ static class EditTextWithEventHandlers extends EditText private boolean mIsSoftInputRequested = false; private boolean mDisableAutofill = false; + private boolean mIsTextPasted = false; @Nullable private ViewTreeObserver.OnWindowFocusChangeListener mOnWindowFocusChangeListener; @@ -1228,6 +1235,11 @@ protected void onTextChanged(CharSequence text, int start, int lengthBefore, int TextInput.dispatchTextChangedEvent( mTextChangedEventHandler, EditTextWithEventHandlers.this, text.toString()); } + if (mIsTextPasted && mTextPastedEventHandler != null) { + TextInput.dispatchTextPastedEvent( + mTextPastedEventHandler, EditTextWithEventHandlers.this, text.toString()); + mIsTextPasted = false; + } // Line count of changed text. int lineCount = getLineCount(); if (mLineCount != UNMEASURED_LINE_COUNT @@ -1237,6 +1249,14 @@ protected void onTextChanged(CharSequence text, int start, int lengthBefore, int } } + @Override + public boolean onTextContextMenuItem(int id) { + if (id == android.R.id.paste && mTextPastedEventHandler != null) { + mIsTextPasted = true; + } + return super.onTextContextMenuItem(id); + } + @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); @@ -1307,6 +1327,11 @@ void setDisableAutofill(Boolean value) { mDisableAutofill = value; } + void setTextPastedEventHandler( + @Nullable EventHandler textPastedEventEventHandler) { + mTextPastedEventHandler = textPastedEventEventHandler; + } + void setTextChangedEventHandler( @Nullable EventHandler textChangedEventHandler) { mTextChangedEventHandler = textChangedEventHandler; diff --git a/litho-widget/src/main/java/com/facebook/litho/widget/TextPastedEvent.java b/litho-widget/src/main/java/com/facebook/litho/widget/TextPastedEvent.java new file mode 100644 index 00000000000..1fee0624bba --- /dev/null +++ b/litho-widget/src/main/java/com/facebook/litho/widget/TextPastedEvent.java @@ -0,0 +1,28 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.facebook.litho.widget; + +import android.widget.EditText; +import com.facebook.litho.annotations.Event; +import com.facebook.litho.annotations.EventHandlerRebindMode; + +/** Event sent by EditText when the text entered is pasted from clipboard. */ +@Event(mode = EventHandlerRebindMode.NONE) +public class TextPastedEvent { + public EditText view; + public String text; +}