1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-09-14 14:21:23 +00:00
Files
seahub/frontend/src/components/review-list-view/review-comments.js

305 lines
10 KiB
JavaScript
Raw Normal View History

2018-10-23 13:13:44 +08:00
import React from 'react';
2018-10-25 10:39:16 +08:00
import PropTypes from 'prop-types';
import { processor } from '../../utils/seafile-markdown2html';
2018-11-28 12:43:53 +08:00
import { Button, Dropdown, DropdownToggle, DropdownMenu, DropdownItem } from 'reactstrap';
2018-10-23 13:13:44 +08:00
import { seafileAPI } from '../../utils/seafile-api';
import { gettext, draftFilePath, draftRepoID } from '../../utils/constants';
2018-10-23 13:13:44 +08:00
import Loading from '../../components/loading.js';
2019-02-14 15:44:22 +08:00
import { username } from '../../utils/constants.js';
2018-10-23 13:13:44 +08:00
import '../../css/review-comments.css';
2018-10-25 10:39:16 +08:00
const commentPropTypes = {
2019-05-30 17:30:59 +08:00
listComments: PropTypes.func.isRequired,
2018-10-25 10:39:16 +08:00
inResizing: PropTypes.bool.isRequired,
2019-05-30 17:30:59 +08:00
commentsList: PropTypes.array.isRequired,
2018-11-23 10:19:36 +08:00
scrollToQuote: PropTypes.func.isRequired
2018-10-25 10:39:16 +08:00
};
2018-10-23 13:13:44 +08:00
class ReviewComments extends React.Component {
constructor(props) {
super(props);
this.state = {
2018-10-25 10:39:16 +08:00
inResizing: false,
commentFooterHeight: 25,
2018-11-28 12:43:53 +08:00
showResolvedComment: true,
2018-11-23 10:19:36 +08:00
comment: '',
2018-10-25 10:39:16 +08:00
};
2018-10-23 13:13:44 +08:00
}
handleCommentChange = (event) => {
2019-04-26 12:07:55 +08:00
this.setState({ comment: event.target.value });
2018-10-23 13:13:44 +08:00
}
submitComment = () => {
2018-11-23 10:19:36 +08:00
let comment = this.state.comment.trim();
if (comment.length > 0) {
2019-05-30 17:30:59 +08:00
seafileAPI.postComment(draftRepoID, draftFilePath, comment).then(() => {
this.props.listComments();
2018-12-03 14:03:21 +08:00
});
2019-04-26 12:07:55 +08:00
this.setState({ comment: '' });
2018-10-23 13:13:44 +08:00
}
}
resolveComment = (event) => {
seafileAPI.updateComment(draftRepoID, event.target.id, 'true').then((res) => {
2019-05-30 17:30:59 +08:00
this.props.listComments();
2018-10-23 13:13:44 +08:00
});
}
editComment = (commentID, newComment) => {
seafileAPI.updateComment(draftRepoID, commentID, null, null, newComment).then((res) => {
2019-05-30 17:30:59 +08:00
this.props.listComments();
});
}
toggleResolvedComment = () => {
2019-04-26 12:07:55 +08:00
this.setState({ showResolvedComment: !this.state.showResolvedComment });
}
2018-10-23 13:13:44 +08:00
deleteComment = (event) => {
seafileAPI.deleteComment(draftRepoID, event.target.id).then((res) => {
2019-05-30 17:30:59 +08:00
this.props.listComments();
2018-10-23 13:13:44 +08:00
});
}
2018-10-25 10:39:16 +08:00
onResizeMouseUp = () => {
if (this.state.inResizing) {
2019-04-26 12:07:55 +08:00
this.setState({ inResizing: false });
2018-10-25 10:39:16 +08:00
}
}
onResizeMouseDown = () => {
2019-04-26 12:07:55 +08:00
this.setState({ inResizing: true });
2018-10-25 10:39:16 +08:00
};
onResizeMouseMove = (event) => {
2018-11-28 12:43:53 +08:00
let rate = 100 - (event.nativeEvent.clientY - 120 ) / this.refs.comment.clientHeight * 100;
2018-10-25 10:39:16 +08:00
if (rate < 20 || rate > 70) {
2018-11-12 09:28:02 +08:00
if (rate < 20) {
2019-04-26 12:07:55 +08:00
this.setState({ commentFooterHeight: 25 });
2018-11-12 09:28:02 +08:00
}
if (rate > 70) {
2019-04-26 12:07:55 +08:00
this.setState({ commentFooterHeight: 65 });
2018-11-12 09:28:02 +08:00
}
2019-04-26 12:07:55 +08:00
this.setState({ inResizing: false });
2018-10-25 10:39:16 +08:00
return null;
}
2019-04-26 12:07:55 +08:00
this.setState({ commentFooterHeight: rate });
2018-10-25 10:39:16 +08:00
};
2018-11-23 10:19:36 +08:00
2018-12-08 12:13:02 +08:00
scrollToQuote = (newIndex, oldIndex, quote) => {
this.props.scrollToQuote(newIndex, oldIndex, quote);
2019-04-26 12:07:55 +08:00
this.setState({ comment: '' });
2018-11-23 10:19:36 +08:00
}
componentWillReceiveProps(nextProps) {
2019-05-30 17:30:59 +08:00
if (this.props.commentsList.length < nextProps.commentsList.length) {
let that = this;
setTimeout(() => {
that.refs.commentsList.scrollTo(0, 10000);
}, 100);
2018-11-23 10:19:36 +08:00
}
}
2018-10-23 13:13:44 +08:00
render() {
2018-10-25 10:39:16 +08:00
const onResizeMove = this.state.inResizing ? this.onResizeMouseMove : null;
2019-05-30 17:30:59 +08:00
const { commentsList } = this.props;
2018-10-23 13:13:44 +08:00
return (
2018-10-25 10:39:16 +08:00
<div className={(this.state.inResizing || this.props.inResizing)?
'seafile-comment seafile-comment-resizing' : 'seafile-comment'}
onMouseMove={onResizeMove} onMouseUp={this.onResizeMouseUp} ref="comment">
2018-10-23 13:13:44 +08:00
<div className="seafile-comment-title">
2018-11-28 12:43:53 +08:00
<div className={'seafile-comment-title-text'}>{gettext('Show resolved comments')}</div>
<div className={'seafile-comment-title-toggle'}>
<label className="custom-switch" id="toggle-resolved-comments">
2018-11-28 12:43:53 +08:00
<input type="checkbox" name="option" className="custom-switch-input"
onChange={this.toggleResolvedComment}
checked={this.state.showResolvedComment && 'checked'}
/>
<span className="custom-switch-indicator"></span>
</label>
</div>
2018-10-23 13:13:44 +08:00
</div>
2019-04-26 12:07:55 +08:00
<div style={{height:(100 - this.state.commentFooterHeight)+'%'}}>
2019-05-30 17:30:59 +08:00
{commentsList.length === 0 &&
2019-04-26 12:07:55 +08:00
<div className="seafile-comment-list">
2018-10-25 10:39:16 +08:00
<div className="comment-vacant">{gettext('No comment yet.')}</div>
</div>
}
2019-05-30 17:30:59 +08:00
<ul className="seafile-comment-list" ref='commentsList'>
{(commentsList.length > 0) &&
commentsList.map((item, index) => {
return (
<CommentItem
item={item}
key={index}
showResolvedComment={this.state.showResolvedComment}
resolveComment={this.resolveComment}
editComment={this.editComment}
scrollToQuote={this.scrollToQuote}
deleteComment={this.deleteComment}
/>
);
})
}
</ul>
2018-10-25 10:39:16 +08:00
</div>
<div className="seafile-comment-footer" style={{height:this.state.commentFooterHeight+'%'}}>
<div className="seafile-comment-row-resize" onMouseDown={this.onResizeMouseDown}></div>
2018-10-23 13:13:44 +08:00
<div className="seafile-add-comment">
2019-04-26 12:07:55 +08:00
<textarea
className="add-comment-input"
value={this.state.comment}
placeholder={gettext('Add a comment.')}
onChange={this.handleCommentChange}
clos="100" rows="3" warp="virtual"
></textarea>
2019-05-30 17:30:59 +08:00
<Button className="comment-btn" color="success" size="sm" onClick={this.submitComment}>{gettext('Submit')}</Button>
2018-10-23 13:13:44 +08:00
</div>
</div>
</div>
2018-10-25 10:39:16 +08:00
);
2018-10-23 13:13:44 +08:00
}
}
2018-10-25 10:39:16 +08:00
ReviewComments.propTypes = commentPropTypes;
const commentItemPropTypes = {
2018-11-23 10:19:36 +08:00
item: PropTypes.object.isRequired,
2018-10-25 10:39:16 +08:00
deleteComment: PropTypes.func.isRequired,
resolveComment: PropTypes.func.isRequired,
editComment: PropTypes.func.isRequired,
2018-11-23 10:19:36 +08:00
showResolvedComment: PropTypes.bool.isRequired,
scrollToQuote: PropTypes.func.isRequired
2018-10-25 10:39:16 +08:00
};
2018-10-23 13:13:44 +08:00
class CommentItem extends React.Component {
constructor(props) {
super(props);
this.state = {
dropdownOpen: false,
2018-12-08 12:13:02 +08:00
comment: '',
quote: '',
newComment: this.props.item.comment,
editable: false,
2018-10-25 10:39:16 +08:00
};
2018-10-23 13:13:44 +08:00
}
toggleDropDownMenu = () => {
2019-04-26 12:07:55 +08:00
this.setState({ dropdownOpen: !this.state.dropdownOpen });
2018-10-23 13:13:44 +08:00
}
2018-12-08 12:13:02 +08:00
convertComment = (item) => {
2019-04-26 12:07:55 +08:00
processor.process(item.comment).then((result) => {
let comment = String(result);
this.setState({ comment: comment });
});
processor.process(item.quote).then((result) => {
let quote = String(result);
this.setState({ quote: quote });
});
2018-10-23 13:13:44 +08:00
}
2018-11-23 10:19:36 +08:00
scrollToQuote = () => {
2018-12-08 12:13:02 +08:00
const item = this.props.item;
this.props.scrollToQuote(item.newIndex, item.oldIndex, item.quote);
2018-11-23 10:19:36 +08:00
}
toggleEditComment = () => {
2019-04-26 12:07:55 +08:00
this.setState({ editable: !this.state.editable });
}
updateComment = (event) => {
const newComment = this.state.newComment;
if (this.props.item.comment !== newComment) {
this.props.editComment(event.target.id, newComment);
}
this.toggleEditComment();
}
handleCommentChange = (event) => {
2019-04-26 12:07:55 +08:00
this.setState({ newComment: event.target.value });
}
2018-10-23 13:13:44 +08:00
componentWillMount() {
2018-12-08 12:13:02 +08:00
this.convertComment(this.props.item);
2018-10-23 13:13:44 +08:00
}
componentWillReceiveProps(nextProps) {
2018-12-08 12:13:02 +08:00
this.convertComment(nextProps.item);
2018-10-23 13:13:44 +08:00
}
render() {
2018-12-08 12:13:02 +08:00
const item = this.props.item;
2019-04-26 12:07:55 +08:00
if (item.resolved && !this.props.showResolvedComment) return null;
if (this.state.editable) {
return(
<li className="seafile-comment-item" id={item.id}>
<div className="seafile-comment-info">
<img className="avatar" src={item.avatarUrl} alt=""/>
<div className="reviewer-info">
<div className="reviewer-name ellipsis">{item.name}</div>
<div className="review-time">{item.time}</div>
</div>
</div>
<div className="seafile-edit-comment">
2019-04-26 12:07:55 +08:00
<textarea
className="edit-comment-input"
value={this.state.newComment}
onChange={this.handleCommentChange}
clos="100" rows="3" warp="virtual"
></textarea>
<Button className="comment-btn" color="success" size="sm" onClick={this.updateComment} id={item.id}>
{gettext('Update')}</Button>{' '}
<Button className="comment-btn" color="secondary" size="sm" onClick={this.toggleEditComment}>
{gettext('Cancel')}</Button>
</div>
</li>
);
}
2018-10-23 13:13:44 +08:00
return (
2018-12-08 12:13:02 +08:00
<li className={item.resolved ? 'seafile-comment-item seafile-comment-item-resolved'
: 'seafile-comment-item'} id={item.id}>
2018-10-23 13:13:44 +08:00
<div className="seafile-comment-info">
2018-12-08 12:13:02 +08:00
<img className="avatar" src={item.avatarUrl} alt=""/>
2018-10-23 13:13:44 +08:00
<div className="reviewer-info">
<div className="reviewer-name ellipsis">{item.name}</div>
2018-12-08 12:13:02 +08:00
<div className="review-time">{item.time}</div>
2018-10-23 13:13:44 +08:00
</div>
2019-04-26 12:07:55 +08:00
{!item.resolved &&
<Dropdown isOpen={this.state.dropdownOpen} size="sm"
className="seafile-comment-dropdown" toggle={this.toggleDropDownMenu}>
<DropdownToggle className="seafile-comment-dropdown-btn">
<i className="fas fa-ellipsis-v"></i>
</DropdownToggle>
<DropdownMenu>
2019-04-26 12:07:55 +08:00
{(item.userEmail === username) &&
<DropdownItem onClick={this.props.deleteComment}
2018-12-08 12:13:02 +08:00
className="delete-comment" id={item.id}>{gettext('Delete')}</DropdownItem>}
2019-04-26 12:07:55 +08:00
{(item.userEmail === username) &&
<DropdownItem onClick={this.toggleEditComment}
className="edit-comment" id={item.id}>{gettext('Edit')}</DropdownItem>}
<DropdownItem onClick={this.props.resolveComment}
2018-12-08 12:13:02 +08:00
className="seafile-comment-resolved" id={item.id}>{gettext('Mark as resolved')}</DropdownItem>
</DropdownMenu>
</Dropdown>
}
2018-10-23 13:13:44 +08:00
</div>
2019-04-26 12:07:55 +08:00
{(item.newIndex >= -1 && item.oldIndex >= -1) &&
2018-12-08 12:13:02 +08:00
<blockquote className="seafile-comment-content">
<div onClick={this.scrollToQuote} dangerouslySetInnerHTML={{ __html: this.state.quote }}></div>
</blockquote>
2018-11-23 10:19:36 +08:00
}
2018-12-08 12:13:02 +08:00
<div className="seafile-comment-content" dangerouslySetInnerHTML={{ __html: this.state.comment }}></div>
2018-10-23 13:13:44 +08:00
</li>
2018-10-25 10:39:16 +08:00
);
2018-10-23 13:13:44 +08:00
}
}
2018-10-25 10:39:16 +08:00
CommentItem.propTypes = commentItemPropTypes;
2018-10-23 13:13:44 +08:00
export default ReviewComments;