1 import { Component, linkEvent } from 'inferno';
3 CommentNode as CommentNodeI,
4 CommentForm as CommentFormI,
10 } from '../interfaces';
11 import { Subscription } from 'rxjs';
14 capitalizeFirstLetter,
15 mentionDropdownFetchLimit,
21 import { WebSocketService, UserService } from '../services';
22 import autosize from 'autosize';
23 import { i18n } from '../i18next';
24 import { T } from 'inferno-i18next';
25 import Tribute from 'tributejs/src/Tribute.js';
26 import emojiShortName from 'emoji-short-name';
28 interface CommentFormProps {
31 onReplyCancel?(): any;
36 interface CommentFormState {
37 commentForm: CommentFormI;
40 imageLoading: boolean;
43 export class CommentForm extends Component<CommentFormProps, CommentFormState> {
44 private id = `comment-form-${randomStr()}`;
45 private userSub: Subscription;
46 private communitySub: Subscription;
48 private emptyState: CommentFormState = {
52 post_id: this.props.node
53 ? this.props.node.comment.post_id
55 creator_id: UserService.Instance.user
56 ? UserService.Instance.user.id
59 buttonTitle: !this.props.node
60 ? capitalizeFirstLetter(i18n.t('post'))
62 ? capitalizeFirstLetter(i18n.t('edit'))
63 : capitalizeFirstLetter(i18n.t('reply')),
68 constructor(props: any, context: any) {
69 super(props, context);
71 this.tribute = new Tribute({
76 menuItemTemplate: (item: any) => {
77 let emoji = `:${item.original.key}:`;
78 return `${item.original.val} ${emoji}`;
80 selectTemplate: (item: any) => {
81 return `:${item.original.key}:`;
83 values: Object.entries(emojiShortName).map(e => {
84 return { key: e[1], val: e[0] };
87 autocompleteMode: true,
88 menuItemLimit: mentionDropdownFetchLimit,
93 selectTemplate: (item: any) => {
94 return `[/u/${item.original.key}](/u/${item.original.key})`;
96 values: (text: string, cb: any) => {
97 this.userSearch(text, (users: any) => cb(users));
100 autocompleteMode: true,
101 menuItemLimit: mentionDropdownFetchLimit,
107 selectTemplate: (item: any) => {
108 return `[/c/${item.original.key}](/c/${item.original.key})`;
110 values: (text: string, cb: any) => {
111 this.communitySearch(text, (communities: any) => cb(communities));
114 autocompleteMode: true,
115 menuItemLimit: mentionDropdownFetchLimit,
120 this.state = this.emptyState;
122 if (this.props.node) {
123 if (this.props.edit) {
124 this.state.commentForm.edit_id = this.props.node.comment.id;
125 this.state.commentForm.parent_id = this.props.node.comment.parent_id;
126 this.state.commentForm.content = this.props.node.comment.content;
127 this.state.commentForm.creator_id = this.props.node.comment.creator_id;
129 // A reply gets a new parent id
130 this.state.commentForm.parent_id = this.props.node.comment.id;
135 componentDidMount() {
136 var textarea: any = document.getElementById(this.id);
138 this.tribute.attach(textarea);
139 textarea.addEventListener('tribute-replaced', () => {
140 this.state.commentForm.content = textarea.value;
141 this.setState(this.state);
142 autosize.update(textarea);
149 <form onSubmit={linkEvent(this, this.handleCommentSubmit)}>
150 <div class="form-group row">
151 <div className={`col-sm-12`}>
154 className={`form-control ${this.state.previewMode && 'd-none'}`}
155 value={this.state.commentForm.content}
156 onInput={linkEvent(this, this.handleCommentContentChange)}
158 disabled={this.props.disabled}
162 {this.state.previewMode && (
165 dangerouslySetInnerHTML={mdToHtml(
166 this.state.commentForm.content
173 <div class="col-sm-12">
176 class="btn btn-sm btn-secondary mr-2"
177 disabled={this.props.disabled}
179 {this.state.buttonTitle}
181 {this.state.commentForm.content && (
183 className={`btn btn-sm mr-2 btn-secondary ${this.state
184 .previewMode && 'active'}`}
185 onClick={linkEvent(this, this.handlePreviewToggle)}
187 <T i18nKey="preview">#</T>
190 {this.props.node && (
193 class="btn btn-sm btn-secondary mr-2"
194 onClick={linkEvent(this, this.handleReplyCancel)}
196 <T i18nKey="cancel">#</T>
200 href={markdownHelpUrl}
202 class="d-inline-block float-right text-muted small font-weight-bold"
204 <T i18nKey="formatting_help">#</T>
206 <form class="d-inline-block mr-2 float-right text-muted small font-weight-bold">
208 htmlFor={`file-upload-${this.id}`}
209 className={`${UserService.Instance.user && 'pointer'}`}
211 <T i18nKey="upload_image">#</T>
214 id={`file-upload-${this.id}`}
216 accept="image/*,video/*"
219 disabled={!UserService.Instance.user}
220 onChange={linkEvent(this, this.handleImageUpload)}
223 {this.state.imageLoading && (
224 <svg class="icon icon-spinner spin">
225 <use xlinkHref="#icon-spinner"></use>
235 handleCommentSubmit(i: CommentForm, event: any) {
236 event.preventDefault();
238 WebSocketService.Instance.editComment(i.state.commentForm);
240 WebSocketService.Instance.createComment(i.state.commentForm);
243 i.state.previewMode = false;
244 i.state.commentForm.content = undefined;
245 event.target.reset();
248 i.props.onReplyCancel();
251 autosize.update(document.querySelector('textarea'));
254 handleCommentContentChange(i: CommentForm, event: any) {
255 i.state.commentForm.content = event.target.value;
259 handlePreviewToggle(i: CommentForm, event: any) {
260 event.preventDefault();
261 i.state.previewMode = !i.state.previewMode;
265 handleReplyCancel(i: CommentForm) {
266 i.props.onReplyCancel();
269 handleImageUpload(i: CommentForm, event: any) {
270 event.preventDefault();
271 let file = event.target.files[0];
272 const imageUploadUrl = `/pictshare/api/upload.php`;
273 const formData = new FormData();
274 formData.append('file', file);
276 i.state.imageLoading = true;
279 fetch(imageUploadUrl, {
283 .then(res => res.json())
285 let url = `${window.location.origin}/pictshare/${res.url}`;
287 res.filetype == 'mp4' ? `[vid](${url}/raw)` : `![](${url})`;
288 let content = i.state.commentForm.content;
289 content = content ? `${content} ${markdown}` : markdown;
290 i.state.commentForm.content = content;
291 i.state.imageLoading = false;
295 i.state.imageLoading = false;
297 toast(error, 'danger');
301 userSearch(text: string, cb: any) {
303 let form: SearchForm = {
305 type_: SearchType[SearchType.Users],
306 sort: SortType[SortType.TopAll],
308 limit: mentionDropdownFetchLimit,
311 WebSocketService.Instance.search(form);
313 this.userSub = WebSocketService.Instance.subject.subscribe(
315 let res = wsJsonToRes(msg);
316 if (res.op == UserOperation.Search) {
317 let data = res.data as SearchResponse;
318 let users = data.users.map(u => {
319 return { key: u.name };
322 this.userSub.unsubscribe();
325 err => console.error(err),
326 () => console.log('complete')
333 communitySearch(text: string, cb: any) {
335 let form: SearchForm = {
337 type_: SearchType[SearchType.Communities],
338 sort: SortType[SortType.TopAll],
340 limit: mentionDropdownFetchLimit,
343 WebSocketService.Instance.search(form);
345 this.communitySub = WebSocketService.Instance.subject.subscribe(
347 let res = wsJsonToRes(msg);
348 if (res.op == UserOperation.Search) {
349 let data = res.data as SearchResponse;
350 let communities = data.communities.map(u => {
351 return { key: u.name };
354 this.communitySub.unsubscribe();
357 err => console.error(err),
358 () => console.log('complete')