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)}
99 onPaste={linkEvent(this, this.handleImageUploadPaste)}
101 disabled={this.props.disabled}
105 {this.state.previewMode && (
108 dangerouslySetInnerHTML={mdToHtml(
109 this.state.commentForm.content
116 <div class="col-sm-12">
119 class="btn btn-sm btn-secondary mr-2"
120 disabled={this.props.disabled}
122 {this.state.buttonTitle}
124 {this.state.commentForm.content && (
126 className={`btn btn-sm mr-2 btn-secondary ${this.state
127 .previewMode && 'active'}`}
128 onClick={linkEvent(this, this.handlePreviewToggle)}
130 <T i18nKey="preview">#</T>
133 {this.props.node && (
136 class="btn btn-sm btn-secondary mr-2"
137 onClick={linkEvent(this, this.handleReplyCancel)}
139 <T i18nKey="cancel">#</T>
143 href={markdownHelpUrl}
145 class="d-inline-block float-right text-muted small font-weight-bold"
147 <T i18nKey="formatting_help">#</T>
149 <form class="d-inline-block mr-2 float-right text-muted small font-weight-bold">
151 htmlFor={`file-upload-${this.id}`}
152 className={`${UserService.Instance.user && 'pointer'}`}
154 <T i18nKey="upload_image">#</T>
157 id={`file-upload-${this.id}`}
159 accept="image/*,video/*"
162 disabled={!UserService.Instance.user}
163 onChange={linkEvent(this, this.handleImageUpload)}
166 {this.state.imageLoading && (
167 <svg class="icon icon-spinner spin">
168 <use xlinkHref="#icon-spinner"></use>
178 handleCommentSubmit(i: CommentForm, event: any) {
179 event.preventDefault();
181 WebSocketService.Instance.editComment(i.state.commentForm);
183 WebSocketService.Instance.createComment(i.state.commentForm);
186 i.state.previewMode = false;
187 i.state.commentForm.content = undefined;
188 event.target.reset();
191 i.props.onReplyCancel();
194 autosize.update(document.querySelector('textarea'));
197 handleCommentContentChange(i: CommentForm, event: any) {
198 i.state.commentForm.content = event.target.value;
202 handlePreviewToggle(i: CommentForm, event: any) {
203 event.preventDefault();
204 i.state.previewMode = !i.state.previewMode;
208 handleReplyCancel(i: CommentForm) {
209 i.props.onReplyCancel();
212 handleImageUploadPaste(i: CommentForm, event: any) {
213 let image = event.clipboardData.files[0];
215 i.handleImageUpload(i, image);
219 handleImageUpload(i: CommentForm, event: any) {
222 event.preventDefault();
223 file = event.target.files[0];
228 const imageUploadUrl = `/pictshare/api/upload.php`;
229 const formData = new FormData();
230 formData.append('file', file);
232 i.state.imageLoading = true;
235 fetch(imageUploadUrl, {
239 .then(res => res.json())
241 let url = `${window.location.origin}/pictshare/${res.url}`;
243 res.filetype == 'mp4' ? `[vid](${url}/raw)` : `![](${url})`;
244 let content = i.state.commentForm.content;
245 content = content ? `${content}\n${imageMarkdown}` : imageMarkdown;
246 i.state.commentForm.content = content;
247 i.state.imageLoading = false;
249 var textarea: any = document.getElementById(i.id);
250 autosize.update(textarea);
253 i.state.imageLoading = false;
255 toast(error, 'danger');