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.

195 lines
8.9 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. const disabled = this.props.is_submitting || this.props.is_uploading;
  104. if (this.props.in_reply_to) {
  105. replyArea = <ReplyIndicator status={this.props.in_reply_to} onCancel={this.props.onCancelReply} />;
  106. }
  107. let reply_to_other = !!this.props.in_reply_to && (this.props.in_reply_to.getIn(['account', 'id']) !== this.props.me);
  108. return (
  109. <div style={{ padding: '10px' }}>
  110. <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) }}>
  111. {({ opacity, height }) =>
  112. <div className="spoiler-input" style={{ height: `${height}px`, overflow: 'hidden', opacity: opacity / 100 }}>
  113. <input placeholder={intl.formatMessage(messages.spoiler_placeholder)} value={this.props.spoiler_text} onChange={this.handleChangeSpoilerText} type="text" className="spoiler-input__input" />
  114. </div>
  115. }
  116. </Motion>
  117. {replyArea}
  118. <AutosuggestTextarea
  119. ref={this.setAutosuggestTextarea}
  120. placeholder={intl.formatMessage(messages.placeholder)}
  121. disabled={disabled}
  122. fileDropDate={this.props.fileDropDate}
  123. value={this.props.text}
  124. onChange={this.handleChange}
  125. suggestions={this.props.suggestions}
  126. onKeyDown={this.handleKeyDown}
  127. onSuggestionsFetchRequested={this.onSuggestionsFetchRequested}
  128. onSuggestionsClearRequested={this.onSuggestionsClearRequested}
  129. onSuggestionSelected={this.onSuggestionSelected}
  130. />
  131. <div style={{ marginTop: '10px', overflow: 'hidden' }}>
  132. <div style={{ float: 'right' }}><Button text={intl.formatMessage(messages.publish)} onClick={this.handleSubmit} disabled={disabled} /></div>
  133. <div style={{ float: 'right', marginRight: '16px', lineHeight: '36px' }}><CharacterCounter max={500} text={[this.props.spoiler_text, this.props.text].join('')} /></div>
  134. <UploadButtonContainer style={{ paddingTop: '4px' }} />
  135. </div>
  136. <label style={{ display: 'block', lineHeight: '24px', verticalAlign: 'middle', marginTop: '10px', borderTop: '1px solid #282c37', paddingTop: '10px' }}>
  137. <Toggle checked={this.props.private} onChange={this.handleChangeVisibility} />
  138. <span style={{ display: 'inline-block', verticalAlign: 'middle', marginBottom: '14px', marginLeft: '8px', color: '#9baec8' }}><FormattedMessage id='compose_form.private' defaultMessage='Mark as private' /></span>
  139. </label>
  140. <label style={{ display: 'block', lineHeight: '24px', verticalAlign: 'middle' }}>
  141. <Toggle checked={this.props.spoiler} onChange={this.handleChangeSpoilerness} />
  142. <span style={{ display: 'inline-block', verticalAlign: 'middle', marginBottom: '14px', marginLeft: '8px', color: '#9baec8' }}><FormattedMessage id='compose_form.spoiler' defaultMessage='Hide behind content warning' /></span>
  143. </label>
  144. <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) }}>
  145. {({ opacity, height }) =>
  146. <label style={{ display: 'block', lineHeight: '24px', verticalAlign: 'middle', height: `${height}px`, overflow: 'hidden', opacity: opacity / 100 }}>
  147. <Toggle checked={this.props.unlisted} onChange={this.handleChangeListability} />
  148. <span style={{ display: 'inline-block', verticalAlign: 'middle', marginBottom: '14px', marginLeft: '8px', color: '#9baec8' }}><FormattedMessage id='compose_form.unlisted' defaultMessage='Do not display in public timeline' /></span>
  149. </label>
  150. }
  151. </Motion>
  152. <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) }}>
  153. {({ opacity, height }) =>
  154. <label style={{ display: 'block', lineHeight: '24px', verticalAlign: 'middle', height: `${height}px`, overflow: 'hidden', opacity: opacity / 100 }}>
  155. <Toggle checked={this.props.sensitive} onChange={this.handleChangeSensitivity} />
  156. <span style={{ display: 'inline-block', verticalAlign: 'middle', marginBottom: '14px', marginLeft: '8px', color: '#9baec8' }}><FormattedMessage id='compose_form.sensitive' defaultMessage='Mark content as sensitive' /></span>
  157. </label>
  158. }
  159. </Motion>
  160. </div>
  161. );
  162. }
  163. });
  164. export default injectIntl(ComposeForm);