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 29405 cursor displayed before emoji on ios native #30835

Merged
merged 15 commits into from
Dec 28, 2023
Merged
Changes from 11 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
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {useIsFocused, useNavigation} from '@react-navigation/native';
import lodashGet from 'lodash/get';
import React, {useCallback, useEffect, useImperativeHandle, useMemo, useRef, useState} from 'react';
import {findNodeHandle, NativeModules, View} from 'react-native';
import {findNodeHandle, InteractionManager, NativeModules, Platform, View} from 'react-native';
import {withOnyx} from 'react-native-onyx';
import _ from 'underscore';
import Composer from '@components/Composer';
Expand Down Expand Up @@ -39,6 +39,8 @@ import {defaultProps, propTypes} from './composerWithSuggestionsProps';

const {RNTextInputReset} = NativeModules;

const isIOSNative = Platform.OS === 'ios';

/**
* Broadcast that the user is typing. Debounced to limit how often we publish client events.
* @param {String} reportID
Expand Down Expand Up @@ -133,6 +135,8 @@ function ComposerWithSuggestions({
const textInputRef = useRef(null);
const insertedEmojisRef = useRef([]);

const syncSelectionWithOnChangeTextRef = useRef(null);

// A flag to indicate whether the onScroll callback is likely triggered by a layout change (caused by text change) or not
const isScrollLikelyLayoutTriggered = useRef(false);
const suggestions = lodashGet(suggestionsRef, 'current.getSuggestions', () => [])();
Expand Down Expand Up @@ -232,6 +236,11 @@ function ComposerWithSuggestions({
setValue(newCommentConverted);
if (commentValue !== newComment) {
const position = Math.max(selection.end + (newComment.length - commentRef.current.length), cursorPosition || 0);

if (isIOSNative) {
syncSelectionWithOnChangeTextRef.current = {position, value: newComment};
}

setSelection({
start: position,
end: position,
Expand Down Expand Up @@ -535,7 +544,27 @@ function ComposerWithSuggestions({
ref={setTextInputRef}
placeholder={inputPlaceholder}
placeholderTextColor={theme.placeholderText}
onChangeText={(commentValue) => updateComment(commentValue, true)}
onChangeText={(commentValue) => {
if (syncSelectionWithOnChangeTextRef.current !== null) {
setSelection({
start: syncSelectionWithOnChangeTextRef.current.position,
end: syncSelectionWithOnChangeTextRef.current.position,
});
}

updateComment(commentValue, true);

if (isIOSNative && syncSelectionWithOnChangeTextRef.current !== null) {
const positionSnapshot = syncSelectionWithOnChangeTextRef.current.position;
syncSelectionWithOnChangeTextRef.current = null;

InteractionManager.runAfterInteractions(() => {
// note: this implementation is only available on non-web RN, thus the wrapping
// 'if' block contains a redundant (since the ref is only used on iOS) platform check
textInputRef.current.setSelection(positionSnapshot, positionSnapshot);
});
}
}}
onKeyPress={triggerHotkeyActions}
textAlignVertical="top"
style={[styles.textInputCompose, isComposerFullSize ? styles.textInputFullCompose : styles.textInputCollapseCompose]}
Expand Down