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', twemojiHtmlStr => {
234 if (this.state.commentForm.content == null) {
235 this.state.commentForm.content = '';
237 var el = document.createElement('div');
238 el.innerHTML = twemojiHtmlStr;
239 let nativeUnicode = (el.childNodes[0] as HTMLElement).getAttribute('alt');
240 let shortName = `:${emojiShortName[nativeUnicode]}:`;
241 this.state.commentForm.content += shortName;
242 this.setState(this.state);
247 this.state.previewMode = false;
248 this.state.loading = false;
249 this.state.commentForm.content = '';
250 this.setState(this.state);
251 let form: any = document.getElementById(this.formId);
253 if (this.props.node) {
254 this.props.onReplyCancel();
256 autosize.update(document.querySelector('textarea'));
257 this.setState(this.state);
260 handleCommentSubmit(i: CommentForm, event: any) {
261 event.preventDefault();
263 WebSocketService.Instance.editComment(i.state.commentForm);
265 WebSocketService.Instance.createComment(i.state.commentForm);
268 i.state.loading = true;
272 handleEmojiPickerClick(_i: CommentForm, event: any) {
273 emojiPicker.togglePicker(event.target);
276 handleCommentContentChange(i: CommentForm, event: any) {
277 i.state.commentForm.content = event.target.value;
281 handlePreviewToggle(i: CommentForm, event: any) {
282 event.preventDefault();
283 i.state.previewMode = !i.state.previewMode;
287 handleReplyCancel(i: CommentForm) {
288 i.props.onReplyCancel();
291 handleImageUploadPaste(i: CommentForm, event: any) {
292 let image = event.clipboardData.files[0];
294 i.handleImageUpload(i, image);
298 handleImageUpload(i: CommentForm, event: any) {
301 event.preventDefault();
302 file = event.target.files[0];
307 const imageUploadUrl = `/pictrs/image`;
308 const formData = new FormData();
309 formData.append('file', file);
311 i.state.imageLoading = true;
314 fetch(imageUploadUrl, {
318 .then(res => res.json())
320 let url = `${window.location.origin}/pictrs/${res.url}`;
322 res.filetype == 'mp4' ? `[vid](${url}/raw)` : `![](${url})`;
323 let content = i.state.commentForm.content;
324 content = content ? `${content}\n${imageMarkdown}` : imageMarkdown;
325 i.state.commentForm.content = content;
326 i.state.imageLoading = false;
328 let textarea: any = document.getElementById(i.id);
329 autosize.update(textarea);
332 i.state.imageLoading = false;
334 toast(error, 'danger');
338 parseMessage(msg: WebSocketJsonResponse) {
339 let res = wsJsonToRes(msg);
341 // Only do the showing and hiding if logged in
342 if (UserService.Instance.user) {
343 if (res.op == UserOperation.CreateComment) {
344 let data = res.data as CommentResponse;
345 if (data.comment.creator_id == UserService.Instance.user.id) {
346 this.handleFinished();
348 } else if (res.op == UserOperation.EditComment) {
349 let data = res.data as CommentResponse;
350 if (data.comment.creator_id == UserService.Instance.user.id) {
351 this.handleFinished();