Message Input: Fix auto-focus when forwarding

This commit is contained in:
Alexander Zinchuk 2021-12-13 15:37:06 +01:00
parent eb05b49dd6
commit 9a66de1a05
3 changed files with 25 additions and 13 deletions

View File

@ -28,6 +28,8 @@ import EmojiTooltip from './EmojiTooltip.async';
import './AttachmentModal.scss'; import './AttachmentModal.scss';
export type OwnProps = { export type OwnProps = {
chatId: string;
threadId: number;
attachments: ApiAttachment[]; attachments: ApiAttachment[];
caption: string; caption: string;
isReady?: boolean; isReady?: boolean;
@ -47,6 +49,8 @@ export type OwnProps = {
const DROP_LEAVE_TIMEOUT_MS = 150; const DROP_LEAVE_TIMEOUT_MS = 150;
const AttachmentModal: FC<OwnProps> = ({ const AttachmentModal: FC<OwnProps> = ({
chatId,
threadId,
attachments, attachments,
caption, caption,
isReady, isReady,
@ -246,13 +250,15 @@ const AttachmentModal: FC<OwnProps> = ({
/> />
<MessageInput <MessageInput
id="caption-input-text" id="caption-input-text"
chatId={chatId}
threadId={threadId}
isAttachmentModalInput isAttachmentModalInput
html={caption} html={caption}
editableInputId={EDITABLE_INPUT_MODAL_ID} editableInputId={EDITABLE_INPUT_MODAL_ID}
placeholder={lang('Caption')} placeholder={lang('Caption')}
onUpdate={onCaptionUpdate} onUpdate={onCaptionUpdate}
onSend={onSend} onSend={onSend}
shouldSetFocus={Boolean(attachments.length)} canAutoFocus={Boolean(isReady && attachments.length)}
/> />
</div> </div>
</div> </div>

View File

@ -799,6 +799,8 @@ const Composer: FC<OwnProps & StateProps & DispatchProps> = ({
</Portal> </Portal>
)} )}
<AttachmentModal <AttachmentModal
chatId={chatId}
threadId={threadId}
attachments={attachments} attachments={attachments}
caption={attachments.length ? html : ''} caption={attachments.length ? html : ''}
groupChatMembers={groupChatMembers} groupChatMembers={groupChatMembers}
@ -899,6 +901,8 @@ const Composer: FC<OwnProps & StateProps & DispatchProps> = ({
)} )}
<MessageInput <MessageInput
id="message-input-text" id="message-input-text"
chatId={chatId}
threadId={threadId}
html={!attachments.length ? html : ''} html={!attachments.length ? html : ''}
placeholder={ placeholder={
activeVoiceRecording && windowWidth <= SCREEN_WIDTH_TO_HIDE_PLACEHOLDER activeVoiceRecording && windowWidth <= SCREEN_WIDTH_TO_HIDE_PLACEHOLDER
@ -906,7 +910,7 @@ const Composer: FC<OwnProps & StateProps & DispatchProps> = ({
: botKeyboardPlaceholder || lang('Message') : botKeyboardPlaceholder || lang('Message')
} }
forcedPlaceholder={inlineBotHelp} forcedPlaceholder={inlineBotHelp}
shouldSetFocus={!attachments.length} canAutoFocus={isReady && !attachments.length}
shouldSuppressFocus={IS_SINGLE_COLUMN_LAYOUT && isSymbolMenuOpen} shouldSuppressFocus={IS_SINGLE_COLUMN_LAYOUT && isSymbolMenuOpen}
shouldSuppressTextFormatter={isEmojiTooltipOpen || isMentionTooltipOpen || isInlineBotTooltipOpen} shouldSuppressTextFormatter={isEmojiTooltipOpen || isMentionTooltipOpen || isInlineBotTooltipOpen}
onUpdate={setHtml} onUpdate={setHtml}

View File

@ -8,7 +8,7 @@ import { GlobalActions } from '../../../global/types';
import { IAnchorPosition, ISettings } from '../../../types'; import { IAnchorPosition, ISettings } from '../../../types';
import { EDITABLE_INPUT_ID } from '../../../config'; import { EDITABLE_INPUT_ID } from '../../../config';
import { selectCurrentMessageList, selectReplyingToId } from '../../../modules/selectors'; import { selectReplyingToId } from '../../../modules/selectors';
import { debounce } from '../../../util/schedulers'; import { debounce } from '../../../util/schedulers';
import focusEditableElement from '../../../util/focusEditableElement'; import focusEditableElement from '../../../util/focusEditableElement';
import buildClassName from '../../../util/buildClassName'; import buildClassName from '../../../util/buildClassName';
@ -34,12 +34,14 @@ const TRANSITION_DURATION_FACTOR = 50;
type OwnProps = { type OwnProps = {
id: string; id: string;
chatId: string;
threadId: number;
isAttachmentModalInput?: boolean; isAttachmentModalInput?: boolean;
editableInputId?: string; editableInputId?: string;
html: string; html: string;
placeholder: string; placeholder: string;
forcedPlaceholder?: string; forcedPlaceholder?: string;
shouldSetFocus: boolean; canAutoFocus: boolean;
shouldSuppressFocus?: boolean; shouldSuppressFocus?: boolean;
shouldSuppressTextFormatter?: boolean; shouldSuppressTextFormatter?: boolean;
onUpdate: (html: string) => void; onUpdate: (html: string) => void;
@ -48,7 +50,7 @@ type OwnProps = {
}; };
type StateProps = { type StateProps = {
currentChatId?: string; chatId?: string;
replyingToId?: number; replyingToId?: number;
noTabCapture?: boolean; noTabCapture?: boolean;
messageSendKeyCombo?: ISettings['messageSendKeyCombo']; messageSendKeyCombo?: ISettings['messageSendKeyCombo'];
@ -77,18 +79,18 @@ function clearSelection() {
const MessageInput: FC<OwnProps & StateProps & DispatchProps> = ({ const MessageInput: FC<OwnProps & StateProps & DispatchProps> = ({
id, id,
chatId,
isAttachmentModalInput, isAttachmentModalInput,
editableInputId, editableInputId,
html, html,
placeholder, placeholder,
forcedPlaceholder, forcedPlaceholder,
shouldSetFocus, canAutoFocus,
shouldSuppressFocus, shouldSuppressFocus,
shouldSuppressTextFormatter, shouldSuppressTextFormatter,
onUpdate, onUpdate,
onSuppressedFocus, onSuppressedFocus,
onSend, onSend,
currentChatId,
replyingToId, replyingToId,
noTabCapture, noTabCapture,
messageSendKeyCombo, messageSendKeyCombo,
@ -125,6 +127,8 @@ const MessageInput: FC<OwnProps & StateProps & DispatchProps> = ({
} }
}, [html]); }, [html]);
const chatIdRef = useRef(chatId);
chatIdRef.current = chatId;
const focusInput = useCallback(() => { const focusInput = useCallback(() => {
if (isHeavyAnimating()) { if (isHeavyAnimating()) {
setTimeout(focusInput, FOCUS_DELAY_MS); setTimeout(focusInput, FOCUS_DELAY_MS);
@ -335,10 +339,10 @@ const MessageInput: FC<OwnProps & StateProps & DispatchProps> = ({
return; return;
} }
if (shouldSetFocus) { if (canAutoFocus) {
focusInput(); focusInput();
} }
}, [currentChatId, focusInput, replyingToId, shouldSetFocus]); }, [chatId, focusInput, replyingToId, canAutoFocus]);
useEffect(() => { useEffect(() => {
if (noTabCapture) { if (noTabCapture) {
@ -407,14 +411,12 @@ const MessageInput: FC<OwnProps & StateProps & DispatchProps> = ({
}; };
export default memo(withGlobal<OwnProps>( export default memo(withGlobal<OwnProps>(
(global): StateProps => { (global, { chatId, threadId }: OwnProps): StateProps => {
const { chatId: currentChatId, threadId } = selectCurrentMessageList(global) || {};
const { messageSendKeyCombo } = global.settings.byKey; const { messageSendKeyCombo } = global.settings.byKey;
return { return {
currentChatId,
messageSendKeyCombo, messageSendKeyCombo,
replyingToId: currentChatId && threadId ? selectReplyingToId(global, currentChatId, threadId) : undefined, replyingToId: chatId && threadId ? selectReplyingToId(global, chatId, threadId) : undefined,
noTabCapture: global.isPollModalOpen || global.payment.isPaymentModalOpen, noTabCapture: global.isPollModalOpen || global.payment.isPaymentModalOpen,
}; };
}, },