2022-08-26 06:21:41 +00:00
|
|
|
|
import React, { Component, RefObject } from 'react';
|
2022-08-11 13:32:44 +00:00
|
|
|
|
import { WithTranslation } from 'react-i18next';
|
2018-08-29 17:24:25 +00:00
|
|
|
|
|
2022-10-20 09:11:27 +00:00
|
|
|
|
import { IReduxState, IStore } from '../../../app/types';
|
2021-03-18 07:08:34 +00:00
|
|
|
|
import { isMobileBrowser } from '../../../base/environment/utils';
|
2022-08-11 13:32:44 +00:00
|
|
|
|
import { translate } from '../../../base/i18n/functions';
|
2022-09-06 17:32:20 +00:00
|
|
|
|
import { IconPlane, IconSmile } from '../../../base/icons/svg';
|
2022-08-11 13:32:44 +00:00
|
|
|
|
import { connect } from '../../../base/redux/functions';
|
|
|
|
|
import Button from '../../../base/ui/components/web/Button';
|
|
|
|
|
import Input from '../../../base/ui/components/web/Input';
|
2021-09-09 22:46:28 +00:00
|
|
|
|
import { areSmileysDisabled } from '../../functions';
|
2019-03-21 16:38:29 +00:00
|
|
|
|
|
2022-08-11 13:32:44 +00:00
|
|
|
|
// @ts-ignore
|
2018-08-29 17:24:25 +00:00
|
|
|
|
import SmileysPanel from './SmileysPanel';
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The type of the React {@code Component} props of {@link ChatInput}.
|
|
|
|
|
*/
|
2022-10-20 09:11:27 +00:00
|
|
|
|
interface IProps extends WithTranslation {
|
2022-08-11 13:32:44 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Whether chat emoticons are disabled.
|
|
|
|
|
*/
|
2022-09-08 09:52:36 +00:00
|
|
|
|
_areSmileysDisabled: boolean;
|
2018-08-29 17:24:25 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Invoked to send chat messages.
|
|
|
|
|
*/
|
2022-10-11 10:47:54 +00:00
|
|
|
|
dispatch: IStore['dispatch'];
|
2018-08-29 17:24:25 +00:00
|
|
|
|
|
2019-10-07 12:35:04 +00:00
|
|
|
|
/**
|
|
|
|
|
* Callback to invoke on message send.
|
|
|
|
|
*/
|
2022-09-08 09:52:36 +00:00
|
|
|
|
onSend: Function;
|
2022-08-11 13:32:44 +00:00
|
|
|
|
}
|
2018-08-29 17:24:25 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The type of the React {@code Component} state of {@link ChatInput}.
|
|
|
|
|
*/
|
|
|
|
|
type State = {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* User provided nickname when the input text is provided in the view.
|
|
|
|
|
*/
|
2022-09-08 09:52:36 +00:00
|
|
|
|
message: string;
|
2018-08-29 17:24:25 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Whether or not the smiley selector is visible.
|
|
|
|
|
*/
|
2022-09-08 09:52:36 +00:00
|
|
|
|
showSmileysPanel: boolean;
|
2018-08-29 17:24:25 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Implements a React Component for drafting and submitting a chat message.
|
|
|
|
|
*
|
2021-11-04 21:10:43 +00:00
|
|
|
|
* @augments Component
|
2018-08-29 17:24:25 +00:00
|
|
|
|
*/
|
2022-10-20 09:11:27 +00:00
|
|
|
|
class ChatInput extends Component<IProps, State> {
|
2022-08-26 06:21:41 +00:00
|
|
|
|
_textArea?: RefObject<HTMLTextAreaElement>;
|
2018-08-29 17:24:25 +00:00
|
|
|
|
|
|
|
|
|
state = {
|
|
|
|
|
message: '',
|
|
|
|
|
showSmileysPanel: false
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Initializes a new {@code ChatInput} instance.
|
|
|
|
|
*
|
|
|
|
|
* @param {Object} props - The read-only properties with which the new
|
|
|
|
|
* instance is to be initialized.
|
|
|
|
|
*/
|
2022-10-20 09:11:27 +00:00
|
|
|
|
constructor(props: IProps) {
|
2018-08-29 17:24:25 +00:00
|
|
|
|
super(props);
|
|
|
|
|
|
2022-08-26 06:21:41 +00:00
|
|
|
|
this._textArea = React.createRef<HTMLTextAreaElement>();
|
2018-08-29 17:24:25 +00:00
|
|
|
|
|
|
|
|
|
// Bind event handlers so they are only bound once for every instance.
|
|
|
|
|
this._onDetectSubmit = this._onDetectSubmit.bind(this);
|
|
|
|
|
this._onMessageChange = this._onMessageChange.bind(this);
|
|
|
|
|
this._onSmileySelect = this._onSmileySelect.bind(this);
|
2021-02-23 07:39:20 +00:00
|
|
|
|
this._onSubmitMessage = this._onSubmitMessage.bind(this);
|
2022-08-11 13:32:44 +00:00
|
|
|
|
this._toggleSmileysPanel = this._toggleSmileysPanel.bind(this);
|
2018-08-29 17:24:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Implements React's {@link Component#componentDidMount()}.
|
|
|
|
|
*
|
|
|
|
|
* @inheritdoc
|
|
|
|
|
*/
|
|
|
|
|
componentDidMount() {
|
2021-03-18 07:08:34 +00:00
|
|
|
|
if (isMobileBrowser()) {
|
|
|
|
|
// Ensure textarea is not focused when opening chat on mobile browser.
|
2022-08-26 06:21:41 +00:00
|
|
|
|
this._textArea?.current && this._textArea.current.blur();
|
2021-03-18 07:08:34 +00:00
|
|
|
|
}
|
2018-08-29 17:24:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Implements React's {@link Component#render()}.
|
|
|
|
|
*
|
|
|
|
|
* @inheritdoc
|
|
|
|
|
* @returns {ReactElement}
|
|
|
|
|
*/
|
|
|
|
|
render() {
|
|
|
|
|
return (
|
2021-02-23 07:39:20 +00:00
|
|
|
|
<div className = { `chat-input-container${this.state.message.trim().length ? ' populated' : ''}` }>
|
|
|
|
|
<div id = 'chat-input' >
|
2022-08-11 13:32:44 +00:00
|
|
|
|
{!this.props._areSmileysDisabled && this.state.showSmileysPanel && (
|
|
|
|
|
<div
|
|
|
|
|
className = 'smiley-input'>
|
2021-09-09 22:46:28 +00:00
|
|
|
|
<div
|
2022-08-11 13:32:44 +00:00
|
|
|
|
className = 'smileys-panel' >
|
2021-09-09 22:46:28 +00:00
|
|
|
|
<SmileysPanel
|
|
|
|
|
onSmileySelect = { this._onSmileySelect } />
|
|
|
|
|
</div>
|
2021-02-23 07:39:20 +00:00
|
|
|
|
</div>
|
2022-08-11 13:32:44 +00:00
|
|
|
|
)}
|
|
|
|
|
<Input
|
|
|
|
|
autoFocus = { true }
|
|
|
|
|
className = 'chat-input'
|
|
|
|
|
icon = { this.props._areSmileysDisabled ? undefined : IconSmile }
|
|
|
|
|
iconClick = { this._toggleSmileysPanel }
|
|
|
|
|
maxRows = { 5 }
|
|
|
|
|
onChange = { this._onMessageChange }
|
|
|
|
|
onKeyPress = { this._onDetectSubmit }
|
|
|
|
|
placeholder = { this.props.t('chat.messagebox') }
|
2022-08-26 06:21:41 +00:00
|
|
|
|
ref = { this._textArea }
|
2022-08-11 13:32:44 +00:00
|
|
|
|
textarea = { true }
|
|
|
|
|
value = { this.state.message } />
|
|
|
|
|
<Button
|
|
|
|
|
accessibilityLabel = { this.props.t('chat.sendButton') }
|
|
|
|
|
disabled = { !this.state.message.trim() }
|
|
|
|
|
icon = { IconPlane }
|
|
|
|
|
onClick = { this._onSubmitMessage }
|
|
|
|
|
size = { isMobileBrowser() ? 'large' : 'medium' } />
|
2018-08-29 17:24:25 +00:00
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Place cursor focus on this component's text area.
|
|
|
|
|
*
|
2019-05-07 22:33:12 +00:00
|
|
|
|
* @private
|
2018-08-29 17:24:25 +00:00
|
|
|
|
* @returns {void}
|
|
|
|
|
*/
|
2019-05-07 22:33:12 +00:00
|
|
|
|
_focus() {
|
2022-08-26 06:21:41 +00:00
|
|
|
|
this._textArea?.current && this._textArea.current.focus();
|
2018-08-29 17:24:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-02-23 07:39:20 +00:00
|
|
|
|
/**
|
|
|
|
|
* Submits the message to the chat window.
|
|
|
|
|
*
|
|
|
|
|
* @returns {void}
|
|
|
|
|
*/
|
|
|
|
|
_onSubmitMessage() {
|
|
|
|
|
const trimmed = this.state.message.trim();
|
|
|
|
|
|
|
|
|
|
if (trimmed) {
|
|
|
|
|
this.props.onSend(trimmed);
|
|
|
|
|
|
|
|
|
|
this.setState({ message: '' });
|
2021-03-18 07:08:34 +00:00
|
|
|
|
|
|
|
|
|
// Keep the textarea in focus when sending messages via submit button.
|
|
|
|
|
this._focus();
|
2021-02-23 07:39:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
2018-08-29 17:24:25 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Detects if enter has been pressed. If so, submit the message in the chat
|
|
|
|
|
* window.
|
|
|
|
|
*
|
|
|
|
|
* @param {string} event - Keyboard event.
|
|
|
|
|
* @private
|
|
|
|
|
* @returns {void}
|
|
|
|
|
*/
|
2022-08-11 13:32:44 +00:00
|
|
|
|
_onDetectSubmit(event: any) {
|
2022-04-07 14:03:07 +00:00
|
|
|
|
// Composition events used to add accents to characters
|
|
|
|
|
// despite their absence from standard US keyboards,
|
|
|
|
|
// to build up logograms of many Asian languages
|
|
|
|
|
// from their base components or categories and so on.
|
|
|
|
|
if (event.isComposing || event.keyCode === 229) {
|
|
|
|
|
// keyCode 229 means that user pressed some button,
|
|
|
|
|
// but input method is still processing that.
|
|
|
|
|
// This is a standard behavior for some input methods
|
|
|
|
|
// like entering japanese or сhinese hieroglyphs.
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-10 12:48:44 +00:00
|
|
|
|
if (event.key === 'Enter'
|
|
|
|
|
&& event.shiftKey === false
|
|
|
|
|
&& event.ctrlKey === false) {
|
2018-08-29 17:24:25 +00:00
|
|
|
|
event.preventDefault();
|
2021-06-10 12:48:44 +00:00
|
|
|
|
event.stopPropagation();
|
2018-08-29 17:24:25 +00:00
|
|
|
|
|
2021-02-23 07:39:20 +00:00
|
|
|
|
this._onSubmitMessage();
|
2018-08-29 17:24:25 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Updates the known message the user is drafting.
|
|
|
|
|
*
|
2022-08-11 13:32:44 +00:00
|
|
|
|
* @param {string} value - Keyboard event.
|
2018-08-29 17:24:25 +00:00
|
|
|
|
* @private
|
|
|
|
|
* @returns {void}
|
|
|
|
|
*/
|
2022-08-11 13:32:44 +00:00
|
|
|
|
_onMessageChange(value: string) {
|
|
|
|
|
this.setState({ message: value });
|
2018-08-29 17:24:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Appends a selected smileys to the chat message draft.
|
|
|
|
|
*
|
|
|
|
|
* @param {string} smileyText - The value of the smiley to append to the
|
|
|
|
|
* chat message.
|
|
|
|
|
* @private
|
|
|
|
|
* @returns {void}
|
|
|
|
|
*/
|
2022-08-11 13:32:44 +00:00
|
|
|
|
_onSmileySelect(smileyText: string) {
|
2021-06-10 12:48:44 +00:00
|
|
|
|
if (smileyText) {
|
|
|
|
|
this.setState({
|
|
|
|
|
message: `${this.state.message} ${smileyText}`,
|
|
|
|
|
showSmileysPanel: false
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
this.setState({
|
|
|
|
|
showSmileysPanel: false
|
|
|
|
|
});
|
|
|
|
|
}
|
2018-08-29 17:24:25 +00:00
|
|
|
|
|
2019-05-07 22:33:12 +00:00
|
|
|
|
this._focus();
|
2018-08-29 17:24:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Callback invoked to hide or show the smileys selector.
|
|
|
|
|
*
|
|
|
|
|
* @private
|
|
|
|
|
* @returns {void}
|
|
|
|
|
*/
|
2022-08-11 13:32:44 +00:00
|
|
|
|
_toggleSmileysPanel() {
|
2021-06-10 12:48:44 +00:00
|
|
|
|
if (this.state.showSmileysPanel) {
|
|
|
|
|
this._focus();
|
|
|
|
|
}
|
2018-08-29 17:24:25 +00:00
|
|
|
|
this.setState({ showSmileysPanel: !this.state.showSmileysPanel });
|
2021-06-10 12:48:44 +00:00
|
|
|
|
}
|
2018-08-29 17:24:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-09-09 22:46:28 +00:00
|
|
|
|
/**
|
|
|
|
|
* Function that maps parts of Redux state tree into component props.
|
|
|
|
|
*
|
|
|
|
|
* @param {Object} state - Redux state.
|
|
|
|
|
* @private
|
|
|
|
|
* @returns {{
|
|
|
|
|
* _areSmileysDisabled: boolean
|
|
|
|
|
* }}
|
|
|
|
|
*/
|
2022-10-20 09:11:27 +00:00
|
|
|
|
const mapStateToProps = (state: IReduxState) => {
|
2021-09-09 22:46:28 +00:00
|
|
|
|
return {
|
|
|
|
|
_areSmileysDisabled: areSmileysDisabled(state)
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default translate(connect(mapStateToProps)(ChatInput));
|