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.

208 lines
8.2 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 } from 'react-intl';
  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 WarningContainer from '../containers/warning_container';
  18. import ImmutablePureComponent from 'react-immutable-pure-component';
  19. import { length } from 'stringz';
  20. const messages = defineMessages({
  21. placeholder: { id: 'compose_form.placeholder', defaultMessage: 'What is on your mind?' },
  22. spoiler_placeholder: { id: 'compose_form.spoiler_placeholder', defaultMessage: 'Content warning' },
  23. publish: { id: 'compose_form.publish', defaultMessage: 'Toot' },
  24. publishLoud: { id: 'compose_form.publish_loud', defaultMessage: '{publish}!' },
  25. });
  26. @injectIntl
  27. export default 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. if (this.props.text !== this.autosuggestTextarea.textarea.value) {
  64. // Something changed the text inside the textarea (e.g. browser extensions like Grammarly)
  65. // Update the state to match the current text
  66. this.props.onChange(this.autosuggestTextarea.textarea.value);
  67. }
  68. this.props.onSubmit();
  69. }
  70. onSuggestionsClearRequested = () => {
  71. this.props.onClearSuggestions();
  72. }
  73. onSuggestionsFetchRequested = debounce((token) => {
  74. this.props.onFetchSuggestions(token);
  75. }, 500, { trailing: true })
  76. onSuggestionSelected = (tokenStart, token, value) => {
  77. this._restoreCaret = null;
  78. this.props.onSuggestionSelected(tokenStart, token, value);
  79. }
  80. handleChangeSpoilerText = (e) => {
  81. this.props.onChangeSpoilerText(e.target.value);
  82. }
  83. componentWillReceiveProps (nextProps) {
  84. // If this is the update where we've finished uploading,
  85. // save the last caret position so we can restore it below!
  86. if (!nextProps.is_uploading && this.props.is_uploading) {
  87. this._restoreCaret = this.autosuggestTextarea.textarea.selectionStart;
  88. }
  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 we've just finished uploading an image, and have a saved caret position,
  97. // restores the cursor to that position after the text changes!
  98. if (this.props.focusDate !== prevProps.focusDate || (prevProps.is_uploading && !this.props.is_uploading && typeof this._restoreCaret === 'number')) {
  99. let selectionEnd, selectionStart;
  100. if (this.props.preselectDate !== prevProps.preselectDate) {
  101. selectionEnd = this.props.text.length;
  102. selectionStart = this.props.text.search(/\s/) + 1;
  103. } else if (typeof this._restoreCaret === 'number') {
  104. selectionStart = this._restoreCaret;
  105. selectionEnd = this._restoreCaret;
  106. } else {
  107. selectionEnd = this.props.text.length;
  108. selectionStart = selectionEnd;
  109. }
  110. this.autosuggestTextarea.textarea.setSelectionRange(selectionStart, selectionEnd);
  111. this.autosuggestTextarea.textarea.focus();
  112. } else if(prevProps.is_submitting && !this.props.is_submitting) {
  113. this.autosuggestTextarea.textarea.focus();
  114. }
  115. }
  116. setAutosuggestTextarea = (c) => {
  117. this.autosuggestTextarea = c;
  118. }
  119. handleEmojiPick = (data) => {
  120. const position = this.autosuggestTextarea.textarea.selectionStart;
  121. this._restoreCaret = position + data.shortname.length + 1;
  122. this.props.onPickEmoji(position, data);
  123. }
  124. render () {
  125. const { intl, onPaste, showSearch } = this.props;
  126. const disabled = this.props.is_submitting;
  127. const text = [this.props.spoiler_text, this.props.text].join('');
  128. let publishText = '';
  129. if (this.props.privacy === 'private' || this.props.privacy === 'direct') {
  130. publishText = <span className='compose-form__publish-private'><i className='fa fa-lock' /> {intl.formatMessage(messages.publish)}</span>;
  131. } else {
  132. publishText = this.props.privacy !== 'unlisted' ? intl.formatMessage(messages.publishLoud, { publish: intl.formatMessage(messages.publish) }) : intl.formatMessage(messages.publish);
  133. }
  134. return (
  135. <div className='compose-form'>
  136. <Collapsable isVisible={this.props.spoiler} fullHeight={50}>
  137. <div className='spoiler-input'>
  138. <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' />
  139. </div>
  140. </Collapsable>
  141. <WarningContainer />
  142. <ReplyIndicatorContainer />
  143. <div className='compose-form__autosuggest-wrapper'>
  144. <AutosuggestTextarea
  145. ref={this.setAutosuggestTextarea}
  146. placeholder={intl.formatMessage(messages.placeholder)}
  147. disabled={disabled}
  148. value={this.props.text}
  149. onChange={this.handleChange}
  150. suggestions={this.props.suggestions}
  151. onKeyDown={this.handleKeyDown}
  152. onSuggestionsFetchRequested={this.onSuggestionsFetchRequested}
  153. onSuggestionsClearRequested={this.onSuggestionsClearRequested}
  154. onSuggestionSelected={this.onSuggestionSelected}
  155. onPaste={onPaste}
  156. autoFocus={!showSearch}
  157. />
  158. <EmojiPickerDropdown onPickEmoji={this.handleEmojiPick} />
  159. </div>
  160. <div className='compose-form__modifiers'>
  161. <UploadFormContainer />
  162. </div>
  163. <div className='compose-form__buttons-wrapper'>
  164. <div className='compose-form__buttons'>
  165. <UploadButtonContainer />
  166. <PrivacyDropdownContainer />
  167. <SensitiveButtonContainer />
  168. <SpoilerButtonContainer />
  169. </div>
  170. <div className='compose-form__publish'>
  171. <div className='character-counter__wrapper'><CharacterCounter max={500} text={text} /></div>
  172. <div className='compose-form__publish-button-wrapper'><Button text={publishText} onClick={this.handleSubmit} disabled={disabled || this.props.is_uploading || length(text) > 500 || (text.length !==0 && text.trim().length === 0)} block /></div>
  173. </div>
  174. </div>
  175. </div>
  176. );
  177. }
  178. }