The code powering m.abunchtell.com https://m.abunchtell.com
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 

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