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.

244 lines
9.6 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 AutosuggestInput from '../../../components/autosuggest_input';
  9. import PollButtonContainer from '../containers/poll_button_container';
  10. import UploadButtonContainer from '../containers/upload_button_container';
  11. import { defineMessages, injectIntl } from 'react-intl';
  12. import SpoilerButtonContainer from '../containers/spoiler_button_container';
  13. import PrivacyDropdownContainer from '../containers/privacy_dropdown_container';
  14. import EmojiPickerDropdown from '../containers/emoji_picker_dropdown_container';
  15. import PollFormContainer from '../containers/poll_form_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. import Icon from 'mastodon/components/icon';
  23. const allowedAroundShortCode = '><\u0085\u0020\u00a0\u1680\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u202f\u205f\u3000\u2028\u2029\u0009\u000a\u000b\u000c\u000d';
  24. const messages = defineMessages({
  25. placeholder: { id: 'compose_form.placeholder', defaultMessage: 'What is on your mind?' },
  26. spoiler_placeholder: { id: 'compose_form.spoiler_placeholder', defaultMessage: 'Write your warning here' },
  27. publish: { id: 'compose_form.publish', defaultMessage: 'Toot' },
  28. publishLoud: { id: 'compose_form.publish_loud', defaultMessage: '{publish}!' },
  29. });
  30. export default @injectIntl
  31. class ComposeForm extends ImmutablePureComponent {
  32. static contextTypes = {
  33. router: PropTypes.object,
  34. };
  35. static propTypes = {
  36. intl: PropTypes.object.isRequired,
  37. text: PropTypes.string.isRequired,
  38. suggestions: ImmutablePropTypes.list,
  39. spoiler: PropTypes.bool,
  40. privacy: PropTypes.string,
  41. spoilerText: PropTypes.string,
  42. focusDate: PropTypes.instanceOf(Date),
  43. caretPosition: PropTypes.number,
  44. preselectDate: PropTypes.instanceOf(Date),
  45. isSubmitting: PropTypes.bool,
  46. isChangingUpload: PropTypes.bool,
  47. isUploading: PropTypes.bool,
  48. onChange: PropTypes.func.isRequired,
  49. onSubmit: PropTypes.func.isRequired,
  50. onClearSuggestions: PropTypes.func.isRequired,
  51. onFetchSuggestions: PropTypes.func.isRequired,
  52. onSuggestionSelected: PropTypes.func.isRequired,
  53. onChangeSpoilerText: PropTypes.func.isRequired,
  54. onPaste: PropTypes.func.isRequired,
  55. onPickEmoji: PropTypes.func.isRequired,
  56. showSearch: PropTypes.bool,
  57. anyMedia: PropTypes.bool,
  58. };
  59. static defaultProps = {
  60. showSearch: false,
  61. };
  62. handleChange = (e) => {
  63. this.props.onChange(e.target.value);
  64. }
  65. handleKeyDown = (e) => {
  66. if (e.keyCode === 13 && (e.ctrlKey || e.metaKey)) {
  67. this.handleSubmit();
  68. }
  69. }
  70. handleSubmit = () => {
  71. if (this.props.text !== this.autosuggestTextarea.textarea.value) {
  72. // Something changed the text inside the textarea (e.g. browser extensions like Grammarly)
  73. // Update the state to match the current text
  74. this.props.onChange(this.autosuggestTextarea.textarea.value);
  75. }
  76. // Submit disabled:
  77. const { isSubmitting, isChangingUpload, isUploading, anyMedia } = this.props;
  78. const fulltext = [this.props.spoilerText, countableText(this.props.text)].join('');
  79. if (isSubmitting || isUploading || isChangingUpload || length(fulltext) > 500 || (fulltext.length !== 0 && fulltext.trim().length === 0 && !anyMedia)) {
  80. return;
  81. }
  82. this.props.onSubmit(this.context.router ? this.context.router.history : null);
  83. }
  84. onSuggestionsClearRequested = () => {
  85. this.props.onClearSuggestions();
  86. }
  87. onSuggestionsFetchRequested = (token) => {
  88. this.props.onFetchSuggestions(token);
  89. }
  90. onSuggestionSelected = (tokenStart, token, value) => {
  91. this.props.onSuggestionSelected(tokenStart, token, value, ['text']);
  92. }
  93. onSpoilerSuggestionSelected = (tokenStart, token, value) => {
  94. this.props.onSuggestionSelected(tokenStart, token, value, ['spoiler_text']);
  95. }
  96. handleChangeSpoilerText = (e) => {
  97. this.props.onChangeSpoilerText(e.target.value);
  98. }
  99. componentDidUpdate (prevProps) {
  100. // This statement does several things:
  101. // - If we're beginning a reply, and,
  102. // - Replying to zero or one users, places the cursor at the end of the textbox.
  103. // - Replying to more than one user, selects any usernames past the first;
  104. // this provides a convenient shortcut to drop everyone else from the conversation.
  105. if (this.props.focusDate !== prevProps.focusDate) {
  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.props.caretPosition === 'number') {
  111. selectionStart = this.props.caretPosition;
  112. selectionEnd = this.props.caretPosition;
  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.isSubmitting && !this.props.isSubmitting) {
  120. this.autosuggestTextarea.textarea.focus();
  121. } else if (this.props.spoiler !== prevProps.spoiler) {
  122. if (this.props.spoiler) {
  123. this.spoilerText.input.focus();
  124. } else {
  125. this.autosuggestTextarea.textarea.focus();
  126. }
  127. }
  128. }
  129. setAutosuggestTextarea = (c) => {
  130. this.autosuggestTextarea = c;
  131. }
  132. setSpoilerText = (c) => {
  133. this.spoilerText = c;
  134. }
  135. handleEmojiPick = (data) => {
  136. const { text } = this.props;
  137. const position = this.autosuggestTextarea.textarea.selectionStart;
  138. const needsSpace = data.custom && position > 0 && !allowedAroundShortCode.includes(text[position - 1]);
  139. this.props.onPickEmoji(position, data, needsSpace);
  140. }
  141. render () {
  142. const { intl, onPaste, showSearch, anyMedia } = this.props;
  143. const disabled = this.props.isSubmitting;
  144. const text = [this.props.spoilerText, countableText(this.props.text)].join('');
  145. const disabledButton = disabled || this.props.isUploading || this.props.isChangingUpload || length(text) > 500 || (text.length !== 0 && text.trim().length === 0 && !anyMedia);
  146. let publishText = '';
  147. if (this.props.privacy === 'private' || this.props.privacy === 'direct') {
  148. publishText = <span className='compose-form__publish-private'><Icon id='lock' /> {intl.formatMessage(messages.publish)}</span>;
  149. } else {
  150. publishText = this.props.privacy !== 'unlisted' ? intl.formatMessage(messages.publishLoud, { publish: intl.formatMessage(messages.publish) }) : intl.formatMessage(messages.publish);
  151. }
  152. return (
  153. <div className='compose-form'>
  154. <WarningContainer />
  155. <ReplyIndicatorContainer />
  156. <div className={`spoiler-input ${this.props.spoiler ? 'spoiler-input--visible' : ''}`}>
  157. <AutosuggestInput
  158. placeholder={intl.formatMessage(messages.spoiler_placeholder)}
  159. value={this.props.spoilerText}
  160. onChange={this.handleChangeSpoilerText}
  161. onKeyDown={this.handleKeyDown}
  162. disabled={!this.props.spoiler}
  163. ref={this.setSpoilerText}
  164. suggestions={this.props.suggestions}
  165. onSuggestionsFetchRequested={this.onSuggestionsFetchRequested}
  166. onSuggestionsClearRequested={this.onSuggestionsClearRequested}
  167. onSuggestionSelected={this.onSpoilerSuggestionSelected}
  168. searchTokens={[':']}
  169. id='cw-spoiler-input'
  170. className='spoiler-input__input'
  171. />
  172. </div>
  173. <div className='compose-form__autosuggest-wrapper'>
  174. <AutosuggestTextarea
  175. ref={this.setAutosuggestTextarea}
  176. placeholder={intl.formatMessage(messages.placeholder)}
  177. disabled={disabled}
  178. value={this.props.text}
  179. onChange={this.handleChange}
  180. suggestions={this.props.suggestions}
  181. onKeyDown={this.handleKeyDown}
  182. onSuggestionsFetchRequested={this.onSuggestionsFetchRequested}
  183. onSuggestionsClearRequested={this.onSuggestionsClearRequested}
  184. onSuggestionSelected={this.onSuggestionSelected}
  185. onPaste={onPaste}
  186. autoFocus={!showSearch && !isMobile(window.innerWidth)}
  187. />
  188. <EmojiPickerDropdown onPickEmoji={this.handleEmojiPick} />
  189. </div>
  190. <div className='compose-form__modifiers'>
  191. <UploadFormContainer />
  192. <PollFormContainer />
  193. </div>
  194. <div className='compose-form__buttons-wrapper'>
  195. <div className='compose-form__buttons'>
  196. <UploadButtonContainer />
  197. <PollButtonContainer />
  198. <PrivacyDropdownContainer />
  199. <SpoilerButtonContainer />
  200. </div>
  201. <div className='character-counter__wrapper'><CharacterCounter max={500} text={text} /></div>
  202. </div>
  203. <div className='compose-form__publish'>
  204. <div className='compose-form__publish-button-wrapper'><Button text={publishText} onClick={this.handleSubmit} disabled={disabledButton} block /></div>
  205. </div>
  206. </div>
  207. );
  208. }
  209. }