The code powering m.abunchtell.com https://m.abunchtell.com
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 

215 Zeilen
8.5 KiB

  1. import React from 'react';
  2. import CharacterCounter from './character_counter';
  3. import Button from '../../../components/button';
  4. import ImmutablePropTypes from 'react-immutable-proptypes';
  5. import PropTypes from 'prop-types';
  6. import ReplyIndicatorContainer from '../containers/reply_indicator_container';
  7. import AutosuggestTextarea from '../../../components/autosuggest_textarea';
  8. import { debounce } from 'lodash';
  9. import UploadButtonContainer from '../containers/upload_button_container';
  10. import { defineMessages, injectIntl } from 'react-intl';
  11. import Collapsable from '../../../components/collapsable';
  12. import SpoilerButtonContainer from '../containers/spoiler_button_container';
  13. import PrivacyDropdownContainer from '../containers/privacy_dropdown_container';
  14. import SensitiveButtonContainer from '../containers/sensitive_button_container';
  15. import EmojiPickerDropdown from '../containers/emoji_picker_dropdown_container';
  16. import UploadFormContainer from '../containers/upload_form_container';
  17. import WarningContainer from '../containers/warning_container';
  18. import { isMobile } from '../../../is_mobile';
  19. import ImmutablePureComponent from 'react-immutable-pure-component';
  20. import { length } from 'stringz';
  21. import { countableText } from '../util/counter';
  22. const messages = defineMessages({
  23. placeholder: { id: 'compose_form.placeholder', defaultMessage: 'What is on your mind?' },
  24. spoiler_placeholder: { id: 'compose_form.spoiler_placeholder', defaultMessage: 'Write your warning here' },
  25. publish: { id: 'compose_form.publish', defaultMessage: 'Toot' },
  26. publishLoud: { id: 'compose_form.publish_loud', defaultMessage: '{publish}!' },
  27. });
  28. @injectIntl
  29. export default class ComposeForm extends ImmutablePureComponent {
  30. static propTypes = {
  31. intl: PropTypes.object.isRequired,
  32. text: PropTypes.string.isRequired,
  33. suggestion_token: PropTypes.string,
  34. suggestions: ImmutablePropTypes.list,
  35. spoiler: PropTypes.bool,
  36. privacy: PropTypes.string,
  37. spoiler_text: PropTypes.string,
  38. focusDate: PropTypes.instanceOf(Date),
  39. preselectDate: PropTypes.instanceOf(Date),
  40. is_submitting: PropTypes.bool,
  41. is_uploading: PropTypes.bool,
  42. me: PropTypes.string,
  43. onChange: PropTypes.func.isRequired,
  44. onSubmit: PropTypes.func.isRequired,
  45. onClearSuggestions: PropTypes.func.isRequired,
  46. onFetchSuggestions: PropTypes.func.isRequired,
  47. onSuggestionSelected: PropTypes.func.isRequired,
  48. onChangeSpoilerText: PropTypes.func.isRequired,
  49. onPaste: PropTypes.func.isRequired,
  50. onPickEmoji: PropTypes.func.isRequired,
  51. showSearch: PropTypes.bool,
  52. };
  53. static defaultProps = {
  54. showSearch: false,
  55. };
  56. handleChange = (e) => {
  57. this.props.onChange(e.target.value);
  58. }
  59. handleKeyDown = (e) => {
  60. if (e.keyCode === 13 && (e.ctrlKey || e.metaKey)) {
  61. this.handleSubmit();
  62. }
  63. }
  64. handleSubmit = () => {
  65. if (this.props.text !== this.autosuggestTextarea.textarea.value) {
  66. // Something changed the text inside the textarea (e.g. browser extensions like Grammarly)
  67. // Update the state to match the current text
  68. this.props.onChange(this.autosuggestTextarea.textarea.value);
  69. }
  70. this.props.onSubmit();
  71. }
  72. onSuggestionsClearRequested = () => {
  73. this.props.onClearSuggestions();
  74. }
  75. onSuggestionsFetchRequested = debounce((token) => {
  76. this.props.onFetchSuggestions(token);
  77. }, 500, { trailing: true })
  78. onSuggestionSelected = (tokenStart, token, value) => {
  79. this._restoreCaret = null;
  80. this.props.onSuggestionSelected(tokenStart, token, value);
  81. }
  82. handleChangeSpoilerText = (e) => {
  83. this.props.onChangeSpoilerText(e.target.value);
  84. }
  85. componentWillReceiveProps (nextProps) {
  86. // If this is the update where we've finished uploading,
  87. // save the last caret position so we can restore it below!
  88. if (!nextProps.is_uploading && this.props.is_uploading) {
  89. this._restoreCaret = this.autosuggestTextarea.textarea.selectionStart;
  90. }
  91. }
  92. componentDidUpdate (prevProps) {
  93. // This statement does several things:
  94. // - If we're beginning a reply, and,
  95. // - Replying to zero or one users, places the cursor at the end of the textbox.
  96. // - Replying to more than one user, selects any usernames past the first;
  97. // this provides a convenient shortcut to drop everyone else from the conversation.
  98. // - If we've just finished uploading an image, and have a saved caret position,
  99. // restores the cursor to that position after the text changes!
  100. if (this.props.focusDate !== prevProps.focusDate || (prevProps.is_uploading && !this.props.is_uploading && typeof this._restoreCaret === 'number')) {
  101. let selectionEnd, selectionStart;
  102. if (this.props.preselectDate !== prevProps.preselectDate) {
  103. selectionEnd = this.props.text.length;
  104. selectionStart = this.props.text.search(/\s/) + 1;
  105. } else if (typeof this._restoreCaret === 'number') {
  106. selectionStart = this._restoreCaret;
  107. selectionEnd = this._restoreCaret;
  108. } else {
  109. selectionEnd = this.props.text.length;
  110. selectionStart = selectionEnd;
  111. }
  112. this.autosuggestTextarea.textarea.setSelectionRange(selectionStart, selectionEnd);
  113. this.autosuggestTextarea.textarea.focus();
  114. } else if(prevProps.is_submitting && !this.props.is_submitting) {
  115. this.autosuggestTextarea.textarea.focus();
  116. }
  117. }
  118. setAutosuggestTextarea = (c) => {
  119. this.autosuggestTextarea = c;
  120. }
  121. handleEmojiPick = (data) => {
  122. const position = this.autosuggestTextarea.textarea.selectionStart;
  123. const emojiChar = data.native;
  124. this._restoreCaret = position + emojiChar.length + 1;
  125. this.props.onPickEmoji(position, data);
  126. }
  127. render () {
  128. const { intl, onPaste, showSearch } = this.props;
  129. const disabled = this.props.is_submitting;
  130. const text = [this.props.spoiler_text, countableText(this.props.text)].join('');
  131. let publishText = '';
  132. if (this.props.privacy === 'private' || this.props.privacy === 'direct') {
  133. publishText = <span className='compose-form__publish-private'><i className='fa fa-lock' /> {intl.formatMessage(messages.publish)}</span>;
  134. } else {
  135. publishText = this.props.privacy !== 'unlisted' ? intl.formatMessage(messages.publishLoud, { publish: intl.formatMessage(messages.publish) }) : intl.formatMessage(messages.publish);
  136. }
  137. return (
  138. <div className='compose-form'>
  139. <Collapsable isVisible={this.props.spoiler} fullHeight={50}>
  140. <div className='spoiler-input'>
  141. <label>
  142. <span style={{ display: 'none' }}>{intl.formatMessage(messages.spoiler_placeholder)}</span>
  143. <input placeholder={intl.formatMessage(messages.spoiler_placeholder)} value={this.props.spoiler_text} onChange={this.handleChangeSpoilerText} onKeyDown={this.handleKeyDown} type='text' className='spoiler-input__input' id='cw-spoiler-input' />
  144. </label>
  145. </div>
  146. </Collapsable>
  147. <WarningContainer />
  148. <ReplyIndicatorContainer />
  149. <div className='compose-form__autosuggest-wrapper'>
  150. <AutosuggestTextarea
  151. ref={this.setAutosuggestTextarea}
  152. placeholder={intl.formatMessage(messages.placeholder)}
  153. disabled={disabled}
  154. value={this.props.text}
  155. onChange={this.handleChange}
  156. suggestions={this.props.suggestions}
  157. onKeyDown={this.handleKeyDown}
  158. onSuggestionsFetchRequested={this.onSuggestionsFetchRequested}
  159. onSuggestionsClearRequested={this.onSuggestionsClearRequested}
  160. onSuggestionSelected={this.onSuggestionSelected}
  161. onPaste={onPaste}
  162. autoFocus={!showSearch && !isMobile(window.innerWidth)}
  163. />
  164. <EmojiPickerDropdown onPickEmoji={this.handleEmojiPick} />
  165. </div>
  166. <div className='compose-form__modifiers'>
  167. <UploadFormContainer />
  168. </div>
  169. <div className='compose-form__buttons-wrapper'>
  170. <div className='compose-form__buttons'>
  171. <UploadButtonContainer />
  172. <PrivacyDropdownContainer />
  173. <SensitiveButtonContainer />
  174. <SpoilerButtonContainer />
  175. </div>
  176. <div className='compose-form__publish'>
  177. <div className='character-counter__wrapper'><CharacterCounter max={500} text={text} /></div>
  178. <div className='compose-form__publish-button-wrapper'><Button text={publishText} onClick={this.handleSubmit} disabled={disabled || this.props.is_uploading || length(text) > 500 || (text.length !== 0 && text.trim().length === 0)} block /></div>
  179. </div>
  180. </div>
  181. </div>
  182. );
  183. }
  184. }