The code powering m.abunchtell.com https://m.abunchtell.com
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 

212 rader
8.2 KiB

  1. import CharacterCounter from './character_counter';
  2. import Button from '../../../components/button';
  3. import PureRenderMixin from 'react-addons-pure-render-mixin';
  4. import ImmutablePropTypes from 'react-immutable-proptypes';
  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. const ComposeForm = React.createClass({
  24. propTypes: {
  25. intl: React.PropTypes.object.isRequired,
  26. text: React.PropTypes.string.isRequired,
  27. suggestion_token: React.PropTypes.string,
  28. suggestions: ImmutablePropTypes.list,
  29. spoiler: React.PropTypes.bool,
  30. privacy: React.PropTypes.string,
  31. spoiler_text: React.PropTypes.string,
  32. focusDate: React.PropTypes.instanceOf(Date),
  33. preselectDate: React.PropTypes.instanceOf(Date),
  34. is_submitting: React.PropTypes.bool,
  35. is_uploading: React.PropTypes.bool,
  36. me: React.PropTypes.number,
  37. needsPrivacyWarning: React.PropTypes.bool,
  38. mentionedDomains: React.PropTypes.array.isRequired,
  39. onChange: React.PropTypes.func.isRequired,
  40. onSubmit: React.PropTypes.func.isRequired,
  41. onClearSuggestions: React.PropTypes.func.isRequired,
  42. onFetchSuggestions: React.PropTypes.func.isRequired,
  43. onSuggestionSelected: React.PropTypes.func.isRequired,
  44. onChangeSpoilerText: React.PropTypes.func.isRequired,
  45. onPaste: React.PropTypes.func.isRequired,
  46. onPickEmoji: React.PropTypes.func.isRequired
  47. },
  48. mixins: [PureRenderMixin],
  49. handleChange (e) {
  50. this.props.onChange(e.target.value);
  51. },
  52. handleKeyDown (e) {
  53. if (e.keyCode === 13 && (e.ctrlKey || e.metaKey)) {
  54. this.props.onSubmit();
  55. }
  56. },
  57. handleSubmit () {
  58. this.props.onSubmit();
  59. },
  60. onSuggestionsClearRequested () {
  61. this.props.onClearSuggestions();
  62. },
  63. @debounce(500)
  64. onSuggestionsFetchRequested (token) {
  65. this.props.onFetchSuggestions(token);
  66. },
  67. onSuggestionSelected (tokenStart, token, value) {
  68. this._restoreCaret = null;
  69. this.props.onSuggestionSelected(tokenStart, token, value);
  70. },
  71. handleChangeSpoilerText (e) {
  72. this.props.onChangeSpoilerText(e.target.value);
  73. },
  74. componentWillReceiveProps (nextProps) {
  75. // If this is the update where we've finished uploading,
  76. // save the last caret position so we can restore it below!
  77. if (!nextProps.is_uploading && this.props.is_uploading) {
  78. this._restoreCaret = this.autosuggestTextarea.textarea.selectionStart;
  79. }
  80. },
  81. componentDidUpdate (prevProps) {
  82. // This statement does several things:
  83. // - If we're beginning a reply, and,
  84. // - Replying to zero or one users, places the cursor at the end of the textbox.
  85. // - Replying to more than one user, selects any usernames past the first;
  86. // this provides a convenient shortcut to drop everyone else from the conversation.
  87. // - If we've just finished uploading an image, and have a saved caret position,
  88. // restores the cursor to that position after the text changes!
  89. if (this.props.focusDate !== prevProps.focusDate || (prevProps.is_uploading && !this.props.is_uploading && typeof this._restoreCaret === 'number')) {
  90. let selectionEnd, selectionStart;
  91. if (this.props.preselectDate !== prevProps.preselectDate) {
  92. selectionEnd = this.props.text.length;
  93. selectionStart = this.props.text.search(/\s/) + 1;
  94. } else if (typeof this._restoreCaret === 'number') {
  95. selectionStart = this._restoreCaret;
  96. selectionEnd = this._restoreCaret;
  97. } else {
  98. selectionEnd = this.props.text.length;
  99. selectionStart = selectionEnd;
  100. }
  101. this.autosuggestTextarea.textarea.setSelectionRange(selectionStart, selectionEnd);
  102. this.autosuggestTextarea.textarea.focus();
  103. }
  104. },
  105. setAutosuggestTextarea (c) {
  106. this.autosuggestTextarea = c;
  107. },
  108. handleEmojiPick (data) {
  109. const position = this.autosuggestTextarea.textarea.selectionStart;
  110. this._restoreCaret = position + data.shortname.length + 1;
  111. this.props.onPickEmoji(position, data);
  112. },
  113. render () {
  114. const { intl, needsPrivacyWarning, mentionedDomains, onPaste } = this.props;
  115. const disabled = this.props.is_submitting;
  116. const text = [this.props.spoiler_text, this.props.text].join('');
  117. let publishText = '';
  118. let privacyWarning = '';
  119. let reply_to_other = false;
  120. if (needsPrivacyWarning) {
  121. privacyWarning = (
  122. <div className='compose-form__warning'>
  123. <FormattedMessage
  124. id='compose_form.privacy_disclaimer'
  125. 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?'
  126. values={{ domains: <strong>{mentionedDomains.join(', ')}</strong>, domainsCount: mentionedDomains.length }}
  127. />
  128. </div>
  129. );
  130. }
  131. if (this.props.privacy === 'private' || this.props.privacy === 'direct') {
  132. publishText = <span><i className='fa fa-lock' /> {intl.formatMessage(messages.publish)}</span>;
  133. } else {
  134. publishText = intl.formatMessage(messages.publish) + (this.props.privacy !== 'unlisted' ? '!' : '');
  135. }
  136. return (
  137. <div style={{ padding: '10px' }}>
  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" />
  141. </div>
  142. </Collapsable>
  143. {privacyWarning}
  144. <ReplyIndicatorContainer />
  145. <div style={{ position: 'relative' }}>
  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. />
  159. <EmojiPickerDropdown onPickEmoji={this.handleEmojiPick} />
  160. </div>
  161. <div className='compose-form__modifiers'>
  162. <UploadFormContainer />
  163. </div>
  164. <div style={{ display: 'flex', justifyContent: 'space-between' }}>
  165. <div className='compose-form__buttons'>
  166. <UploadButtonContainer />
  167. <PrivacyDropdownContainer />
  168. <SensitiveButtonContainer />
  169. <SpoilerButtonContainer />
  170. </div>
  171. <div style={{ display: 'flex' }}>
  172. <div style={{ paddingTop: '10px', marginRight: '16px', lineHeight: '36px' }}><CharacterCounter max={500} text={text} /></div>
  173. <div style={{ paddingTop: '10px' }}><Button text={publishText} onClick={this.handleSubmit} disabled={disabled || text.replace(/[\uD800-\uDBFF][\uDC00-\uDFFF]/g, "_").length > 500} /></div>
  174. </div>
  175. </div>
  176. </div>
  177. );
  178. }
  179. });
  180. export default injectIntl(ComposeForm);