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.
 
 
 
 

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