1 import { Component, linkEvent } from 'inferno';
2 import { Prompt } from 'inferno-router';
3 import { PostListings } from './post-listings';
4 import { Subscription } from 'rxjs';
5 import { retryWhen, delay, take } from 'rxjs/operators';
13 ListCommunitiesResponse,
20 WebSocketJsonResponse,
21 } from '../interfaces';
22 import { WebSocketService, UserService } from '../services';
27 capitalizeFirstLetter,
40 import autosize from 'autosize';
41 import Tribute from 'tributejs/src/Tribute.js';
42 import emojiShortName from 'emoji-short-name';
43 import Selectr from 'mobius1-selectr';
44 import { i18n } from '../i18next';
46 const MAX_POST_TITLE_LENGTH = 200;
48 interface PostFormProps {
49 post?: Post; // If a post is given, that means this is an edit
50 params?: PostFormParams;
52 onCreate?(id: number): any;
53 onEdit?(post: Post): any;
56 interface PostFormState {
58 communities: Array<Community>;
60 imageLoading: boolean;
62 suggestedTitle: string;
63 suggestedPosts: Array<Post>;
64 crossPosts: Array<Post>;
68 export class PostForm extends Component<PostFormProps, PostFormState> {
69 private id = `post-form-${randomStr()}`;
70 private tribute: Tribute;
71 private subscription: Subscription;
72 private emptyState: PostFormState = {
78 creator_id: UserService.Instance.user
79 ? UserService.Instance.user.id
86 suggestedTitle: undefined,
89 enable_nsfw: undefined,
92 constructor(props: any, context: any) {
93 super(props, context);
94 this.fetchSimilarPosts = debounce(this.fetchSimilarPosts).bind(this);
95 this.fetchPageTitle = debounce(this.fetchPageTitle).bind(this);
97 this.tribute = setupTribute();
98 this.setupEmojiPicker();
100 this.state = this.emptyState;
102 if (this.props.post) {
103 this.state.postForm = {
104 body: this.props.post.body,
105 // NOTE: debouncing breaks both these for some reason, unless you use defaultValue
106 name: this.props.post.name,
107 community_id: this.props.post.community_id,
108 edit_id: this.props.post.id,
109 creator_id: this.props.post.creator_id,
110 url: this.props.post.url,
111 nsfw: this.props.post.nsfw,
116 if (this.props.params) {
117 this.state.postForm.name = this.props.params.name;
118 if (this.props.params.url) {
119 this.state.postForm.url = this.props.params.url;
121 if (this.props.params.body) {
122 this.state.postForm.body = this.props.params.body;
126 this.subscription = WebSocketService.Instance.subject
127 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
129 msg => this.parseMessage(msg),
130 err => console.error(err),
131 () => console.log('complete')
134 let listCommunitiesForm: ListCommunitiesForm = {
135 sort: SortType[SortType.TopAll],
139 WebSocketService.Instance.listCommunities(listCommunitiesForm);
140 WebSocketService.Instance.getSite();
143 componentDidMount() {
144 var textarea: any = document.getElementById(this.id);
146 this.tribute.attach(textarea);
147 textarea.addEventListener('tribute-replaced', () => {
148 this.state.postForm.body = textarea.value;
149 this.setState(this.state);
150 autosize.update(textarea);
155 componentWillUnmount() {
156 this.subscription.unsubscribe();
164 !this.state.loading &&
165 (this.state.postForm.name ||
166 this.state.postForm.url ||
167 this.state.postForm.body)
169 message={i18n.t('block_leaving')}
171 <form onSubmit={linkEvent(this, this.handlePostSubmit)}>
172 <div class="form-group row">
173 <label class="col-sm-2 col-form-label" htmlFor="post-url">
176 <div class="col-sm-10">
181 value={this.state.postForm.url}
182 onInput={linkEvent(this, this.handlePostUrlChange)}
183 onPaste={linkEvent(this, this.handleImageUploadPaste)}
185 {this.state.suggestedTitle && (
187 class="mt-1 text-muted small font-weight-bold pointer"
188 onClick={linkEvent(this, this.copySuggestedTitle)}
190 {i18n.t('copy_suggested_title', {
191 title: this.state.suggestedTitle,
197 htmlFor="file-upload"
199 UserService.Instance.user && 'pointer'
200 } d-inline-block float-right text-muted font-weight-bold`}
201 data-tippy-content={i18n.t('upload_image')}
203 <svg class="icon icon-inline">
204 <use xlinkHref="#icon-image"></use>
210 accept="image/*,video/*"
213 disabled={!UserService.Instance.user}
214 onChange={linkEvent(this, this.handleImageUpload)}
217 {validURL(this.state.postForm.url) && (
219 href={`${archiveUrl}/?run=1&url=${encodeURIComponent(
220 this.state.postForm.url
223 class="mr-2 d-inline-block float-right text-muted small font-weight-bold"
225 {i18n.t('archive_link')}
228 {this.state.imageLoading && (
229 <svg class="icon icon-spinner spin">
230 <use xlinkHref="#icon-spinner"></use>
233 {isImage(this.state.postForm.url) && (
234 <img src={this.state.postForm.url} class="img-fluid" />
236 {this.state.crossPosts.length > 0 && (
238 <div class="my-1 text-muted small font-weight-bold">
239 {i18n.t('cross_posts')}
241 <PostListings showCommunity posts={this.state.crossPosts} />
246 <div class="form-group row">
247 <label class="col-sm-2 col-form-label" htmlFor="post-title">
250 <div class="col-sm-10">
252 value={this.state.postForm.name}
254 onInput={linkEvent(this, this.handlePostNameChange)}
259 maxLength={MAX_POST_TITLE_LENGTH}
261 {this.state.suggestedPosts.length > 0 && (
263 <div class="my-1 text-muted small font-weight-bold">
264 {i18n.t('related_posts')}
266 <PostListings posts={this.state.suggestedPosts} />
272 <div class="form-group row">
273 <label class="col-sm-2 col-form-label" htmlFor={this.id}>
276 <div class="col-sm-10">
279 value={this.state.postForm.body}
280 onInput={linkEvent(this, this.handlePostBodyChange)}
281 className={`form-control ${this.state.previewMode && 'd-none'}`}
285 {this.state.previewMode && (
287 className="card card-body md-div"
288 dangerouslySetInnerHTML={mdToHtml(this.state.postForm.body)}
291 {this.state.postForm.body && (
293 className={`mt-1 mr-2 btn btn-sm btn-secondary ${
294 this.state.previewMode && 'active'
296 onClick={linkEvent(this, this.handlePreviewToggle)}
302 href={markdownHelpUrl}
304 class="d-inline-block float-right text-muted font-weight-bold"
305 title={i18n.t('formatting_help')}
307 <svg class="icon icon-inline">
308 <use xlinkHref="#icon-help-circle"></use>
312 onClick={linkEvent(this, this.handleEmojiPickerClick)}
313 class="pointer unselectable d-inline-block mr-3 float-right text-muted font-weight-bold"
314 data-tippy-content={i18n.t('emoji_picker')}
316 <svg class="icon icon-inline">
317 <use xlinkHref="#icon-smile"></use>
322 {!this.props.post && (
323 <div class="form-group row">
324 <label class="col-sm-2 col-form-label" htmlFor="post-community">
325 {i18n.t('community')}
327 <div class="col-sm-10">
331 value={this.state.postForm.community_id}
332 onInput={linkEvent(this, this.handlePostCommunityChange)}
334 <option>{i18n.t('select_a_community')}</option>
335 {this.state.communities.map(community => (
336 <option value={community.id}>{community.name}</option>
342 {this.state.enable_nsfw && (
343 <div class="form-group row">
344 <div class="col-sm-10">
345 <div class="form-check">
347 class="form-check-input"
350 checked={this.state.postForm.nsfw}
351 onChange={linkEvent(this, this.handlePostNsfwChange)}
353 <label class="form-check-label" htmlFor="post-nsfw">
360 <div class="form-group row">
361 <div class="col-sm-10">
364 !this.state.postForm.community_id || this.state.loading
367 class="btn btn-secondary mr-2"
369 {this.state.loading ? (
370 <svg class="icon icon-spinner spin">
371 <use xlinkHref="#icon-spinner"></use>
373 ) : this.props.post ? (
374 capitalizeFirstLetter(i18n.t('save'))
376 capitalizeFirstLetter(i18n.t('create'))
379 {this.props.post && (
382 class="btn btn-secondary"
383 onClick={linkEvent(this, this.handleCancel)}
396 emojiPicker.on('emoji', twemojiHtmlStr => {
397 if (this.state.postForm.body == null) {
398 this.state.postForm.body = '';
400 var el = document.createElement('div');
401 el.innerHTML = twemojiHtmlStr;
402 let nativeUnicode = (el.childNodes[0] as HTMLElement).getAttribute('alt');
403 let shortName = `:${emojiShortName[nativeUnicode]}:`;
404 this.state.postForm.body += shortName;
405 this.setState(this.state);
409 handlePostSubmit(i: PostForm, event: any) {
410 event.preventDefault();
412 WebSocketService.Instance.editPost(i.state.postForm);
414 WebSocketService.Instance.createPost(i.state.postForm);
416 i.state.loading = true;
420 copySuggestedTitle(i: PostForm) {
421 i.state.postForm.name = i.state.suggestedTitle.substring(
423 MAX_POST_TITLE_LENGTH
425 i.state.suggestedTitle = undefined;
429 handlePostUrlChange(i: PostForm, event: any) {
430 i.state.postForm.url = event.target.value;
436 if (validURL(this.state.postForm.url)) {
437 let form: SearchForm = {
438 q: this.state.postForm.url,
439 type_: SearchType[SearchType.Url],
440 sort: SortType[SortType.TopAll],
445 WebSocketService.Instance.search(form);
447 // Fetch the page title
448 getPageTitle(this.state.postForm.url).then(d => {
449 this.state.suggestedTitle = d;
450 this.setState(this.state);
453 this.state.suggestedTitle = undefined;
454 this.state.crossPosts = [];
458 handlePostNameChange(i: PostForm, event: any) {
459 i.state.postForm.name = event.target.value;
461 i.fetchSimilarPosts();
464 fetchSimilarPosts() {
465 let form: SearchForm = {
466 q: this.state.postForm.name,
467 type_: SearchType[SearchType.Posts],
468 sort: SortType[SortType.TopAll],
469 community_id: this.state.postForm.community_id,
474 if (this.state.postForm.name !== '') {
475 WebSocketService.Instance.search(form);
477 this.state.suggestedPosts = [];
480 this.setState(this.state);
483 handlePostBodyChange(i: PostForm, event: any) {
484 i.state.postForm.body = event.target.value;
488 handlePostCommunityChange(i: PostForm, event: any) {
489 i.state.postForm.community_id = Number(event.target.value);
493 handlePostNsfwChange(i: PostForm, event: any) {
494 i.state.postForm.nsfw = event.target.checked;
498 handleCancel(i: PostForm) {
502 handlePreviewToggle(i: PostForm, event: any) {
503 event.preventDefault();
504 i.state.previewMode = !i.state.previewMode;
508 handleImageUploadPaste(i: PostForm, event: any) {
509 let image = event.clipboardData.files[0];
511 i.handleImageUpload(i, image);
515 handleImageUpload(i: PostForm, event: any) {
518 event.preventDefault();
519 file = event.target.files[0];
524 const imageUploadUrl = `/pictrs/image`;
525 const formData = new FormData();
526 formData.append('images[]', file);
528 i.state.imageLoading = true;
531 fetch(imageUploadUrl, {
535 .then(res => res.json())
537 console.log('pictrs upload:');
539 if (res.msg == 'ok') {
540 let hash = res.files[0].file;
541 let url = `${window.location.origin}/pictrs/image/${hash}`;
542 let deleteToken = res.files[0].delete_token;
543 let deleteUrl = `${window.location.origin}/pictrs/image/delete/${deleteToken}/${hash}`;
544 i.state.postForm.url = url;
545 i.state.imageLoading = false;
548 i18n.t('click_to_delete_picture'),
549 i18n.t('picture_deleted'),
553 i.state.imageLoading = false;
555 toast(JSON.stringify(res), 'danger');
559 i.state.imageLoading = false;
561 toast(error, 'danger');
565 handleEmojiPickerClick(_i: PostForm, event: any) {
566 emojiPicker.togglePicker(event.target);
569 parseMessage(msg: WebSocketJsonResponse) {
570 let res = wsJsonToRes(msg);
572 toast(i18n.t(msg.error), 'danger');
573 this.state.loading = false;
574 this.setState(this.state);
576 } else if (res.op == UserOperation.ListCommunities) {
577 let data = res.data as ListCommunitiesResponse;
578 this.state.communities = data.communities;
579 if (this.props.post) {
580 this.state.postForm.community_id = this.props.post.community_id;
581 } else if (this.props.params && this.props.params.community) {
582 let foundCommunityId = data.communities.find(
583 r => r.name == this.props.params.community
585 this.state.postForm.community_id = foundCommunityId;
587 // By default, the null valued 'Select a Community'
589 this.setState(this.state);
591 // Set up select searching
592 let selectId: any = document.getElementById('post-community');
594 let selector = new Selectr(selectId, { nativeDropdown: false });
595 selector.on('selectr.select', option => {
596 this.state.postForm.community_id = Number(option.value);
597 this.setState(this.state);
600 } else if (res.op == UserOperation.CreatePost) {
601 let data = res.data as PostResponse;
602 if (data.post.creator_id == UserService.Instance.user.id) {
603 this.state.loading = false;
604 this.props.onCreate(data.post.id);
606 } else if (res.op == UserOperation.EditPost) {
607 let data = res.data as PostResponse;
608 if (data.post.creator_id == UserService.Instance.user.id) {
609 this.state.loading = false;
610 this.props.onEdit(data.post);
612 } else if (res.op == UserOperation.Search) {
613 let data = res.data as SearchResponse;
615 if (data.type_ == SearchType[SearchType.Posts]) {
616 this.state.suggestedPosts = data.posts;
617 } else if (data.type_ == SearchType[SearchType.Url]) {
618 this.state.crossPosts = data.posts;
620 this.setState(this.state);
621 } else if (res.op == UserOperation.GetSite) {
622 let data = res.data as GetSiteResponse;
623 this.state.enable_nsfw = data.site.enable_nsfw;
624 this.setState(this.state);