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.

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