The code powering m.abunchtell.com https://m.abunchtell.com
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.
 
 
 
 

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