The code powering m.abunchtell.com https://m.abunchtell.com
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.
 
 
 
 

212 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 ReplyIndicator from './reply_indicator';
  6. import UploadButton from './upload_button';
  7. import Autosuggest from 'react-autosuggest';
  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 getTokenForSuggestions = (str, caretPosition) => {
  18. let word;
  19. let left = str.slice(0, caretPosition).search(/\S+$/);
  20. let right = str.slice(caretPosition).search(/\s/);
  21. if (right < 0) {
  22. word = str.slice(left);
  23. } else {
  24. word = str.slice(left, right + caretPosition);
  25. }
  26. if (!word || word.trim().length < 2 || word[0] !== '@') {
  27. return null;
  28. }
  29. word = word.trim().toLowerCase().slice(1);
  30. if (word.length > 0) {
  31. return word;
  32. } else {
  33. return null;
  34. }
  35. };
  36. const getSuggestionValue = suggestionId => suggestionId;
  37. const renderSuggestion = suggestionId => <AutosuggestAccountContainer id={suggestionId} />;
  38. const textareaStyle = {
  39. display: 'block',
  40. boxSizing: 'border-box',
  41. width: '100%',
  42. height: '100px',
  43. resize: 'none',
  44. border: 'none',
  45. color: '#282c37',
  46. padding: '10px',
  47. fontFamily: 'Roboto',
  48. fontSize: '14px',
  49. margin: '0',
  50. resize: 'vertical'
  51. };
  52. const renderInputComponent = inputProps => (
  53. <textarea {...inputProps} className='compose-form__textarea' style={textareaStyle} />
  54. );
  55. const ComposeForm = React.createClass({
  56. propTypes: {
  57. text: React.PropTypes.string.isRequired,
  58. suggestion_token: React.PropTypes.string,
  59. suggestions: React.PropTypes.array,
  60. sensitive: React.PropTypes.bool,
  61. unlisted: React.PropTypes.bool,
  62. is_submitting: React.PropTypes.bool,
  63. is_uploading: React.PropTypes.bool,
  64. in_reply_to: ImmutablePropTypes.map,
  65. onChange: React.PropTypes.func.isRequired,
  66. onSubmit: React.PropTypes.func.isRequired,
  67. onCancelReply: React.PropTypes.func.isRequired,
  68. onClearSuggestions: React.PropTypes.func.isRequired,
  69. onFetchSuggestions: React.PropTypes.func.isRequired,
  70. onSuggestionSelected: React.PropTypes.func.isRequired,
  71. onChangeSensitivity: React.PropTypes.func.isRequired,
  72. onChangeVisibility: React.PropTypes.func.isRequired
  73. },
  74. mixins: [PureRenderMixin],
  75. handleChange (e) {
  76. if (typeof e.target.value === 'undefined' || typeof e.target.value === 'number') {
  77. return;
  78. }
  79. this.props.onChange(e.target.value);
  80. },
  81. handleKeyUp (e) {
  82. if (e.keyCode === 13 && e.ctrlKey) {
  83. this.props.onSubmit();
  84. }
  85. },
  86. handleSubmit () {
  87. this.props.onSubmit();
  88. },
  89. componentDidUpdate (prevProps) {
  90. if (prevProps.text !== this.props.text || prevProps.in_reply_to !== this.props.in_reply_to) {
  91. const textarea = this.autosuggest.input;
  92. if (textarea) {
  93. textarea.focus();
  94. }
  95. }
  96. },
  97. onSuggestionsClearRequested () {
  98. this.props.onClearSuggestions();
  99. },
  100. @debounce(500)
  101. onSuggestionsFetchRequested ({ value }) {
  102. const textarea = this.autosuggest.input;
  103. if (textarea) {
  104. const token = getTokenForSuggestions(value, textarea.selectionStart);
  105. if (token !== null) {
  106. this.props.onFetchSuggestions(token);
  107. } else {
  108. this.props.onClearSuggestions();
  109. }
  110. }
  111. },
  112. onSuggestionSelected (e, { suggestionValue }) {
  113. const textarea = this.autosuggest.input;
  114. if (textarea) {
  115. this.props.onSuggestionSelected(textarea.selectionStart, suggestionValue);
  116. }
  117. },
  118. setRef (c) {
  119. this.autosuggest = c;
  120. },
  121. handleChangeSensitivity (e) {
  122. this.props.onChangeSensitivity(e.target.checked);
  123. },
  124. handleChangeVisibility (e) {
  125. this.props.onChangeVisibility(e.target.checked);
  126. },
  127. render () {
  128. const { intl } = this.props;
  129. let replyArea = '';
  130. const disabled = this.props.is_submitting || this.props.is_uploading;
  131. if (this.props.in_reply_to) {
  132. replyArea = <ReplyIndicator status={this.props.in_reply_to} onCancel={this.props.onCancelReply} />;
  133. }
  134. const inputProps = {
  135. placeholder: intl.formatMessage(messages.placeholder),
  136. value: this.props.text,
  137. onKeyUp: this.handleKeyUp,
  138. onChange: this.handleChange,
  139. disabled: disabled
  140. };
  141. return (
  142. <div style={{ padding: '10px' }}>
  143. {replyArea}
  144. <Autosuggest
  145. ref={this.setRef}
  146. suggestions={this.props.suggestions}
  147. focusFirstSuggestion={true}
  148. onSuggestionsFetchRequested={this.onSuggestionsFetchRequested}
  149. onSuggestionsClearRequested={this.onSuggestionsClearRequested}
  150. onSuggestionSelected={this.onSuggestionSelected}
  151. getSuggestionValue={getSuggestionValue}
  152. renderSuggestion={renderSuggestion}
  153. renderInputComponent={renderInputComponent}
  154. inputProps={inputProps}
  155. />
  156. <div style={{ marginTop: '10px', overflow: 'hidden' }}>
  157. <div style={{ float: 'right' }}><Button text={intl.formatMessage(messages.publish)} onClick={this.handleSubmit} disabled={disabled} /></div>
  158. <div style={{ float: 'right', marginRight: '16px', lineHeight: '36px' }}><CharacterCounter max={500} text={this.props.text} /></div>
  159. <UploadButtonContainer style={{ paddingTop: '4px' }} />
  160. </div>
  161. <label style={{ display: 'block', lineHeight: '24px', verticalAlign: 'middle', marginTop: '10px', borderTop: '1px solid #282c37', paddingTop: '10px' }}>
  162. <Toggle checked={this.props.unlisted} onChange={this.handleChangeVisibility} />
  163. <span style={{ display: 'inline-block', verticalAlign: 'middle', marginBottom: '14px', marginLeft: '8px', color: '#9baec8' }}><FormattedMessage id='compose_form.unlisted' defaultMessage='Unlisted mode' /></span>
  164. </label>
  165. <label style={{ display: 'block', lineHeight: '24px', verticalAlign: 'middle' }}>
  166. <Toggle checked={this.props.sensitive} onChange={this.handleChangeSensitivity} />
  167. <span style={{ display: 'inline-block', verticalAlign: 'middle', marginBottom: '14px', marginLeft: '8px', color: '#9baec8' }}><FormattedMessage id='compose_form.sensitive' defaultMessage='Mark content as sensitive' /></span>
  168. </label>
  169. </div>
  170. );
  171. }
  172. });
  173. export default injectIntl(ComposeForm);