The code powering m.abunchtell.com https://m.abunchtell.com
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 

254 righe
9.8 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) > 500 || (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. this.composeForm.scrollIntoView();
  103. }
  104. }
  105. componentDidUpdate (prevProps) {
  106. // This statement does several things:
  107. // - If we're beginning a reply, and,
  108. // - Replying to zero or one users, places the cursor at the end of the textbox.
  109. // - Replying to more than one user, selects any usernames past the first;
  110. // this provides a convenient shortcut to drop everyone else from the conversation.
  111. if (this.props.focusDate !== prevProps.focusDate) {
  112. let selectionEnd, selectionStart;
  113. if (this.props.preselectDate !== prevProps.preselectDate) {
  114. selectionEnd = this.props.text.length;
  115. selectionStart = this.props.text.search(/\s/) + 1;
  116. } else if (typeof this.props.caretPosition === 'number') {
  117. selectionStart = this.props.caretPosition;
  118. selectionEnd = this.props.caretPosition;
  119. } else {
  120. selectionEnd = this.props.text.length;
  121. selectionStart = selectionEnd;
  122. }
  123. this.autosuggestTextarea.textarea.setSelectionRange(selectionStart, selectionEnd);
  124. this.autosuggestTextarea.textarea.focus();
  125. } else if(prevProps.isSubmitting && !this.props.isSubmitting) {
  126. this.autosuggestTextarea.textarea.focus();
  127. } else if (this.props.spoiler !== prevProps.spoiler) {
  128. if (this.props.spoiler) {
  129. this.spoilerText.input.focus();
  130. } else {
  131. this.autosuggestTextarea.textarea.focus();
  132. }
  133. }
  134. }
  135. setAutosuggestTextarea = (c) => {
  136. this.autosuggestTextarea = c;
  137. }
  138. setSpoilerText = (c) => {
  139. this.spoilerText = c;
  140. }
  141. setRef = c => {
  142. this.composeForm = c;
  143. };
  144. handleEmojiPick = (data) => {
  145. const { text } = this.props;
  146. const position = this.autosuggestTextarea.textarea.selectionStart;
  147. const needsSpace = data.custom && position > 0 && !allowedAroundShortCode.includes(text[position - 1]);
  148. this.props.onPickEmoji(position, data, needsSpace);
  149. }
  150. render () {
  151. const { intl, onPaste, showSearch, anyMedia } = this.props;
  152. const disabled = this.props.isSubmitting;
  153. const text = [this.props.spoilerText, countableText(this.props.text)].join('');
  154. const disabledButton = disabled || this.props.isUploading || this.props.isChangingUpload || length(text) > 500 || (text.length !== 0 && text.trim().length === 0 && !anyMedia);
  155. let publishText = '';
  156. if (this.props.privacy === 'private' || this.props.privacy === 'direct') {
  157. publishText = <span className='compose-form__publish-private'><Icon id='lock' /> {intl.formatMessage(messages.publish)}</span>;
  158. } else {
  159. publishText = this.props.privacy !== 'unlisted' ? intl.formatMessage(messages.publishLoud, { publish: intl.formatMessage(messages.publish) }) : intl.formatMessage(messages.publish);
  160. }
  161. return (
  162. <div className='compose-form' ref={this.setRef}>
  163. <WarningContainer />
  164. <ReplyIndicatorContainer />
  165. <div className={`spoiler-input ${this.props.spoiler ? 'spoiler-input--visible' : ''}`}>
  166. <AutosuggestInput
  167. placeholder={intl.formatMessage(messages.spoiler_placeholder)}
  168. value={this.props.spoilerText}
  169. onChange={this.handleChangeSpoilerText}
  170. onKeyDown={this.handleKeyDown}
  171. disabled={!this.props.spoiler}
  172. ref={this.setSpoilerText}
  173. suggestions={this.props.suggestions}
  174. onSuggestionsFetchRequested={this.onSuggestionsFetchRequested}
  175. onSuggestionsClearRequested={this.onSuggestionsClearRequested}
  176. onSuggestionSelected={this.onSpoilerSuggestionSelected}
  177. searchTokens={[':']}
  178. id='cw-spoiler-input'
  179. className='spoiler-input__input'
  180. />
  181. </div>
  182. <AutosuggestTextarea
  183. ref={this.setAutosuggestTextarea}
  184. placeholder={intl.formatMessage(messages.placeholder)}
  185. disabled={disabled}
  186. value={this.props.text}
  187. onChange={this.handleChange}
  188. suggestions={this.props.suggestions}
  189. onFocus={this.handleFocus}
  190. onKeyDown={this.handleKeyDown}
  191. onSuggestionsFetchRequested={this.onSuggestionsFetchRequested}
  192. onSuggestionsClearRequested={this.onSuggestionsClearRequested}
  193. onSuggestionSelected={this.onSuggestionSelected}
  194. onPaste={onPaste}
  195. autoFocus={!showSearch && !isMobile(window.innerWidth)}
  196. >
  197. <EmojiPickerDropdown onPickEmoji={this.handleEmojiPick} />
  198. <div className='compose-form__modifiers'>
  199. <UploadFormContainer />
  200. <PollFormContainer />
  201. </div>
  202. </AutosuggestTextarea>
  203. <div className='compose-form__buttons-wrapper'>
  204. <div className='compose-form__buttons'>
  205. <UploadButtonContainer />
  206. <PollButtonContainer />
  207. <PrivacyDropdownContainer />
  208. <SpoilerButtonContainer />
  209. </div>
  210. <div className='character-counter__wrapper'><CharacterCounter max={500} text={text} /></div>
  211. </div>
  212. <div className='compose-form__publish'>
  213. <div className='compose-form__publish-button-wrapper'><Button text={publishText} onClick={this.handleSubmit} disabled={disabledButton} block /></div>
  214. </div>
  215. </div>
  216. );
  217. }
  218. }