import React from 'react'; import PropTypes from 'prop-types'; import CommentPreview from '../PageComment/CommentPreview'; import Button from 'react-bootstrap/es/Button'; import FormControl from 'react-bootstrap/es/FormControl'; 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: '', }; this.updateState = this.updateState.bind(this); this.postComment = this.postComment.bind(this); this.renderHtml = this.renderHtml.bind(this); this.getCommentHtml = this.getCommentHtml.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 }); } getCommentHtml(){ if(this.state.isMarkdown){ this.renderHtml(this.state.comment) } else { this.setState({ html: 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: false, }); }); } renderHtml(markdown) { var context = { markdown, dom: this.previewElement, }; const crowiRenderer = this.props.crowiRenderer; const interceptorManager = this.props.crowi.interceptorManager; interceptorManager.process('prePreviewRender', context) .then(() => interceptorManager.process('prePreviewPreProcess', context)) .then(() => { context.markdown = crowiRenderer.preProcess(context.markdown); }) .then(() => interceptorManager.process('postPreviewPreProcess', context)) .then(() => { var parsedHTML = crowiRenderer.process(context.markdown); context['parsedHTML'] = parsedHTML; }) .then(() => interceptorManager.process('prePreviewPostProcess', context)) .then(() => { context.parsedHTML = crowiRenderer.postProcess(context.parsedHTML, context.dom); }) .then(() => interceptorManager.process('postPreviewPostProcess', context)) .then(() => interceptorManager.process('prePreviewRenderHtml', context)) .then(() => {    this.setState({ html: context.parsedHTML }); }) // process interceptors for post rendering .then(() => interceptorManager.process('postPreviewRenderHtml', context)); } generateInnerHtml(html) { return {__html: html}; } render() { console.log(this.state.html) //{% if not user %}disabled{% endif %}をtextareaとbuttonに追加 // denounce/throttle return (
Markdown
this.previewElement = el} />
); } } CommentForm.propTypes = { crowi: PropTypes.object.isRequired, onPostComplete: PropTypes.func, pageId: PropTypes.string, revisionId: PropTypes.string, crowiRenderer: PropTypes.object.isRequired, };