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.

165 lines
7.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 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. publish: { id: 'compose_form.publish', defaultMessage: 'Publish' }
  17. });
  18. const ComposeForm = React.createClass({
  19. propTypes: {
  20. intl: React.PropTypes.object.isRequired,
  21. text: React.PropTypes.string.isRequired,
  22. suggestion_token: React.PropTypes.string,
  23. suggestions: ImmutablePropTypes.list,
  24. sensitive: React.PropTypes.bool,
  25. unlisted: React.PropTypes.bool,
  26. private: React.PropTypes.bool,
  27. fileDropDate: React.PropTypes.instanceOf(Date),
  28. is_submitting: React.PropTypes.bool,
  29. is_uploading: React.PropTypes.bool,
  30. in_reply_to: ImmutablePropTypes.map,
  31. media_count: React.PropTypes.number,
  32. onChange: React.PropTypes.func.isRequired,
  33. onSubmit: React.PropTypes.func.isRequired,
  34. onCancelReply: React.PropTypes.func.isRequired,
  35. onClearSuggestions: React.PropTypes.func.isRequired,
  36. onFetchSuggestions: React.PropTypes.func.isRequired,
  37. onSuggestionSelected: React.PropTypes.func.isRequired,
  38. onChangeSensitivity: React.PropTypes.func.isRequired,
  39. onChangeVisibility: React.PropTypes.func.isRequired,
  40. onChangeListability: React.PropTypes.func.isRequired,
  41. },
  42. mixins: [PureRenderMixin],
  43. handleChange (e) {
  44. this.props.onChange(e.target.value);
  45. },
  46. handleKeyDown (e) {
  47. if (e.keyCode === 13 && (e.ctrlKey || e.metaKey)) {
  48. this.props.onSubmit();
  49. }
  50. },
  51. handleSubmit () {
  52. this.props.onSubmit();
  53. },
  54. onSuggestionsClearRequested () {
  55. this.props.onClearSuggestions();
  56. },
  57. @debounce(500)
  58. onSuggestionsFetchRequested (token) {
  59. this.props.onFetchSuggestions(token);
  60. },
  61. onSuggestionSelected (tokenStart, token, value) {
  62. this.props.onSuggestionSelected(tokenStart, token, value);
  63. },
  64. handleChangeSensitivity (e) {
  65. this.props.onChangeSensitivity(e.target.checked);
  66. },
  67. handleChangeVisibility (e) {
  68. this.props.onChangeVisibility(e.target.checked);
  69. },
  70. handleChangeListability (e) {
  71. this.props.onChangeListability(e.target.checked);
  72. },
  73. componentDidUpdate (prevProps) {
  74. 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'))) {
  75. // If replying to zero or one users, places the cursor at the end of the textbox.
  76. // If replying to more than one user, selects any usernames past the first;
  77. // this provides a convenient shortcut to drop everyone else from the conversation.
  78. const selectionStart = this.props.text.search(/\s/) + 1;
  79. const selectionEnd = this.props.text.length;
  80. this.autosuggestTextarea.textarea.setSelectionRange(selectionStart, selectionEnd);
  81. this.autosuggestTextarea.textarea.focus();
  82. }
  83. },
  84. setAutosuggestTextarea (c) {
  85. this.autosuggestTextarea = c;
  86. },
  87. render () {
  88. const { intl } = this.props;
  89. let replyArea = '';
  90. const disabled = this.props.is_submitting || this.props.is_uploading;
  91. if (this.props.in_reply_to) {
  92. replyArea = <ReplyIndicator status={this.props.in_reply_to} onCancel={this.props.onCancelReply} />;
  93. }
  94. return (
  95. <div style={{ padding: '10px' }}>
  96. {replyArea}
  97. <AutosuggestTextarea
  98. ref={this.setAutosuggestTextarea}
  99. placeholder={intl.formatMessage(messages.placeholder)}
  100. disabled={disabled}
  101. fileDropDate={this.props.fileDropDate}
  102. value={this.props.text}
  103. onChange={this.handleChange}
  104. suggestions={this.props.suggestions}
  105. onKeyDown={this.handleKeyDown}
  106. onSuggestionsFetchRequested={this.onSuggestionsFetchRequested}
  107. onSuggestionsClearRequested={this.onSuggestionsClearRequested}
  108. onSuggestionSelected={this.onSuggestionSelected}
  109. />
  110. <div style={{ marginTop: '10px', overflow: 'hidden' }}>
  111. <div style={{ float: 'right' }}><Button text={intl.formatMessage(messages.publish)} onClick={this.handleSubmit} disabled={disabled} /></div>
  112. <div style={{ float: 'right', marginRight: '16px', lineHeight: '36px' }}><CharacterCounter max={500} text={this.props.text} /></div>
  113. <UploadButtonContainer style={{ paddingTop: '4px' }} />
  114. </div>
  115. <label style={{ display: 'block', lineHeight: '24px', verticalAlign: 'middle', marginTop: '10px', borderTop: '1px solid #282c37', paddingTop: '10px' }}>
  116. <Toggle checked={this.props.private} onChange={this.handleChangeVisibility} />
  117. <span style={{ display: 'inline-block', verticalAlign: 'middle', marginBottom: '14px', marginLeft: '8px', color: '#9baec8' }}><FormattedMessage id='compose_form.private' defaultMessage='Mark as private' /></span>
  118. </label>
  119. <Motion defaultStyle={{ opacity: (this.props.private || this.props.in_reply_to) ? 0 : 100, height: (this.props.private || this.props_in_reply_to) ? 39.5 : 0 }} style={{ opacity: spring((this.props.private || this.props.in_reply_to) ? 0 : 100), height: spring((this.props.private || this.props_in_reply_to) ? 0 : 39.5) }}>
  120. {({ opacity, height }) =>
  121. <label style={{ display: 'block', lineHeight: '24px', verticalAlign: 'middle', height: `${height}px`, overflow: 'hidden', opacity: opacity / 100 }}>
  122. <Toggle checked={this.props.unlisted} onChange={this.handleChangeListability} />
  123. <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>
  124. </label>
  125. }
  126. </Motion>
  127. <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) }}>
  128. {({ opacity, height }) =>
  129. <label style={{ display: 'block', lineHeight: '24px', verticalAlign: 'middle', height: `${height}px`, overflow: 'hidden', opacity: opacity / 100 }}>
  130. <Toggle checked={this.props.sensitive} onChange={this.handleChangeSensitivity} />
  131. <span style={{ display: 'inline-block', verticalAlign: 'middle', marginBottom: '14px', marginLeft: '8px', color: '#9baec8' }}><FormattedMessage id='compose_form.sensitive' defaultMessage='Mark content as sensitive' /></span>
  132. </label>
  133. }
  134. </Motion>
  135. </div>
  136. );
  137. }
  138. });
  139. export default injectIntl(ComposeForm);