You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

221 lines
8.8 KiB

  1. import React from 'react';
  2. import CharacterCounter from './character_counter';
  3. import Button from '../../../components/button';
  4. import ImmutablePropTypes from 'react-immutable-proptypes';
  5. import PropTypes from 'prop-types';
  6. import ReplyIndicatorContainer from '../containers/reply_indicator_container';
  7. import AutosuggestTextarea from '../../../components/autosuggest_textarea';
  8. import UploadButtonContainer from '../containers/upload_button_container';
  9. import { defineMessages, injectIntl } from 'react-intl';
  10. import SpoilerButtonContainer from '../containers/spoiler_button_container';
  11. import PrivacyDropdownContainer from '../containers/privacy_dropdown_container';
  12. import SensitiveButtonContainer from '../containers/sensitive_button_container';
  13. import EmojiPickerDropdown from '../containers/emoji_picker_dropdown_container';
  14. import UploadFormContainer from '../containers/upload_form_container';
  15. import WarningContainer from '../containers/warning_container';
  16. import { isMobile } from '../../../is_mobile';
  17. import ImmutablePureComponent from 'react-immutable-pure-component';
  18. import { length } from 'stringz';
  19. import { countableText } from '../util/counter';
  20. const allowedAroundShortCode = '><\u0085\u0020\u00a0\u1680\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u202f\u205f\u3000\u2028\u2029\u0009\u000a\u000b\u000c\u000d';
  21. const messages = defineMessages({
  22. placeholder: { id: 'compose_form.placeholder', defaultMessage: 'What is on your mind?' },
  23. spoiler_placeholder: { id: 'compose_form.spoiler_placeholder', defaultMessage: 'Write your warning here' },
  24. publish: { id: 'compose_form.publish', defaultMessage: 'Toot' },
  25. publishLoud: { id: 'compose_form.publish_loud', defaultMessage: '{publish}!' },
  26. });
  27. export default @injectIntl
  28. class ComposeForm extends ImmutablePureComponent {
  29. static propTypes = {
  30. intl: PropTypes.object.isRequired,
  31. text: PropTypes.string.isRequired,
  32. suggestion_token: PropTypes.string,
  33. suggestions: ImmutablePropTypes.list,
  34. spoiler: PropTypes.bool,
  35. privacy: PropTypes.string,
  36. spoiler_text: PropTypes.string,
  37. focusDate: PropTypes.instanceOf(Date),
  38. caretPosition: PropTypes.number,
  39. preselectDate: PropTypes.instanceOf(Date),
  40. is_submitting: PropTypes.bool,
  41. is_uploading: PropTypes.bool,
  42. onChange: PropTypes.func.isRequired,
  43. onSubmit: PropTypes.func.isRequired,
  44. onClearSuggestions: PropTypes.func.isRequired,
  45. onFetchSuggestions: PropTypes.func.isRequired,
  46. onSuggestionSelected: PropTypes.func.isRequired,
  47. onChangeSpoilerText: PropTypes.func.isRequired,
  48. onPaste: PropTypes.func.isRequired,
  49. onPickEmoji: PropTypes.func.isRequired,
  50. showSearch: PropTypes.bool,
  51. anyMedia: PropTypes.bool,
  52. };
  53. static defaultProps = {
  54. showSearch: false,
  55. };
  56. handleChange = (e) => {
  57. this.props.onChange(e.target.value);
  58. }
  59. handleKeyDown = (e) => {
  60. if (e.keyCode === 13 && (e.ctrlKey || e.metaKey)) {
  61. this.handleSubmit();
  62. }
  63. }
  64. handleSubmit = () => {
  65. if (this.props.text !== this.autosuggestTextarea.textarea.value) {
  66. // Something changed the text inside the textarea (e.g. browser extensions like Grammarly)
  67. // Update the state to match the current text
  68. this.props.onChange(this.autosuggestTextarea.textarea.value);
  69. }
  70. // Submit disabled:
  71. const { is_submitting, is_uploading, anyMedia } = this.props;
  72. const fulltext = [this.props.spoiler_text, countableText(this.props.text)].join('');
  73. if (is_submitting || is_uploading || length(fulltext) > 500 || (fulltext.length !== 0 && fulltext.trim().length === 0 && !anyMedia)) {
  74. return;
  75. }
  76. this.props.onSubmit();
  77. }
  78. onSuggestionsClearRequested = () => {
  79. this.props.onClearSuggestions();
  80. }
  81. onSuggestionsFetchRequested = (token) => {
  82. this.props.onFetchSuggestions(token);
  83. }
  84. onSuggestionSelected = (tokenStart, token, value) => {
  85. this.props.onSuggestionSelected(tokenStart, token, value);
  86. }
  87. handleChangeSpoilerText = (e) => {
  88. this.props.onChangeSpoilerText(e.target.value);
  89. }
  90. componentDidUpdate (prevProps) {
  91. // This statement does several things:
  92. // - If we're beginning a reply, and,
  93. // - Replying to zero or one users, places the cursor at the end of the textbox.
  94. // - Replying to more than one user, selects any usernames past the first;
  95. // this provides a convenient shortcut to drop everyone else from the conversation.
  96. if (this.props.focusDate !== prevProps.focusDate) {
  97. let selectionEnd, selectionStart;
  98. if (this.props.preselectDate !== prevProps.preselectDate) {
  99. selectionEnd = this.props.text.length;
  100. selectionStart = this.props.text.search(/\s/) + 1;
  101. } else if (typeof this.props.caretPosition === 'number') {
  102. selectionStart = this.props.caretPosition;
  103. selectionEnd = this.props.caretPosition;
  104. } else {
  105. selectionEnd = this.props.text.length;
  106. selectionStart = selectionEnd;
  107. }
  108. this.autosuggestTextarea.textarea.setSelectionRange(selectionStart, selectionEnd);
  109. this.autosuggestTextarea.textarea.focus();
  110. } else if(prevProps.is_submitting && !this.props.is_submitting) {
  111. this.autosuggestTextarea.textarea.focus();
  112. } else if (this.props.spoiler !== prevProps.spoiler) {
  113. if (this.props.spoiler) {
  114. this.spoilerText.focus();
  115. } else {
  116. this.autosuggestTextarea.textarea.focus();
  117. }
  118. }
  119. }
  120. setAutosuggestTextarea = (c) => {
  121. this.autosuggestTextarea = c;
  122. }
  123. setSpoilerText = (c) => {
  124. this.spoilerText = c;
  125. }
  126. handleEmojiPick = (data) => {
  127. const { text } = this.props;
  128. const position = this.autosuggestTextarea.textarea.selectionStart;
  129. const needsSpace = data.custom && position > 0 && !allowedAroundShortCode.includes(text[position - 1]);
  130. this.props.onPickEmoji(position, data, needsSpace);
  131. }
  132. render () {
  133. const { intl, onPaste, showSearch, anyMedia } = this.props;
  134. const disabled = this.props.is_submitting;
  135. const text = [this.props.spoiler_text, countableText(this.props.text)].join('');
  136. const disabledButton = disabled || this.props.is_uploading || length(text) > 500 || (text.length !== 0 && text.trim().length === 0 && !anyMedia);
  137. let publishText = '';
  138. if (this.props.privacy === 'private' || this.props.privacy === 'direct') {
  139. publishText = <span className='compose-form__publish-private'><i className='fa fa-lock' /> {intl.formatMessage(messages.publish)}</span>;
  140. } else {
  141. publishText = this.props.privacy !== 'unlisted' ? intl.formatMessage(messages.publishLoud, { publish: intl.formatMessage(messages.publish) }) : intl.formatMessage(messages.publish);
  142. }
  143. return (
  144. <div className='compose-form'>
  145. <WarningContainer />
  146. <ReplyIndicatorContainer />
  147. <div className={`spoiler-input ${this.props.spoiler ? 'spoiler-input--visible' : ''}`}>
  148. <label>
  149. <span style={{ display: 'none' }}>{intl.formatMessage(messages.spoiler_placeholder)}</span>
  150. <input placeholder={intl.formatMessage(messages.spoiler_placeholder)} value={this.props.spoiler_text} onChange={this.handleChangeSpoilerText} onKeyDown={this.handleKeyDown} type='text' className='spoiler-input__input' id='cw-spoiler-input' ref={this.setSpoilerText} />
  151. </label>
  152. </div>
  153. <div className='compose-form__autosuggest-wrapper'>
  154. <AutosuggestTextarea
  155. ref={this.setAutosuggestTextarea}
  156. placeholder={intl.formatMessage(messages.placeholder)}
  157. disabled={disabled}
  158. value={this.props.text}
  159. onChange={this.handleChange}
  160. suggestions={this.props.suggestions}
  161. onKeyDown={this.handleKeyDown}
  162. onSuggestionsFetchRequested={this.onSuggestionsFetchRequested}
  163. onSuggestionsClearRequested={this.onSuggestionsClearRequested}
  164. onSuggestionSelected={this.onSuggestionSelected}
  165. onPaste={onPaste}
  166. autoFocus={!showSearch && !isMobile(window.innerWidth)}
  167. />
  168. <EmojiPickerDropdown onPickEmoji={this.handleEmojiPick} />
  169. </div>
  170. <div className='compose-form__modifiers'>
  171. <UploadFormContainer />
  172. </div>
  173. <div className='compose-form__buttons-wrapper'>
  174. <div className='compose-form__buttons'>
  175. <UploadButtonContainer />
  176. <PrivacyDropdownContainer />
  177. <SensitiveButtonContainer />
  178. <SpoilerButtonContainer />
  179. </div>
  180. <div className='character-counter__wrapper'><CharacterCounter max={500} text={text} /></div>
  181. </div>
  182. <div className='compose-form__publish'>
  183. <div className='compose-form__publish-button-wrapper'><Button text={publishText} onClick={this.handleSubmit} disabled={disabledButton} block /></div>
  184. </div>
  185. </div>
  186. );
  187. }
  188. }