import React from 'react'; import PropTypes from 'prop-types'; import ReactUtils from '../ReactUtils'; import CommentEditor from '../PageComment/CommentEditor'; import CommentPreview from '../PageComment/CommentPreview'; import Button from 'react-bootstrap/es/Button'; import Tab from 'react-bootstrap/es/Tab'; import Tabs from 'react-bootstrap/es/Tabs'; import UserPicture from '../User/UserPicture'; /** * * @author Yuki Takei * * @export * @class Comment * @extends {React.Component} */ export default class CommentForm extends React.Component { constructor(props) { super(props); this.state = { comment: '', isMarkdown: true, html: '', key: 1, }; this.updateState = this.updateState.bind(this); this.postComment = this.postComment.bind(this); this.renderHtml = this.renderHtml.bind(this); this.handleSelect = this.handleSelect.bind(this); } updateState(event) { const target = event.target; const value = target.type === 'checkbox' ? target.checked : target.value; const name = target.name; this.setState({ [name]: value }); } handleSelect(key) { this.setState({ key }); this.renderHtml(this.state.comment); } /** * Load data of comments and rerender */ postComment(event) { event.preventDefault(); this.props.crowi.apiPost('/comments.add', { commentForm: { comment: this.state.comment, _csrf: this.props.crowi.csrfToken, page_id: this.props.pageId, revision_id: this.props.revisionId, is_markdown: this.state.isMarkdown, } }) .then((res) => { if (this.props.onPostComplete != null) { this.props.onPostComplete(res.comment); } this.setState({ comment: '', isMarkdown: true, html: '', key: 1, }); }); } getCommentHtml() { return ( this.previewElement = el}/> ); } renderHtml(markdown) { var context = { markdown, dom: this.previewElement, }; const crowiRenderer = this.props.crowiRenderer; const interceptorManager = this.props.crowi.interceptorManager; interceptorManager.process('preRenderCommnetPreview', context) .then(() => interceptorManager.process('prePreProcess', context)) .then(() => { context.markdown = crowiRenderer.preProcess(context.markdown); }) .then(() => interceptorManager.process('postPreProcess', context)) .then(() => { var parsedHTML = crowiRenderer.process(context.markdown); context['parsedHTML'] = parsedHTML; }) .then(() => interceptorManager.process('prePostProcess', context)) .then(() => { context.parsedHTML = crowiRenderer.postProcess(context.parsedHTML, context.dom); }) .then(() => interceptorManager.process('postPostProcess', context)) .then(() => interceptorManager.process('preRenderCommentPreviewHtml', context)) .then(() => { this.setState({ html: context.parsedHTML }); }) // process interceptors for post rendering .then(() => interceptorManager.process('postRenderCommentPreviewHtml', context)); } generateInnerHtml(html) { return {__html: html}; } render() { const crowi = this.props.crowi; const username = crowi.me; const user = crowi.findUser(username); const creatorsPage = `/user/${username}`; const comment = this.state.comment; const commentPreview = this.state.isMarkdown ? this.getCommentHtml(): ReactUtils.nl2br(comment); return (
{ username &&
{/* */} { this.state.isMarkdown == true &&
{commentPreview}
}
{ this.state.key == 1 && }
}
); } } CommentForm.propTypes = { crowi: PropTypes.object.isRequired, onPostComplete: PropTypes.func, pageId: PropTypes.string, revisionId: PropTypes.string, crowiRenderer: PropTypes.object.isRequired, };