File size: 6,619 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 |
import { Button } from '@automattic/components';
import clsx from 'clsx';
import { localize } from 'i18n-calypso';
import { get } from 'lodash';
import PropTypes from 'prop-types';
import { Component } from 'react';
import { connect } from 'react-redux';
import AutoDirection from 'calypso/components/auto-direction';
import FormTextarea from 'calypso/components/forms/form-textarea';
import { decodeEntities } from 'calypso/lib/formatting';
import {
bumpStat,
composeAnalytics,
recordTracksEvent,
withAnalytics,
} from 'calypso/state/analytics/actions';
import { changeCommentStatus, replyComment } from 'calypso/state/comments/actions';
import { getSiteComment } from 'calypso/state/comments/selectors';
import { removeNotice, successNotice } from 'calypso/state/notices/actions';
import { getSelectedSiteId } from 'calypso/state/ui/selectors';
const TEXTAREA_HEIGHT_COLLAPSED = 47; // 1 line
const TEXTAREA_HEIGHT_FOCUSED = 68; // 2 lines
const TEXTAREA_MAX_HEIGHT = 236; // 10 lines
const TEXTAREA_VERTICAL_BORDER = 2;
export class CommentReply extends Component {
static propTypes = {
commentId: PropTypes.number,
commentsListQuery: PropTypes.object,
isReplyVisible: PropTypes.bool,
};
state = {
hasReplyFocus: false,
replyContent: '',
textareaHeight: TEXTAREA_HEIGHT_COLLAPSED,
};
componentDidMount() {
this.textarea.focus();
}
componentDidUpdate( prevProps ) {
if ( ! prevProps.isReplyVisible && this.props.isReplyVisible ) {
this.textarea.focus();
}
}
storeTextareaRef = ( textarea ) => ( this.textarea = textarea );
blurReply = () => this.setState( { hasReplyFocus: false } );
focusReply = () => this.setState( { hasReplyFocus: true } );
calculateTextareaHeight = () => {
const textareaHeight = Math.min(
TEXTAREA_MAX_HEIGHT,
this.textarea.scrollHeight + TEXTAREA_VERTICAL_BORDER
);
return Math.max( TEXTAREA_HEIGHT_FOCUSED, textareaHeight );
};
getPlaceholder = () => {
const { authorDisplayName: commentAuthor, translate } = this.props;
return commentAuthor
? translate( 'Reply to %(commentAuthor)s…', { args: { commentAuthor } } )
: translate( 'Reply to comment…' );
};
keyDownHandler = ( event ) => {
// Use Ctrl+Enter to submit comment
if ( event.keyCode === 13 && ( event.ctrlKey || event.metaKey ) ) {
event.preventDefault();
this.submitReply();
}
};
submit = ( event ) => {
event.preventDefault();
this.submitReply();
};
submitReply = () => {
const { approveComment, commentStatus, postId, replyToComment, siteId, translate } = this.props;
const { replyContent } = this.state;
this.props.removeNotice( 'comment-notice' );
const alsoApprove = 'approved' !== commentStatus;
replyToComment( replyContent, siteId, postId, { alsoApprove } );
this.props.successNotice(
alsoApprove
? translate( 'Comment approved and reply submitted.' )
: translate( 'Reply submitted.' ),
{
id: 'comment-notice',
isPersistent: true,
}
);
this.setState( { replyContent: '' } );
this.blurReply();
if ( alsoApprove ) {
approveComment( siteId, postId, { previousStatus: commentStatus } );
}
// Back navigation scrolling fix
if ( window ) {
const path = get( window, 'history.state.path' );
const newPath = path.replace( /[#].*/, '' );
window.history.replaceState( window.history.state, '', newPath );
}
};
updateTextarea = ( event ) => {
const { value: replyContent } = event.target;
const textareaHeight = this.calculateTextareaHeight();
this.setState( { replyContent, textareaHeight } );
};
render() {
const { translate } = this.props;
const { hasReplyFocus, replyContent, textareaHeight } = this.state;
const hasReplyContent = replyContent.trim().length > 0;
// Only show the scrollbar if the textarea content exceeds the max height
const hasScrollbar = textareaHeight === TEXTAREA_MAX_HEIGHT;
const buttonClasses = clsx( 'comment__reply-submit', {
'has-scrollbar': hasScrollbar,
'is-active': hasReplyContent,
} );
const textareaClasses = clsx( 'comment__reply-textarea', {
'has-content': hasReplyContent,
'has-focus': hasReplyFocus,
'has-scrollbar': hasScrollbar,
} );
// Without focus, force the textarea to collapse even if it was manually resized
const textareaStyle = {
height: hasReplyFocus || hasReplyContent ? textareaHeight : TEXTAREA_HEIGHT_COLLAPSED,
};
return (
<form className="comment__reply">
<AutoDirection>
<FormTextarea
className={ textareaClasses }
onBlur={ this.blurReply }
onChange={ this.updateTextarea }
onFocus={ this.focusReply }
onKeyDown={ this.keyDownHandler }
placeholder={ this.getPlaceholder() }
forwardedRef={ this.storeTextareaRef }
style={ textareaStyle }
value={ replyContent }
/>
</AutoDirection>
<Button
borderless
className={ buttonClasses }
compact
disabled={ ! hasReplyContent }
onClick={ this.submit }
tabIndex="0"
>
{ translate( 'Send' ) }
</Button>
</form>
);
}
}
const mapStateToProps = ( state, { commentId } ) => {
const siteId = getSelectedSiteId( state );
const comment = getSiteComment( state, siteId, commentId );
return {
authorDisplayName: decodeEntities( get( comment, 'author.name' ) ),
commentStatus: get( comment, 'status' ),
postId: get( comment, 'post.ID' ),
siteId,
};
};
const mapDispatchToProps = ( dispatch, { commentId, commentsListQuery } ) => ( {
approveComment: ( siteId, postId, analytics = {} ) =>
dispatch(
withAnalytics(
composeAnalytics(
recordTracksEvent( 'calypso_comment_management_change_status', {
also_unlike: false,
is_undo: false,
previous_status: analytics.previousStatus,
status: 'approved',
} ),
bumpStat( 'calypso_comment_management', 'comment_status_changed_to_approved' )
),
changeCommentStatus( siteId, postId, commentId, 'approved' )
)
),
removeNotice: ( noticeId ) => dispatch( removeNotice( noticeId ) ),
replyToComment: ( replyContent, siteId, postId, analytics = { alsoApprove: false } ) =>
dispatch(
withAnalytics(
composeAnalytics(
recordTracksEvent( 'calypso_comment_management_reply', {
also_approve: analytics.alsoApprove,
} ),
bumpStat( 'calypso_comment_management', 'comment_reply' )
),
replyComment( replyContent, siteId, postId, commentId, commentsListQuery )
)
),
successNotice: ( text, options ) => dispatch( successNotice( text, options ) ),
} );
export default connect( mapStateToProps, mapDispatchToProps )( localize( CommentReply ) );
|