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,
41 import autosize from 'autosize';
42 import Tribute from 'tributejs/src/Tribute.js';
43 import emojiShortName from 'emoji-short-name';
44 import Selectr from 'mobius1-selectr';
45 import { i18n } from '../i18next';
47 const MAX_POST_TITLE_LENGTH = 200;
49 interface PostFormProps {
50 post?: Post; // If a post is given, that means this is an edit
51 params?: PostFormParams;
53 onCreate?(id: number): any;
54 onEdit?(post: Post): any;
57 interface PostFormState {
59 communities: Array<Community>;
61 imageLoading: boolean;
63 suggestedTitle: string;
64 suggestedPosts: Array<Post>;
65 crossPosts: Array<Post>;
69 export class PostForm extends Component<PostFormProps, PostFormState> {
70 private id = `post-form-${randomStr()}`;
71 private tribute: Tribute;
72 private subscription: Subscription;
73 private emptyState: PostFormState = {
79 creator_id: UserService.Instance.user
80 ? UserService.Instance.user.id
87 suggestedTitle: undefined,
90 enable_nsfw: undefined,
93 constructor(props: any, context: any) {
94 super(props, context);
95 this.fetchSimilarPosts = debounce(this.fetchSimilarPosts).bind(this);
96 this.fetchPageTitle = debounce(this.fetchPageTitle).bind(this);
98 this.tribute = setupTribute();
99 this.setupEmojiPicker();
101 this.state = this.emptyState;
103 if (this.props.post) {
104 this.state.postForm = {
105 body: this.props.post.body,
106 // NOTE: debouncing breaks both these for some reason, unless you use defaultValue
107 name: this.props.post.name,
108 community_id: this.props.post.community_id,
109 edit_id: this.props.post.id,
110 creator_id: this.props.post.creator_id,
111 url: this.props.post.url,
112 nsfw: this.props.post.nsfw,
117 if (this.props.params) {
118 this.state.postForm.name = this.props.params.name;
119 if (this.props.params.url) {
120 this.state.postForm.url = this.props.params.url;
122 if (this.props.params.body) {
123 this.state.postForm.body = this.props.params.body;
127 this.subscription = WebSocketService.Instance.subject
128 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
130 msg => this.parseMessage(msg),
131 err => console.error(err),
132 () => console.log('complete')
135 let listCommunitiesForm: ListCommunitiesForm = {
136 sort: SortType[SortType.TopAll],
140 WebSocketService.Instance.listCommunities(listCommunitiesForm);
141 WebSocketService.Instance.getSite();
144 componentDidMount() {
145 var textarea: any = document.getElementById(this.id);
147 this.tribute.attach(textarea);
148 textarea.addEventListener('tribute-replaced', () => {
149 this.state.postForm.body = textarea.value;
150 this.setState(this.state);
151 autosize.update(textarea);
156 componentWillUnmount() {
157 this.subscription.unsubscribe();
165 !this.state.loading &&
166 (this.state.postForm.name ||
167 this.state.postForm.url ||
168 this.state.postForm.body)
170 message={i18n.t('block_leaving')}
172 <form onSubmit={linkEvent(this, this.handlePostSubmit)}>
173 <div class="form-group row">
174 <label class="col-sm-2 col-form-label" htmlFor="post-url">
177 <div class="col-sm-10">
182 value={this.state.postForm.url}
183 onInput={linkEvent(this, this.handlePostUrlChange)}
184 onPaste={linkEvent(this, this.handleImageUploadPaste)}
186 {this.state.suggestedTitle && (
188 class="mt-1 text-muted small font-weight-bold pointer"
189 onClick={linkEvent(this, this.copySuggestedTitle)}
191 {i18n.t('copy_suggested_title', {
192 title: this.state.suggestedTitle,
198 htmlFor="file-upload"
200 UserService.Instance.user && 'pointer'
201 } d-inline-block float-right text-muted font-weight-bold`}
202 data-tippy-content={i18n.t('upload_image')}
204 <svg class="icon icon-inline">
205 <use xlinkHref="#icon-image"></use>
211 accept="image/*,video/*"
214 disabled={!UserService.Instance.user}
215 onChange={linkEvent(this, this.handleImageUpload)}
218 {validURL(this.state.postForm.url) && (
220 href={`${archiveUrl}/?run=1&url=${encodeURIComponent(
221 this.state.postForm.url
224 class="mr-2 d-inline-block float-right text-muted small font-weight-bold"
226 {i18n.t('archive_link')}
229 {this.state.imageLoading && (
230 <svg class="icon icon-spinner spin">
231 <use xlinkHref="#icon-spinner"></use>
234 {isImage(this.state.postForm.url) && (
235 <img src={this.state.postForm.url} class="img-fluid" />
237 {this.state.crossPosts.length > 0 && (
239 <div class="my-1 text-muted small font-weight-bold">
240 {i18n.t('cross_posts')}
242 <PostListings showCommunity posts={this.state.crossPosts} />
247 <div class="form-group row">
248 <label class="col-sm-2 col-form-label" htmlFor="post-title">
251 <div class="col-sm-10">
253 value={this.state.postForm.name}
255 onInput={linkEvent(this, this.handlePostNameChange)}
260 maxLength={MAX_POST_TITLE_LENGTH}
262 {this.state.suggestedPosts.length > 0 && (
264 <div class="my-1 text-muted small font-weight-bold">
265 {i18n.t('related_posts')}
267 <PostListings posts={this.state.suggestedPosts} />
273 <div class="form-group row">
274 <label class="col-sm-2 col-form-label" htmlFor={this.id}>
277 <div class="col-sm-10">
280 value={this.state.postForm.body}
281 onInput={linkEvent(this, this.handlePostBodyChange)}
282 className={`form-control ${this.state.previewMode && 'd-none'}`}
286 {this.state.previewMode && (
288 className="card card-body md-div"
289 dangerouslySetInnerHTML={mdToHtml(this.state.postForm.body)}
292 {this.state.postForm.body && (
294 className={`mt-1 mr-2 btn btn-sm btn-secondary ${
295 this.state.previewMode && 'active'
297 onClick={linkEvent(this, this.handlePreviewToggle)}
303 href={markdownHelpUrl}
305 class="d-inline-block float-right text-muted font-weight-bold"
306 title={i18n.t('formatting_help')}
308 <svg class="icon icon-inline">
309 <use xlinkHref="#icon-help-circle"></use>
313 onClick={linkEvent(this, this.handleEmojiPickerClick)}
314 class="pointer unselectable d-inline-block mr-3 float-right text-muted font-weight-bold"
315 data-tippy-content={i18n.t('emoji_picker')}
317 <svg class="icon icon-inline">
318 <use xlinkHref="#icon-smile"></use>
323 {!this.props.post && (
324 <div class="form-group row">
325 <label class="col-sm-2 col-form-label" htmlFor="post-community">
326 {i18n.t('community')}
328 <div class="col-sm-10">
332 value={this.state.postForm.community_id}
333 onInput={linkEvent(this, this.handlePostCommunityChange)}
335 <option>{i18n.t('select_a_community')}</option>
336 {this.state.communities.map(community => (
337 <option value={community.id}>
340 : `${hostname(community.actor_id)}/${community.name}`}
347 {this.state.enable_nsfw && (
348 <div class="form-group row">
349 <div class="col-sm-10">
350 <div class="form-check">
352 class="form-check-input"
355 checked={this.state.postForm.nsfw}
356 onChange={linkEvent(this, this.handlePostNsfwChange)}
358 <label class="form-check-label" htmlFor="post-nsfw">
365 <div class="form-group row">
366 <div class="col-sm-10">
369 !this.state.postForm.community_id || this.state.loading
372 class="btn btn-secondary mr-2"
374 {this.state.loading ? (
375 <svg class="icon icon-spinner spin">
376 <use xlinkHref="#icon-spinner"></use>
378 ) : this.props.post ? (
379 capitalizeFirstLetter(i18n.t('save'))
381 capitalizeFirstLetter(i18n.t('create'))
384 {this.props.post && (
387 class="btn btn-secondary"
388 onClick={linkEvent(this, this.handleCancel)}
401 emojiPicker.on('emoji', twemojiHtmlStr => {
402 if (this.state.postForm.body == null) {
403 this.state.postForm.body = '';
405 var el = document.createElement('div');
406 el.innerHTML = twemojiHtmlStr;
407 let nativeUnicode = (el.childNodes[0] as HTMLElement).getAttribute('alt');
408 let shortName = `:${emojiShortName[nativeUnicode]}:`;
409 this.state.postForm.body += shortName;
410 this.setState(this.state);
414 handlePostSubmit(i: PostForm, event: any) {
415 event.preventDefault();
417 // Coerce empty url string to undefined
418 if (i.state.postForm.url && i.state.postForm.url === '') {
419 i.state.postForm.url = undefined;
423 WebSocketService.Instance.editPost(i.state.postForm);
425 WebSocketService.Instance.createPost(i.state.postForm);
427 i.state.loading = true;
431 copySuggestedTitle(i: PostForm) {
432 i.state.postForm.name = i.state.suggestedTitle.substring(
434 MAX_POST_TITLE_LENGTH
436 i.state.suggestedTitle = undefined;
440 handlePostUrlChange(i: PostForm, event: any) {
441 i.state.postForm.url = event.target.value;
447 if (validURL(this.state.postForm.url)) {
448 let form: SearchForm = {
449 q: this.state.postForm.url,
450 type_: SearchType[SearchType.Url],
451 sort: SortType[SortType.TopAll],
456 WebSocketService.Instance.search(form);
458 // Fetch the page title
459 getPageTitle(this.state.postForm.url).then(d => {
460 this.state.suggestedTitle = d;
461 this.setState(this.state);
464 this.state.suggestedTitle = undefined;
465 this.state.crossPosts = [];
469 handlePostNameChange(i: PostForm, event: any) {
470 i.state.postForm.name = event.target.value;
472 i.fetchSimilarPosts();
475 fetchSimilarPosts() {
476 let form: SearchForm = {
477 q: this.state.postForm.name,
478 type_: SearchType[SearchType.Posts],
479 sort: SortType[SortType.TopAll],
480 community_id: this.state.postForm.community_id,
485 if (this.state.postForm.name !== '') {
486 WebSocketService.Instance.search(form);
488 this.state.suggestedPosts = [];
491 this.setState(this.state);
494 handlePostBodyChange(i: PostForm, event: any) {
495 i.state.postForm.body = event.target.value;
499 handlePostCommunityChange(i: PostForm, event: any) {
500 i.state.postForm.community_id = Number(event.target.value);
504 handlePostNsfwChange(i: PostForm, event: any) {
505 i.state.postForm.nsfw = event.target.checked;
509 handleCancel(i: PostForm) {
513 handlePreviewToggle(i: PostForm, event: any) {
514 event.preventDefault();
515 i.state.previewMode = !i.state.previewMode;
519 handleImageUploadPaste(i: PostForm, event: any) {
520 let image = event.clipboardData.files[0];
522 i.handleImageUpload(i, image);
526 handleImageUpload(i: PostForm, event: any) {
529 event.preventDefault();
530 file = event.target.files[0];
535 const imageUploadUrl = `/pictrs/image`;
536 const formData = new FormData();
537 formData.append('images[]', file);
539 i.state.imageLoading = true;
542 fetch(imageUploadUrl, {
546 .then(res => res.json())
548 console.log('pictrs upload:');
550 if (res.msg == 'ok') {
551 let hash = res.files[0].file;
552 let url = `${window.location.origin}/pictrs/image/${hash}`;
553 let deleteToken = res.files[0].delete_token;
554 let deleteUrl = `${window.location.origin}/pictrs/image/delete/${deleteToken}/${hash}`;
555 i.state.postForm.url = url;
556 i.state.imageLoading = false;
559 i18n.t('click_to_delete_picture'),
560 i18n.t('picture_deleted'),
564 i.state.imageLoading = false;
566 toast(JSON.stringify(res), 'danger');
570 i.state.imageLoading = false;
572 toast(error, 'danger');
576 handleEmojiPickerClick(_i: PostForm, event: any) {
577 emojiPicker.togglePicker(event.target);
580 parseMessage(msg: WebSocketJsonResponse) {
581 let res = wsJsonToRes(msg);
583 toast(i18n.t(msg.error), 'danger');
584 this.state.loading = false;
585 this.setState(this.state);
587 } else if (res.op == UserOperation.ListCommunities) {
588 let data = res.data as ListCommunitiesResponse;
589 this.state.communities = data.communities;
590 if (this.props.post) {
591 this.state.postForm.community_id = this.props.post.community_id;
592 } else if (this.props.params && this.props.params.community) {
593 let foundCommunityId = data.communities.find(
594 r => r.name == this.props.params.community
596 this.state.postForm.community_id = foundCommunityId;
598 // By default, the null valued 'Select a Community'
600 this.setState(this.state);
602 // Set up select searching
603 let selectId: any = document.getElementById('post-community');
605 let selector = new Selectr(selectId, { nativeDropdown: false });
606 selector.on('selectr.select', option => {
607 this.state.postForm.community_id = Number(option.value);
608 this.setState(this.state);
611 } else if (res.op == UserOperation.CreatePost) {
612 let data = res.data as PostResponse;
613 if (data.post.creator_id == UserService.Instance.user.id) {
614 this.state.loading = false;
615 this.props.onCreate(data.post.id);
617 } else if (res.op == UserOperation.EditPost) {
618 let data = res.data as PostResponse;
619 if (data.post.creator_id == UserService.Instance.user.id) {
620 this.state.loading = false;
621 this.props.onEdit(data.post);
623 } else if (res.op == UserOperation.Search) {
624 let data = res.data as SearchResponse;
626 if (data.type_ == SearchType[SearchType.Posts]) {
627 this.state.suggestedPosts = data.posts;
628 } else if (data.type_ == SearchType[SearchType.Url]) {
629 this.state.crossPosts = data.posts;
631 this.setState(this.state);
632 } else if (res.op == UserOperation.GetSite) {
633 let data = res.data as GetSiteResponse;
634 this.state.enable_nsfw = data.site.enable_nsfw;
635 this.setState(this.state);