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';
|
2019-02-25 10:09:17 +00:00
|
|
|
import { gettext } from '../../utils/constants';
|
|
|
|
import { seafileAPI } from '../../utils/seafile-api';
|
2019-06-26 06:56:03 +00:00
|
|
|
import ParticipantsList from './participants-list';
|
2019-07-16 02:01:09 +00:00
|
|
|
import { Utils } from '../../utils/utils';
|
|
|
|
import toaster from '../toast';
|
2019-07-24 13:04:47 +00:00
|
|
|
import { MentionsInput, Mention } from 'react-mentions';
|
|
|
|
import { defaultStyle, defaultMentionStyle } from '../../css/react-mentions-default-style';
|
2019-02-25 10:09:17 +00:00
|
|
|
import '../../css/comments-list.css';
|
2019-02-14 12:10:15 +00:00
|
|
|
|
2019-02-25 10:09:17 +00:00
|
|
|
const { username, repoID, filePath } = window.app.pageOptions;
|
2019-02-14 12:10:15 +00:00
|
|
|
|
2019-02-25 10:09:17 +00:00
|
|
|
const CommentPanelPropTypes = {
|
2019-03-25 03:49:47 +00:00
|
|
|
toggleCommentPanel: PropTypes.func.isRequired,
|
|
|
|
commentsNumber: PropTypes.number,
|
2019-06-26 06:56:03 +00:00
|
|
|
participants: PropTypes.array,
|
|
|
|
onParticipantsChange: PropTypes.func,
|
2019-02-14 12:10:15 +00:00
|
|
|
};
|
|
|
|
|
2019-02-25 10:09:17 +00:00
|
|
|
class CommentPanel extends React.Component {
|
2019-02-14 12:10:15 +00:00
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.state = {
|
|
|
|
commentsList: [],
|
|
|
|
showResolvedComment: true,
|
2019-07-20 01:51:19 +00:00
|
|
|
participants: null,
|
2019-07-24 13:04:47 +00:00
|
|
|
relatedUsers: null,
|
|
|
|
comment: '',
|
2019-02-14 12:10:15 +00:00
|
|
|
};
|
2019-07-26 08:51:47 +00:00
|
|
|
this.toBeAddedParticipant = [];
|
2019-02-14 12:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
toggleResolvedComment = () => {
|
2019-07-24 13:04:47 +00:00
|
|
|
this.setState({ showResolvedComment: !this.state.showResolvedComment });
|
2019-02-14 12:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
listComments = () => {
|
|
|
|
seafileAPI.listComments(repoID, filePath).then((res) => {
|
|
|
|
this.setState({
|
|
|
|
commentsList: res.data.comments
|
|
|
|
});
|
2019-07-16 02:01:09 +00:00
|
|
|
}).catch(error => {
|
|
|
|
let errMessage = Utils.getErrorMsg(error);
|
|
|
|
toaster.danger(errMessage);
|
2019-02-14 12:10:15 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-07-24 13:04:47 +00:00
|
|
|
listRepoRelatedUsers = () => {
|
|
|
|
seafileAPI.listRepoRelatedUsers(repoID).then((res) => {
|
|
|
|
let users = res.data.user_list.map((item) => {
|
2019-07-26 08:51:47 +00:00
|
|
|
return { id: item.email, display: item.name};
|
2019-07-24 13:04:47 +00:00
|
|
|
});
|
|
|
|
this.setState({ relatedUsers: users });
|
2019-02-14 12:10:15 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-07-24 13:04:47 +00:00
|
|
|
handleCommentChange = (event) => {
|
|
|
|
this.setState({ comment: event.target.value });
|
|
|
|
}
|
|
|
|
|
2019-07-26 08:51:47 +00:00
|
|
|
addComment = () => {
|
|
|
|
if (!this.state.comment.trim()) return;
|
|
|
|
seafileAPI.postComment(repoID, filePath, this.state.comment.trim()).then(() => {
|
|
|
|
this.listComments();
|
|
|
|
}).catch(err => {
|
|
|
|
toaster.danger(Utils.getErrorMsg(err));
|
|
|
|
});
|
|
|
|
this.setState({ comment: '' });
|
2020-11-02 05:56:35 +00:00
|
|
|
}
|
2019-07-26 08:51:47 +00:00
|
|
|
|
|
|
|
onSubmit = () => {
|
|
|
|
this.addParticipant(username);
|
|
|
|
if (this.toBeAddedParticipant.length === 0) {
|
|
|
|
this.addComment();
|
|
|
|
} else {
|
|
|
|
seafileAPI.addFileParticipants(repoID, filePath, this.toBeAddedParticipant).then((res) => {
|
|
|
|
this.onParticipantsChange(repoID, filePath);
|
|
|
|
this.toBeAddedParticipant = [];
|
|
|
|
this.addComment();
|
|
|
|
}).catch((err) => {
|
|
|
|
toaster.danger(Utils.getErrorMsg(err));
|
2019-02-14 12:10:15 +00:00
|
|
|
});
|
|
|
|
}
|
2019-07-22 08:34:25 +00:00
|
|
|
}
|
|
|
|
|
2019-02-14 12:10:15 +00:00
|
|
|
resolveComment = (event) => {
|
|
|
|
seafileAPI.updateComment(repoID, event.target.id, 'true').then(() => {
|
|
|
|
this.listComments();
|
2019-07-16 02:01:09 +00:00
|
|
|
}).catch(error => {
|
|
|
|
let errMessage = Utils.getErrorMsg(error);
|
|
|
|
toaster.danger(errMessage);
|
2019-02-14 12:10:15 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
deleteComment = (event) => {
|
|
|
|
seafileAPI.deleteComment(repoID, event.target.id).then(() => {
|
|
|
|
this.listComments();
|
2019-07-16 02:01:09 +00:00
|
|
|
}).catch(error => {
|
|
|
|
let errMessage = Utils.getErrorMsg(error);
|
|
|
|
toaster.danger(errMessage);
|
2019-02-14 12:10:15 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-03-14 02:15:25 +00:00
|
|
|
editComment = (commentID, newComment) => {
|
|
|
|
seafileAPI.updateComment(repoID, commentID, null, null, newComment).then((res) => {
|
|
|
|
this.listComments();
|
2019-07-16 02:01:09 +00:00
|
|
|
}).catch(error => {
|
|
|
|
let errMessage = Utils.getErrorMsg(error);
|
|
|
|
toaster.danger(errMessage);
|
2019-03-14 02:15:25 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-07-20 01:51:19 +00:00
|
|
|
onParticipantsChange = () => {
|
|
|
|
if (this.props.onParticipantsChange) {
|
|
|
|
this.props.onParticipantsChange();
|
|
|
|
} else {
|
|
|
|
this.getParticipants();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
getParticipants = () => {
|
|
|
|
if (this.props.participants) {
|
2019-07-24 13:04:47 +00:00
|
|
|
this.setState({ participants: this.props.participants });
|
2019-07-20 01:51:19 +00:00
|
|
|
} else {
|
|
|
|
seafileAPI.listFileParticipants(repoID, filePath).then((res) => {
|
2019-07-24 13:04:47 +00:00
|
|
|
this.setState({ participants: res.data.participant_list });
|
2019-07-22 08:34:25 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-24 13:04:47 +00:00
|
|
|
checkParticipant = (email) => {
|
|
|
|
return this.state.participants.map((participant) => {return participant.email;}).includes(email);
|
|
|
|
}
|
|
|
|
|
|
|
|
addParticipant = (email) => {
|
|
|
|
if (this.checkParticipant(email)) return;
|
2019-07-26 08:51:47 +00:00
|
|
|
this.toBeAddedParticipant.push(email);
|
2019-07-24 13:04:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
renderUserSuggestion = (entry, search, highlightedDisplay, index, focused) => {
|
|
|
|
return <div className={`user ${focused ? 'focused' : ''}`}>{highlightedDisplay}</div>;
|
|
|
|
}
|
|
|
|
|
2019-02-14 12:10:15 +00:00
|
|
|
componentDidMount() {
|
|
|
|
this.listComments();
|
2019-07-20 01:51:19 +00:00
|
|
|
this.getParticipants();
|
2019-07-24 13:04:47 +00:00
|
|
|
this.listRepoRelatedUsers();
|
2019-02-14 12:10:15 +00:00
|
|
|
}
|
|
|
|
|
2019-03-25 03:49:47 +00:00
|
|
|
componentWillReceiveProps(nextProps) {
|
|
|
|
if (this.props.commentsNumber !== nextProps.commentsNumber) {
|
|
|
|
this.listComments();
|
|
|
|
}
|
2019-07-22 08:34:25 +00:00
|
|
|
if (this.props.participants !== nextProps.participants) {
|
|
|
|
this.setState({ participants: nextProps.participants });
|
|
|
|
}
|
2019-03-25 03:49:47 +00:00
|
|
|
}
|
|
|
|
|
2019-02-14 12:10:15 +00:00
|
|
|
render() {
|
2019-07-24 13:04:47 +00:00
|
|
|
const { participants, commentsList } = this.state;
|
2019-02-14 12:10:15 +00:00
|
|
|
return (
|
|
|
|
<div className="seafile-comment">
|
|
|
|
<div className="seafile-comment-title">
|
2019-07-24 13:04:47 +00:00
|
|
|
<div onClick={this.props.toggleCommentPanel} className="seafile-comment-title-close">
|
|
|
|
<i className="fa fa-times-circle"/>
|
2019-02-14 12:10:15 +00:00
|
|
|
</div>
|
2019-07-24 13:04:47 +00:00
|
|
|
<div className="seafile-comment-title-text">{gettext('Comments')}</div>
|
2019-02-14 12:10:15 +00:00
|
|
|
</div>
|
|
|
|
<div className="seafile-comment-toggle-resolved">
|
2019-07-24 13:04:47 +00:00
|
|
|
<div className="seafile-comment-title-text">{gettext('Show resolved comments')}</div>
|
|
|
|
<div className="seafile-comment-title-toggle d-flex">
|
2019-02-14 12:10:15 +00:00
|
|
|
<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>
|
2019-07-24 13:04:47 +00:00
|
|
|
<ul className="seafile-comment-list">
|
|
|
|
{commentsList.length > 0 &&
|
|
|
|
commentsList.map((item, index = 0, arr) => {
|
2019-02-14 12:10:15 +00:00
|
|
|
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}
|
2019-03-14 02:15:25 +00:00
|
|
|
editComment={this.editComment}
|
2019-02-14 12:10:15 +00:00
|
|
|
showResolvedComment={this.state.showResolvedComment}
|
|
|
|
/>
|
|
|
|
</React.Fragment>
|
|
|
|
);
|
|
|
|
})
|
|
|
|
}
|
2019-07-24 13:04:47 +00:00
|
|
|
{commentsList.length == 0 &&
|
2019-02-19 11:59:42 +00:00
|
|
|
<li className="comment-vacant">{gettext('No comment yet.')}</li>}
|
2019-02-14 12:10:15 +00:00
|
|
|
</ul>
|
|
|
|
<div className="seafile-comment-footer">
|
2019-06-26 06:56:03 +00:00
|
|
|
{participants &&
|
|
|
|
<ParticipantsList
|
2019-07-20 01:51:19 +00:00
|
|
|
onParticipantsChange={this.onParticipantsChange}
|
2019-06-26 06:56:03 +00:00
|
|
|
participants={participants}
|
|
|
|
repoID={repoID}
|
|
|
|
filePath={filePath}
|
2019-06-28 07:52:22 +00:00
|
|
|
showIconTip={true}
|
2019-06-26 06:56:03 +00:00
|
|
|
/>
|
|
|
|
}
|
2019-07-24 13:04:47 +00:00
|
|
|
<MentionsInput
|
|
|
|
value={this.state.comment}
|
|
|
|
onChange={this.handleCommentChange}
|
2019-03-14 02:15:25 +00:00
|
|
|
placeholder={gettext('Add a comment.')}
|
2019-07-24 13:04:47 +00:00
|
|
|
style={defaultStyle}
|
|
|
|
>
|
|
|
|
<Mention
|
|
|
|
trigger="@"
|
2019-07-26 08:51:47 +00:00
|
|
|
displayTransform={(username, display) => `@${display}`}
|
2019-07-24 13:04:47 +00:00
|
|
|
data={this.state.relatedUsers}
|
|
|
|
renderSuggestion={this.renderUserSuggestion}
|
|
|
|
style={defaultMentionStyle}
|
2019-07-26 08:51:47 +00:00
|
|
|
onAdd={(id, display) => {this.addParticipant(id);}}
|
2019-07-27 08:33:18 +00:00
|
|
|
appendSpaceOnAdd={true}
|
2019-07-24 13:04:47 +00:00
|
|
|
/>
|
|
|
|
</MentionsInput>
|
2019-07-26 08:51:47 +00:00
|
|
|
<Button className="submit-comment" color="primary" size="sm" onClick={this.onSubmit}>
|
2019-03-14 02:15:25 +00:00
|
|
|
{gettext('Submit')}</Button>
|
2019-02-14 12:10:15 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-25 10:09:17 +00:00
|
|
|
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,
|
2019-03-14 02:15:25 +00:00
|
|
|
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: '',
|
2019-03-14 02:15:25 +00:00
|
|
|
newComment: this.props.item.comment,
|
|
|
|
editable: false,
|
2019-02-14 12:10:15 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
toggleDropDownMenu = () => {
|
|
|
|
this.setState({
|
|
|
|
dropdownOpen: !this.state.dropdownOpen,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
convertComment = (mdFile) => {
|
2019-07-26 08:51:47 +00:00
|
|
|
processor.process(mdFile).then((result) => {
|
|
|
|
let html = String(result);
|
|
|
|
this.setState({ html: html });
|
|
|
|
});
|
2019-02-14 12:10:15 +00:00
|
|
|
}
|
|
|
|
|
2019-03-14 02:15:25 +00: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,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-07-26 08:51:47 +00:00
|
|
|
onCommentClick = (e) => {
|
|
|
|
// click participant link, page shouldn't jump
|
|
|
|
if (e.target.nodeName !== 'A') return;
|
|
|
|
const preNode = e.target.previousSibling;
|
|
|
|
if (preNode && preNode.nodeType === 3 && preNode.nodeValue.slice(-1) === '@') {
|
|
|
|
e.preventDefault();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
2019-03-14 02:15:25 +00:00
|
|
|
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">
|
2019-05-28 09:53:51 +00:00
|
|
|
<div className="reviewer-name ellipsis">{item.user_name}</div>
|
2019-03-14 02:15:25 +00:00
|
|
|
<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-07-26 08:51:47 +00:00
|
|
|
<Button className="comment-btn" color="secondary" size="sm" onClick={this.toggleEditComment}>{gettext('Cancel')}</Button>
|
2019-03-14 02:15:25 +00:00
|
|
|
</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">
|
2019-05-28 09:53:51 +00:00
|
|
|
<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>
|
2019-07-26 08:51:47 +00:00
|
|
|
{(item.user_email === username) &&
|
2019-02-14 12:10:15 +00:00
|
|
|
<DropdownItem onClick={this.props.deleteComment} className="delete-comment"
|
|
|
|
id={item.id}>{gettext('Delete')}</DropdownItem>}
|
2019-07-26 08:51:47 +00:00
|
|
|
{(item.user_email === username) &&
|
|
|
|
<DropdownItem onClick={this.toggleEditComment}
|
|
|
|
className="edit-comment" id={item.id}>{gettext('Edit')}</DropdownItem>
|
2019-03-14 02:15:25 +00:00
|
|
|
}
|
2019-07-26 08:51:47 +00:00
|
|
|
{!item.resolved &&
|
2019-02-14 12:10:15 +00:00
|
|
|
<DropdownItem onClick={this.props.resolveComment} className="seafile-comment-resolved"
|
|
|
|
id={item.id}>{gettext('Mark as resolved')}</DropdownItem>
|
|
|
|
}
|
|
|
|
</DropdownMenu>
|
|
|
|
</Dropdown>
|
|
|
|
</div>
|
2019-07-26 08:51:47 +00:00
|
|
|
<div
|
|
|
|
className="seafile-comment-content"
|
|
|
|
dangerouslySetInnerHTML={{ __html: this.state.html }}
|
|
|
|
onClick={e => this.onCommentClick(e)}
|
|
|
|
></div>
|
2019-02-14 12:10:15 +00:00
|
|
|
</li>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CommentItem.propTypes = commentItemPropTypes;
|
|
|
|
|
2019-02-25 10:09:17 +00:00
|
|
|
export default CommentPanel;
|