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.

256 lines
9.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 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. singleColumn: PropTypes.bool,
  59. };
  60. static defaultProps = {
  61. showSearch: false,
  62. };
  63. handleChange = (e) => {
  64. this.props.onChange(e.target.value);
  65. }
  66. handleKeyDown = (e) => {
  67. if (e.keyCode === 13 && (e.ctrlKey || e.metaKey)) {
  68. this.handleSubmit();
  69. }
  70. }
  71. handleSubmit = () => {
  72. if (this.props.text !== this.autosuggestTextarea.textarea.value) {
  73. // Something changed the text inside the textarea (e.g. browser extensions like Grammarly)
  74. // Update the state to match the current text
  75. this.props.onChange(this.autosuggestTextarea.textarea.value);
  76. }
  77. // Submit disabled:
  78. const { isSubmitting, isChangingUpload, isUploading, anyMedia } = this.props;
  79. const fulltext = [this.props.spoilerText, countableText(this.props.text)].join('');
  80. if (isSubmitting || isUploading || isChangingUpload || length(fulltext) > 5000 || (fulltext.length !== 0 && fulltext.trim().length === 0 && !anyMedia)) {
  81. return;
  82. }
  83. this.props.onSubmit(this.context.router ? this.context.router.history : null);
  84. }
  85. onSuggestionsClearRequested = () => {
  86. this.props.onClearSuggestions();
  87. }
  88. onSuggestionsFetchRequested = (token) => {
  89. this.props.onFetchSuggestions(token);
  90. }
  91. onSuggestionSelected = (tokenStart, token, value) => {
  92. this.props.onSuggestionSelected(tokenStart, token, value, ['text']);
  93. }
  94. onSpoilerSuggestionSelected = (tokenStart, token, value) => {
  95. this.props.onSuggestionSelected(tokenStart, token, value, ['spoiler_text']);
  96. }
  97. handleChangeSpoilerText = (e) => {
  98. this.props.onChangeSpoilerText(e.target.value);
  99. }
  100. handleFocus = () => {
  101. if (this.composeForm && !this.props.singleColumn) {
  102. const { left, right } = this.composeForm.getBoundingClientRect();
  103. if (left < 0 || right > (window.innerWidth || document.documentElement.clientWidth)) {
  104. this.composeForm.scrollIntoView();
  105. }
  106. }
  107. }
  108. componentDidUpdate (prevProps) {
  109. // This statement does several things:
  110. // - If we're beginning a reply, and,
  111. // - Replying to zero or one users, places the cursor at the end of the textbox.
  112. // - Replying to more than one user, selects any usernames past the first;
  113. // this provides a convenient shortcut to drop everyone else from the conversation.
  114. if (this.props.focusDate !== prevProps.focusDate) {
  115. let selectionEnd, selectionStart;
  116. if (this.props.preselectDate !== prevProps.preselectDate) {
  117. selectionEnd = this.props.text.length;
  118. selectionStart = this.props.text.search(/\s/) + 1;
  119. } else if (typeof this.props.caretPosition === 'number') {
  120. selectionStart = this.props.caretPosition;
  121. selectionEnd = this.props.caretPosition;
  122. } else {
  123. selectionEnd = this.props.text.length;
  124. selectionStart = selectionEnd;
  125. }
  126. this.autosuggestTextarea.textarea.setSelectionRange(selectionStart, selectionEnd);
  127. this.autosuggestTextarea.textarea.focus();
  128. } else if(prevProps.isSubmitting && !this.props.isSubmitting) {
  129. this.autosuggestTextarea.textarea.focus();
  130. } else if (this.props.spoiler !== prevProps.spoiler) {
  131. if (this.props.spoiler) {
  132. this.spoilerText.input.focus();
  133. } else {
  134. this.autosuggestTextarea.textarea.focus();
  135. }
  136. }
  137. }
  138. setAutosuggestTextarea = (c) => {
  139. this.autosuggestTextarea = c;
  140. }
  141. setSpoilerText = (c) => {
  142. this.spoilerText = c;
  143. }
  144. setRef = c => {
  145. this.composeForm = c;
  146. };
  147. handleEmojiPick = (data) => {
  148. const { text } = this.props;
  149. const position = this.autosuggestTextarea.textarea.selectionStart;
  150. const needsSpace = data.custom && position > 0 && !allowedAroundShortCode.includes(text[position - 1]);
  151. this.props.onPickEmoji(position, data, needsSpace);
  152. }
  153. render () {
  154. const { intl, onPaste, showSearch, anyMedia } = this.props;
  155. const disabled = this.props.isSubmitting;
  156. const text = [this.props.spoilerText, countableText(this.props.text)].join('');
  157. const disabledButton = disabled || this.props.isUploading || this.props.isChangingUpload || length(text) > 5000 || (text.length !== 0 && text.trim().length === 0 && !anyMedia);
  158. let publishText = '';
  159. if (this.props.privacy === 'private' || this.props.privacy === 'direct') {
  160. publishText = <span className='compose-form__publish-private'><Icon id='lock' /> {intl.formatMessage(messages.publish)}</span>;
  161. } else {
  162. publishText = this.props.privacy !== 'unlisted' ? intl.formatMessage(messages.publishLoud, { publish: intl.formatMessage(messages.publish) }) : intl.formatMessage(messages.publish);
  163. }
  164. return (
  165. <div className='compose-form'>
  166. <WarningContainer />
  167. <ReplyIndicatorContainer />
  168. <div className={`spoiler-input ${this.props.spoiler ? 'spoiler-input--visible' : ''}`} ref={this.setRef}>
  169. <AutosuggestInput
  170. placeholder={intl.formatMessage(messages.spoiler_placeholder)}
  171. value={this.props.spoilerText}
  172. onChange={this.handleChangeSpoilerText}
  173. onKeyDown={this.handleKeyDown}
  174. disabled={!this.props.spoiler}
  175. ref={this.setSpoilerText}
  176. suggestions={this.props.suggestions}
  177. onSuggestionsFetchRequested={this.onSuggestionsFetchRequested}
  178. onSuggestionsClearRequested={this.onSuggestionsClearRequested}
  179. onSuggestionSelected={this.onSpoilerSuggestionSelected}
  180. searchTokens={[':']}
  181. id='cw-spoiler-input'
  182. className='spoiler-input__input'
  183. />
  184. </div>
  185. <AutosuggestTextarea
  186. ref={this.setAutosuggestTextarea}
  187. placeholder={intl.formatMessage(messages.placeholder)}
  188. disabled={disabled}
  189. value={this.props.text}
  190. onChange={this.handleChange}
  191. suggestions={this.props.suggestions}
  192. onFocus={this.handleFocus}
  193. onKeyDown={this.handleKeyDown}
  194. onSuggestionsFetchRequested={this.onSuggestionsFetchRequested}
  195. onSuggestionsClearRequested={this.onSuggestionsClearRequested}
  196. onSuggestionSelected={this.onSuggestionSelected}
  197. onPaste={onPaste}
  198. autoFocus={!showSearch && !isMobile(window.innerWidth)}
  199. >
  200. <EmojiPickerDropdown onPickEmoji={this.handleEmojiPick} />
  201. <div className='compose-form__modifiers'>
  202. <UploadFormContainer />
  203. <PollFormContainer />
  204. </div>
  205. </AutosuggestTextarea>
  206. <div className='compose-form__buttons-wrapper'>
  207. <div className='compose-form__buttons'>
  208. <UploadButtonContainer />
  209. <PollButtonContainer />
  210. <PrivacyDropdownContainer />
  211. <SpoilerButtonContainer />
  212. </div>
  213. <div className='character-counter__wrapper'><CharacterCounter max={5000} text={text} /></div>
  214. </div>
  215. <div className='compose-form__publish'>
  216. <div className='compose-form__publish-button-wrapper'><Button text={publishText} onClick={this.handleSubmit} disabled={disabledButton} block /></div>
  217. </div>
  218. </div>
  219. );
  220. }
  221. }