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.

202 lines
8.7 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 ReplyIndicator from './reply_indicator';
  6. import UploadButton from './upload_button';
  7. import AutosuggestTextarea from '../../../components/autosuggest_textarea';
  8. import AutosuggestAccountContainer from '../../compose/containers/autosuggest_account_container';
  9. import { debounce } from 'react-decoration';
  10. import UploadButtonContainer from '../containers/upload_button_container';
  11. import { defineMessages, injectIntl, FormattedMessage } from 'react-intl';
  12. import Toggle from 'react-toggle';
  13. import { Motion, spring } from 'react-motion';
  14. const messages = defineMessages({
  15. placeholder: { id: 'compose_form.placeholder', defaultMessage: 'What is on your mind?' },
  16. spoiler_placeholder: { id: 'compose_form.spoiler_placeholder', defaultMessage: 'Content warning' },
  17. publish: { id: 'compose_form.publish', defaultMessage: 'Publish' }
  18. });
  19. const ComposeForm = React.createClass({
  20. propTypes: {
  21. intl: React.PropTypes.object.isRequired,
  22. text: React.PropTypes.string.isRequired,
  23. suggestion_token: React.PropTypes.string,
  24. suggestions: ImmutablePropTypes.list,
  25. sensitive: React.PropTypes.bool,
  26. spoiler: React.PropTypes.bool,
  27. spoiler_text: React.PropTypes.string,
  28. unlisted: React.PropTypes.bool,
  29. private: React.PropTypes.bool,
  30. fileDropDate: React.PropTypes.instanceOf(Date),
  31. is_submitting: React.PropTypes.bool,
  32. is_uploading: React.PropTypes.bool,
  33. in_reply_to: ImmutablePropTypes.map,
  34. media_count: React.PropTypes.number,
  35. me: React.PropTypes.number,
  36. onChange: React.PropTypes.func.isRequired,
  37. onSubmit: React.PropTypes.func.isRequired,
  38. onCancelReply: React.PropTypes.func.isRequired,
  39. onClearSuggestions: React.PropTypes.func.isRequired,
  40. onFetchSuggestions: React.PropTypes.func.isRequired,
  41. onSuggestionSelected: React.PropTypes.func.isRequired,
  42. onChangeSensitivity: React.PropTypes.func.isRequired,
  43. onChangeSpoilerness: React.PropTypes.func.isRequired,
  44. onChangeSpoilerText: React.PropTypes.func.isRequired,
  45. onChangeVisibility: React.PropTypes.func.isRequired,
  46. onChangeListability: 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.props.onSuggestionSelected(tokenStart, token, value);
  69. },
  70. handleChangeSensitivity (e) {
  71. this.props.onChangeSensitivity(e.target.checked);
  72. },
  73. handleChangeSpoilerness (e) {
  74. this.props.onChangeSpoilerness(e.target.checked);
  75. this.props.onChangeSpoilerText('');
  76. },
  77. handleChangeSpoilerText (e) {
  78. this.props.onChangeSpoilerText(e.target.value);
  79. },
  80. handleChangeVisibility (e) {
  81. this.props.onChangeVisibility(e.target.checked);
  82. },
  83. handleChangeListability (e) {
  84. this.props.onChangeListability(e.target.checked);
  85. },
  86. componentDidUpdate (prevProps) {
  87. if ((prevProps.in_reply_to === null && this.props.in_reply_to !== null) || (prevProps.in_reply_to !== null && this.props.in_reply_to !== null && prevProps.in_reply_to.get('id') !== this.props.in_reply_to.get('id'))) {
  88. // If replying to zero or one users, places the cursor at the end of the textbox.
  89. // If 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. const selectionStart = this.props.text.search(/\s/) + 1;
  92. const selectionEnd = this.props.text.length;
  93. this.autosuggestTextarea.textarea.setSelectionRange(selectionStart, selectionEnd);
  94. this.autosuggestTextarea.textarea.focus();
  95. }
  96. },
  97. setAutosuggestTextarea (c) {
  98. this.autosuggestTextarea = c;
  99. },
  100. render () {
  101. const { intl } = this.props;
  102. let replyArea = '';
  103. let publishText = '';
  104. const disabled = this.props.is_submitting || this.props.is_uploading;
  105. if (this.props.in_reply_to) {
  106. replyArea = <ReplyIndicator status={this.props.in_reply_to} onCancel={this.props.onCancelReply} />;
  107. }
  108. let reply_to_other = !!this.props.in_reply_to && (this.props.in_reply_to.getIn(['account', 'id']) !== this.props.me);
  109. if (this.props.private) {
  110. publishText = <span><i className='fa fa-lock' /> {intl.formatMessage(messages.publish)}</span>;
  111. } else {
  112. publishText = intl.formatMessage(messages.publish) + (!this.props.unlisted ? '!' : '');
  113. }
  114. return (
  115. <div style={{ padding: '10px' }}>
  116. <Motion defaultStyle={{ opacity: !this.props.spoiler ? 0 : 100, height: !this.props.spoiler ? 50 : 0 }} style={{ opacity: spring(!this.props.spoiler ? 0 : 100), height: spring(!this.props.spoiler ? 0 : 50) }}>
  117. {({ opacity, height }) =>
  118. <div className="spoiler-input" style={{ height: `${height}px`, overflow: 'hidden', opacity: opacity / 100 }}>
  119. <input placeholder={intl.formatMessage(messages.spoiler_placeholder)} value={this.props.spoiler_text} onChange={this.handleChangeSpoilerText} type="text" className="spoiler-input__input" />
  120. </div>
  121. }
  122. </Motion>
  123. {replyArea}
  124. <AutosuggestTextarea
  125. ref={this.setAutosuggestTextarea}
  126. placeholder={intl.formatMessage(messages.placeholder)}
  127. disabled={disabled}
  128. fileDropDate={this.props.fileDropDate}
  129. value={this.props.text}
  130. onChange={this.handleChange}
  131. suggestions={this.props.suggestions}
  132. onKeyDown={this.handleKeyDown}
  133. onSuggestionsFetchRequested={this.onSuggestionsFetchRequested}
  134. onSuggestionsClearRequested={this.onSuggestionsClearRequested}
  135. onSuggestionSelected={this.onSuggestionSelected}
  136. />
  137. <div style={{ marginTop: '10px', overflow: 'hidden' }}>
  138. <div style={{ float: 'right' }}><Button text={publishText} onClick={this.handleSubmit} disabled={disabled} /></div>
  139. <div style={{ float: 'right', marginRight: '16px', lineHeight: '36px' }}><CharacterCounter max={500} text={[this.props.spoiler_text, this.props.text].join('')} /></div>
  140. <UploadButtonContainer style={{ paddingTop: '4px' }} />
  141. </div>
  142. <label className='compose-form__label with-border' style={{ marginTop: '10px' }}>
  143. <Toggle checked={this.props.spoiler} onChange={this.handleChangeSpoilerness} />
  144. <span className='compose-form__label__text'><FormattedMessage id='compose_form.spoiler' defaultMessage='Hide text behind warning' /></span>
  145. </label>
  146. <label className='compose-form__label with-border'>
  147. <Toggle checked={this.props.private} onChange={this.handleChangeVisibility} />
  148. <span className='compose-form__label__text'><FormattedMessage id='compose_form.private' defaultMessage='Mark as private' /></span>
  149. </label>
  150. <Motion defaultStyle={{ opacity: (this.props.private || reply_to_other) ? 0 : 100, height: (this.props.private || reply_to_other) ? 39.5 : 0 }} style={{ opacity: spring((this.props.private || reply_to_other) ? 0 : 100), height: spring((this.props.private || reply_to_other) ? 0 : 39.5) }}>
  151. {({ opacity, height }) =>
  152. <label className='compose-form__label' style={{ height: `${height}px`, overflow: 'hidden', opacity: opacity / 100 }}>
  153. <Toggle checked={this.props.unlisted} onChange={this.handleChangeListability} />
  154. <span className='compose-form__label__text'><FormattedMessage id='compose_form.unlisted' defaultMessage='Do not display in public timeline' /></span>
  155. </label>
  156. }
  157. </Motion>
  158. <Motion defaultStyle={{ opacity: 0, height: 0 }} style={{ opacity: spring(this.props.media_count === 0 ? 0 : 100), height: spring(this.props.media_count === 0 ? 0 : 39.5) }}>
  159. {({ opacity, height }) =>
  160. <label className='compose-form__label' style={{ height: `${height}px`, overflow: 'hidden', opacity: opacity / 100 }}>
  161. <Toggle checked={this.props.sensitive} onChange={this.handleChangeSensitivity} />
  162. <span className='compose-form__label__text'><FormattedMessage id='compose_form.sensitive' defaultMessage='Mark media as sensitive' /></span>
  163. </label>
  164. }
  165. </Motion>
  166. </div>
  167. );
  168. }
  169. });
  170. export default injectIntl(ComposeForm);