1 import { Component, linkEvent } from 'inferno';
2 import { Subscription } from 'rxjs';
3 import { retryWhen, delay, take } from 'rxjs/operators';
4 import { Prompt } from 'inferno-router';
6 CommentNode as CommentNodeI,
7 CommentForm as CommentFormI,
11 } from '../interfaces';
13 capitalizeFirstLetter,
22 import { WebSocketService, UserService } from '../services';
23 import autosize from 'autosize';
24 import Tribute from 'tributejs/src/Tribute.js';
25 import emojiShortName from 'emoji-short-name';
26 import { i18n } from '../i18next';
28 interface CommentFormProps {
31 onReplyCancel?(): any;
36 interface CommentFormState {
37 commentForm: CommentFormI;
41 imageLoading: boolean;
44 export class CommentForm extends Component<CommentFormProps, CommentFormState> {
45 private id = `comment-textarea-${randomStr()}`;
46 private formId = `comment-form-${randomStr()}`;
47 private tribute: Tribute;
48 private subscription: Subscription;
49 private emptyState: CommentFormState = {
53 post_id: this.props.node
54 ? this.props.node.comment.post_id
56 creator_id: UserService.Instance.user
57 ? UserService.Instance.user.id
60 buttonTitle: !this.props.node
61 ? capitalizeFirstLetter(i18n.t('post'))
63 ? capitalizeFirstLetter(i18n.t('edit'))
64 : capitalizeFirstLetter(i18n.t('reply')),
70 constructor(props: any, context: any) {
71 super(props, context);
73 this.tribute = setupTribute();
74 this.setupEmojiPicker();
76 this.state = this.emptyState;
78 if (this.props.node) {
79 if (this.props.edit) {
80 this.state.commentForm.edit_id = this.props.node.comment.id;
81 this.state.commentForm.parent_id = this.props.node.comment.parent_id;
82 this.state.commentForm.content = this.props.node.comment.content;
83 this.state.commentForm.creator_id = this.props.node.comment.creator_id;
85 // A reply gets a new parent id
86 this.state.commentForm.parent_id = this.props.node.comment.id;
90 this.subscription = WebSocketService.Instance.subject
91 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
93 msg => this.parseMessage(msg),
94 err => console.error(err),
95 () => console.log('complete')
100 var textarea: any = document.getElementById(this.id);
102 this.tribute.attach(textarea);
103 textarea.addEventListener('tribute-replaced', () => {
104 this.state.commentForm.content = textarea.value;
105 this.setState(this.state);
106 autosize.update(textarea);
110 componentWillUnmount() {
111 this.subscription.unsubscribe();
118 when={this.state.commentForm.content}
119 message={i18n.t('block_leaving')}
123 onSubmit={linkEvent(this, this.handleCommentSubmit)}
125 <div class="form-group row">
126 <div className={`col-sm-12`}>
129 className={`form-control ${this.state.previewMode && 'd-none'}`}
130 value={this.state.commentForm.content}
131 onInput={linkEvent(this, this.handleCommentContentChange)}
132 onPaste={linkEvent(this, this.handleImageUploadPaste)}
134 disabled={this.props.disabled}
138 {this.state.previewMode && (
141 dangerouslySetInnerHTML={mdToHtml(
142 this.state.commentForm.content
149 <div class="col-sm-12">
152 class="btn btn-sm btn-secondary mr-2"
153 disabled={this.props.disabled}
155 {this.state.loading ? (
156 <svg class="icon icon-spinner spin">
157 <use xlinkHref="#icon-spinner"></use>
160 <span>{this.state.buttonTitle}</span>
163 {this.state.commentForm.content && (
165 className={`btn btn-sm mr-2 btn-secondary ${this.state
166 .previewMode && 'active'}`}
167 onClick={linkEvent(this, this.handlePreviewToggle)}
172 {this.props.node && (
175 class="btn btn-sm btn-secondary mr-2"
176 onClick={linkEvent(this, this.handleReplyCancel)}
182 href={markdownHelpUrl}
184 class="d-inline-block float-right text-muted font-weight-bold"
185 title={i18n.t('formatting_help')}
187 <svg class="icon icon-inline">
188 <use xlinkHref="#icon-help-circle"></use>
191 <form class="d-inline-block mr-3 float-right text-muted font-weight-bold">
193 htmlFor={`file-upload-${this.id}`}
194 className={`${UserService.Instance.user && 'pointer'}`}
195 data-tippy-content={i18n.t('upload_image')}
197 <svg class="icon icon-inline">
198 <use xlinkHref="#icon-image"></use>
202 id={`file-upload-${this.id}`}
204 accept="image/*,video/*"
207 disabled={!UserService.Instance.user}
208 onChange={linkEvent(this, this.handleImageUpload)}
211 {this.state.imageLoading && (
212 <svg class="icon icon-spinner spin">
213 <use xlinkHref="#icon-spinner"></use>
217 onClick={linkEvent(this, this.handleEmojiPickerClick)}
218 class="pointer unselectable d-inline-block mr-3 float-right text-muted font-weight-bold"
219 data-tippy-content={i18n.t('emoji_picker')}
221 <svg class="icon icon-inline">
222 <use xlinkHref="#icon-smile"></use>
233 emojiPicker.on('emoji', emoji => {
234 if (this.state.commentForm.content == null) {
235 this.state.commentForm.content = '';
237 let shortName = `:${emojiShortName[emoji]}:`;
238 this.state.commentForm.content += shortName;
239 this.setState(this.state);
244 this.state.previewMode = false;
245 this.state.loading = false;
246 this.state.commentForm.content = '';
247 this.setState(this.state);
248 let form: any = document.getElementById(this.formId);
250 if (this.props.node) {
251 this.props.onReplyCancel();
253 autosize.update(document.querySelector('textarea'));
254 this.setState(this.state);
257 handleCommentSubmit(i: CommentForm, event: any) {
258 event.preventDefault();
260 WebSocketService.Instance.editComment(i.state.commentForm);
262 WebSocketService.Instance.createComment(i.state.commentForm);
265 i.state.loading = true;
269 handleEmojiPickerClick(_i: CommentForm, event: any) {
270 emojiPicker.togglePicker(event.target);
273 handleCommentContentChange(i: CommentForm, event: any) {
274 i.state.commentForm.content = event.target.value;
278 handlePreviewToggle(i: CommentForm, event: any) {
279 event.preventDefault();
280 i.state.previewMode = !i.state.previewMode;
284 handleReplyCancel(i: CommentForm) {
285 i.props.onReplyCancel();
288 handleImageUploadPaste(i: CommentForm, event: any) {
289 let image = event.clipboardData.files[0];
291 i.handleImageUpload(i, image);
295 handleImageUpload(i: CommentForm, event: any) {
298 event.preventDefault();
299 file = event.target.files[0];
304 const imageUploadUrl = `/pictshare/api/upload.php`;
305 const formData = new FormData();
306 formData.append('file', file);
308 i.state.imageLoading = true;
311 fetch(imageUploadUrl, {
315 .then(res => res.json())
317 let url = `${window.location.origin}/pictshare/${res.url}`;
319 res.filetype == 'mp4' ? `[vid](${url}/raw)` : `![](${url})`;
320 let content = i.state.commentForm.content;
321 content = content ? `${content}\n${imageMarkdown}` : imageMarkdown;
322 i.state.commentForm.content = content;
323 i.state.imageLoading = false;
325 let textarea: any = document.getElementById(i.id);
326 autosize.update(textarea);
329 i.state.imageLoading = false;
331 toast(error, 'danger');
335 parseMessage(msg: WebSocketJsonResponse) {
336 let res = wsJsonToRes(msg);
338 // Only do the showing and hiding if logged in
339 if (UserService.Instance.user) {
340 if (res.op == UserOperation.CreateComment) {
341 let data = res.data as CommentResponse;
342 if (data.comment.creator_id == UserService.Instance.user.id) {
343 this.handleFinished();
345 } else if (res.op == UserOperation.EditComment) {
346 let data = res.data as CommentResponse;
347 if (data.comment.creator_id == UserService.Instance.user.id) {
348 this.handleFinished();