2019-04-08 05:19:02 +00:00
|
|
|
import { Component, linkEvent } from 'inferno';
|
2019-08-30 03:11:29 +00:00
|
|
|
import { CommentNode as CommentNodeI, CommentForm as CommentFormI, SearchForm, SearchType, SortType, UserOperation, SearchResponse } from '../interfaces';
|
|
|
|
import { Subscription } from "rxjs";
|
2019-09-08 16:54:53 +00:00
|
|
|
import { capitalizeFirstLetter, mentionDropdownFetchLimit, msgOp, mdToHtml, randomStr, markdownHelpUrl } from '../utils';
|
2019-04-15 23:12:06 +00:00
|
|
|
import { WebSocketService, UserService } from '../services';
|
2019-04-08 05:19:02 +00:00
|
|
|
import * as autosize from 'autosize';
|
2019-08-10 00:14:43 +00:00
|
|
|
import { i18n } from '../i18next';
|
|
|
|
import { T } from 'inferno-i18next';
|
2019-08-30 19:34:38 +00:00
|
|
|
import Tribute from 'tributejs/src/Tribute.js';
|
2019-09-04 22:22:31 +00:00
|
|
|
import * as emojiShortName from 'emoji-short-name';
|
2019-04-08 05:19:02 +00:00
|
|
|
|
|
|
|
interface CommentFormProps {
|
|
|
|
postId?: number;
|
|
|
|
node?: CommentNodeI;
|
|
|
|
onReplyCancel?(): any;
|
|
|
|
edit?: boolean;
|
2019-04-15 23:12:06 +00:00
|
|
|
disabled?: boolean;
|
2019-04-08 05:19:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
interface CommentFormState {
|
|
|
|
commentForm: CommentFormI;
|
|
|
|
buttonTitle: string;
|
2019-09-01 04:10:48 +00:00
|
|
|
previewMode: boolean;
|
2019-09-08 16:54:53 +00:00
|
|
|
imageLoading: boolean;
|
2019-04-08 05:19:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export class CommentForm extends Component<CommentFormProps, CommentFormState> {
|
|
|
|
|
2019-09-01 04:10:48 +00:00
|
|
|
private id = `comment-form-${randomStr()}`;
|
2019-08-30 03:11:29 +00:00
|
|
|
private userSub: Subscription;
|
|
|
|
private communitySub: Subscription;
|
|
|
|
private tribute: any;
|
2019-04-08 05:19:02 +00:00
|
|
|
private emptyState: CommentFormState = {
|
|
|
|
commentForm: {
|
|
|
|
auth: null,
|
|
|
|
content: null,
|
2019-04-15 23:12:06 +00:00
|
|
|
post_id: this.props.node ? this.props.node.comment.post_id : this.props.postId,
|
2019-04-16 23:04:23 +00:00
|
|
|
creator_id: UserService.Instance.user ? UserService.Instance.user.id : null,
|
2019-04-08 05:19:02 +00:00
|
|
|
},
|
2019-08-10 00:14:43 +00:00
|
|
|
buttonTitle: !this.props.node ? capitalizeFirstLetter(i18n.t('post')) : this.props.edit ? capitalizeFirstLetter(i18n.t('edit')) : capitalizeFirstLetter(i18n.t('reply')),
|
2019-09-01 04:10:48 +00:00
|
|
|
previewMode: false,
|
2019-09-08 16:54:53 +00:00
|
|
|
imageLoading: false,
|
2019-04-08 05:19:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
constructor(props: any, context: any) {
|
|
|
|
super(props, context);
|
|
|
|
|
2019-08-30 04:38:06 +00:00
|
|
|
this.tribute = new Tribute({
|
2019-08-30 03:11:29 +00:00
|
|
|
collection: [
|
2019-08-31 00:40:59 +00:00
|
|
|
// Emojis
|
|
|
|
{
|
|
|
|
trigger: ':',
|
|
|
|
menuItemTemplate: (item: any) => {
|
|
|
|
let emoji = `:${item.original.key}:`;
|
2019-09-02 22:55:50 +00:00
|
|
|
return `${item.original.val} ${emoji}`;
|
2019-08-31 00:40:59 +00:00
|
|
|
},
|
|
|
|
selectTemplate: (item: any) => {
|
|
|
|
return `:${item.original.key}:`;
|
|
|
|
},
|
2019-09-04 22:22:31 +00:00
|
|
|
values: Object.entries(emojiShortName).map(e => {return {'key': e[1], 'val': e[0]}}),
|
2019-08-31 00:40:59 +00:00
|
|
|
allowSpaces: false,
|
|
|
|
autocompleteMode: true,
|
|
|
|
menuItemLimit: 10,
|
|
|
|
},
|
2019-08-30 03:11:29 +00:00
|
|
|
// Users
|
|
|
|
{
|
|
|
|
trigger: '@',
|
|
|
|
selectTemplate: (item: any) => {
|
2019-08-30 04:41:41 +00:00
|
|
|
return `[/u/${item.original.key}](/u/${item.original.key})`;
|
2019-08-30 03:11:29 +00:00
|
|
|
},
|
|
|
|
values: (text: string, cb: any) => {
|
2019-08-30 19:34:38 +00:00
|
|
|
this.userSearch(text, (users: any) => cb(users));
|
2019-08-30 03:11:29 +00:00
|
|
|
},
|
2019-08-31 00:40:59 +00:00
|
|
|
allowSpaces: false,
|
2019-08-30 03:11:29 +00:00
|
|
|
autocompleteMode: true,
|
2019-08-31 00:40:59 +00:00
|
|
|
menuItemLimit: 10,
|
2019-08-30 03:11:29 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
// Communities
|
|
|
|
{
|
|
|
|
trigger: '#',
|
|
|
|
selectTemplate: (item: any) => {
|
2019-08-30 04:41:41 +00:00
|
|
|
return `[/c/${item.original.key}](/c/${item.original.key})`;
|
2019-08-30 03:11:29 +00:00
|
|
|
},
|
|
|
|
values: (text: string, cb: any) => {
|
2019-08-30 19:34:38 +00:00
|
|
|
this.communitySearch(text, (communities: any) => cb(communities));
|
2019-08-30 03:11:29 +00:00
|
|
|
},
|
2019-08-31 00:40:59 +00:00
|
|
|
allowSpaces: false,
|
2019-08-30 03:11:29 +00:00
|
|
|
autocompleteMode: true,
|
2019-08-31 00:40:59 +00:00
|
|
|
menuItemLimit: 10,
|
2019-08-30 03:11:29 +00:00
|
|
|
}
|
|
|
|
]
|
|
|
|
});
|
2019-08-10 00:14:43 +00:00
|
|
|
|
2019-04-08 05:19:02 +00:00
|
|
|
this.state = this.emptyState;
|
|
|
|
|
|
|
|
if (this.props.node) {
|
|
|
|
if (this.props.edit) {
|
|
|
|
this.state.commentForm.edit_id = this.props.node.comment.id;
|
|
|
|
this.state.commentForm.parent_id = this.props.node.comment.parent_id;
|
|
|
|
this.state.commentForm.content = this.props.node.comment.content;
|
2019-04-15 23:12:06 +00:00
|
|
|
this.state.commentForm.creator_id = this.props.node.comment.creator_id;
|
2019-04-08 05:19:02 +00:00
|
|
|
} else {
|
|
|
|
// A reply gets a new parent id
|
|
|
|
this.state.commentForm.parent_id = this.props.node.comment.id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
2019-08-30 03:11:29 +00:00
|
|
|
var textarea: any = document.getElementById(this.id);
|
|
|
|
autosize(textarea);
|
|
|
|
this.tribute.attach(textarea);
|
|
|
|
textarea.addEventListener('tribute-replaced', () => {
|
|
|
|
this.state.commentForm.content = textarea.value;
|
|
|
|
this.setState(this.state);
|
|
|
|
autosize.update(textarea);
|
|
|
|
});
|
2019-04-08 05:19:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
2019-08-18 01:59:59 +00:00
|
|
|
<div class="mb-3">
|
2019-04-08 05:19:02 +00:00
|
|
|
<form onSubmit={linkEvent(this, this.handleCommentSubmit)}>
|
|
|
|
<div class="form-group row">
|
|
|
|
<div class="col-sm-12">
|
2019-09-01 04:10:48 +00:00
|
|
|
<textarea id={this.id} className={`form-control ${this.state.previewMode && 'd-none'}`} value={this.state.commentForm.content} onInput={linkEvent(this, this.handleCommentContentChange)} required disabled={this.props.disabled} rows={2} maxLength={10000} />
|
|
|
|
{this.state.previewMode &&
|
|
|
|
<div className="md-div" dangerouslySetInnerHTML={mdToHtml(this.state.commentForm.content)} />
|
|
|
|
}
|
2019-04-08 05:19:02 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div class="row">
|
|
|
|
<div class="col-sm-12">
|
2019-04-15 23:12:06 +00:00
|
|
|
<button type="submit" class="btn btn-sm btn-secondary mr-2" disabled={this.props.disabled}>{this.state.buttonTitle}</button>
|
2019-09-01 04:10:48 +00:00
|
|
|
{this.state.commentForm.content &&
|
|
|
|
<button className={`btn btn-sm mr-2 btn-secondary ${this.state.previewMode && 'active'}`} onClick={linkEvent(this, this.handlePreviewToggle)}><T i18nKey="preview">#</T></button>
|
|
|
|
}
|
2019-09-08 16:54:53 +00:00
|
|
|
{this.props.node && <button type="button" class="btn btn-sm btn-secondary mr-2" onClick={linkEvent(this, this.handleReplyCancel)}><T i18nKey="cancel">#</T></button>}
|
2019-09-01 04:10:48 +00:00
|
|
|
<a href={markdownHelpUrl} target="_blank" class="d-inline-block float-right text-muted small font-weight-bold"><T i18nKey="formatting_help">#</T></a>
|
2019-09-08 03:42:01 +00:00
|
|
|
<form class="d-inline-block mr-2 float-right text-muted small font-weight-bold">
|
|
|
|
<label htmlFor={`file-upload-${this.id}`} class="pointer"><T i18nKey="upload_image">#</T></label>
|
2019-09-08 16:54:53 +00:00
|
|
|
<input id={`file-upload-${this.id}`} type="file" accept="image/*,video/*" name="file" class="d-none" onChange={linkEvent(this, this.handleImageUpload)} />
|
2019-09-08 03:42:01 +00:00
|
|
|
</form>
|
2019-09-08 16:54:53 +00:00
|
|
|
{this.state.imageLoading &&
|
|
|
|
<svg class="icon icon-spinner spin"><use xlinkHref="#icon-spinner"></use></svg>
|
|
|
|
}
|
2019-04-08 05:19:02 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</form>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleCommentSubmit(i: CommentForm, event: any) {
|
2019-04-16 23:04:23 +00:00
|
|
|
event.preventDefault();
|
2019-04-08 05:19:02 +00:00
|
|
|
if (i.props.edit) {
|
|
|
|
WebSocketService.Instance.editComment(i.state.commentForm);
|
|
|
|
} else {
|
|
|
|
WebSocketService.Instance.createComment(i.state.commentForm);
|
|
|
|
}
|
|
|
|
|
2019-09-01 04:10:48 +00:00
|
|
|
i.state.previewMode = false;
|
2019-04-08 05:19:02 +00:00
|
|
|
i.state.commentForm.content = undefined;
|
|
|
|
event.target.reset();
|
2019-09-08 03:42:01 +00:00
|
|
|
i.setState(i.state);
|
2019-04-08 05:19:02 +00:00
|
|
|
if (i.props.node) {
|
|
|
|
i.props.onReplyCancel();
|
|
|
|
}
|
2019-08-10 00:14:43 +00:00
|
|
|
|
2019-07-16 05:56:46 +00:00
|
|
|
autosize.update(document.querySelector('textarea'));
|
2019-04-08 05:19:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
handleCommentContentChange(i: CommentForm, event: any) {
|
|
|
|
i.state.commentForm.content = event.target.value;
|
|
|
|
i.setState(i.state);
|
|
|
|
}
|
|
|
|
|
2019-09-01 04:10:48 +00:00
|
|
|
handlePreviewToggle(i: CommentForm, event: any) {
|
|
|
|
event.preventDefault();
|
|
|
|
i.state.previewMode = !i.state.previewMode;
|
|
|
|
i.setState(i.state);
|
|
|
|
}
|
|
|
|
|
2019-04-08 05:19:02 +00:00
|
|
|
handleReplyCancel(i: CommentForm) {
|
|
|
|
i.props.onReplyCancel();
|
|
|
|
}
|
2019-09-08 03:42:01 +00:00
|
|
|
|
|
|
|
handleImageUpload(i: CommentForm, event: any) {
|
|
|
|
event.preventDefault();
|
|
|
|
let file = event.target.files[0];
|
|
|
|
const imageUploadUrl = `/pictshare/api/upload.php`;
|
|
|
|
const formData = new FormData();
|
|
|
|
formData.append('file', file);
|
2019-09-08 16:54:53 +00:00
|
|
|
|
|
|
|
i.state.imageLoading = true;
|
|
|
|
i.setState(i.state);
|
|
|
|
|
2019-09-08 03:42:01 +00:00
|
|
|
fetch(imageUploadUrl, {
|
|
|
|
method: 'POST',
|
|
|
|
body: formData,
|
|
|
|
})
|
|
|
|
.then(res => res.json())
|
|
|
|
.then(res => {
|
|
|
|
let url = `${window.location.origin}/pictshare/${res.url}`;
|
|
|
|
let markdown = (res.filetype == 'mp4') ? `[vid](${url}/raw)` : `![](${url})`;
|
|
|
|
let content = i.state.commentForm.content;
|
|
|
|
content = (content) ? `${content} ${markdown}` : markdown;
|
|
|
|
i.state.commentForm.content = content;
|
2019-09-08 16:54:53 +00:00
|
|
|
i.state.imageLoading = false;
|
|
|
|
i.setState(i.state);
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
i.state.imageLoading = false;
|
2019-09-08 03:42:01 +00:00
|
|
|
i.setState(i.state);
|
2019-09-08 16:54:53 +00:00
|
|
|
alert(error);
|
2019-09-08 03:42:01 +00:00
|
|
|
})
|
|
|
|
}
|
2019-08-30 03:11:29 +00:00
|
|
|
|
|
|
|
userSearch(text: string, cb: any) {
|
|
|
|
if (text) {
|
|
|
|
let form: SearchForm = {
|
|
|
|
q: text,
|
|
|
|
type_: SearchType[SearchType.Users],
|
|
|
|
sort: SortType[SortType.TopAll],
|
|
|
|
page: 1,
|
2019-09-01 04:10:48 +00:00
|
|
|
limit: mentionDropdownFetchLimit,
|
2019-08-30 03:11:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
WebSocketService.Instance.search(form);
|
|
|
|
|
|
|
|
this.userSub = WebSocketService.Instance.subject
|
|
|
|
.subscribe(
|
|
|
|
(msg) => {
|
|
|
|
let op: UserOperation = msgOp(msg);
|
|
|
|
if (op == UserOperation.Search) {
|
|
|
|
let res: SearchResponse = msg;
|
|
|
|
let users = res.users.map(u => {return {key: u.name}});
|
|
|
|
cb(users);
|
|
|
|
this.userSub.unsubscribe();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
(err) => console.error(err),
|
|
|
|
() => console.log('complete')
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
cb([]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
communitySearch(text: string, cb: any) {
|
|
|
|
if (text) {
|
|
|
|
let form: SearchForm = {
|
|
|
|
q: text,
|
|
|
|
type_: SearchType[SearchType.Communities],
|
|
|
|
sort: SortType[SortType.TopAll],
|
|
|
|
page: 1,
|
2019-09-01 04:10:48 +00:00
|
|
|
limit: mentionDropdownFetchLimit,
|
2019-08-30 03:11:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
WebSocketService.Instance.search(form);
|
|
|
|
|
|
|
|
this.communitySub = WebSocketService.Instance.subject
|
|
|
|
.subscribe(
|
|
|
|
(msg) => {
|
|
|
|
let op: UserOperation = msgOp(msg);
|
|
|
|
if (op == UserOperation.Search) {
|
|
|
|
let res: SearchResponse = msg;
|
|
|
|
let communities = res.communities.map(u => {return {key: u.name}});
|
|
|
|
cb(communities);
|
|
|
|
this.communitySub.unsubscribe();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
(err) => console.error(err),
|
|
|
|
() => console.log('complete')
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
cb([]);
|
|
|
|
}
|
|
|
|
}
|
2019-04-08 05:19:02 +00:00
|
|
|
}
|