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

Fix problem with cursor jumping back to start when adding an emoji in edit mode #37666

Merged
merged 5 commits into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
34 changes: 0 additions & 34 deletions src/libs/focusComposerWithDelay.ts

This file was deleted.

52 changes: 52 additions & 0 deletions src/libs/focusComposerWithDelay/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import type {TextInput} from 'react-native';
import ComposerFocusManager from '@libs/ComposerFocusManager';
import shouldSetSelectionRange from '@libs/shouldSetSelectionRange';
import * as EmojiPickerAction from '@userActions/EmojiPickerAction';
import type {FocusComposerWithDelay} from './types';

const setSelectionRange = shouldSetSelectionRange();

/**
* Create a function that focuses the composer.
*/
function focusComposerWithDelay(textInput: TextInput | HTMLTextAreaElement | null): FocusComposerWithDelay {
/**
* Focus the text input
* @param [shouldDelay] Impose delay before focusing the text input
* @param [forceSetSelection] Force selection range of text input
*/
return (shouldDelay = false, forceSetSelection = undefined) => {
// There could be other animations running while we trigger manual focus.
// This prevents focus from making those animations janky.
if (!textInput || EmojiPickerAction.isEmojiPickerVisible()) {
return;
}

if (!shouldDelay) {
textInput.focus();
if (forceSetSelection) {
if (setSelectionRange) {
(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 (setSelectionRange) {
(textInput as HTMLTextAreaElement).setSelectionRange(forceSetSelection.start, forceSetSelection.end);
} else {
(textInput as TextInput).setSelection(forceSetSelection.start, forceSetSelection.end);
}
}
});
};
}

export default focusComposerWithDelay;
8 changes: 8 additions & 0 deletions src/libs/focusComposerWithDelay/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
type Selection = {
start: number;
end: number;
};

type FocusComposerWithDelay = (shouldDelay?: boolean, forceSetSelection?: Selection) => void;

export type {Selection, FocusComposerWithDelay};
5 changes: 5 additions & 0 deletions src/libs/shouldSetSelectionRange/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import type ShouldSetSelectionRange from './types';

const shouldSetSelectionRange: ShouldSetSelectionRange = () => false;

export default shouldSetSelectionRange;
5 changes: 5 additions & 0 deletions src/libs/shouldSetSelectionRange/index.website.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import type ShouldSetSelectionRange from './types';

const shouldSetSelectionRange: ShouldSetSelectionRange = () => true;

export default shouldSetSelectionRange;
3 changes: 3 additions & 0 deletions src/libs/shouldSetSelectionRange/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
type ShouldSetSelectionRange = () => boolean;

export default ShouldSetSelectionRange;
31 changes: 22 additions & 9 deletions src/pages/home/report/ReportActionItemMessageEdit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import * as Browser from '@libs/Browser';
import * as ComposerUtils from '@libs/ComposerUtils';
import * as EmojiUtils from '@libs/EmojiUtils';
import focusComposerWithDelay from '@libs/focusComposerWithDelay';
import type {Selection} from '@libs/focusComposerWithDelay/types';
import focusEditAfterCancelDelete from '@libs/focusEditAfterCancelDelete';
import onyxSubscribe from '@libs/onyxSubscribe';
import ReportActionComposeFocusManager from '@libs/ReportActionComposeFocusManager';
Expand All @@ -40,6 +41,7 @@ import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type * as OnyxTypes from '@src/types/onyx';
import * as ReportActionContextMenu from './ContextMenu/ReportActionContextMenu';
import shouldUseEmojiPickerSelection from './shouldUseEmojiPickerSelection';

type ReportActionItemMessageEditProps = {
/** All the data of the action */
Expand Down Expand Up @@ -105,10 +107,7 @@ function ReportActionItemMessageEdit(
}
return initialDraft;
});
const [selection, setSelection] = useState<{
start: number;
end: number;
}>(getInitialSelection);
const [selection, setSelection] = useState<Selection>(getInitialSelection);
const [isFocused, setIsFocused] = useState<boolean>(false);
const {hasExceededMaxCommentLength, validateCommentMaxLength} = useHandleExceedMaxCommentLength();
const [modal, setModal] = useState<OnyxTypes.Modal>({
Expand All @@ -121,6 +120,7 @@ function ReportActionItemMessageEdit(
const isFocusedRef = useRef<boolean>(false);
const insertedEmojis = useRef<Emoji[]>([]);
const draftRef = useRef(draft);
const emojiPickerSelectionRef = useRef<Selection | undefined>(undefined);

useEffect(() => {
if (ReportActionsUtils.isDeletedAction(action) || (action.message && draftMessage === action.message[0].html)) {
Expand Down Expand Up @@ -329,14 +329,22 @@ function ReportActionItemMessageEdit(
deleteDraft();
}, [action, debouncedSaveDraft, deleteDraft, draft, reportID]);

const shouldUseEmojiPickerSelectionRef = shouldUseEmojiPickerSelection();
/**
* @param emoji
*/
const addEmojiToTextBox = (emoji: string) => {
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 (shouldUseEmojiPickerSelectionRef) {
// immediately set the selection again on android and 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 @@ -450,7 +458,12 @@ function ReportActionItemMessageEdit(
<View style={styles.editChatItemEmojiWrapper}>
<EmojiPickerButton
isDisabled={shouldDisableEmojiPicker}
onModalHide={() => focus(true)}
onModalHide={() => {
const emojiPickerSelection = emojiPickerSelectionRef.current ? {...emojiPickerSelectionRef.current} : undefined;
emojiPickerSelectionRef.current = undefined;

focus(true, emojiPickerSelection);
}}
onEmojiSelected={addEmojiToTextBox}
id={emojiButtonID}
emojiPickerID={action.reportActionID}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import type ShouldUseEmojiPickerSelection from './types';

const shouldUseEmojiPickerSelection: ShouldUseEmojiPickerSelection = () => true;

export default shouldUseEmojiPickerSelection;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import type ShouldUseEmojiPickerSelection from './types';

const shouldUseEmojiPickerSelection: ShouldUseEmojiPickerSelection = () => false;

export default shouldUseEmojiPickerSelection;
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import * as Browser from '@libs/Browser';
import type ShouldUseEmojiPickerSelection from './types';

const isMobileChrome = Browser.isMobileChrome();

const shouldUseEmojiPickerSelection: ShouldUseEmojiPickerSelection = () => isMobileChrome;

export default shouldUseEmojiPickerSelection;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
type ShouldUseEmojiPickerSelection = () => boolean;

export default ShouldUseEmojiPickerSelection;
Loading