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 font-weight-bold"
145 title={i18n.t('formatting_help')}
147 <svg class="icon icon-inline">
148 <use xlinkHref="#icon-help-circle"></use>
151 <form class="d-inline-block mr-3 float-right text-muted font-weight-bold">
153 htmlFor={`file-upload-${this.id}`}
154 className={`${UserService.Instance.user && 'pointer'}`}
155 data-tippy-content={i18n.t('upload_image')}
157 <svg class="icon icon-inline">
158 <use xlinkHref="#icon-image"></use>
162 id={`file-upload-${this.id}`}
164 accept="image/*,video/*"
167 disabled={!UserService.Instance.user}
168 onChange={linkEvent(this, this.handleImageUpload)}
171 {this.state.imageLoading && (
172 <svg class="icon icon-spinner spin">
173 <use xlinkHref="#icon-spinner"></use>
183 handleCommentSubmit(i: CommentForm, event: any) {
184 event.preventDefault();
186 WebSocketService.Instance.editComment(i.state.commentForm);
188 WebSocketService.Instance.createComment(i.state.commentForm);
191 i.state.previewMode = false;
192 i.state.commentForm.content = undefined;
193 event.target.reset();
196 i.props.onReplyCancel();
199 autosize.update(document.querySelector('textarea'));
202 handleCommentContentChange(i: CommentForm, event: any) {
203 i.state.commentForm.content = event.target.value;
207 handlePreviewToggle(i: CommentForm, event: any) {
208 event.preventDefault();
209 i.state.previewMode = !i.state.previewMode;
213 handleReplyCancel(i: CommentForm) {
214 i.props.onReplyCancel();
217 handleImageUploadPaste(i: CommentForm, event: any) {
218 let image = event.clipboardData.files[0];
220 i.handleImageUpload(i, image);
224 handleImageUpload(i: CommentForm, event: any) {
227 event.preventDefault();
228 file = event.target.files[0];
233 const imageUploadUrl = `/pictshare/api/upload.php`;
234 const formData = new FormData();
235 formData.append('file', file);
237 i.state.imageLoading = true;
240 fetch(imageUploadUrl, {
244 .then(res => res.json())
246 let url = `${window.location.origin}/pictshare/${res.url}`;
248 res.filetype == 'mp4' ? `[vid](${url}/raw)` : `![](${url})`;
249 let content = i.state.commentForm.content;
250 content = content ? `${content}\n${imageMarkdown}` : imageMarkdown;
251 i.state.commentForm.content = content;
252 i.state.imageLoading = false;
254 var textarea: any = document.getElementById(i.id);
255 autosize.update(textarea);
258 i.state.imageLoading = false;
260 toast(error, 'danger');