1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-08-17 14:37:58 +00:00
seahub/frontend/src/components/file-view/comment-panel.js

288 lines
9.1 KiB
JavaScript
Raw Normal View History

2019-02-14 12:10:15 +00:00
import React from 'react';
import PropTypes from 'prop-types';
import moment from 'moment';
import { processor } from '@seafile/seafile-editor/dist/utils/seafile-markdown2html';
import { Button, Dropdown, DropdownToggle, DropdownMenu, DropdownItem } from 'reactstrap';
import { gettext } from '../../utils/constants';
import { seafileAPI } from '../../utils/seafile-api';
import ParticipantsList from './participants-list';
import '../../css/comments-list.css';
2019-02-14 12:10:15 +00:00
const { username, repoID, filePath } = window.app.pageOptions;
2019-02-14 12:10:15 +00:00
const CommentPanelPropTypes = {
2019-03-25 03:49:47 +00:00
toggleCommentPanel: PropTypes.func.isRequired,
commentsNumber: PropTypes.number,
participants: PropTypes.array,
onParticipantsChange: PropTypes.func,
2019-02-14 12:10:15 +00:00
};
class CommentPanel extends React.Component {
2019-02-14 12:10:15 +00:00
constructor(props) {
super(props);
this.state = {
commentsList: [],
showResolvedComment: true,
};
}
toggleResolvedComment = () => {
this.setState({
showResolvedComment: !this.state.showResolvedComment
});
}
listComments = () => {
seafileAPI.listComments(repoID, filePath).then((res) => {
this.setState({
commentsList: res.data.comments
});
});
}
handleCommentChange = (event) => {
this.setState({
comment: event.target.value,
});
}
submitComment = () => {
let comment = this.refs.commentTextarea.value;
if (comment.trim()) {
seafileAPI.postComment(repoID, filePath, comment).then(() => {
this.listComments();
});
}
this.refs.commentTextarea.value = '';
}
resolveComment = (event) => {
seafileAPI.updateComment(repoID, event.target.id, 'true').then(() => {
this.listComments();
});
}
deleteComment = (event) => {
seafileAPI.deleteComment(repoID, event.target.id).then(() => {
this.listComments();
});
}
editComment = (commentID, newComment) => {
seafileAPI.updateComment(repoID, commentID, null, null, newComment).then((res) => {
this.listComments();
});
}
2019-02-14 12:10:15 +00:00
componentDidMount() {
this.listComments();
}
2019-03-25 03:49:47 +00:00
componentWillReceiveProps(nextProps) {
if (this.props.commentsNumber !== nextProps.commentsNumber) {
this.listComments();
}
}
2019-02-14 12:10:15 +00:00
render() {
const { participants } = this.props;
2019-02-14 12:10:15 +00:00
return (
<div className="seafile-comment">
<div className="seafile-comment-title">
<div onClick={this.props.toggleCommentPanel} className={'seafile-comment-title-close'}>
2019-02-14 12:10:15 +00:00
<i className={'fa fa-times-circle'}/>
</div>
<div className={'seafile-comment-title-text'}>{gettext('Comments')}</div>
2019-02-14 12:10:15 +00:00
</div>
<div className="seafile-comment-toggle-resolved">
<div className={'seafile-comment-title-text'}>{gettext('Show resolved comments')}</div>
<div className={'seafile-comment-title-toggle d-flex'}>
<label className="custom-switch" id="toggle-resolved-comments">
<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>
</div>
<ul className={'seafile-comment-list'}>
{this.state.commentsList.length > 0 &&
this.state.commentsList.map((item, index = 0, arr) => {
let oldTime = (new Date(item.created_at)).getTime();
let time = moment(oldTime).format('YYYY-MM-DD HH:mm');
return (
<React.Fragment key={item.id}>
<CommentItem
item={item} time={time}
deleteComment={this.deleteComment}
resolveComment={this.resolveComment}
editComment={this.editComment}
2019-02-14 12:10:15 +00:00
showResolvedComment={this.state.showResolvedComment}
/>
</React.Fragment>
);
})
}
{(this.state.commentsList.length == 0 ) &&
<li className="comment-vacant">{gettext('No comment yet.')}</li>}
2019-02-14 12:10:15 +00:00
</ul>
<div className="seafile-comment-footer">
{participants &&
<ParticipantsList
onParticipantsChange={this.props.onParticipantsChange}
participants={participants}
repoID={repoID}
filePath={filePath}
2019-06-28 07:52:22 +00:00
showIconTip={true}
/>
}
<textarea
className="add-comment-input" ref="commentTextarea"
placeholder={gettext('Add a comment.')}
clos="100" rows="3" warp="virtual"></textarea>
<Button
2019-06-25 07:23:14 +00:00
className="submit-comment" color="primary"
size="sm" onClick={this.submitComment} >
{gettext('Submit')}</Button>
2019-02-14 12:10:15 +00:00
</div>
</div>
);
}
}
CommentPanel.propTypes = CommentPanelPropTypes;
2019-02-14 12:10:15 +00:00
const commentItemPropTypes = {
time: PropTypes.string.isRequired,
item: PropTypes.object.isRequired,
deleteComment: PropTypes.func.isRequired,
resolveComment: PropTypes.func.isRequired,
showResolvedComment: PropTypes.bool.isRequired,
editComment: PropTypes.func.isRequired,
2019-02-14 12:10:15 +00:00
};
class CommentItem extends React.Component {
constructor(props) {
super(props);
this.state = {
dropdownOpen: false,
html: '',
newComment: this.props.item.comment,
editable: false,
2019-02-14 12:10:15 +00:00
};
}
toggleDropDownMenu = () => {
this.setState({
dropdownOpen: !this.state.dropdownOpen,
});
}
convertComment = (mdFile) => {
processor.process(mdFile).then(
(result) => {
let html = String(result);
this.setState({
html: html
});
}
);
}
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,
});
}
2019-02-14 12:10:15 +00:00
componentWillMount() {
this.convertComment(this.props.item.comment);
}
componentWillReceiveProps(nextProps) {
this.convertComment(nextProps.item.comment);
}
render() {
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.avatar_url} alt=""/>
<div className="reviewer-info">
<div className="reviewer-name ellipsis">{item.user_name}</div>
<div className="review-time">{this.props.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 10:12:25 +00:00
<Button className="comment-btn" color="secondary" size="sm" onClick={this.toggleEditComment}> {gettext('Cancel')}</Button>
</div>
</li>
);
}
2019-02-14 12:10:15 +00:00
return (
<li className={item.resolved ? 'seafile-comment-item seafile-comment-item-resolved'
: 'seafile-comment-item'} id={item.id}>
<div className="seafile-comment-info">
<img className="avatar" src={item.avatar_url} alt=""/>
<div className="reviewer-info">
<div className="reviewer-name ellipsis">{item.user_name}</div>
2019-02-14 12:10:15 +00:00
<div className="review-time">{this.props.time}</div>
</div>
<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>
{
(item.user_email === username) &&
<DropdownItem onClick={this.props.deleteComment} className="delete-comment"
id={item.id}>{gettext('Delete')}</DropdownItem>}
{
(item.user_email === username) &&
<DropdownItem onClick={this.toggleEditComment}
className="edit-comment" id={item.id}>{gettext('Edit')}</DropdownItem>
}
2019-02-14 12:10:15 +00:00
{
!item.resolved &&
<DropdownItem onClick={this.props.resolveComment} className="seafile-comment-resolved"
id={item.id}>{gettext('Mark as resolved')}</DropdownItem>
}
</DropdownMenu>
</Dropdown>
</div>
<div className="seafile-comment-content" dangerouslySetInnerHTML={{ __html: this.state.html }}></div>
</li>
);
}
}
CommentItem.propTypes = commentItemPropTypes;
export default CommentPanel;