From 4602fb916fa781f0bcd3bce99cf575b683467832 Mon Sep 17 00:00:00 2001 From: Mattermost Build Date: Mon, 5 Oct 2020 16:18:04 -0400 Subject: [PATCH] Emit PASTE_FILES event only from last subscribed input (#4872) (#4875) (cherry picked from commit ae43ba11870954707b75a4e3bf710bec15469ee0) Co-authored-by: Miguel Alatzar --- .../rnbeta/RNPasteableActionCallback.java | 7 ++++++- app/components/pasteable_text_input/index.js | 10 ++++++++++ .../pasteable_text_input/index.test.js | 17 +++++++++++++++-- 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/android/app/src/main/java/com/mattermost/rnbeta/RNPasteableActionCallback.java b/android/app/src/main/java/com/mattermost/rnbeta/RNPasteableActionCallback.java index 44ba12c3d..6ebe0dd82 100644 --- a/android/app/src/main/java/com/mattermost/rnbeta/RNPasteableActionCallback.java +++ b/android/app/src/main/java/com/mattermost/rnbeta/RNPasteableActionCallback.java @@ -82,7 +82,12 @@ public class RNPasteableActionCallback implements ActionMode.Callback { return null; } - String text = item.getText().toString(); + CharSequence chars = item.getText(); + if (chars == null) { + return null; + } + + String text = chars.toString(); if (text.length() > 0) { return null; } diff --git a/app/components/pasteable_text_input/index.js b/app/components/pasteable_text_input/index.js index 01ea5ec7e..b8ac6e917 100644 --- a/app/components/pasteable_text_input/index.js +++ b/app/components/pasteable_text_input/index.js @@ -27,7 +27,17 @@ export class PasteableTextInput extends React.PureComponent { } } + getLastSubscriptionKey = () => { + const subscriptions = OnPasteEventEmitter._subscriber._subscriptionsForType.onPaste?.filter((sub) => sub); // eslint-disable-line no-underscore-dangle + return subscriptions?.length && subscriptions[subscriptions.length - 1].key; + } + onPaste = (event) => { + const lastSubscriptionKey = this.getLastSubscriptionKey(); + if (this.subscription.key !== lastSubscriptionKey) { + return; + } + let data = null; let error = null; diff --git a/app/components/pasteable_text_input/index.test.js b/app/components/pasteable_text_input/index.test.js index bf6631207..237077854 100644 --- a/app/components/pasteable_text_input/index.test.js +++ b/app/components/pasteable_text_input/index.test.js @@ -12,6 +12,8 @@ import {PasteableTextInput} from './index'; const nativeEventEmitter = new NativeEventEmitter(); describe('PasteableTextInput', () => { + const emit = jest.spyOn(EventEmitter, 'emit'); + test('should render pasteable text input', () => { const onPaste = jest.fn(); const text = 'My Text'; @@ -24,12 +26,11 @@ describe('PasteableTextInput', () => { test('should call onPaste props if native onPaste trigger', () => { const event = {someData: 'data'}; const text = 'My Text'; - const onPaste = jest.spyOn(EventEmitter, 'emit'); shallow( {text}, ); nativeEventEmitter.emit('onPaste', event); - expect(onPaste).toHaveBeenCalledWith(PASTE_FILES, null, event); + expect(emit).toHaveBeenCalledWith(PASTE_FILES, null, event); }); test('should remove onPaste listener when unmount', () => { @@ -43,4 +44,16 @@ describe('PasteableTextInput', () => { component.instance().componentWillUnmount(); expect(mockRemove).toHaveBeenCalled(); }); + + test('should emit PASTE_FILES event only for last subscription', () => { + const component1 = shallow(); + const instance1 = component1.instance(); + const component2 = shallow(); + const instance2 = component2.instance(); + + instance1.onPaste(); + expect(emit).not.toHaveBeenCalled(); + instance2.onPaste(); + expect(emit).toHaveBeenCalledTimes(1); + }); });