1 import { Component, linkEvent } from 'inferno';
2 import { Prompt } from 'inferno-router';
4 CommentNode as CommentNodeI,
5 CommentForm as CommentFormI,
6 } from '../interfaces';
15 import { WebSocketService, UserService } from '../services';
16 import autosize from 'autosize';
17 import Tribute from 'tributejs/src/Tribute.js';
18 import { i18n } from '../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);
92 when={this.state.commentForm.content}
93 message={i18n.t('block_leaving')}
95 <form onSubmit={linkEvent(this, this.handleCommentSubmit)}>
96 <div class="form-group row">
97 <div className={`col-sm-12`}>
100 className={`form-control ${this.state.previewMode && 'd-none'}`}
101 value={this.state.commentForm.content}
102 onInput={linkEvent(this, this.handleCommentContentChange)}
103 onPaste={linkEvent(this, this.handleImageUploadPaste)}
105 disabled={this.props.disabled}
109 {this.state.previewMode && (
112 dangerouslySetInnerHTML={mdToHtml(
113 this.state.commentForm.content
120 <div class="col-sm-12">
123 class="btn btn-sm btn-secondary mr-2"
124 disabled={this.props.disabled}
126 {this.state.buttonTitle}
128 {this.state.commentForm.content && (
130 className={`btn btn-sm mr-2 btn-secondary ${this.state
131 .previewMode && 'active'}`}
132 onClick={linkEvent(this, this.handlePreviewToggle)}
137 {this.props.node && (
140 class="btn btn-sm btn-secondary mr-2"
141 onClick={linkEvent(this, this.handleReplyCancel)}
147 href={markdownHelpUrl}
149 class="d-inline-block float-right text-muted font-weight-bold"
150 title={i18n.t('formatting_help')}
152 <svg class="icon icon-inline">
153 <use xlinkHref="#icon-help-circle"></use>
156 <form class="d-inline-block mr-3 float-right text-muted font-weight-bold">
158 htmlFor={`file-upload-${this.id}`}
159 className={`${UserService.Instance.user && 'pointer'}`}
160 data-tippy-content={i18n.t('upload_image')}
162 <svg class="icon icon-inline">
163 <use xlinkHref="#icon-image"></use>
167 id={`file-upload-${this.id}`}
169 accept="image/*,video/*"
172 disabled={!UserService.Instance.user}
173 onChange={linkEvent(this, this.handleImageUpload)}
176 {this.state.imageLoading && (
177 <svg class="icon icon-spinner spin">
178 <use xlinkHref="#icon-spinner"></use>
188 handleCommentSubmit(i: CommentForm, event: any) {
189 event.preventDefault();
191 WebSocketService.Instance.editComment(i.state.commentForm);
193 WebSocketService.Instance.createComment(i.state.commentForm);
196 i.state.previewMode = false;
197 i.state.commentForm.content = undefined;
198 event.target.reset();
201 i.props.onReplyCancel();
204 autosize.update(document.querySelector('textarea'));
207 handleCommentContentChange(i: CommentForm, event: any) {
208 i.state.commentForm.content = event.target.value;
212 handlePreviewToggle(i: CommentForm, event: any) {
213 event.preventDefault();
214 i.state.previewMode = !i.state.previewMode;
218 handleReplyCancel(i: CommentForm) {
219 i.props.onReplyCancel();
222 handleImageUploadPaste(i: CommentForm, event: any) {
223 let image = event.clipboardData.files[0];
225 i.handleImageUpload(i, image);
229 handleImageUpload(i: CommentForm, event: any) {
232 event.preventDefault();
233 file = event.target.files[0];
238 const imageUploadUrl = `/pictshare/api/upload.php`;
239 const formData = new FormData();
240 formData.append('file', file);
242 i.state.imageLoading = true;
245 fetch(imageUploadUrl, {
249 .then(res => res.json())
251 let url = `${window.location.origin}/pictshare/${res.url}`;
253 res.filetype == 'mp4' ? `[vid](${url}/raw)` : `![](${url})`;
254 let content = i.state.commentForm.content;
255 content = content ? `${content}\n${imageMarkdown}` : imageMarkdown;
256 i.state.commentForm.content = content;
257 i.state.imageLoading = false;
259 var textarea: any = document.getElementById(i.id);
260 autosize.update(textarea);
263 i.state.imageLoading = false;
265 toast(error, 'danger');