Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed problem with cursor jumping back to start when adding an emoji in edit mode #31611

Closed
wants to merge 5 commits into from
Closed
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
26 changes: 23 additions & 3 deletions src/libs/focusComposerWithDelay.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
import {TextInput} from 'react-native';
import {Platform, TextInput} from 'react-native';
import CONST from '@src/CONST';
import * as EmojiPickerAction from './actions/EmojiPickerAction';
import ComposerFocusManager from './ComposerFocusManager';

type Selection = {
start: number;
end: number;
};

type FocusComposerWithDelay = (shouldDelay?: boolean) => void;
/**
* Create a function that focuses the composer.
*/
function focusComposerWithDelay(textInput: TextInput | null): FocusComposerWithDelay {
function focusComposerWithDelay(textInput: TextInput | HTMLTextAreaElement | null): FocusComposerWithDelay {
/**
* Focus the text input
* @param [shouldDelay] Impose delay before focusing the text input
*/
return (shouldDelay = false) => {
return (shouldDelay = false, forceSetSelection?: Selection) => {
// There could be other animations running while we trigger manual focus.
// This prevents focus from making those animations janky.
if (!textInput || EmojiPickerAction.isEmojiPickerVisible()) {
Expand All @@ -20,13 +26,27 @@ function focusComposerWithDelay(textInput: TextInput | null): FocusComposerWithD

if (!shouldDelay) {
textInput.focus();
if (forceSetSelection) {
if (Platform.OS === CONST.PLATFORM.WEB) {
artus9033 marked this conversation as resolved.
Show resolved Hide resolved
(textInput as HTMLTextAreaElement).setSelectionRange(forceSetSelection.start, forceSetSelection.end);
} else {
(textInput as TextInput).setSelection(forceSetSelection.start, forceSetSelection.end);
}
}
return;
}
ComposerFocusManager.isReadyToFocus().then(() => {
if (!textInput) {
return;
}
textInput.focus();
if (forceSetSelection) {
if (Platform.OS === CONST.PLATFORM.WEB) {
(textInput as HTMLTextAreaElement).setSelectionRange(forceSetSelection.start, forceSetSelection.end);
} else {
(textInput as TextInput).setSelection(forceSetSelection.start, forceSetSelection.end);
}
}
});
};
}
Expand Down
26 changes: 21 additions & 5 deletions src/pages/home/report/ReportActionItemMessageEdit.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ const emojiButtonID = 'emojiButton';
const messageEditInput = 'messageEditInput';

const isMobileSafari = Browser.isMobileSafari();
const isMobileChrome = Browser.isMobileChrome();

function ReportActionItemMessageEdit(props) {
const theme = useTheme();
Expand All @@ -87,6 +88,8 @@ function ReportActionItemMessageEdit(props) {
const {isKeyboardShown} = useKeyboardState();
const {isSmallScreenWidth} = useWindowDimensions();

const emojiPickerSelectionRef = useRef(undefined);

const getInitialDraft = () => {
if (props.draftMessage === props.action.message[0].html) {
// We only convert the report action message to markdown if the draft message is unchanged.
Expand Down Expand Up @@ -335,10 +338,18 @@ function ReportActionItemMessageEdit(props) {
* @param {String} emoji
*/
const addEmojiToTextBox = (emoji) => {
setSelection((prevSelection) => ({
start: prevSelection.start + emoji.length + CONST.SPACE_LENGTH,
end: prevSelection.start + emoji.length + CONST.SPACE_LENGTH,
}));
const newSelection = {
start: selection.start + emoji.length + CONST.SPACE_LENGTH,
end: selection.start + emoji.length + CONST.SPACE_LENGTH,
};
setSelection(newSelection);

if (isMobileChrome) {
// immediately set the selection again on Chrome mobile after focusing the
// input which seems to change the cursor position for a brief moment
emojiPickerSelectionRef.current = newSelection;
}

updateDraft(ComposerUtils.insertText(draft, selection, `${emoji} `));
};

Expand Down Expand Up @@ -440,7 +451,12 @@ function ReportActionItemMessageEdit(props) {
<View style={styles.editChatItemEmojiWrapper}>
<EmojiPickerButton
isDisabled={props.shouldDisableEmojiPicker}
onModalHide={() => focus(true)}
onModalHide={() => {
const emojiPickerSelection = emojiPickerSelectionRef.current ? {...emojiPickerSelectionRef.current} : undefined;
emojiPickerSelectionRef.current = undefined;

focus(true, emojiPickerSelection);
}}
onEmojiSelected={addEmojiToTextBox}
id={emojiButtonID}
emojiPickerID={props.action.reportActionID}
Expand Down
Loading