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.

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