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

356 lines
11 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';
2018-11-23 10:19:36 +08:00
import reviewComment from '../../models/review-comment.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 = {
getCommentsNumber: PropTypes.func.isRequired,
inResizing: PropTypes.bool.isRequired,
2018-12-03 14:03:21 +08:00
commentsNumber: PropTypes.number,
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 = {
commentsList: [],
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
}
2018-11-12 09:28:02 +08:00
listComments = (scroll) => {
seafileAPI.listComments(draftRepoID, draftFilePath).then((response) => {
2018-11-23 10:19:36 +08:00
let commentList = [];
response.data.comments.forEach(item => {
let commentItem = new reviewComment(item);
commentList.push(commentItem);
});
2018-10-23 13:13:44 +08:00
this.setState({
2018-11-23 10:19:36 +08:00
commentsList: commentList
2018-10-23 13:13:44 +08:00
});
2018-11-12 09:28:02 +08:00
if (scroll) {
let that = this;
setTimeout(() => {
that.refs.commentsList.scrollTo(0, 10000);
}, 100);
2018-11-12 09:28:02 +08:00
}
2018-10-23 13:13:44 +08:00
});
}
handleCommentChange = (event) => {
this.setState({
comment: event.target.value,
});
}
submitComment = () => {
2018-11-23 10:19:36 +08:00
let comment = this.state.comment.trim();
if (comment.length > 0) {
seafileAPI.postComment(draftRepoID, draftFilePath, comment).then((response) => {
2018-12-03 14:03:21 +08:00
this.listComments(true);
this.props.getCommentsNumber();
});
2018-11-23 10:19:36 +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-02-14 15:44:22 +08:00
this.props.getCommentsNumber();
2018-10-23 13:13:44 +08:00
this.listComments();
});
}
editComment = (commentID, newComment) => {
seafileAPI.updateComment(draftRepoID, commentID, null, null, newComment).then((res) => {
this.props.getCommentsNumber();
this.listComments();
});
}
toggleResolvedComment = () => {
this.setState({
showResolvedComment: !this.state.showResolvedComment
});
}
2018-10-23 13:13:44 +08:00
deleteComment = (event) => {
seafileAPI.deleteComment(draftRepoID, event.target.id).then((res) => {
2018-10-23 13:13:44 +08:00
this.props.getCommentsNumber();
this.listComments();
});
}
2018-10-25 10:39:16 +08:00
onResizeMouseUp = () => {
if (this.state.inResizing) {
this.setState({
inResizing: false
});
}
}
onResizeMouseDown = () => {
this.setState({
inResizing: true
});
};
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) {
this.setState({
commentFooterHeight: 25
});
}
if (rate > 70) {
this.setState({
commentFooterHeight: 65
});
}
2018-10-25 10:39:16 +08:00
this.setState({
inResizing: false
});
return null;
}
this.setState({
commentFooterHeight: rate
});
};
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);
2018-11-23 10:19:36 +08:00
this.setState({
comment: ''
});
}
2018-10-23 13:13:44 +08:00
componentWillMount() {
this.listComments();
}
2018-11-23 10:19:36 +08:00
componentWillReceiveProps(nextProps) {
2018-12-03 14:03:21 +08:00
if (this.props.commentsNumber !== nextProps.commentsNumber) {
this.listComments();
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;
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>
2018-10-25 10:39:16 +08:00
<div style={{height:(100-this.state.commentFooterHeight)+'%'}}>
{ this.props.commentsNumber == 0 &&
<div className={'seafile-comment-list'}>
<div className="comment-vacant">{gettext('No comment yet.')}</div>
</div>
}
2018-11-12 09:28:02 +08:00
{ (this.state.commentsList.length === 0 && this.props.commentsNumber > 0) &&
<div className={'seafile-comment-list'}><Loading/></div>
}
{ this.state.commentsList.length > 0 &&
<ul className={'seafile-comment-list'} ref='commentsList'>
{ (this.state.commentsList.length > 0 && this.props.commentsNumber > 0) &&
2018-11-23 10:19:36 +08:00
this.state.commentsList.map((item, index) => {
2018-11-12 09:28:02 +08:00
return (
2018-11-23 10:19:36 +08:00
<CommentItem item={item} showResolvedComment={this.state.showResolvedComment}
resolveComment={this.resolveComment} key={index} editComment={this.editComment}
2018-11-23 10:19:36 +08:00
scrollToQuote={this.scrollToQuote} deleteComment={this.deleteComment}/>
2018-11-12 09:28:02 +08:00
);
})
}
</ul>
2018-10-23 13:13:44 +08:00
}
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">
2018-11-23 10:19:36 +08:00
<textarea className="add-comment-input" value={this.state.comment}
placeholder={gettext('Add a comment.')} onChange={this.handleCommentChange}
2018-10-23 13:13:44 +08:00
clos="100" rows="3" warp="virtual"></textarea>
<Button className="comment-btn" color="success"
2018-10-23 13:13:44 +08:00
size="sm" onClick={this.submitComment}>
{gettext('Submit')}</Button>
</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 = () => {
this.setState({
dropdownOpen: !this.state.dropdownOpen,
2018-10-25 10:39:16 +08:00
});
2018-10-23 13:13:44 +08:00
}
2018-12-08 12:13:02 +08:00
convertComment = (item) => {
processor.process(item.comment).then(
(result) => {
let comment = String(result);
this.setState({
comment: comment
});
}
);
processor.process(item.quote).then(
2018-10-23 13:13:44 +08:00
(result) => {
2018-12-08 12:13:02 +08:00
let quote = String(result);
2018-10-23 13:13:44 +08:00
this.setState({
2018-12-08 12:13:02 +08:00
quote: quote
2018-10-25 10:39:16 +08:00
});
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 = () => {
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) => {
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;
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">{item.name}</div>
<div className="review-time">{item.time}</div>
</div>
</div>
<div className="seafile-edit-comment">
<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>{' '}
2019-03-21 18:12:25 +08:00
<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">
2018-12-08 12:13:02 +08:00
<div className="reviewer-name">{item.name}</div>
<div className="review-time">{item.time}</div>
2018-10-23 13:13:44 +08:00
</div>
2018-12-08 12:13:02 +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-02-14 15:44:22 +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>}
{ (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>
2018-12-08 12:13:02 +08:00
{ (item.newIndex >= -1 && item.oldIndex >= -1) &&
<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;