Skip to content

Commit

Permalink
allowing to text input spec to be able to listen to text pasted event
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Martin Purita authored and facebook-github-bot committed Sep 27, 2024
1 parent 5ce04fe commit 374e2a0
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
EditorActionEvent.class,
SetTextEvent.class,
InputConnectionEvent.class,
TextPastedEvent.class,
})
class MaterialTextInputSpec {

Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@
EditorActionEvent.class,
SetTextEvent.class,
InputConnectionEvent.class,
TextPastedEvent.class,
})
class TextInputSpec {

Expand Down Expand Up @@ -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(
Expand All @@ -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) {
Expand All @@ -929,6 +932,7 @@ static void onBindEditText(
editText.setKeyPreImeEventEventHandler(keyPreImeEventHandler);
editText.setEditorActionEventHandler(EditorActionEventHandler);
editText.setInputConnectionEventHandler(inputConnectionEventHandler);
editText.setTextPastedEventHandler(textPastedEventHandler);
}

@OnUnmount
Expand Down Expand Up @@ -963,6 +967,7 @@ static void onUnbind(final ComponentContext c, EditTextWithEventHandlers editTex
if (SDK_INT >= M) {
editText.setCustomInsertionActionModeCallback(null);
}
editText.setTextPastedEventHandler(null);
}

@Nullable
Expand Down Expand Up @@ -1184,6 +1189,7 @@ static class EditTextWithEventHandlers extends EditText

private static final int UNMEASURED_LINE_COUNT = -1;

@Nullable private EventHandler<TextPastedEvent> mTextPastedEventHandler;
@Nullable private EventHandler<TextChangedEvent> mTextChangedEventHandler;
@Nullable private EventHandler<SelectionChangedEvent> mSelectionChangedEventHandler;
@Nullable private EventHandler<InputFocusChangedEvent> mInputFocusChangedEventHandler;
Expand All @@ -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;

Expand Down Expand Up @@ -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
Expand All @@ -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);
Expand Down Expand Up @@ -1307,6 +1327,11 @@ void setDisableAutofill(Boolean value) {
mDisableAutofill = value;
}

void setTextPastedEventHandler(
@Nullable EventHandler<TextPastedEvent> textPastedEventEventHandler) {
mTextPastedEventHandler = textPastedEventEventHandler;
}

void setTextChangedEventHandler(
@Nullable EventHandler<TextChangedEvent> textChangedEventHandler) {
mTextChangedEventHandler = textChangedEventHandler;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit 374e2a0

Please sign in to comment.