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,
23 import { WebSocketService, UserService } from '../services';
24 import autosize from 'autosize';
25 import Tribute from 'tributejs/src/Tribute.js';
26 import emojiShortName from 'emoji-short-name';
27 import { i18n } from '../i18next';
29 interface CommentFormProps {
32 onReplyCancel?(): any;
37 interface CommentFormState {
38 commentForm: CommentFormI;
42 imageLoading: boolean;
45 export class CommentForm extends Component<CommentFormProps, CommentFormState> {
46 private id = `comment-textarea-${randomStr()}`;
47 private formId = `comment-form-${randomStr()}`;
48 private tribute: Tribute;
49 private subscription: Subscription;
50 private emptyState: CommentFormState = {
54 post_id: this.props.node
55 ? this.props.node.comment.post_id
57 creator_id: UserService.Instance.user
58 ? UserService.Instance.user.id
61 buttonTitle: !this.props.node
62 ? capitalizeFirstLetter(i18n.t('post'))
64 ? capitalizeFirstLetter(i18n.t('save'))
65 : capitalizeFirstLetter(i18n.t('reply')),
71 constructor(props: any, context: any) {
72 super(props, context);
74 this.tribute = setupTribute();
75 this.setupEmojiPicker();
77 this.state = this.emptyState;
79 if (this.props.node) {
80 if (this.props.edit) {
81 this.state.commentForm.edit_id = this.props.node.comment.id;
82 this.state.commentForm.parent_id = this.props.node.comment.parent_id;
83 this.state.commentForm.content = this.props.node.comment.content;
84 this.state.commentForm.creator_id = this.props.node.comment.creator_id;
86 // A reply gets a new parent id
87 this.state.commentForm.parent_id = this.props.node.comment.id;
91 this.subscription = WebSocketService.Instance.subject
92 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
94 msg => this.parseMessage(msg),
95 err => console.error(err),
96 () => console.log('complete')
100 componentDidMount() {
101 var textarea: any = document.getElementById(this.id);
103 this.tribute.attach(textarea);
104 textarea.addEventListener('tribute-replaced', () => {
105 this.state.commentForm.content = textarea.value;
106 this.setState(this.state);
107 autosize.update(textarea);
111 componentWillUnmount() {
112 this.subscription.unsubscribe();
119 when={this.state.commentForm.content}
120 message={i18n.t('block_leaving')}
124 onSubmit={linkEvent(this, this.handleCommentSubmit)}
126 <div class="form-group row">
127 <div className={`col-sm-12`}>
130 className={`form-control ${this.state.previewMode && 'd-none'}`}
131 value={this.state.commentForm.content}
132 onInput={linkEvent(this, this.handleCommentContentChange)}
133 onPaste={linkEvent(this, this.handleImageUploadPaste)}
135 disabled={this.props.disabled}
139 {this.state.previewMode && (
141 className="card card-body md-div"
142 dangerouslySetInnerHTML={mdToHtml(
143 this.state.commentForm.content
150 <div class="col-sm-12">
153 class="btn btn-sm btn-secondary mr-2"
154 disabled={this.props.disabled || this.state.loading}
156 {this.state.loading ? (
157 <svg class="icon icon-spinner spin">
158 <use xlinkHref="#icon-spinner"></use>
161 <span>{this.state.buttonTitle}</span>
164 {this.state.commentForm.content && (
166 className={`btn btn-sm mr-2 btn-secondary ${
167 this.state.previewMode && 'active'
169 onClick={linkEvent(this, this.handlePreviewToggle)}
174 {this.props.node && (
177 class="btn btn-sm btn-secondary mr-2"
178 onClick={linkEvent(this, this.handleReplyCancel)}
184 href={markdownHelpUrl}
186 class="d-inline-block float-right text-muted font-weight-bold"
187 title={i18n.t('formatting_help')}
189 <svg class="icon icon-inline">
190 <use xlinkHref="#icon-help-circle"></use>
193 <form class="d-inline-block mr-3 float-right text-muted font-weight-bold">
195 htmlFor={`file-upload-${this.id}`}
196 className={`${UserService.Instance.user && 'pointer'}`}
197 data-tippy-content={i18n.t('upload_image')}
199 <svg class="icon icon-inline">
200 <use xlinkHref="#icon-image"></use>
204 id={`file-upload-${this.id}`}
206 accept="image/*,video/*"
209 disabled={!UserService.Instance.user}
210 onChange={linkEvent(this, this.handleImageUpload)}
213 {this.state.imageLoading && (
214 <svg class="icon icon-spinner spin">
215 <use xlinkHref="#icon-spinner"></use>
219 onClick={linkEvent(this, this.handleEmojiPickerClick)}
220 class="pointer unselectable d-inline-block mr-3 float-right text-muted font-weight-bold"
221 data-tippy-content={i18n.t('emoji_picker')}
223 <svg class="icon icon-inline">
224 <use xlinkHref="#icon-smile"></use>
235 emojiPicker.on('emoji', twemojiHtmlStr => {
236 if (this.state.commentForm.content == null) {
237 this.state.commentForm.content = '';
239 var el = document.createElement('div');
240 el.innerHTML = twemojiHtmlStr;
241 let nativeUnicode = (el.childNodes[0] as HTMLElement).getAttribute('alt');
242 let shortName = `:${emojiShortName[nativeUnicode]}:`;
243 this.state.commentForm.content += shortName;
244 this.setState(this.state);
248 handleFinished(op: UserOperation, data: CommentResponse) {
250 this.props.node !== undefined && data.comment.parent_id !== null;
252 +!(data.comment.parent_id !== null) ^ +(this.props.node !== undefined);
255 (data.comment.creator_id == UserService.Instance.user.id &&
256 ((op == UserOperation.CreateComment &&
257 // If its a reply, make sure parent child match
259 data.comment.parent_id == this.props.node.comment.id) ||
260 // Otherwise, check the XOR of the two
261 (!isReply && xor))) ||
262 // If its a comment edit, only check that its from your user, and that its a
265 (op == UserOperation.EditComment && data.comment.content)
267 this.state.previewMode = false;
268 this.state.loading = false;
269 this.state.commentForm.content = '';
270 this.setState(this.state);
271 let form: any = document.getElementById(this.formId);
273 if (this.props.node) {
274 this.props.onReplyCancel();
276 autosize.update(form);
277 this.setState(this.state);
281 handleCommentSubmit(i: CommentForm, event: any) {
282 event.preventDefault();
284 WebSocketService.Instance.editComment(i.state.commentForm);
286 WebSocketService.Instance.createComment(i.state.commentForm);
289 i.state.loading = true;
293 handleEmojiPickerClick(_i: CommentForm, event: any) {
294 emojiPicker.togglePicker(event.target);
297 handleCommentContentChange(i: CommentForm, event: any) {
298 i.state.commentForm.content = event.target.value;
302 handlePreviewToggle(i: CommentForm, event: any) {
303 event.preventDefault();
304 i.state.previewMode = !i.state.previewMode;
308 handleReplyCancel(i: CommentForm) {
309 i.props.onReplyCancel();
312 handleImageUploadPaste(i: CommentForm, event: any) {
313 let image = event.clipboardData.files[0];
315 i.handleImageUpload(i, image);
319 handleImageUpload(i: CommentForm, event: any) {
322 event.preventDefault();
323 file = event.target.files[0];
328 const imageUploadUrl = `/pictrs/image`;
329 const formData = new FormData();
330 formData.append('images[]', file);
332 i.state.imageLoading = true;
335 fetch(imageUploadUrl, {
339 .then(res => res.json())
341 console.log('pictrs upload:');
343 if (res.msg == 'ok') {
344 let hash = res.files[0].file;
345 let url = `${window.location.origin}/pictrs/image/${hash}`;
346 let deleteToken = res.files[0].delete_token;
347 let deleteUrl = `${window.location.origin}/pictrs/image/delete/${deleteToken}/${hash}`;
348 let imageMarkdown = `![](${url})`;
349 let content = i.state.commentForm.content;
350 content = content ? `${content}\n${imageMarkdown}` : imageMarkdown;
351 i.state.commentForm.content = content;
352 i.state.imageLoading = false;
354 let textarea: any = document.getElementById(i.id);
355 autosize.update(textarea);
357 i18n.t('click_to_delete_picture'),
358 i18n.t('picture_deleted'),
362 i.state.imageLoading = false;
364 toast(JSON.stringify(res), 'danger');
368 i.state.imageLoading = false;
370 toast(error, 'danger');
374 parseMessage(msg: WebSocketJsonResponse) {
375 let res = wsJsonToRes(msg);
377 // Only do the showing and hiding if logged in
378 if (UserService.Instance.user) {
379 if (res.op == UserOperation.CreateComment) {
380 let data = res.data as CommentResponse;
381 this.handleFinished(res.op, data);
382 } else if (res.op == UserOperation.EditComment) {
383 let data = res.data as CommentResponse;
384 this.handleFinished(res.op, data);