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.

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