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.

223 lines
8.7 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. const messages = defineMessages({
  19. placeholder: { id: 'compose_form.placeholder', defaultMessage: 'What is on your mind?' },
  20. spoiler_placeholder: { id: 'compose_form.spoiler_placeholder', defaultMessage: 'Content warning' },
  21. publish: { id: 'compose_form.publish', defaultMessage: 'Toot' }
  22. });
  23. class ComposeForm extends React.PureComponent {
  24. constructor (props, context) {
  25. super(props, context);
  26. this.handleChange = this.handleChange.bind(this);
  27. this.handleKeyDown = this.handleKeyDown.bind(this);
  28. this.handleSubmit = this.handleSubmit.bind(this);
  29. this.onSuggestionsClearRequested = this.onSuggestionsClearRequested.bind(this);
  30. this.onSuggestionsFetchRequested = this.onSuggestionsFetchRequested.bind(this);
  31. this.onSuggestionSelected = this.onSuggestionSelected.bind(this);
  32. this.handleChangeSpoilerText = this.handleChangeSpoilerText.bind(this);
  33. this.setAutosuggestTextarea = this.setAutosuggestTextarea.bind(this);
  34. this.handleEmojiPick = this.handleEmojiPick.bind(this);
  35. }
  36. handleChange (e) {
  37. this.props.onChange(e.target.value);
  38. }
  39. handleKeyDown (e) {
  40. if (e.keyCode === 13 && (e.ctrlKey || e.metaKey)) {
  41. this.props.onSubmit();
  42. }
  43. }
  44. handleSubmit () {
  45. this.autosuggestTextarea.textarea.style.height = "auto";
  46. this.props.onSubmit();
  47. }
  48. onSuggestionsClearRequested () {
  49. this.props.onClearSuggestions();
  50. }
  51. @debounce(500)
  52. onSuggestionsFetchRequested (token) {
  53. this.props.onFetchSuggestions(token);
  54. }
  55. onSuggestionSelected (tokenStart, token, value) {
  56. this._restoreCaret = null;
  57. this.props.onSuggestionSelected(tokenStart, token, value);
  58. }
  59. handleChangeSpoilerText (e) {
  60. this.props.onChangeSpoilerText(e.target.value);
  61. }
  62. componentWillReceiveProps (nextProps) {
  63. // If this is the update where we've finished uploading,
  64. // save the last caret position so we can restore it below!
  65. if (!nextProps.is_uploading && this.props.is_uploading) {
  66. this._restoreCaret = this.autosuggestTextarea.textarea.selectionStart;
  67. }
  68. }
  69. componentDidUpdate (prevProps) {
  70. // This statement does several things:
  71. // - If we're beginning a reply, and,
  72. // - Replying to zero or one users, places the cursor at the end of the textbox.
  73. // - Replying to more than one user, selects any usernames past the first;
  74. // this provides a convenient shortcut to drop everyone else from the conversation.
  75. // - If we've just finished uploading an image, and have a saved caret position,
  76. // restores the cursor to that position after the text changes!
  77. if (this.props.focusDate !== prevProps.focusDate || (prevProps.is_uploading && !this.props.is_uploading && typeof this._restoreCaret === 'number')) {
  78. let selectionEnd, selectionStart;
  79. if (this.props.preselectDate !== prevProps.preselectDate) {
  80. selectionEnd = this.props.text.length;
  81. selectionStart = this.props.text.search(/\s/) + 1;
  82. } else if (typeof this._restoreCaret === 'number') {
  83. selectionStart = this._restoreCaret;
  84. selectionEnd = this._restoreCaret;
  85. } else {
  86. selectionEnd = this.props.text.length;
  87. selectionStart = selectionEnd;
  88. }
  89. this.autosuggestTextarea.textarea.setSelectionRange(selectionStart, selectionEnd);
  90. this.autosuggestTextarea.textarea.focus();
  91. }
  92. }
  93. setAutosuggestTextarea (c) {
  94. this.autosuggestTextarea = c;
  95. }
  96. handleEmojiPick (data) {
  97. const position = this.autosuggestTextarea.textarea.selectionStart;
  98. this._restoreCaret = position + data.shortname.length + 1;
  99. this.props.onPickEmoji(position, data);
  100. }
  101. render () {
  102. const { intl, needsPrivacyWarning, mentionedDomains, onPaste } = this.props;
  103. const disabled = this.props.is_submitting;
  104. const text = [this.props.spoiler_text, this.props.text].join('');
  105. let publishText = '';
  106. let privacyWarning = '';
  107. let reply_to_other = false;
  108. if (needsPrivacyWarning) {
  109. privacyWarning = (
  110. <div className='compose-form__warning'>
  111. <FormattedMessage
  112. id='compose_form.privacy_disclaimer'
  113. defaultMessage='Your private status will be delivered to mentioned users on {domains}. Do you trust {domainsCount, plural, one {that server} other {those servers}} to not leak your status?'
  114. values={{ domains: <strong>{mentionedDomains.join(', ')}</strong>, domainsCount: mentionedDomains.length }}
  115. />
  116. </div>
  117. );
  118. }
  119. if (this.props.privacy === 'private' || this.props.privacy === 'direct') {
  120. publishText = <span><i className='fa fa-lock' /> {intl.formatMessage(messages.publish)}</span>;
  121. } else {
  122. publishText = intl.formatMessage(messages.publish) + (this.props.privacy !== 'unlisted' ? '!' : '');
  123. }
  124. return (
  125. <div style={{ padding: '10px' }}>
  126. <Collapsable isVisible={this.props.spoiler} fullHeight={50}>
  127. <div className="spoiler-input">
  128. <input placeholder={intl.formatMessage(messages.spoiler_placeholder)} value={this.props.spoiler_text} onChange={this.handleChangeSpoilerText} onKeyDown={this.handleKeyDown} type="text" className="spoiler-input__input" />
  129. </div>
  130. </Collapsable>
  131. {privacyWarning}
  132. <ReplyIndicatorContainer />
  133. <div style={{ position: 'relative' }}>
  134. <AutosuggestTextarea
  135. ref={this.setAutosuggestTextarea}
  136. placeholder={intl.formatMessage(messages.placeholder)}
  137. disabled={disabled}
  138. value={this.props.text}
  139. onChange={this.handleChange}
  140. suggestions={this.props.suggestions}
  141. onKeyDown={this.handleKeyDown}
  142. onSuggestionsFetchRequested={this.onSuggestionsFetchRequested}
  143. onSuggestionsClearRequested={this.onSuggestionsClearRequested}
  144. onSuggestionSelected={this.onSuggestionSelected}
  145. onPaste={onPaste}
  146. />
  147. <EmojiPickerDropdown onPickEmoji={this.handleEmojiPick} />
  148. </div>
  149. <div className='compose-form__modifiers'>
  150. <UploadFormContainer />
  151. </div>
  152. <div style={{ display: 'flex', justifyContent: 'space-between' }}>
  153. <div className='compose-form__buttons'>
  154. <UploadButtonContainer />
  155. <PrivacyDropdownContainer />
  156. <SensitiveButtonContainer />
  157. <SpoilerButtonContainer />
  158. </div>
  159. <div style={{ display: 'flex', minWidth: 0 }}>
  160. <div style={{ paddingTop: '10px', marginRight: '16px', lineHeight: '36px' }}><CharacterCounter max={500} text={text} /></div>
  161. <div style={{ paddingTop: '10px', overflow: 'hidden' }}><Button text={publishText} onClick={this.handleSubmit} disabled={disabled || text.replace(/[\uD800-\uDBFF][\uDC00-\uDFFF]/g, "_").length > 500} block /></div>
  162. </div>
  163. </div>
  164. </div>
  165. );
  166. }
  167. }
  168. ComposeForm.propTypes = {
  169. intl: PropTypes.object.isRequired,
  170. text: PropTypes.string.isRequired,
  171. suggestion_token: PropTypes.string,
  172. suggestions: ImmutablePropTypes.list,
  173. spoiler: PropTypes.bool,
  174. privacy: PropTypes.string,
  175. spoiler_text: PropTypes.string,
  176. focusDate: PropTypes.instanceOf(Date),
  177. preselectDate: PropTypes.instanceOf(Date),
  178. is_submitting: PropTypes.bool,
  179. is_uploading: PropTypes.bool,
  180. me: PropTypes.number,
  181. needsPrivacyWarning: PropTypes.bool,
  182. mentionedDomains: PropTypes.array.isRequired,
  183. onChange: PropTypes.func.isRequired,
  184. onSubmit: PropTypes.func.isRequired,
  185. onClearSuggestions: PropTypes.func.isRequired,
  186. onFetchSuggestions: PropTypes.func.isRequired,
  187. onSuggestionSelected: PropTypes.func.isRequired,
  188. onChangeSpoilerText: PropTypes.func.isRequired,
  189. onPaste: PropTypes.func.isRequired,
  190. onPickEmoji: PropTypes.func.isRequired
  191. };
  192. export default injectIntl(ComposeForm);