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.

134 lines
5.0 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. const messages = defineMessages({
  14. placeholder: { id: 'compose_form.placeholder', defaultMessage: 'What is on your mind?' },
  15. publish: { id: 'compose_form.publish', defaultMessage: 'Publish' }
  16. });
  17. const ComposeForm = React.createClass({
  18. propTypes: {
  19. text: React.PropTypes.string.isRequired,
  20. suggestion_token: React.PropTypes.string,
  21. suggestions: ImmutablePropTypes.list,
  22. sensitive: React.PropTypes.bool,
  23. unlisted: React.PropTypes.bool,
  24. is_submitting: React.PropTypes.bool,
  25. is_uploading: React.PropTypes.bool,
  26. in_reply_to: ImmutablePropTypes.map,
  27. onChange: React.PropTypes.func.isRequired,
  28. onSubmit: React.PropTypes.func.isRequired,
  29. onCancelReply: React.PropTypes.func.isRequired,
  30. onClearSuggestions: React.PropTypes.func.isRequired,
  31. onFetchSuggestions: React.PropTypes.func.isRequired,
  32. onSuggestionSelected: React.PropTypes.func.isRequired,
  33. onChangeSensitivity: React.PropTypes.func.isRequired,
  34. onChangeVisibility: React.PropTypes.func.isRequired
  35. },
  36. mixins: [PureRenderMixin],
  37. handleChange (e) {
  38. this.props.onChange(e.target.value);
  39. },
  40. handleKeyUp (e) {
  41. if (e.keyCode === 13 && (e.ctrlKey || e.metaKey)) {
  42. this.props.onSubmit();
  43. }
  44. },
  45. handleSubmit () {
  46. this.props.onSubmit();
  47. },
  48. onSuggestionsClearRequested () {
  49. this.props.onClearSuggestions();
  50. },
  51. @debounce(500)
  52. onSuggestionsFetchRequested (token) {
  53. this.props.onFetchSuggestions(token);
  54. },
  55. onSuggestionSelected (tokenStart, token, value) {
  56. this.props.onSuggestionSelected(tokenStart, token, value);
  57. },
  58. handleChangeSensitivity (e) {
  59. this.props.onChangeSensitivity(e.target.checked);
  60. },
  61. handleChangeVisibility (e) {
  62. this.props.onChangeVisibility(e.target.checked);
  63. },
  64. componentDidUpdate (prevProps) {
  65. if (prevProps.in_reply_to !== this.props.in_reply_to) {
  66. this.autosuggestTextarea.textarea.focus();
  67. }
  68. },
  69. setAutosuggestTextarea (c) {
  70. this.autosuggestTextarea = c;
  71. },
  72. render () {
  73. const { intl } = this.props;
  74. let replyArea = '';
  75. const disabled = this.props.is_submitting || this.props.is_uploading;
  76. if (this.props.in_reply_to) {
  77. replyArea = <ReplyIndicator status={this.props.in_reply_to} onCancel={this.props.onCancelReply} />;
  78. }
  79. return (
  80. <div style={{ padding: '10px' }}>
  81. {replyArea}
  82. <AutosuggestTextarea
  83. ref={this.setAutosuggestTextarea}
  84. placeholder={intl.formatMessage(messages.placeholder)}
  85. disabled={disabled}
  86. value={this.props.text}
  87. onChange={this.handleChange}
  88. suggestions={this.props.suggestions}
  89. onKeyUp={this.handleKeyUp}
  90. onSuggestionsFetchRequested={this.onSuggestionsFetchRequested}
  91. onSuggestionsClearRequested={this.onSuggestionsClearRequested}
  92. onSuggestionSelected={this.onSuggestionSelected}
  93. />
  94. <div style={{ marginTop: '10px', overflow: 'hidden' }}>
  95. <div style={{ float: 'right' }}><Button text={intl.formatMessage(messages.publish)} onClick={this.handleSubmit} disabled={disabled} /></div>
  96. <div style={{ float: 'right', marginRight: '16px', lineHeight: '36px' }}><CharacterCounter max={500} text={this.props.text} /></div>
  97. <UploadButtonContainer style={{ paddingTop: '4px' }} />
  98. </div>
  99. <label style={{ display: 'block', lineHeight: '24px', verticalAlign: 'middle', marginTop: '10px', borderTop: '1px solid #282c37', paddingTop: '10px' }}>
  100. <Toggle checked={this.props.private} onChange={this.handleChangeVisibility} />
  101. <span style={{ display: 'inline-block', verticalAlign: 'middle', marginBottom: '14px', marginLeft: '8px', color: '#9baec8' }}><FormattedMessage id='compose_form.private' defaultMessage='Mark as private' /></span>
  102. </label>
  103. <label style={{ display: 'block', lineHeight: '24px', verticalAlign: 'middle' }}>
  104. <Toggle checked={this.props.sensitive} onChange={this.handleChangeSensitivity} />
  105. <span style={{ display: 'inline-block', verticalAlign: 'middle', marginBottom: '14px', marginLeft: '8px', color: '#9baec8' }}><FormattedMessage id='compose_form.sensitive' defaultMessage='Mark content as sensitive' /></span>
  106. </label>
  107. </div>
  108. );
  109. }
  110. });
  111. export default injectIntl(ComposeForm);