2017-12-24 06:16:45 +00:00
import React from 'react' ;
import ImmutablePropTypes from 'react-immutable-proptypes' ;
2019-04-21 17:07:48 +00:00
import PropTypes from 'prop-types' ;
import ReplyIndicatorContainer from '../containers/reply_indicator_container' ;
import AutosuggestTextarea from '../../../components/autosuggest_textarea' ;
2019-04-20 19:28:03 +00:00
import { defineMessages , injectIntl } from 'react-intl' ;
2019-04-21 17:07:48 +00: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 19:28:03 +00:00
import ImmutablePureComponent from 'react-immutable-pure-component' ;
2019-04-21 17:07:48 +00:00
import { countableText } from 'flavours/glitch/util/counter' ;
2019-04-21 16:31:26 +00:00
import OptionsContainer from '../containers/options_container' ;
2019-04-21 16:48:33 +00:00
import Publisher from './publisher' ;
2019-04-21 13:57:06 +00:00
import TextareaIcons from './textarea_icons' ;
2017-12-24 06:16:45 +00:00
2018-08-29 13:26:24 +00:00
const messages = defineMessages ( {
2019-04-21 10:44:30 +00:00
placeholder : { id : 'compose_form.placeholder' , defaultMessage : 'What is on your mind?' } ,
2018-08-29 13:26:24 +00: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 21:02:09 +00:00
spoiler _placeholder : { id : 'compose_form.spoiler_placeholder' , defaultMessage : 'Write your warning here' } ,
2018-08-29 13:26:24 +00:00
} ) ;
2019-04-20 19:28:03 +00:00
export default @ injectIntl
class ComposeForm extends ImmutablePureComponent {
2017-12-24 06:16:45 +00:00
2019-04-20 19:28:03 +00:00
static contextTypes = {
router : PropTypes . object ,
} ;
2017-12-24 06:16:45 +00:00
2019-04-20 19:28:03 +00:00
static propTypes = {
intl : PropTypes . object . isRequired ,
2019-04-21 17:07:48 +00:00
text : PropTypes . string ,
suggestions : ImmutablePropTypes . list ,
spoiler : PropTypes . bool ,
privacy : PropTypes . string ,
spoilerText : PropTypes . string ,
2019-04-20 19:28:03 +00:00
focusDate : PropTypes . instanceOf ( Date ) ,
caretPosition : PropTypes . number ,
2019-04-21 17:07:48 +00:00
preselectDate : PropTypes . instanceOf ( Date ) ,
2019-04-20 19:28:03 +00:00
isSubmitting : PropTypes . bool ,
isChangingUpload : PropTypes . bool ,
isUploading : PropTypes . bool ,
2019-04-21 17:07:48 +00: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 ,
advancedOptions : ImmutablePropTypes . map ,
2019-04-20 19:28:03 +00: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 ,
onMount : PropTypes . func ,
onUnmount : PropTypes . func ,
2019-04-21 17:07:48 +00:00
onPaste : PropTypes . func ,
2019-04-20 19:28:03 +00:00
onMediaDescriptionConfirm : PropTypes . func ,
} ;
2017-12-24 06:16:45 +00:00
2019-04-21 17:07:48 +00:00
static defaultProps = {
showSearch : false ,
} ;
2017-12-24 06:16:45 +00:00
2019-04-21 17:07:48 +00:00
handleChange = ( e ) => {
this . props . onChange ( e . target . value ) ;
2019-04-20 19:28:03 +00:00
}
2017-12-24 06:16:45 +00:00
2019-04-20 21:02:09 +00:00
handleKeyDown = ( { ctrlKey , keyCode , metaKey , altKey } ) => {
// We submit the status on control/meta + enter.
if ( keyCode === 13 && ( ctrlKey || metaKey ) ) {
2019-04-21 20:59:58 +00:00
this . handleSubmit ( ) ;
2019-04-20 21:02:09 +00:00
}
// Submit the status with secondary visibility on alt + enter.
if ( keyCode === 13 && altKey ) {
2019-04-21 20:59:58 +00:00
this . handleSecondarySubmit ( ) ;
2019-04-20 21:02:09 +00:00
}
}
2019-04-20 19:28:03 +00:00
handleSubmit = ( ) => {
2018-08-30 09:46:45 +00:00
const { textarea : { value } , uploadForm } = this ;
2017-12-24 06:16:45 +00:00
const {
2019-04-21 17:07:48 +00:00
onChange ,
2018-01-03 20:36:21 +00:00
onSubmit ,
2018-04-02 15:22:32 +00:00
isSubmitting ,
2018-12-16 14:26:04 +00:00
isChangingUpload ,
2018-04-02 15:22:32 +00:00
isUploading ,
2018-08-29 13:26:24 +00:00
media ,
2018-04-02 15:22:32 +00:00
anyMedia ,
2018-01-03 20:36:21 +00:00
text ,
2018-08-29 13:26:24 +00:00
mediaDescriptionConfirmation ,
onMediaDescriptionConfirm ,
2017-12-24 06:16:45 +00:00
} = this . props ;
// If something changes inside the textarea, then we update the
// state before submitting.
2019-04-21 17:07:48 +00:00
if ( onChange && text !== value ) {
onChange ( value ) ;
2017-12-24 06:16:45 +00:00
}
2018-04-02 15:22:32 +00:00
// Submit disabled:
2018-12-16 14:26:04 +00:00
if ( isSubmitting || isUploading || isChangingUpload || ( ! text . trim ( ) . length && ! anyMedia ) ) {
2018-04-02 15:22:32 +00:00
return ;
}
2018-08-29 13:26:24 +00:00
// Submit unless there are media with missing descriptions
if ( mediaDescriptionConfirmation && onMediaDescriptionConfirm && media && media . some ( item => ! item . get ( 'description' ) ) ) {
const firstWithoutDescription = media . findIndex ( item => ! item . get ( 'description' ) ) ;
2018-08-30 09:46:45 +00:00
if ( uploadForm ) {
const inputs = uploadForm . querySelectorAll ( '.composer--upload_form--item input' ) ;
if ( inputs . length == media . size && firstWithoutDescription !== - 1 ) {
inputs [ firstWithoutDescription ] . focus ( ) ;
}
2018-08-29 13:26:24 +00:00
}
2018-12-13 15:50:37 +00:00
onMediaDescriptionConfirm ( this . context . router ? this . context . router . history : null ) ;
2018-08-29 13:26:24 +00:00
} else if ( onSubmit ) {
2018-12-13 15:50:37 +00:00
onSubmit ( this . context . router ? this . context . router . history : null ) ;
2017-12-24 06:16:45 +00:00
}
2019-04-20 19:28:03 +00:00
}
2017-12-24 06:16:45 +00:00
2019-04-21 17:07:48 +00:00
// Changes the text value of the spoiler.
handleChangeSpoiler = ( { target : { value } } ) => {
const { onChangeSpoilerText } = this . props ;
if ( onChangeSpoilerText ) {
onChangeSpoilerText ( value ) ;
}
}
// 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 20:59:58 +00:00
this . handleSubmit ( ) ;
2019-04-21 17:07:48 +00:00
}
// Selects a suggestion from the autofill.
onSuggestionSelected = ( tokenStart , token , value ) => {
this . props . onSuggestionSelected ( tokenStart , token , value ) ;
}
// When the escape key is released, we focus the UI.
handleKeyUp = ( { key } ) => {
if ( key === 'Escape' ) {
document . querySelector ( '.ui' ) . parentElement . focus ( ) ;
}
}
2017-12-24 06:16:45 +00:00
// Sets a reference to the textarea.
2019-04-21 10:44:30 +00:00
setAutosuggestTextarea = ( textareaComponent ) => {
2018-01-03 20:36:21 +00:00
if ( textareaComponent ) {
this . textarea = textareaComponent . textarea ;
}
2019-04-20 19:28:03 +00:00
}
2018-08-18 18:53:46 +00:00
// Sets a reference to the CW field.
2019-04-20 19:28:03 +00:00
handleRefSpoilerText = ( spoilerComponent ) => {
2018-08-18 18:53:46 +00:00
if ( spoilerComponent ) {
2019-04-20 21:02:09 +00:00
this . spoilerText = spoilerComponent ;
2018-08-18 18:53:46 +00:00
}
}
2017-12-24 06:16:45 +00:00
2018-01-06 02:30:06 +00:00
// Tells our state the composer has been mounted.
componentDidMount ( ) {
const { onMount } = this . props ;
if ( onMount ) {
onMount ( ) ;
}
}
// Tells our state the composer has been unmounted.
componentWillUnmount ( ) {
const { onUnmount } = this . props ;
if ( onUnmount ) {
onUnmount ( ) ;
}
}
2017-12-24 06:16:45 +00: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 18:53:46 +00:00
spoilerText ,
2017-12-24 06:16:45 +00:00
} = this ;
const {
2018-01-03 20:36:21 +00:00
focusDate ,
2018-05-22 19:09:07 +00:00
caretPosition ,
2018-01-03 20:36:21 +00:00
isSubmitting ,
preselectDate ,
text ,
2018-09-28 18:58:03 +00:00
preselectOnReply ,
2017-12-24 06:16:45 +00:00
} = this . props ;
let selectionEnd , selectionStart ;
// Caret/selection handling.
2018-05-22 18:21:09 +00:00
if ( focusDate !== prevProps . focusDate ) {
2017-12-24 06:16:45 +00:00
switch ( true ) {
2018-09-28 18:58:03 +00:00
case preselectDate !== prevProps . preselectDate && preselectOnReply :
2017-12-24 06:16:45 +00:00
selectionStart = text . search ( /\s/ ) + 1 ;
selectionEnd = text . length ;
break ;
2018-05-22 19:09:07 +00:00
case ! isNaN ( caretPosition ) && caretPosition !== null :
selectionStart = selectionEnd = caretPosition ;
2017-12-24 06:16:45 +00:00
break ;
default :
selectionStart = selectionEnd = text . length ;
}
2018-01-03 20:36:21 +00:00
if ( textarea ) {
textarea . setSelectionRange ( selectionStart , selectionEnd ) ;
textarea . focus ( ) ;
2018-09-28 16:44:55 +00:00
textarea . scrollIntoView ( ) ;
2018-01-03 20:36:21 +00:00
}
2017-12-24 06:16:45 +00:00
// Refocuses the textarea after submitting.
2018-01-03 20:36:21 +00:00
} else if ( textarea && prevProps . isSubmitting && ! isSubmitting ) {
2017-12-24 06:16:45 +00:00
textarea . focus ( ) ;
2018-08-18 18:53:46 +00:00
} else if ( this . props . spoiler !== prevProps . spoiler ) {
if ( this . props . spoiler ) {
if ( spoilerText ) {
spoilerText . focus ( ) ;
}
} else {
if ( textarea ) {
textarea . focus ( ) ;
}
}
2017-12-24 06:16:45 +00:00
}
}
2019-04-21 10:44:30 +00:00
2017-12-24 06:16:45 +00:00
render ( ) {
const {
2018-01-03 20:36:21 +00:00
handleEmoji ,
handleSecondarySubmit ,
handleSelect ,
handleSubmit ,
handleRefTextarea ,
2019-04-20 19:28:03 +00:00
} = this ;
2017-12-24 06:16:45 +00:00
const {
2018-01-06 02:23:06 +00:00
advancedOptions ,
2018-03-12 17:39:07 +00:00
anyMedia ,
2017-12-24 06:16:45 +00:00
intl ,
2018-01-03 20:36:21 +00:00
isSubmitting ,
2018-12-16 14:26:04 +00:00
isChangingUpload ,
2018-01-03 20:36:21 +00:00
isUploading ,
layout ,
media ,
onChangeSpoilerness ,
onChangeVisibility ,
onClearSuggestions ,
onFetchSuggestions ,
2019-04-21 17:07:48 +00:00
onPaste ,
2018-01-03 20:36:21 +00:00
privacy ,
sensitive ,
showSearch ,
sideArm ,
spoiler ,
spoilerText ,
suggestions ,
text ,
2018-08-22 13:58:57 +00:00
spoilersAlwaysOn ,
2017-12-24 06:16:45 +00:00
} = this . props ;
2018-12-16 14:26:04 +00:00
let disabledButton = isSubmitting || isUploading || isChangingUpload || ( ! text . trim ( ) . length && ! anyMedia ) ;
2018-03-12 17:39:07 +00:00
2017-12-24 06:16:45 +00:00
return (
2018-01-03 20:36:21 +00:00
< div className = 'composer' >
2019-04-20 20:05:09 +00:00
< WarningContainer / >
2019-04-20 20:21:28 +00:00
< ReplyIndicatorContainer / >
2019-04-20 20:05:09 +00:00
2019-04-20 21:02:09 +00:00
< div className = { ` composer--spoiler ${ spoiler ? 'composer--spoiler--visible' : '' } ` } >
< label >
< span style = { { display : 'none' } } > { intl . formatMessage ( messages . spoiler _placeholder ) } < / s p a n >
< input
id = 'glitch.composer.spoiler.input'
placeholder = { intl . formatMessage ( messages . spoiler _placeholder ) }
value = { spoilerText }
onChange = { this . handleChangeSpoiler }
onKeyDown = { this . handleKeyDown }
onKeyUp = { this . handleKeyUp }
disabled = { ! spoiler }
type = 'text'
className = 'spoiler-input__input'
ref = { this . handleRefSpoilerText }
/ >
< / l a b e l >
< / d i v >
2019-04-20 20:05:09 +00:00
2019-04-21 10:44:30 +00:00
< div className = 'composer--textarea' >
2019-04-21 13:57:06 +00:00
< TextareaIcons advancedOptions = { advancedOptions } / >
2019-04-21 10:44:30 +00:00
< AutosuggestTextarea
ref = { this . setAutosuggestTextarea }
placeholder = { intl . formatMessage ( messages . placeholder ) }
disabled = { isSubmitting }
value = { this . props . text }
onChange = { this . handleChange }
suggestions = { this . props . suggestions }
onKeyDown = { this . handleKeyDown }
onSuggestionsFetchRequested = { onFetchSuggestions }
onSuggestionsClearRequested = { onClearSuggestions }
2019-04-21 17:07:48 +00:00
onSuggestionSelected = { this . onSuggestionSelected }
onPaste = { onPaste }
2019-04-21 10:44:30 +00:00
autoFocus = { ! showSearch && ! isMobile ( window . innerWidth , layout ) }
/ >
< EmojiPicker onPickEmoji = { handleEmoji } / >
< / d i v >
2019-03-06 11:30:11 +00:00
< div className = 'compose-form__modifiers' >
2019-04-21 10:09:52 +00:00
< UploadFormContainer / >
2019-04-20 21:27:20 +00:00
< PollFormContainer / >
2019-03-06 11:30:11 +00:00
< / d i v >
2019-04-21 10:44:30 +00:00
2019-04-21 16:31:26 +00:00
< OptionsContainer
2018-01-06 02:23:06 +00:00
advancedOptions = { advancedOptions }
2017-12-24 06:16:45 +00:00
disabled = { isSubmitting }
2018-01-03 20:36:21 +00:00
onChangeVisibility = { onChangeVisibility }
2018-08-22 13:58:57 +00:00
onToggleSpoiler = { spoilersAlwaysOn ? null : onChangeSpoilerness }
2019-04-21 17:07:48 +00:00
onPaste = { onPaste }
2017-12-24 06:16:45 +00:00
privacy = { privacy }
2018-09-04 16:25:28 +00:00
sensitive = { sensitive || ( spoilersAlwaysOn && spoilerText && spoilerText . length > 0 ) }
spoiler = { spoilersAlwaysOn ? ( spoilerText && spoilerText . length > 0 ) : spoiler }
2017-12-24 06:16:45 +00:00
/ >
2019-04-21 10:44:30 +00:00
2019-04-21 16:48:33 +00:00
< Publisher
2018-01-14 22:13:24 +00:00
countText = { ` ${ spoilerText } ${ countableText ( text ) } ${ advancedOptions && advancedOptions . get ( 'do_not_federate' ) ? ' 👁️' : '' } ` }
2018-03-12 17:39:07 +00:00
disabled = { disabledButton }
2018-01-03 20:36:21 +00:00
onSecondarySubmit = { handleSecondarySubmit }
onSubmit = { handleSubmit }
2017-12-24 06:16:45 +00:00
privacy = { privacy }
sideArm = { sideArm }
/ >
< / d i v >
) ;
}
}