Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/components/Composer/implementation/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type {MarkdownStyle} from '@expensify/react-native-live-markdown';
import {useIsFocused} from '@react-navigation/native';
import lodashDebounce from 'lodash/debounce';
import type {BaseSyntheticEvent, ForwardedRef} from 'react';
import React, {useCallback, useEffect, useImperativeHandle, useMemo, useRef, useState} from 'react';
Expand Down Expand Up @@ -252,7 +253,8 @@ function Composer(
// eslint-disable-next-line react-compiler/react-compiler, react-hooks/exhaustive-deps
}, [isComposerFullSize]);

useHtmlPaste(textInput, handlePaste, true);
const isActive = useIsFocused();
useHtmlPaste(textInput, handlePaste, isActive);

useEffect(() => {
setIsRendered(true);
Expand Down
3 changes: 3 additions & 0 deletions src/components/TextInput/BaseTextInput/index.native.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import Text from '@components/Text';
import * as styleConst from '@components/TextInput/styleConst';
import TextInputClearButton from '@components/TextInput/TextInputClearButton';
import TextInputLabel from '@components/TextInput/TextInputLabel';
import useHtmlPaste from '@hooks/useHtmlPaste';
import useLocalize from '@hooks/useLocalize';
import useMarkdownStyle from '@hooks/useMarkdownStyle';
import useStyleUtils from '@hooks/useStyleUtils';
Expand Down Expand Up @@ -99,6 +100,8 @@ function BaseTextInput(
const input = useRef<TextInput | null>(null);
const isLabelActive = useRef(initialActiveLabel);

useHtmlPaste(input, undefined, isMarkdownEnabled);

// AutoFocus which only works on mount:
useEffect(() => {
// We are manually managing focus to prevent this issue: https://github.com/Expensify/App/issues/4514
Expand Down
7 changes: 5 additions & 2 deletions src/components/TextInput/BaseTextInput/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {Str} from 'expensify-common';
import type {ForwardedRef} from 'react';
import type {ForwardedRef, MutableRefObject} from 'react';
import React, {forwardRef, useCallback, useEffect, useMemo, useRef, useState} from 'react';
import type {GestureResponderEvent, LayoutChangeEvent, NativeSyntheticEvent, StyleProp, TextInputFocusEventData, ViewStyle} from 'react-native';
import type {GestureResponderEvent, LayoutChangeEvent, NativeSyntheticEvent, StyleProp, TextInput, TextInputFocusEventData, ViewStyle} from 'react-native';
import {ActivityIndicator, StyleSheet, View} from 'react-native';
import {useSharedValue, withSpring} from 'react-native-reanimated';
import Checkbox from '@components/Checkbox';
Expand All @@ -18,6 +18,7 @@ import Text from '@components/Text';
import * as styleConst from '@components/TextInput/styleConst';
import TextInputClearButton from '@components/TextInput/TextInputClearButton';
import TextInputLabel from '@components/TextInput/TextInputLabel';
import useHtmlPaste from '@hooks/useHtmlPaste';
import useLocalize from '@hooks/useLocalize';
import useMarkdownStyle from '@hooks/useMarkdownStyle';
import useStyleUtils from '@hooks/useStyleUtils';
Expand Down Expand Up @@ -107,6 +108,8 @@ function BaseTextInput(
const isLabelActive = useRef(initialActiveLabel);
const didScrollToEndRef = useRef(false);

useHtmlPaste(input as MutableRefObject<TextInput | null>, undefined, isMarkdownEnabled);

// AutoFocus which only works on mount:
useEffect(() => {
// We are manually managing focus to prevent this issue: https://github.com/Expensify/App/issues/4514
Expand Down
22 changes: 4 additions & 18 deletions src/hooks/useHtmlPaste/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import {useNavigation} from '@react-navigation/native';
import {useCallback, useEffect} from 'react';
import Parser from '@libs/Parser';
import CONST from '@src/CONST';
Expand Down Expand Up @@ -38,9 +37,7 @@ const insertAtCaret = (target: HTMLElement, insertedText: string, maxLength: num
}
};

const useHtmlPaste: UseHtmlPaste = (textInputRef, preHtmlPasteCallback, removeListenerOnScreenBlur = false, maxLength = CONST.MAX_COMMENT_LENGTH + 1) => {
const navigation = useNavigation();

const useHtmlPaste: UseHtmlPaste = (textInputRef, preHtmlPasteCallback, isActive = false, maxLength = CONST.MAX_COMMENT_LENGTH + 1) => {
/**
* Set pasted text to clipboard
* @param {String} text
Expand Down Expand Up @@ -145,27 +142,16 @@ const useHtmlPaste: UseHtmlPaste = (textInputRef, preHtmlPasteCallback, removeLi
);

useEffect(() => {
// we need to re-register listener on navigation focus/blur if the component (like Composer) is not unmounting
// when navigating away to different screen (report) to avoid paste event on other screen being wrongly handled
// by current screen paste listener
let unsubscribeFocus: () => void;
let unsubscribeBlur: () => void;
if (removeListenerOnScreenBlur) {
unsubscribeFocus = navigation.addListener('focus', () => document.addEventListener('paste', handlePaste, true));
unsubscribeBlur = navigation.addListener('blur', () => document.removeEventListener('paste', handlePaste, true));
if (!isActive) {
return;
}

document.addEventListener('paste', handlePaste, true);

return () => {
if (removeListenerOnScreenBlur) {
unsubscribeFocus();
unsubscribeBlur();
}
document.removeEventListener('paste', handlePaste, true);
};
// eslint-disable-next-line react-compiler/react-compiler, react-hooks/exhaustive-deps
}, []);
}, [isActive]);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The changes needed to either add the handlePaste dependency here or retrigger this effect some way when handlePaste changes. Fixed here #70279

};

export default useHtmlPaste;
2 changes: 1 addition & 1 deletion src/hooks/useHtmlPaste/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type {TextInput} from 'react-native';
type UseHtmlPaste = (
textInputRef: MutableRefObject<(HTMLTextAreaElement & TextInput) | TextInput | null>,
preHtmlPasteCallback?: (event: ClipboardEvent) => boolean,
removeListenerOnScreenBlur?: boolean,
isActive?: boolean,
maxLength?: number, // Maximum length of the text input value after pasting
) => void;

Expand Down
3 changes: 0 additions & 3 deletions src/pages/PrivateNotes/PrivateNotesEditPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import type {AnimatedTextInputRef} from '@components/RNTextInput';
import ScreenWrapper from '@components/ScreenWrapper';
import Text from '@components/Text';
import TextInput from '@components/TextInput';
import useHtmlPaste from '@hooks/useHtmlPaste';
import useLocalize from '@hooks/useLocalize';
import useThemeStyles from '@hooks/useThemeStyles';
import Navigation from '@libs/Navigation/Navigation';
Expand Down Expand Up @@ -66,8 +65,6 @@ function PrivateNotesEditPage({route, report, accountID}: PrivateNotesEditPagePr
const privateNotesInput = useRef<AnimatedTextInputRef | null>(null);
const focusTimeoutRef = useRef<NodeJS.Timeout | null>(null);

useHtmlPaste(privateNotesInput);

useFocusEffect(
useCallback(() => {
focusTimeoutRef.current = setTimeout(() => {
Expand Down