2017-12-24 00:16:45 -06:00
import React from 'react' ;
import ImmutablePropTypes from 'react-immutable-proptypes' ;
2019-04-21 12:07:48 -05:00
import PropTypes from 'prop-types' ;
import ReplyIndicatorContainer from '../containers/reply_indicator_container' ;
import AutosuggestTextarea from '../../../components/autosuggest_textarea' ;
2019-04-11 10:18:55 -05:00
import AutosuggestInput from '../../../components/autosuggest_input' ;
2019-04-20 14:28:03 -05:00
import { defineMessages , injectIntl } from 'react-intl' ;
2019-04-21 12:07:48 -05:00
import EmojiPicker from 'flavours/glitch/features/emoji_picker' ;
import PollFormContainer from '../containers/poll_form_container' ;
import UploadFormContainer from '../containers/upload_form_container' ;
import WarningContainer from '../containers/warning_container' ;
import { isMobile } from 'flavours/glitch/util/is_mobile' ;
2019-04-20 14:28:03 -05:00
import ImmutablePureComponent from 'react-immutable-pure-component' ;
2019-04-21 12:07:48 -05:00
import { countableText } from 'flavours/glitch/util/counter' ;
2019-04-21 11:31:26 -05:00
import OptionsContainer from '../containers/options_container' ;
2019-04-21 11:48:33 -05:00
import Publisher from './publisher' ;
2019-04-21 08:57:06 -05:00
import TextareaIcons from './textarea_icons' ;
2019-08-19 14:41:41 -05:00
import { maxChars } from 'flavours/glitch/util/initial_state' ;
import CharacterCounter from './character_counter' ;
2017-12-24 00:16:45 -06:00
2018-08-29 08:26:24 -05:00
const messages = defineMessages ( {
2019-04-21 05:44:30 -05:00
placeholder : { id : 'compose_form.placeholder' , defaultMessage : 'What is on your mind?' } ,
2018-08-29 08:26:24 -05:00
missingDescriptionMessage : { id : 'confirmations.missing_media_description.message' ,
defaultMessage : 'At least one media attachment is lacking a description. Consider describing all media attachments for the visually impaired before sending your toot.' } ,
missingDescriptionConfirm : { id : 'confirmations.missing_media_description.confirm' ,
defaultMessage : 'Send anyway' } ,
2019-04-20 16:02:09 -05:00
spoiler _placeholder : { id : 'compose_form.spoiler_placeholder' , defaultMessage : 'Write your warning here' } ,
2018-08-29 08:26:24 -05:00
} ) ;
2019-04-20 14:28:03 -05:00
export default @ injectIntl
class ComposeForm extends ImmutablePureComponent {
2017-12-24 00:16:45 -06:00
2019-04-20 14:28:03 -05:00
static contextTypes = {
router : PropTypes . object ,
} ;
2017-12-24 00:16:45 -06:00
2019-04-20 14:28:03 -05:00
static propTypes = {
intl : PropTypes . object . isRequired ,
2019-04-21 12:07:48 -05:00
text : PropTypes . string ,
suggestions : ImmutablePropTypes . list ,
spoiler : PropTypes . bool ,
privacy : PropTypes . string ,
spoilerText : PropTypes . string ,
2019-04-20 14:28:03 -05:00
focusDate : PropTypes . instanceOf ( Date ) ,
caretPosition : PropTypes . number ,
2019-04-21 12:07:48 -05:00
preselectDate : PropTypes . instanceOf ( Date ) ,
2019-04-20 14:28:03 -05:00
isSubmitting : PropTypes . bool ,
isChangingUpload : PropTypes . bool ,
isUploading : PropTypes . bool ,
2019-04-21 12:07:48 -05:00
onChange : PropTypes . func ,
onSubmit : PropTypes . func ,
onClearSuggestions : PropTypes . func ,
onFetchSuggestions : PropTypes . func ,
onSuggestionSelected : PropTypes . func ,
onChangeSpoilerText : PropTypes . func ,
onPaste : PropTypes . func ,
onPickEmoji : PropTypes . func ,
showSearch : PropTypes . bool ,
anyMedia : PropTypes . bool ,
2019-06-16 09:02:26 -05:00
singleColumn : PropTypes . bool ,
2019-04-21 12:07:48 -05:00
advancedOptions : ImmutablePropTypes . map ,
2019-04-20 14:28:03 -05:00
layout : PropTypes . string ,
media : ImmutablePropTypes . list ,
sideArm : PropTypes . string ,
sensitive : PropTypes . bool ,
spoilersAlwaysOn : PropTypes . bool ,
mediaDescriptionConfirmation : PropTypes . bool ,
preselectOnReply : PropTypes . bool ,
onChangeSpoilerness : PropTypes . func ,
onChangeVisibility : PropTypes . func ,
2019-04-21 12:07:48 -05:00
onPaste : PropTypes . func ,
2019-04-20 14:28:03 -05:00
onMediaDescriptionConfirm : PropTypes . func ,
} ;
2017-12-24 00:16:45 -06:00
2019-04-21 12:07:48 -05:00
static defaultProps = {
showSearch : false ,
} ;
2017-12-24 00:16:45 -06:00
2019-04-21 12:07:48 -05:00
handleChange = ( e ) => {
this . props . onChange ( e . target . value ) ;
2019-04-20 14:28:03 -05:00
}
2017-12-24 00:16:45 -06:00
2019-04-20 16:02:09 -05:00
handleKeyDown = ( { ctrlKey , keyCode , metaKey , altKey } ) => {
// We submit the status on control/meta + enter.
if ( keyCode === 13 && ( ctrlKey || metaKey ) ) {
2019-04-21 15:59:58 -05:00
this . handleSubmit ( ) ;
2019-04-20 16:02:09 -05:00
}
// Submit the status with secondary visibility on alt + enter.
if ( keyCode === 13 && altKey ) {
2019-04-21 15:59:58 -05:00
this . handleSecondarySubmit ( ) ;
2019-04-20 16:02:09 -05:00
}
}
2019-04-20 14:28:03 -05:00
handleSubmit = ( ) => {
2018-08-30 04:46:45 -05:00
const { textarea : { value } , uploadForm } = this ;
2017-12-24 00:16:45 -06:00
const {
2019-04-21 12:07:48 -05:00
onChange ,
2018-01-03 14:36:21 -06:00
onSubmit ,
2018-04-02 10:22:32 -05:00
isSubmitting ,
2018-12-16 08:26:04 -06:00
isChangingUpload ,
2018-04-02 10:22:32 -05:00
isUploading ,
2018-08-29 08:26:24 -05:00
media ,
2018-04-02 10:22:32 -05:00
anyMedia ,
2018-01-03 14:36:21 -06:00
text ,
2018-08-29 08:26:24 -05:00
mediaDescriptionConfirmation ,
onMediaDescriptionConfirm ,
2017-12-24 00:16:45 -06:00
} = this . props ;
// If something changes inside the textarea, then we update the
// state before submitting.
2019-04-21 12:07:48 -05:00
if ( onChange && text !== value ) {
onChange ( value ) ;
2017-12-24 00:16:45 -06:00
}
2018-04-02 10:22:32 -05:00
// Submit disabled:
2018-12-16 08:26:04 -06:00
if ( isSubmitting || isUploading || isChangingUpload || ( ! text . trim ( ) . length && ! anyMedia ) ) {
2018-04-02 10:22:32 -05:00
return ;
}
2018-08-29 08:26:24 -05:00
// Submit unless there are media with missing descriptions
if ( mediaDescriptionConfirmation && onMediaDescriptionConfirm && media && media . some ( item => ! item . get ( 'description' ) ) ) {
2019-08-20 04:28:17 -05:00
const firstWithoutDescription = media . find ( item => ! item . get ( 'description' ) ) ;
onMediaDescriptionConfirm ( this . context . router ? this . context . router . history : null , firstWithoutDescription . get ( 'id' ) ) ;
2018-08-29 08:26:24 -05:00
} else if ( onSubmit ) {
2018-12-13 09:50:37 -06:00
onSubmit ( this . context . router ? this . context . router . history : null ) ;
2017-12-24 00:16:45 -06:00
}
2019-04-20 14:28:03 -05:00
}
2017-12-24 00:16:45 -06:00
2019-04-21 12:07:48 -05:00
// Changes the text value of the spoiler.
handleChangeSpoiler = ( { target : { value } } ) => {
const { onChangeSpoilerText } = this . props ;
if ( onChangeSpoilerText ) {
onChangeSpoilerText ( value ) ;
}
}
2019-06-07 10:15:18 -05:00
setRef = c => {
this . composeForm = c ;
} ;
2019-04-21 12:07:48 -05:00
// Inserts an emoji at the caret.
handleEmoji = ( data ) => {
const { textarea : { selectionStart } } = this ;
const { onPickEmoji } = this . props ;
if ( onPickEmoji ) {
onPickEmoji ( selectionStart , data ) ;
}
}
// Handles the secondary submit button.
handleSecondarySubmit = ( ) => {
const {
onChangeVisibility ,
sideArm ,
} = this . props ;
if ( sideArm !== 'none' && onChangeVisibility ) {
onChangeVisibility ( sideArm ) ;
}
2019-04-21 15:59:58 -05:00
this . handleSubmit ( ) ;
2019-04-21 12:07:48 -05:00
}
// Selects a suggestion from the autofill.
onSuggestionSelected = ( tokenStart , token , value ) => {
2019-04-11 10:18:55 -05:00
this . props . onSuggestionSelected ( tokenStart , token , value , [ 'text' ] ) ;
}
onSpoilerSuggestionSelected = ( tokenStart , token , value ) => {
this . props . onSuggestionSelected ( tokenStart , token , value , [ 'spoiler_text' ] ) ;
2019-04-21 12:07:48 -05:00
}
// When the escape key is released, we focus the UI.
handleKeyUp = ( { key } ) => {
if ( key === 'Escape' ) {
document . querySelector ( '.ui' ) . parentElement . focus ( ) ;
}
}
2017-12-24 00:16:45 -06:00
// Sets a reference to the textarea.
2019-04-21 05:44:30 -05:00
setAutosuggestTextarea = ( textareaComponent ) => {
2018-01-03 14:36:21 -06:00
if ( textareaComponent ) {
this . textarea = textareaComponent . textarea ;
}
2019-04-20 14:28:03 -05:00
}
2018-08-18 13:53:46 -05:00
// Sets a reference to the CW field.
2019-04-20 14:28:03 -05:00
handleRefSpoilerText = ( spoilerComponent ) => {
2018-08-18 13:53:46 -05:00
if ( spoilerComponent ) {
2019-04-11 10:18:55 -05:00
this . spoilerText = spoilerComponent . input ;
2018-08-18 13:53:46 -05:00
}
}
2017-12-24 00:16:45 -06:00
2019-06-05 08:29:45 -05:00
handleFocus = ( ) => {
2019-06-16 09:02:26 -05:00
if ( this . composeForm && ! this . props . singleColumn ) {
2019-07-06 11:18:08 -05:00
const { left , right } = this . composeForm . getBoundingClientRect ( ) ;
if ( left < 0 || right > ( window . innerWidth || document . documentElement . clientWidth ) ) {
this . composeForm . scrollIntoView ( ) ;
}
2019-06-07 10:15:18 -05:00
}
2019-06-05 08:29:45 -05:00
}
2017-12-24 00:16:45 -06:00
// This statement does several things:
// - If we're beginning a reply, and,
// - Replying to zero or one users, places the cursor at the end
// of the textbox.
// - Replying to more than one user, selects any usernames past
// the first; this provides a convenient shortcut to drop
// everyone else from the conversation.
componentDidUpdate ( prevProps ) {
const {
textarea ,
2018-08-18 13:53:46 -05:00
spoilerText ,
2017-12-24 00:16:45 -06:00
} = this ;
const {
2018-01-03 14:36:21 -06:00
focusDate ,
2018-05-22 14:09:07 -05:00
caretPosition ,
2018-01-03 14:36:21 -06:00
isSubmitting ,
preselectDate ,
text ,
2018-09-28 13:58:03 -05:00
preselectOnReply ,
2019-06-16 09:02:26 -05:00
singleColumn ,
2017-12-24 00:16:45 -06:00
} = this . props ;
let selectionEnd , selectionStart ;
// Caret/selection handling.
2018-05-22 13:21:09 -05:00
if ( focusDate !== prevProps . focusDate ) {
2017-12-24 00:16:45 -06:00
switch ( true ) {
2018-09-28 13:58:03 -05:00
case preselectDate !== prevProps . preselectDate && preselectOnReply :
2017-12-24 00:16:45 -06:00
selectionStart = text . search ( /\s/ ) + 1 ;
selectionEnd = text . length ;
break ;
2018-05-22 14:09:07 -05:00
case ! isNaN ( caretPosition ) && caretPosition !== null :
selectionStart = selectionEnd = caretPosition ;
2017-12-24 00:16:45 -06:00
break ;
default :
selectionStart = selectionEnd = text . length ;
}
2018-01-03 14:36:21 -06:00
if ( textarea ) {
textarea . setSelectionRange ( selectionStart , selectionEnd ) ;
textarea . focus ( ) ;
2019-06-16 09:02:26 -05:00
if ( ! singleColumn ) textarea . scrollIntoView ( ) ;
2018-01-03 14:36:21 -06:00
}
2017-12-24 00:16:45 -06:00
// Refocuses the textarea after submitting.
2018-01-03 14:36:21 -06:00
} else if ( textarea && prevProps . isSubmitting && ! isSubmitting ) {
2017-12-24 00:16:45 -06:00
textarea . focus ( ) ;
2018-08-18 13:53:46 -05:00
} else if ( this . props . spoiler !== prevProps . spoiler ) {
if ( this . props . spoiler ) {
if ( spoilerText ) {
spoilerText . focus ( ) ;
}
} else {
if ( textarea ) {
textarea . focus ( ) ;
}
}
2017-12-24 00:16:45 -06:00
}
}
2019-04-21 05:44:30 -05:00
2017-12-24 00:16:45 -06:00
render ( ) {
const {
2018-01-03 14:36:21 -06:00
handleEmoji ,
handleSecondarySubmit ,
handleSelect ,
handleSubmit ,
handleRefTextarea ,
2019-04-20 14:28:03 -05:00
} = this ;
2017-12-24 00:16:45 -06:00
const {
2018-01-05 20:23:06 -06:00
advancedOptions ,
2018-03-12 12:39:07 -05:00
anyMedia ,
2017-12-24 00:16:45 -06:00
intl ,
2018-01-03 14:36:21 -06:00
isSubmitting ,
2018-12-16 08:26:04 -06:00
isChangingUpload ,
2018-01-03 14:36:21 -06:00
isUploading ,
layout ,
media ,
onChangeSpoilerness ,
onChangeVisibility ,
onClearSuggestions ,
onFetchSuggestions ,
2019-04-21 12:07:48 -05:00
onPaste ,
2018-01-03 14:36:21 -06:00
privacy ,
sensitive ,
showSearch ,
sideArm ,
spoiler ,
spoilerText ,
suggestions ,
text ,
2018-08-22 08:58:57 -05:00
spoilersAlwaysOn ,
2017-12-24 00:16:45 -06:00
} = this . props ;
2018-12-16 08:26:04 -06:00
let disabledButton = isSubmitting || isUploading || isChangingUpload || ( ! text . trim ( ) . length && ! anyMedia ) ;
2018-03-12 12:39:07 -05:00
2019-08-19 14:41:41 -05:00
const countText = ` ${ spoilerText } ${ countableText ( text ) } ${ advancedOptions && advancedOptions . get ( 'do_not_federate' ) ? ' 👁️' : '' } ` ;
2017-12-24 00:16:45 -06:00
return (
2019-06-25 15:57:56 -05:00
< div className = 'composer' >
2019-04-20 15:05:09 -05:00
< WarningContainer / >
2019-04-20 15:21:28 -05:00
< ReplyIndicatorContainer / >
2019-04-20 15:05:09 -05:00
2019-06-25 15:57:56 -05:00
< div className = { ` composer--spoiler ${ spoiler ? 'composer--spoiler--visible' : '' } ` } ref = { this . setRef } >
2019-04-11 10:18:55 -05:00
< AutosuggestInput
placeholder = { intl . formatMessage ( messages . spoiler _placeholder ) }
value = { spoilerText }
onChange = { this . handleChangeSpoiler }
onKeyDown = { this . handleKeyDown }
onKeyUp = { this . handleKeyUp }
disabled = { ! spoiler }
ref = { this . handleRefSpoilerText }
suggestions = { this . props . suggestions }
onSuggestionsFetchRequested = { onFetchSuggestions }
onSuggestionsClearRequested = { onClearSuggestions }
onSuggestionSelected = { this . onSpoilerSuggestionSelected }
searchTokens = { [ ':' ] }
2019-05-03 11:54:06 -05:00
id = 'glitch.composer.spoiler.input'
className = 'spoiler-input__input'
2019-06-06 05:14:11 -05:00
autoFocus = { false }
2019-04-11 10:18:55 -05:00
/ >
2019-04-20 16:02:09 -05:00
< / d i v >
2019-04-20 15:05:09 -05:00
2019-06-02 03:05:54 -05:00
< AutosuggestTextarea
ref = { this . setAutosuggestTextarea }
placeholder = { intl . formatMessage ( messages . placeholder ) }
disabled = { isSubmitting }
value = { this . props . text }
onChange = { this . handleChange }
suggestions = { this . props . suggestions }
onFocus = { this . handleFocus }
onKeyDown = { this . handleKeyDown }
onSuggestionsFetchRequested = { onFetchSuggestions }
onSuggestionsClearRequested = { onClearSuggestions }
onSuggestionSelected = { this . onSuggestionSelected }
onPaste = { onPaste }
autoFocus = { ! showSearch && ! isMobile ( window . innerWidth , layout ) }
>
2019-06-13 10:07:43 -05:00
< EmojiPicker onPickEmoji = { handleEmoji } / >
2019-06-02 03:05:54 -05:00
< TextareaIcons advancedOptions = { advancedOptions } / >
< div className = 'compose-form__modifiers' >
< UploadFormContainer / >
< PollFormContainer / >
< / d i v >
< / A u t o s u g g e s t T e x t a r e a >
2019-04-21 05:44:30 -05:00
2019-08-19 14:41:41 -05:00
< div className = 'composer--options-wrapper' >
< OptionsContainer
advancedOptions = { advancedOptions }
disabled = { isSubmitting }
onChangeVisibility = { onChangeVisibility }
onToggleSpoiler = { spoilersAlwaysOn ? null : onChangeSpoilerness }
onUpload = { onPaste }
privacy = { privacy }
sensitive = { sensitive || ( spoilersAlwaysOn && spoilerText && spoilerText . length > 0 ) }
spoiler = { spoilersAlwaysOn ? ( spoilerText && spoilerText . length > 0 ) : spoiler }
/ >
< div className = 'compose--counter-wrapper' >
< CharacterCounter text = { countText } max = { maxChars } / >
< / d i v >
< / d i v >
2019-04-21 05:44:30 -05:00
2019-04-21 11:48:33 -05:00
< Publisher
2019-08-19 14:41:41 -05:00
countText = { countText }
2018-03-12 12:39:07 -05:00
disabled = { disabledButton }
2018-01-03 14:36:21 -06:00
onSecondarySubmit = { handleSecondarySubmit }
onSubmit = { handleSubmit }
2017-12-24 00:16:45 -06:00
privacy = { privacy }
sideArm = { sideArm }
/ >
< / d i v >
) ;
}
}