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.

170 lines
6.6 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 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 Collapsable from '../../../components/collapsable';
  14. import UnlistedToggleContainer from '../containers/unlisted_toggle_container';
  15. import SpoilerToggleContainer from '../containers/spoiler_toggle_container';
  16. import PrivateToggleContainer from '../containers/private_toggle_container';
  17. import SensitiveToggleContainer from '../containers/sensitive_toggle_container';
  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: 'Publish' }
  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. private: React.PropTypes.bool,
  31. unlisted: React.PropTypes.bool,
  32. spoiler_text: React.PropTypes.string,
  33. fileDropDate: React.PropTypes.instanceOf(Date),
  34. focusDate: React.PropTypes.instanceOf(Date),
  35. preselectDate: React.PropTypes.instanceOf(Date),
  36. is_submitting: React.PropTypes.bool,
  37. is_uploading: React.PropTypes.bool,
  38. me: React.PropTypes.number,
  39. needsPrivacyWarning: React.PropTypes.bool,
  40. mentionedDomains: React.PropTypes.array.isRequired,
  41. onChange: React.PropTypes.func.isRequired,
  42. onSubmit: React.PropTypes.func.isRequired,
  43. onClearSuggestions: React.PropTypes.func.isRequired,
  44. onFetchSuggestions: React.PropTypes.func.isRequired,
  45. onSuggestionSelected: React.PropTypes.func.isRequired,
  46. onChangeSpoilerText: 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. handleChangeSpoilerText (e) {
  71. this.props.onChangeSpoilerText(e.target.value);
  72. },
  73. componentDidUpdate (prevProps) {
  74. if (this.props.focusDate !== prevProps.focusDate) {
  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 selectionEnd = this.props.text.length;
  79. const selectionStart = (this.props.preselectDate !== prevProps.preselectDate) ? (this.props.text.search(/\s/) + 1) : selectionEnd;
  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, needsPrivacyWarning, mentionedDomains } = this.props;
  89. const disabled = this.props.is_submitting || this.props.is_uploading;
  90. let publishText = '';
  91. let privacyWarning = '';
  92. let reply_to_other = false;
  93. if (needsPrivacyWarning) {
  94. privacyWarning = (
  95. <div className='compose-form__warning'>
  96. <FormattedMessage
  97. id='compose_form.privacy_disclaimer'
  98. 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?'
  99. values={{ domains: <strong>{mentionedDomains.join(', ')}</strong>, domainsCount: mentionedDomains.length }}
  100. />
  101. </div>
  102. );
  103. }
  104. if (this.props.private) {
  105. publishText = <span><i className='fa fa-lock' /> {intl.formatMessage(messages.publish)}</span>;
  106. } else {
  107. publishText = intl.formatMessage(messages.publish) + (!this.props.unlisted ? '!' : '');
  108. }
  109. return (
  110. <div style={{ padding: '10px' }}>
  111. <Collapsable isVisible={this.props.spoiler} fullHeight={50}>
  112. <div className="spoiler-input">
  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. </Collapsable>
  116. {privacyWarning}
  117. <ReplyIndicatorContainer />
  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={publishText} 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. <SpoilerToggleContainer />
  137. <PrivateToggleContainer />
  138. <UnlistedToggleContainer />
  139. <SensitiveToggleContainer />
  140. </div>
  141. );
  142. }
  143. });
  144. export default injectIntl(ComposeForm);