logo

mastofe

My custom branche(s) on git.pleroma.social/pleroma/mastofe git clone https://hacktivis.me/git/mastofe.git

compose_form.js (9149B)


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