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';
19 interface CommentFormProps {
22 onReplyCancel?(): any;
27 interface CommentFormState {
28 commentForm: CommentFormI;
31 imageLoading: boolean;
34 export class CommentForm extends Component<CommentFormProps, CommentFormState> {
35 private id = `comment-form-${randomStr()}`;
36 private tribute: Tribute;
37 private emptyState: CommentFormState = {
41 post_id: this.props.node
42 ? this.props.node.comment.post_id
44 creator_id: UserService.Instance.user
45 ? UserService.Instance.user.id
48 buttonTitle: !this.props.node
49 ? capitalizeFirstLetter(i18n.t('post'))
51 ? capitalizeFirstLetter(i18n.t('edit'))
52 : capitalizeFirstLetter(i18n.t('reply')),
57 constructor(props: any, context: any) {
58 super(props, context);
60 this.tribute = setupTribute();
61 this.state = this.emptyState;
63 if (this.props.node) {
64 if (this.props.edit) {
65 this.state.commentForm.edit_id = this.props.node.comment.id;
66 this.state.commentForm.parent_id = this.props.node.comment.parent_id;
67 this.state.commentForm.content = this.props.node.comment.content;
68 this.state.commentForm.creator_id = this.props.node.comment.creator_id;
70 // A reply gets a new parent id
71 this.state.commentForm.parent_id = this.props.node.comment.id;
77 var textarea: any = document.getElementById(this.id);
79 this.tribute.attach(textarea);
80 textarea.addEventListener('tribute-replaced', () => {
81 this.state.commentForm.content = textarea.value;
82 this.setState(this.state);
83 autosize.update(textarea);
90 <form onSubmit={linkEvent(this, this.handleCommentSubmit)}>
91 <div class="form-group row">
92 <div className={`col-sm-12`}>
95 className={`form-control ${this.state.previewMode && 'd-none'}`}
96 value={this.state.commentForm.content}
97 onInput={linkEvent(this, this.handleCommentContentChange)}
98 onPaste={linkEvent(this, this.handleImageUploadPaste)}
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)}
132 {this.props.node && (
135 class="btn btn-sm btn-secondary mr-2"
136 onClick={linkEvent(this, this.handleReplyCancel)}
142 href={markdownHelpUrl}
144 class="d-inline-block float-right text-muted small font-weight-bold"
146 {i18n.t('formatting_help')}
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 {i18n.t('upload_image')}
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 handleImageUploadPaste(i: CommentForm, event: any) {
212 let image = event.clipboardData.files[0];
214 i.handleImageUpload(i, image);
218 handleImageUpload(i: CommentForm, event: any) {
221 event.preventDefault();
222 file = event.target.files[0];
227 const imageUploadUrl = `/pictshare/api/upload.php`;
228 const formData = new FormData();
229 formData.append('file', file);
231 i.state.imageLoading = true;
234 fetch(imageUploadUrl, {
238 .then(res => res.json())
240 let url = `${window.location.origin}/pictshare/${res.url}`;
242 res.filetype == 'mp4' ? `[vid](${url}/raw)` : `![](${url})`;
243 let content = i.state.commentForm.content;
244 content = content ? `${content}\n${imageMarkdown}` : imageMarkdown;
245 i.state.commentForm.content = content;
246 i.state.imageLoading = false;
248 var textarea: any = document.getElementById(i.id);
249 autosize.update(textarea);
252 i.state.imageLoading = false;
254 toast(error, 'danger');