2019-04-08 05:19:02 +00:00
|
|
|
import { Component, linkEvent } from 'inferno';
|
2020-03-08 22:47:27 +00:00
|
|
|
import { Subscription } from 'rxjs';
|
|
|
|
import { retryWhen, delay, take } from 'rxjs/operators';
|
2020-03-06 19:57:52 +00:00
|
|
|
import { Prompt } from 'inferno-router';
|
2019-10-19 00:20:27 +00:00
|
|
|
import {
|
|
|
|
CommentNode as CommentNodeI,
|
|
|
|
CommentForm as CommentFormI,
|
2020-03-08 22:47:27 +00:00
|
|
|
WebSocketJsonResponse,
|
|
|
|
UserOperation,
|
|
|
|
CommentResponse,
|
2019-10-19 00:20:27 +00:00
|
|
|
} from '../interfaces';
|
|
|
|
import {
|
|
|
|
capitalizeFirstLetter,
|
|
|
|
mdToHtml,
|
|
|
|
randomStr,
|
|
|
|
markdownHelpUrl,
|
2020-01-23 03:29:11 +00:00
|
|
|
toast,
|
2020-01-24 18:59:50 +00:00
|
|
|
setupTribute,
|
2020-03-08 22:47:27 +00:00
|
|
|
wsJsonToRes,
|
2020-04-12 16:47:48 +00:00
|
|
|
emojiPicker,
|
2020-06-11 02:47:06 +00:00
|
|
|
pictrsDeleteToast,
|
2019-10-19 00:20:27 +00:00
|
|
|
} from '../utils';
|
2019-04-15 23:12:06 +00:00
|
|
|
import { WebSocketService, UserService } from '../services';
|
2020-01-06 16:22:51 +00:00
|
|
|
import autosize from 'autosize';
|
2020-01-24 18:59:50 +00:00
|
|
|
import Tribute from 'tributejs/src/Tribute.js';
|
2020-04-12 16:47:48 +00:00
|
|
|
import emojiShortName from 'emoji-short-name';
|
2019-08-10 00:14:43 +00:00
|
|
|
import { i18n } from '../i18next';
|
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;
|
2020-03-08 22:47:27 +00:00
|
|
|
loading: 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> {
|
2020-03-08 22:47:27 +00:00
|
|
|
private id = `comment-textarea-${randomStr()}`;
|
|
|
|
private formId = `comment-form-${randomStr()}`;
|
2020-01-24 18:59:50 +00:00
|
|
|
private tribute: Tribute;
|
2020-03-08 22:47:27 +00:00
|
|
|
private subscription: Subscription;
|
2019-04-08 05:19:02 +00:00
|
|
|
private emptyState: CommentFormState = {
|
|
|
|
commentForm: {
|
|
|
|
auth: null,
|
|
|
|
content: null,
|
2019-10-19 00:20:27 +00:00
|
|
|
post_id: this.props.node
|
|
|
|
? this.props.node.comment.post_id
|
|
|
|
: this.props.postId,
|
|
|
|
creator_id: UserService.Instance.user
|
|
|
|
? UserService.Instance.user.id
|
|
|
|
: null,
|
2019-04-08 05:19:02 +00:00
|
|
|
},
|
2019-10-19 00:20:27 +00:00
|
|
|
buttonTitle: !this.props.node
|
|
|
|
? capitalizeFirstLetter(i18n.t('post'))
|
|
|
|
: this.props.edit
|
2020-06-22 22:33:51 +00:00
|
|
|
? capitalizeFirstLetter(i18n.t('save'))
|
2019-10-19 00:20:27 +00:00
|
|
|
: capitalizeFirstLetter(i18n.t('reply')),
|
2019-09-01 04:10:48 +00:00
|
|
|
previewMode: false,
|
2020-03-08 22:47:27 +00:00
|
|
|
loading: false,
|
2019-09-08 16:54:53 +00:00
|
|
|
imageLoading: false,
|
2019-10-19 00:20:27 +00:00
|
|
|
};
|
2019-04-08 05:19:02 +00:00
|
|
|
|
|
|
|
constructor(props: any, context: any) {
|
|
|
|
super(props, context);
|
|
|
|
|
2020-01-24 18:59:50 +00:00
|
|
|
this.tribute = setupTribute();
|
2020-04-12 16:47:48 +00:00
|
|
|
this.setupEmojiPicker();
|
|
|
|
|
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;
|
|
|
|
}
|
2019-10-19 00:20:27 +00:00
|
|
|
}
|
2020-03-08 22:47:27 +00:00
|
|
|
|
|
|
|
this.subscription = WebSocketService.Instance.subject
|
|
|
|
.pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
|
|
|
|
.subscribe(
|
|
|
|
msg => this.parseMessage(msg),
|
|
|
|
err => console.error(err),
|
|
|
|
() => console.log('complete')
|
|
|
|
);
|
2019-04-08 05:19:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
2020-07-09 13:47:26 +00:00
|
|
|
let textarea: any = document.getElementById(this.id);
|
2019-08-30 03:11:29 +00:00
|
|
|
autosize(textarea);
|
|
|
|
this.tribute.attach(textarea);
|
|
|
|
textarea.addEventListener('tribute-replaced', () => {
|
|
|
|
this.state.commentForm.content = textarea.value;
|
|
|
|
this.setState(this.state);
|
|
|
|
autosize.update(textarea);
|
|
|
|
});
|
2020-07-09 13:47:26 +00:00
|
|
|
|
|
|
|
// Quoting of selected text
|
|
|
|
let selectedText = window.getSelection().toString();
|
|
|
|
if (selectedText) {
|
|
|
|
let quotedText =
|
|
|
|
selectedText
|
|
|
|
.split('\n')
|
|
|
|
.map(t => `> ${t}`)
|
|
|
|
.join('\n') + '\n\n';
|
|
|
|
this.state.commentForm.content = quotedText;
|
|
|
|
this.setState(this.state);
|
|
|
|
// Not sure why this needs a delay
|
|
|
|
setTimeout(() => autosize.update(textarea), 10);
|
|
|
|
}
|
|
|
|
|
|
|
|
textarea.focus();
|
2019-04-08 05:19:02 +00:00
|
|
|
}
|
|
|
|
|
2020-07-08 18:32:37 +00:00
|
|
|
componentDidUpdate() {
|
|
|
|
if (this.state.commentForm.content) {
|
|
|
|
window.onbeforeunload = () => true;
|
|
|
|
} else {
|
|
|
|
window.onbeforeunload = undefined;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-08 22:47:27 +00:00
|
|
|
componentWillUnmount() {
|
|
|
|
this.subscription.unsubscribe();
|
2020-07-08 18:32:37 +00:00
|
|
|
window.onbeforeunload = null;
|
2020-03-08 22:47:27 +00:00
|
|
|
}
|
|
|
|
|
2019-04-08 05:19:02 +00:00
|
|
|
render() {
|
|
|
|
return (
|
2019-08-18 01:59:59 +00:00
|
|
|
<div class="mb-3">
|
2020-03-06 19:57:52 +00:00
|
|
|
<Prompt
|
|
|
|
when={this.state.commentForm.content}
|
|
|
|
message={i18n.t('block_leaving')}
|
|
|
|
/>
|
2020-03-08 22:47:27 +00:00
|
|
|
<form
|
|
|
|
id={this.formId}
|
|
|
|
onSubmit={linkEvent(this, this.handleCommentSubmit)}
|
|
|
|
>
|
2019-04-08 05:19:02 +00:00
|
|
|
<div class="form-group row">
|
2019-09-12 00:20:57 +00:00
|
|
|
<div className={`col-sm-12`}>
|
2019-10-19 00:20:27 +00:00
|
|
|
<textarea
|
|
|
|
id={this.id}
|
|
|
|
className={`form-control ${this.state.previewMode && 'd-none'}`}
|
|
|
|
value={this.state.commentForm.content}
|
|
|
|
onInput={linkEvent(this, this.handleCommentContentChange)}
|
2020-01-28 02:59:38 +00:00
|
|
|
onPaste={linkEvent(this, this.handleImageUploadPaste)}
|
2019-10-19 00:20:27 +00:00
|
|
|
required
|
|
|
|
disabled={this.props.disabled}
|
|
|
|
rows={2}
|
|
|
|
maxLength={10000}
|
|
|
|
/>
|
|
|
|
{this.state.previewMode && (
|
|
|
|
<div
|
2020-06-22 22:50:06 +00:00
|
|
|
className="card card-body md-div"
|
2019-10-19 00:20:27 +00:00
|
|
|
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-10-19 00:20:27 +00:00
|
|
|
<button
|
|
|
|
type="submit"
|
|
|
|
class="btn btn-sm btn-secondary mr-2"
|
2020-06-22 23:05:55 +00:00
|
|
|
disabled={this.props.disabled || this.state.loading}
|
2019-10-19 00:20:27 +00:00
|
|
|
>
|
2020-03-08 22:47:27 +00:00
|
|
|
{this.state.loading ? (
|
|
|
|
<svg class="icon icon-spinner spin">
|
|
|
|
<use xlinkHref="#icon-spinner"></use>
|
|
|
|
</svg>
|
|
|
|
) : (
|
|
|
|
<span>{this.state.buttonTitle}</span>
|
|
|
|
)}
|
2019-10-19 00:20:27 +00:00
|
|
|
</button>
|
|
|
|
{this.state.commentForm.content && (
|
|
|
|
<button
|
2020-06-11 02:47:06 +00:00
|
|
|
className={`btn btn-sm mr-2 btn-secondary ${
|
|
|
|
this.state.previewMode && 'active'
|
|
|
|
}`}
|
2019-10-19 00:20:27 +00:00
|
|
|
onClick={linkEvent(this, this.handlePreviewToggle)}
|
|
|
|
>
|
2020-02-02 19:22:35 +00:00
|
|
|
{i18n.t('preview')}
|
2019-10-19 00:20:27 +00:00
|
|
|
</button>
|
|
|
|
)}
|
|
|
|
{this.props.node && (
|
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
class="btn btn-sm btn-secondary mr-2"
|
|
|
|
onClick={linkEvent(this, this.handleReplyCancel)}
|
|
|
|
>
|
2020-02-02 19:22:35 +00:00
|
|
|
{i18n.t('cancel')}
|
2019-10-19 00:20:27 +00:00
|
|
|
</button>
|
|
|
|
)}
|
|
|
|
<a
|
|
|
|
href={markdownHelpUrl}
|
|
|
|
target="_blank"
|
2020-03-03 07:29:45 +00:00
|
|
|
class="d-inline-block float-right text-muted font-weight-bold"
|
|
|
|
title={i18n.t('formatting_help')}
|
2020-06-30 13:40:57 +00:00
|
|
|
rel="noopener"
|
2019-10-19 00:20:27 +00:00
|
|
|
>
|
2020-03-03 07:29:45 +00:00
|
|
|
<svg class="icon icon-inline">
|
|
|
|
<use xlinkHref="#icon-help-circle"></use>
|
|
|
|
</svg>
|
2019-10-19 00:20:27 +00:00
|
|
|
</a>
|
2020-03-03 07:29:45 +00:00
|
|
|
<form class="d-inline-block mr-3 float-right text-muted font-weight-bold">
|
2019-10-19 00:20:27 +00:00
|
|
|
<label
|
|
|
|
htmlFor={`file-upload-${this.id}`}
|
|
|
|
className={`${UserService.Instance.user && 'pointer'}`}
|
2020-03-03 07:29:45 +00:00
|
|
|
data-tippy-content={i18n.t('upload_image')}
|
2019-10-19 00:20:27 +00:00
|
|
|
>
|
2020-03-03 07:29:45 +00:00
|
|
|
<svg class="icon icon-inline">
|
|
|
|
<use xlinkHref="#icon-image"></use>
|
|
|
|
</svg>
|
2019-10-19 00:20:27 +00:00
|
|
|
</label>
|
|
|
|
<input
|
|
|
|
id={`file-upload-${this.id}`}
|
|
|
|
type="file"
|
|
|
|
accept="image/*,video/*"
|
|
|
|
name="file"
|
|
|
|
class="d-none"
|
|
|
|
disabled={!UserService.Instance.user}
|
|
|
|
onChange={linkEvent(this, this.handleImageUpload)}
|
|
|
|
/>
|
2019-09-08 03:42:01 +00:00
|
|
|
</form>
|
2019-10-19 00:20:27 +00:00
|
|
|
{this.state.imageLoading && (
|
|
|
|
<svg class="icon icon-spinner spin">
|
|
|
|
<use xlinkHref="#icon-spinner"></use>
|
|
|
|
</svg>
|
|
|
|
)}
|
2020-04-12 16:47:48 +00:00
|
|
|
<span
|
|
|
|
onClick={linkEvent(this, this.handleEmojiPickerClick)}
|
|
|
|
class="pointer unselectable d-inline-block mr-3 float-right text-muted font-weight-bold"
|
|
|
|
data-tippy-content={i18n.t('emoji_picker')}
|
|
|
|
>
|
|
|
|
<svg class="icon icon-inline">
|
|
|
|
<use xlinkHref="#icon-smile"></use>
|
|
|
|
</svg>
|
|
|
|
</span>
|
2019-04-08 05:19:02 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</form>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-04-12 16:47:48 +00:00
|
|
|
setupEmojiPicker() {
|
2020-04-12 18:55:52 +00:00
|
|
|
emojiPicker.on('emoji', twemojiHtmlStr => {
|
2020-04-12 16:47:48 +00:00
|
|
|
if (this.state.commentForm.content == null) {
|
|
|
|
this.state.commentForm.content = '';
|
|
|
|
}
|
2020-04-12 18:55:52 +00:00
|
|
|
var el = document.createElement('div');
|
|
|
|
el.innerHTML = twemojiHtmlStr;
|
|
|
|
let nativeUnicode = (el.childNodes[0] as HTMLElement).getAttribute('alt');
|
|
|
|
let shortName = `:${emojiShortName[nativeUnicode]}:`;
|
2020-04-12 16:47:48 +00:00
|
|
|
this.state.commentForm.content += shortName;
|
|
|
|
this.setState(this.state);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-06-27 13:03:34 +00:00
|
|
|
handleFinished(op: UserOperation, data: CommentResponse) {
|
2020-06-23 16:52:07 +00:00
|
|
|
let isReply =
|
|
|
|
this.props.node !== undefined && data.comment.parent_id !== null;
|
|
|
|
let xor =
|
|
|
|
+!(data.comment.parent_id !== null) ^ +(this.props.node !== undefined);
|
|
|
|
|
|
|
|
if (
|
|
|
|
(data.comment.creator_id == UserService.Instance.user.id &&
|
2020-06-27 13:03:34 +00:00
|
|
|
((op == UserOperation.CreateComment &&
|
|
|
|
// If its a reply, make sure parent child match
|
|
|
|
isReply &&
|
|
|
|
data.comment.parent_id == this.props.node.comment.id) ||
|
|
|
|
// Otherwise, check the XOR of the two
|
|
|
|
(!isReply && xor))) ||
|
|
|
|
// If its a comment edit, only check that its from your user, and that its a
|
|
|
|
// text edit only
|
|
|
|
|
2020-07-02 00:38:11 +00:00
|
|
|
(data.comment.creator_id == UserService.Instance.user.id &&
|
|
|
|
op == UserOperation.EditComment &&
|
|
|
|
data.comment.content)
|
2020-06-23 16:52:07 +00:00
|
|
|
) {
|
|
|
|
this.state.previewMode = false;
|
|
|
|
this.state.loading = false;
|
|
|
|
this.state.commentForm.content = '';
|
|
|
|
this.setState(this.state);
|
|
|
|
let form: any = document.getElementById(this.formId);
|
|
|
|
form.reset();
|
|
|
|
if (this.props.node) {
|
|
|
|
this.props.onReplyCancel();
|
|
|
|
}
|
|
|
|
autosize.update(form);
|
|
|
|
this.setState(this.state);
|
2020-03-08 22:47:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-08 05:19:02 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2020-03-08 22:47:27 +00:00
|
|
|
i.state.loading = true;
|
2019-09-08 03:42:01 +00:00
|
|
|
i.setState(i.state);
|
2019-04-08 05:19:02 +00:00
|
|
|
}
|
|
|
|
|
2020-04-12 16:47:48 +00:00
|
|
|
handleEmojiPickerClick(_i: CommentForm, event: any) {
|
|
|
|
emojiPicker.togglePicker(event.target);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2020-01-28 02:59:38 +00:00
|
|
|
handleImageUploadPaste(i: CommentForm, event: any) {
|
|
|
|
let image = event.clipboardData.files[0];
|
|
|
|
if (image) {
|
|
|
|
i.handleImageUpload(i, image);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-08 03:42:01 +00:00
|
|
|
handleImageUpload(i: CommentForm, event: any) {
|
2020-01-28 02:59:38 +00:00
|
|
|
let file: any;
|
|
|
|
if (event.target) {
|
|
|
|
event.preventDefault();
|
|
|
|
file = event.target.files[0];
|
|
|
|
} else {
|
|
|
|
file = event;
|
|
|
|
}
|
|
|
|
|
2020-06-08 17:52:32 +00:00
|
|
|
const imageUploadUrl = `/pictrs/image`;
|
2019-09-08 03:42:01 +00:00
|
|
|
const formData = new FormData();
|
2020-06-11 02:47:06 +00:00
|
|
|
formData.append('images[]', 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,
|
|
|
|
})
|
2019-10-19 00:20:27 +00:00
|
|
|
.then(res => res.json())
|
|
|
|
.then(res => {
|
2020-06-11 02:47:06 +00:00
|
|
|
console.log('pictrs upload:');
|
|
|
|
console.log(res);
|
|
|
|
if (res.msg == 'ok') {
|
|
|
|
let hash = res.files[0].file;
|
|
|
|
let url = `${window.location.origin}/pictrs/image/${hash}`;
|
|
|
|
let deleteToken = res.files[0].delete_token;
|
|
|
|
let deleteUrl = `${window.location.origin}/pictrs/image/delete/${deleteToken}/${hash}`;
|
|
|
|
let imageMarkdown = `![](${url})`;
|
|
|
|
let content = i.state.commentForm.content;
|
|
|
|
content = content ? `${content}\n${imageMarkdown}` : imageMarkdown;
|
|
|
|
i.state.commentForm.content = content;
|
|
|
|
i.state.imageLoading = false;
|
|
|
|
i.setState(i.state);
|
|
|
|
let textarea: any = document.getElementById(i.id);
|
|
|
|
autosize.update(textarea);
|
|
|
|
pictrsDeleteToast(
|
|
|
|
i18n.t('click_to_delete_picture'),
|
|
|
|
i18n.t('picture_deleted'),
|
|
|
|
deleteUrl
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
i.state.imageLoading = false;
|
|
|
|
i.setState(i.state);
|
|
|
|
toast(JSON.stringify(res), 'danger');
|
|
|
|
}
|
2019-10-19 00:20:27 +00:00
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
i.state.imageLoading = false;
|
|
|
|
i.setState(i.state);
|
2020-01-23 03:29:11 +00:00
|
|
|
toast(error, 'danger');
|
2019-10-19 00:20:27 +00:00
|
|
|
});
|
2019-09-08 03:42:01 +00:00
|
|
|
}
|
2020-03-08 22:47:27 +00:00
|
|
|
|
|
|
|
parseMessage(msg: WebSocketJsonResponse) {
|
|
|
|
let res = wsJsonToRes(msg);
|
|
|
|
|
|
|
|
// Only do the showing and hiding if logged in
|
|
|
|
if (UserService.Instance.user) {
|
|
|
|
if (res.op == UserOperation.CreateComment) {
|
|
|
|
let data = res.data as CommentResponse;
|
2020-06-27 13:03:34 +00:00
|
|
|
this.handleFinished(res.op, data);
|
2020-03-08 22:47:27 +00:00
|
|
|
} else if (res.op == UserOperation.EditComment) {
|
|
|
|
let data = res.data as CommentResponse;
|
2020-06-27 13:03:34 +00:00
|
|
|
this.handleFinished(res.op, data);
|
2020-03-08 22:47:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-04-08 05:19:02 +00:00
|
|
|
}
|