1 import { Component, linkEvent } from 'inferno';
3 CommentNode as CommentNodeI,
4 CommentForm as CommentFormI,
5 } from '../interfaces';
14 import { WebSocketService, UserService } from '../services';
15 import autosize from 'autosize';
16 import Tribute from 'tributejs/src/Tribute.js';
17 import { i18n } from '../i18next';
18 import { T } from 'inferno-i18next';
20 interface CommentFormProps {
23 onReplyCancel?(): any;
28 interface CommentFormState {
29 commentForm: CommentFormI;
32 imageLoading: boolean;
35 export class CommentForm extends Component<CommentFormProps, CommentFormState> {
36 private id = `comment-form-${randomStr()}`;
37 private tribute: Tribute;
38 private emptyState: CommentFormState = {
42 post_id: this.props.node
43 ? this.props.node.comment.post_id
45 creator_id: UserService.Instance.user
46 ? UserService.Instance.user.id
49 buttonTitle: !this.props.node
50 ? capitalizeFirstLetter(i18n.t('post'))
52 ? capitalizeFirstLetter(i18n.t('edit'))
53 : capitalizeFirstLetter(i18n.t('reply')),
58 constructor(props: any, context: any) {
59 super(props, context);
61 this.tribute = setupTribute();
62 this.state = this.emptyState;
64 if (this.props.node) {
65 if (this.props.edit) {
66 this.state.commentForm.edit_id = this.props.node.comment.id;
67 this.state.commentForm.parent_id = this.props.node.comment.parent_id;
68 this.state.commentForm.content = this.props.node.comment.content;
69 this.state.commentForm.creator_id = this.props.node.comment.creator_id;
71 // A reply gets a new parent id
72 this.state.commentForm.parent_id = this.props.node.comment.id;
78 var textarea: any = document.getElementById(this.id);
80 this.tribute.attach(textarea);
81 textarea.addEventListener('tribute-replaced', () => {
82 this.state.commentForm.content = textarea.value;
83 this.setState(this.state);
84 autosize.update(textarea);
91 <form onSubmit={linkEvent(this, this.handleCommentSubmit)}>
92 <div class="form-group row">
93 <div className={`col-sm-12`}>
96 className={`form-control ${this.state.previewMode && 'd-none'}`}
97 value={this.state.commentForm.content}
98 onInput={linkEvent(this, this.handleCommentContentChange)}
100 disabled={this.props.disabled}
104 {this.state.previewMode && (
107 dangerouslySetInnerHTML={mdToHtml(
108 this.state.commentForm.content
115 <div class="col-sm-12">
118 class="btn btn-sm btn-secondary mr-2"
119 disabled={this.props.disabled}
121 {this.state.buttonTitle}
123 {this.state.commentForm.content && (
125 className={`btn btn-sm mr-2 btn-secondary ${this.state
126 .previewMode && 'active'}`}
127 onClick={linkEvent(this, this.handlePreviewToggle)}
129 <T i18nKey="preview">#</T>
132 {this.props.node && (
135 class="btn btn-sm btn-secondary mr-2"
136 onClick={linkEvent(this, this.handleReplyCancel)}
138 <T i18nKey="cancel">#</T>
142 href={markdownHelpUrl}
144 class="d-inline-block float-right text-muted small font-weight-bold"
146 <T i18nKey="formatting_help">#</T>
148 <form class="d-inline-block mr-2 float-right text-muted small font-weight-bold">
150 htmlFor={`file-upload-${this.id}`}
151 className={`${UserService.Instance.user && 'pointer'}`}
153 <T i18nKey="upload_image">#</T>
156 id={`file-upload-${this.id}`}
158 accept="image/*,video/*"
161 disabled={!UserService.Instance.user}
162 onChange={linkEvent(this, this.handleImageUpload)}
165 {this.state.imageLoading && (
166 <svg class="icon icon-spinner spin">
167 <use xlinkHref="#icon-spinner"></use>
177 handleCommentSubmit(i: CommentForm, event: any) {
178 event.preventDefault();
180 WebSocketService.Instance.editComment(i.state.commentForm);
182 WebSocketService.Instance.createComment(i.state.commentForm);
185 i.state.previewMode = false;
186 i.state.commentForm.content = undefined;
187 event.target.reset();
190 i.props.onReplyCancel();
193 autosize.update(document.querySelector('textarea'));
196 handleCommentContentChange(i: CommentForm, event: any) {
197 i.state.commentForm.content = event.target.value;
201 handlePreviewToggle(i: CommentForm, event: any) {
202 event.preventDefault();
203 i.state.previewMode = !i.state.previewMode;
207 handleReplyCancel(i: CommentForm) {
208 i.props.onReplyCancel();
211 handleImageUpload(i: CommentForm, event: any) {
212 event.preventDefault();
213 let file = event.target.files[0];
214 const imageUploadUrl = `/pictshare/api/upload.php`;
215 const formData = new FormData();
216 formData.append('file', file);
218 i.state.imageLoading = true;
221 fetch(imageUploadUrl, {
225 .then(res => res.json())
227 let url = `${window.location.origin}/pictshare/${res.url}`;
229 res.filetype == 'mp4' ? `[vid](${url}/raw)` : `![](${url})`;
230 let content = i.state.commentForm.content;
231 content = content ? `${content} ${markdown}` : markdown;
232 i.state.commentForm.content = content;
233 i.state.imageLoading = false;
237 i.state.imageLoading = false;
239 toast(error, 'danger');