Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. import React, { Component, RefObject } from 'react';
  2. import { WithTranslation } from 'react-i18next';
  3. import { connect } from 'react-redux';
  4. import { IReduxState, IStore } from '../../../app/types';
  5. import { isMobileBrowser } from '../../../base/environment/utils';
  6. import { translate } from '../../../base/i18n/functions';
  7. import { IconFaceSmile, IconSend } from '../../../base/icons/svg';
  8. import Button from '../../../base/ui/components/web/Button';
  9. import Input from '../../../base/ui/components/web/Input';
  10. import { areSmileysDisabled } from '../../functions';
  11. import SmileysPanel from './SmileysPanel';
  12. /**
  13. * The type of the React {@code Component} props of {@link ChatInput}.
  14. */
  15. interface IProps extends WithTranslation {
  16. /**
  17. * Whether chat emoticons are disabled.
  18. */
  19. _areSmileysDisabled: boolean;
  20. /**
  21. * The id of the message recipient, if any.
  22. */
  23. _privateMessageRecipientId?: string;
  24. /**
  25. * Invoked to send chat messages.
  26. */
  27. dispatch: IStore['dispatch'];
  28. /**
  29. * Callback to invoke on message send.
  30. */
  31. onSend: Function;
  32. }
  33. /**
  34. * The type of the React {@code Component} state of {@link ChatInput}.
  35. */
  36. interface IState {
  37. /**
  38. * User provided nickname when the input text is provided in the view.
  39. */
  40. message: string;
  41. /**
  42. * Whether or not the smiley selector is visible.
  43. */
  44. showSmileysPanel: boolean;
  45. }
  46. /**
  47. * Implements a React Component for drafting and submitting a chat message.
  48. *
  49. * @augments Component
  50. */
  51. class ChatInput extends Component<IProps, IState> {
  52. _textArea?: RefObject<HTMLTextAreaElement>;
  53. state = {
  54. message: '',
  55. showSmileysPanel: false
  56. };
  57. /**
  58. * Initializes a new {@code ChatInput} instance.
  59. *
  60. * @param {Object} props - The read-only properties with which the new
  61. * instance is to be initialized.
  62. */
  63. constructor(props: IProps) {
  64. super(props);
  65. this._textArea = React.createRef<HTMLTextAreaElement>();
  66. // Bind event handlers so they are only bound once for every instance.
  67. this._onDetectSubmit = this._onDetectSubmit.bind(this);
  68. this._onMessageChange = this._onMessageChange.bind(this);
  69. this._onSmileySelect = this._onSmileySelect.bind(this);
  70. this._onSubmitMessage = this._onSubmitMessage.bind(this);
  71. this._toggleSmileysPanel = this._toggleSmileysPanel.bind(this);
  72. }
  73. /**
  74. * Implements React's {@link Component#componentDidMount()}.
  75. *
  76. * @inheritdoc
  77. */
  78. componentDidMount() {
  79. if (isMobileBrowser()) {
  80. // Ensure textarea is not focused when opening chat on mobile browser.
  81. this._textArea?.current && this._textArea.current.blur();
  82. } else {
  83. this._focus();
  84. }
  85. }
  86. /**
  87. * Implements {@code Component#componentDidUpdate}.
  88. *
  89. * @inheritdoc
  90. */
  91. componentDidUpdate(prevProps: Readonly<IProps>) {
  92. if (prevProps._privateMessageRecipientId !== this.props._privateMessageRecipientId) {
  93. this._textArea?.current?.focus();
  94. }
  95. }
  96. /**
  97. * Implements React's {@link Component#render()}.
  98. *
  99. * @inheritdoc
  100. * @returns {ReactElement}
  101. */
  102. render() {
  103. return (
  104. <div className = { `chat-input-container${this.state.message.trim().length ? ' populated' : ''}` }>
  105. <div id = 'chat-input' >
  106. {!this.props._areSmileysDisabled && this.state.showSmileysPanel && (
  107. <div
  108. className = 'smiley-input'>
  109. <div
  110. className = 'smileys-panel' >
  111. <SmileysPanel
  112. onSmileySelect = { this._onSmileySelect } />
  113. </div>
  114. </div>
  115. )}
  116. <Input
  117. className = 'chat-input'
  118. icon = { this.props._areSmileysDisabled ? undefined : IconFaceSmile }
  119. iconClick = { this._toggleSmileysPanel }
  120. id = 'chat-input-messagebox'
  121. maxRows = { 5 }
  122. onChange = { this._onMessageChange }
  123. onKeyPress = { this._onDetectSubmit }
  124. placeholder = { this.props.t('chat.messagebox') }
  125. ref = { this._textArea }
  126. textarea = { true }
  127. value = { this.state.message } />
  128. <Button
  129. accessibilityLabel = { this.props.t('chat.sendButton') }
  130. disabled = { !this.state.message.trim() }
  131. icon = { IconSend }
  132. onClick = { this._onSubmitMessage }
  133. size = { isMobileBrowser() ? 'large' : 'medium' } />
  134. </div>
  135. </div>
  136. );
  137. }
  138. /**
  139. * Place cursor focus on this component's text area.
  140. *
  141. * @private
  142. * @returns {void}
  143. */
  144. _focus() {
  145. this._textArea?.current && this._textArea.current.focus();
  146. }
  147. /**
  148. * Submits the message to the chat window.
  149. *
  150. * @returns {void}
  151. */
  152. _onSubmitMessage() {
  153. const trimmed = this.state.message.trim();
  154. if (trimmed) {
  155. this.props.onSend(trimmed);
  156. this.setState({ message: '' });
  157. // Keep the textarea in focus when sending messages via submit button.
  158. this._focus();
  159. }
  160. }
  161. /**
  162. * Detects if enter has been pressed. If so, submit the message in the chat
  163. * window.
  164. *
  165. * @param {string} event - Keyboard event.
  166. * @private
  167. * @returns {void}
  168. */
  169. _onDetectSubmit(event: any) {
  170. // Composition events used to add accents to characters
  171. // despite their absence from standard US keyboards,
  172. // to build up logograms of many Asian languages
  173. // from their base components or categories and so on.
  174. if (event.isComposing || event.keyCode === 229) {
  175. // keyCode 229 means that user pressed some button,
  176. // but input method is still processing that.
  177. // This is a standard behavior for some input methods
  178. // like entering japanese or сhinese hieroglyphs.
  179. return;
  180. }
  181. if (event.key === 'Enter'
  182. && event.shiftKey === false
  183. && event.ctrlKey === false) {
  184. event.preventDefault();
  185. event.stopPropagation();
  186. this._onSubmitMessage();
  187. }
  188. }
  189. /**
  190. * Updates the known message the user is drafting.
  191. *
  192. * @param {string} value - Keyboard event.
  193. * @private
  194. * @returns {void}
  195. */
  196. _onMessageChange(value: string) {
  197. this.setState({ message: value });
  198. }
  199. /**
  200. * Appends a selected smileys to the chat message draft.
  201. *
  202. * @param {string} smileyText - The value of the smiley to append to the
  203. * chat message.
  204. * @private
  205. * @returns {void}
  206. */
  207. _onSmileySelect(smileyText: string) {
  208. if (smileyText) {
  209. this.setState({
  210. message: `${this.state.message} ${smileyText}`,
  211. showSmileysPanel: false
  212. });
  213. } else {
  214. this.setState({
  215. showSmileysPanel: false
  216. });
  217. }
  218. this._focus();
  219. }
  220. /**
  221. * Callback invoked to hide or show the smileys selector.
  222. *
  223. * @private
  224. * @returns {void}
  225. */
  226. _toggleSmileysPanel() {
  227. if (this.state.showSmileysPanel) {
  228. this._focus();
  229. }
  230. this.setState({ showSmileysPanel: !this.state.showSmileysPanel });
  231. }
  232. }
  233. /**
  234. * Function that maps parts of Redux state tree into component props.
  235. *
  236. * @param {Object} state - Redux state.
  237. * @private
  238. * @returns {{
  239. * _areSmileysDisabled: boolean
  240. * }}
  241. */
  242. const mapStateToProps = (state: IReduxState) => {
  243. const { privateMessageRecipient } = state['features/chat'];
  244. return {
  245. _areSmileysDisabled: areSmileysDisabled(state),
  246. _privateMessageRecipientId: privateMessageRecipient?.id
  247. };
  248. };
  249. export default translate(connect(mapStateToProps)(ChatInput));