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.

209 lines
8.2 KiB

  1. import CharacterCounter from './character_counter';
  2. import Button from '../../../components/button';
  3. import ImmutablePropTypes from 'react-immutable-proptypes';
  4. import PropTypes from 'prop-types';
  5. import ReplyIndicatorContainer from '../containers/reply_indicator_container';
  6. import AutosuggestTextarea from '../../../components/autosuggest_textarea';
  7. import { debounce } from 'react-decoration';
  8. import UploadButtonContainer from '../containers/upload_button_container';
  9. import { defineMessages, injectIntl, FormattedMessage } from 'react-intl';
  10. import Toggle from 'react-toggle';
  11. import Collapsable from '../../../components/collapsable';
  12. import SpoilerButtonContainer from '../containers/spoiler_button_container';
  13. import PrivacyDropdownContainer from '../containers/privacy_dropdown_container';
  14. import SensitiveButtonContainer from '../containers/sensitive_button_container';
  15. import EmojiPickerDropdown from './emoji_picker_dropdown';
  16. import UploadFormContainer from '../containers/upload_form_container';
  17. import TextIconButton from './text_icon_button';
  18. import WarningContainer from '../containers/warning_container';
  19. const messages = defineMessages({
  20. placeholder: { id: 'compose_form.placeholder', defaultMessage: 'What is on your mind?' },
  21. spoiler_placeholder: { id: 'compose_form.spoiler_placeholder', defaultMessage: 'Content warning' },
  22. publish: { id: 'compose_form.publish', defaultMessage: 'Toot' }
  23. });
  24. class ComposeForm extends React.PureComponent {
  25. constructor (props, context) {
  26. super(props, context);
  27. this.handleChange = this.handleChange.bind(this);
  28. this.handleKeyDown = this.handleKeyDown.bind(this);
  29. this.handleSubmit = this.handleSubmit.bind(this);
  30. this.onSuggestionsClearRequested = this.onSuggestionsClearRequested.bind(this);
  31. this.onSuggestionsFetchRequested = this.onSuggestionsFetchRequested.bind(this);
  32. this.onSuggestionSelected = this.onSuggestionSelected.bind(this);
  33. this.handleChangeSpoilerText = this.handleChangeSpoilerText.bind(this);
  34. this.setAutosuggestTextarea = this.setAutosuggestTextarea.bind(this);
  35. this.handleEmojiPick = this.handleEmojiPick.bind(this);
  36. }
  37. handleChange (e) {
  38. this.props.onChange(e.target.value);
  39. }
  40. handleKeyDown (e) {
  41. if (e.keyCode === 13 && (e.ctrlKey || e.metaKey)) {
  42. this.props.onSubmit();
  43. }
  44. }
  45. handleSubmit () {
  46. this.autosuggestTextarea.textarea.style.height = "auto";
  47. this.props.onSubmit();
  48. }
  49. onSuggestionsClearRequested () {
  50. this.props.onClearSuggestions();
  51. }
  52. @debounce(500)
  53. onSuggestionsFetchRequested (token) {
  54. this.props.onFetchSuggestions(token);
  55. }
  56. onSuggestionSelected (tokenStart, token, value) {
  57. this._restoreCaret = null;
  58. this.props.onSuggestionSelected(tokenStart, token, value);
  59. }
  60. handleChangeSpoilerText (e) {
  61. this.props.onChangeSpoilerText(e.target.value);
  62. }
  63. componentWillReceiveProps (nextProps) {
  64. // If this is the update where we've finished uploading,
  65. // save the last caret position so we can restore it below!
  66. if (!nextProps.is_uploading && this.props.is_uploading) {
  67. this._restoreCaret = this.autosuggestTextarea.textarea.selectionStart;
  68. }
  69. }
  70. componentDidUpdate (prevProps) {
  71. // This statement does several things:
  72. // - If we're beginning a reply, and,
  73. // - Replying to zero or one users, places the cursor at the end of the textbox.
  74. // - Replying to more than one user, selects any usernames past the first;
  75. // this provides a convenient shortcut to drop everyone else from the conversation.
  76. // - If we've just finished uploading an image, and have a saved caret position,
  77. // restores the cursor to that position after the text changes!
  78. if (this.props.focusDate !== prevProps.focusDate || (prevProps.is_uploading && !this.props.is_uploading && typeof this._restoreCaret === 'number')) {
  79. let selectionEnd, selectionStart;
  80. if (this.props.preselectDate !== prevProps.preselectDate) {
  81. selectionEnd = this.props.text.length;
  82. selectionStart = this.props.text.search(/\s/) + 1;
  83. } else if (typeof this._restoreCaret === 'number') {
  84. selectionStart = this._restoreCaret;
  85. selectionEnd = this._restoreCaret;
  86. } else {
  87. selectionEnd = this.props.text.length;
  88. selectionStart = selectionEnd;
  89. }
  90. this.autosuggestTextarea.textarea.setSelectionRange(selectionStart, selectionEnd);
  91. this.autosuggestTextarea.textarea.focus();
  92. }
  93. }
  94. setAutosuggestTextarea (c) {
  95. this.autosuggestTextarea = c;
  96. }
  97. handleEmojiPick (data) {
  98. const position = this.autosuggestTextarea.textarea.selectionStart;
  99. this._restoreCaret = position + data.shortname.length + 1;
  100. this.props.onPickEmoji(position, data);
  101. }
  102. render () {
  103. const { intl, onPaste } = this.props;
  104. const disabled = this.props.is_submitting;
  105. const text = [this.props.spoiler_text, this.props.text].join('');
  106. let publishText = '';
  107. let reply_to_other = false;
  108. if (this.props.privacy === 'private' || this.props.privacy === 'direct') {
  109. publishText = <span className='compose-form__publish-private'><i className='fa fa-lock' /> {intl.formatMessage(messages.publish)}</span>;
  110. } else {
  111. publishText = intl.formatMessage(messages.publish) + (this.props.privacy !== 'unlisted' ? '!' : '');
  112. }
  113. return (
  114. <div className='compose-form'>
  115. <Collapsable isVisible={this.props.spoiler} fullHeight={50}>
  116. <div className="spoiler-input">
  117. <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'/>
  118. </div>
  119. </Collapsable>
  120. <WarningContainer />
  121. <ReplyIndicatorContainer />
  122. <div className='compose-form__autosuggest-wrapper'>
  123. <AutosuggestTextarea
  124. ref={this.setAutosuggestTextarea}
  125. placeholder={intl.formatMessage(messages.placeholder)}
  126. disabled={disabled}
  127. value={this.props.text}
  128. onChange={this.handleChange}
  129. suggestions={this.props.suggestions}
  130. onKeyDown={this.handleKeyDown}
  131. onSuggestionsFetchRequested={this.onSuggestionsFetchRequested}
  132. onSuggestionsClearRequested={this.onSuggestionsClearRequested}
  133. onSuggestionSelected={this.onSuggestionSelected}
  134. onPaste={onPaste}
  135. />
  136. <EmojiPickerDropdown onPickEmoji={this.handleEmojiPick} />
  137. </div>
  138. <div className='compose-form__modifiers'>
  139. <UploadFormContainer />
  140. </div>
  141. <div className='compose-form__buttons-wrapper'>
  142. <div className='compose-form__buttons'>
  143. <UploadButtonContainer />
  144. <PrivacyDropdownContainer />
  145. <SensitiveButtonContainer />
  146. <SpoilerButtonContainer />
  147. </div>
  148. <div className='compose-form__publish'>
  149. <div className='character-counter__wrapper'><CharacterCounter max={500} text={text} /></div>
  150. <div className='compose-form__publish-button-wrapper'><Button text={publishText} onClick={this.handleSubmit} disabled={disabled || text.replace(/[\uD800-\uDBFF][\uDC00-\uDFFF]/g, "_").length > 500 || (text.length !==0 && text.trim().length === 0)} block /></div>
  151. </div>
  152. </div>
  153. </div>
  154. );
  155. }
  156. }
  157. ComposeForm.propTypes = {
  158. intl: PropTypes.object.isRequired,
  159. text: PropTypes.string.isRequired,
  160. suggestion_token: PropTypes.string,
  161. suggestions: ImmutablePropTypes.list,
  162. spoiler: PropTypes.bool,
  163. privacy: PropTypes.string,
  164. spoiler_text: PropTypes.string,
  165. focusDate: PropTypes.instanceOf(Date),
  166. preselectDate: PropTypes.instanceOf(Date),
  167. is_submitting: PropTypes.bool,
  168. is_uploading: PropTypes.bool,
  169. me: PropTypes.number,
  170. onChange: PropTypes.func.isRequired,
  171. onSubmit: PropTypes.func.isRequired,
  172. onClearSuggestions: PropTypes.func.isRequired,
  173. onFetchSuggestions: PropTypes.func.isRequired,
  174. onSuggestionSelected: PropTypes.func.isRequired,
  175. onChangeSpoilerText: PropTypes.func.isRequired,
  176. onPaste: PropTypes.func.isRequired,
  177. onPickEmoji: PropTypes.func.isRequired
  178. };
  179. export default injectIntl(ComposeForm);