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,
39 import autosize from 'autosize';
40 import Tribute from 'tributejs/src/Tribute.js';
41 import emojiShortName from 'emoji-short-name';
42 import Selectr from 'mobius1-selectr';
43 import { i18n } from '../i18next';
45 const MAX_POST_TITLE_LENGTH = 200;
47 interface PostFormProps {
48 post?: Post; // If a post is given, that means this is an edit
49 params?: PostFormParams;
51 onCreate?(id: number): any;
52 onEdit?(post: Post): any;
55 interface PostFormState {
57 communities: Array<Community>;
59 imageLoading: boolean;
61 suggestedTitle: string;
62 suggestedPosts: Array<Post>;
63 crossPosts: Array<Post>;
67 export class PostForm extends Component<PostFormProps, PostFormState> {
68 private id = `post-form-${randomStr()}`;
69 private tribute: Tribute;
70 private subscription: Subscription;
71 private emptyState: PostFormState = {
77 creator_id: UserService.Instance.user
78 ? UserService.Instance.user.id
85 suggestedTitle: undefined,
88 enable_nsfw: undefined,
91 constructor(props: any, context: any) {
92 super(props, context);
93 this.fetchSimilarPosts = debounce(this.fetchSimilarPosts).bind(this);
94 this.fetchPageTitle = debounce(this.fetchPageTitle).bind(this);
96 this.tribute = setupTribute();
97 this.setupEmojiPicker();
99 this.state = this.emptyState;
101 if (this.props.post) {
102 this.state.postForm = {
103 body: this.props.post.body,
104 // NOTE: debouncing breaks both these for some reason, unless you use defaultValue
105 name: this.props.post.name,
106 community_id: this.props.post.community_id,
107 edit_id: this.props.post.id,
108 creator_id: this.props.post.creator_id,
109 url: this.props.post.url,
110 nsfw: this.props.post.nsfw,
115 if (this.props.params) {
116 this.state.postForm.name = this.props.params.name;
117 if (this.props.params.url) {
118 this.state.postForm.url = this.props.params.url;
120 if (this.props.params.body) {
121 this.state.postForm.body = this.props.params.body;
125 this.subscription = WebSocketService.Instance.subject
126 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
128 msg => this.parseMessage(msg),
129 err => console.error(err),
130 () => console.log('complete')
133 let listCommunitiesForm: ListCommunitiesForm = {
134 sort: SortType[SortType.TopAll],
138 WebSocketService.Instance.listCommunities(listCommunitiesForm);
139 WebSocketService.Instance.getSite();
142 componentDidMount() {
143 var textarea: any = document.getElementById(this.id);
145 this.tribute.attach(textarea);
146 textarea.addEventListener('tribute-replaced', () => {
147 this.state.postForm.body = textarea.value;
148 this.setState(this.state);
149 autosize.update(textarea);
154 componentWillUnmount() {
155 this.subscription.unsubscribe();
163 !this.state.loading &&
164 (this.state.postForm.name ||
165 this.state.postForm.url ||
166 this.state.postForm.body)
168 message={i18n.t('block_leaving')}
170 <form onSubmit={linkEvent(this, this.handlePostSubmit)}>
171 <div class="form-group row">
172 <label class="col-sm-2 col-form-label" htmlFor="post-url">
175 <div class="col-sm-10">
180 value={this.state.postForm.url}
181 onInput={linkEvent(this, this.handlePostUrlChange)}
182 onPaste={linkEvent(this, this.handleImageUploadPaste)}
184 {this.state.suggestedTitle && (
186 class="mt-1 text-muted small font-weight-bold pointer"
187 onClick={linkEvent(this, this.copySuggestedTitle)}
189 {i18n.t('copy_suggested_title', {
190 title: this.state.suggestedTitle,
196 htmlFor="file-upload"
198 UserService.Instance.user && 'pointer'
199 } d-inline-block float-right text-muted font-weight-bold`}
200 data-tippy-content={i18n.t('upload_image')}
202 <svg class="icon icon-inline">
203 <use xlinkHref="#icon-image"></use>
209 accept="image/*,video/*"
212 disabled={!UserService.Instance.user}
213 onChange={linkEvent(this, this.handleImageUpload)}
216 {validURL(this.state.postForm.url) && (
218 href={`${archiveUrl}/?run=1&url=${encodeURIComponent(
219 this.state.postForm.url
222 class="mr-2 d-inline-block float-right text-muted small font-weight-bold"
224 {i18n.t('archive_link')}
227 {this.state.imageLoading && (
228 <svg class="icon icon-spinner spin">
229 <use xlinkHref="#icon-spinner"></use>
232 {isImage(this.state.postForm.url) && (
233 <img src={this.state.postForm.url} class="img-fluid" />
235 {this.state.crossPosts.length > 0 && (
237 <div class="my-1 text-muted small font-weight-bold">
238 {i18n.t('cross_posts')}
240 <PostListings showCommunity posts={this.state.crossPosts} />
245 <div class="form-group row">
246 <label class="col-sm-2 col-form-label" htmlFor="post-title">
249 <div class="col-sm-10">
251 value={this.state.postForm.name}
253 onInput={linkEvent(this, this.handlePostNameChange)}
258 maxLength={MAX_POST_TITLE_LENGTH}
260 {this.state.suggestedPosts.length > 0 && (
262 <div class="my-1 text-muted small font-weight-bold">
263 {i18n.t('related_posts')}
265 <PostListings posts={this.state.suggestedPosts} />
271 <div class="form-group row">
272 <label class="col-sm-2 col-form-label" htmlFor={this.id}>
275 <div class="col-sm-10">
278 value={this.state.postForm.body}
279 onInput={linkEvent(this, this.handlePostBodyChange)}
280 className={`form-control ${this.state.previewMode && 'd-none'}`}
284 {this.state.previewMode && (
287 dangerouslySetInnerHTML={mdToHtml(this.state.postForm.body)}
290 {this.state.postForm.body && (
292 className={`mt-1 mr-2 btn btn-sm btn-secondary ${
293 this.state.previewMode && 'active'
295 onClick={linkEvent(this, this.handlePreviewToggle)}
301 href={markdownHelpUrl}
303 class="d-inline-block float-right text-muted font-weight-bold"
304 title={i18n.t('formatting_help')}
306 <svg class="icon icon-inline">
307 <use xlinkHref="#icon-help-circle"></use>
311 onClick={linkEvent(this, this.handleEmojiPickerClick)}
312 class="pointer unselectable d-inline-block mr-3 float-right text-muted font-weight-bold"
313 data-tippy-content={i18n.t('emoji_picker')}
315 <svg class="icon icon-inline">
316 <use xlinkHref="#icon-smile"></use>
321 {!this.props.post && (
322 <div class="form-group row">
323 <label class="col-sm-2 col-form-label" htmlFor="post-community">
324 {i18n.t('community')}
326 <div class="col-sm-10">
330 value={this.state.postForm.community_id}
331 onInput={linkEvent(this, this.handlePostCommunityChange)}
333 {this.state.communities.map(community => (
334 <option value={community.id}>{community.name}</option>
340 {this.state.enable_nsfw && (
341 <div class="form-group row">
342 <div class="col-sm-10">
343 <div class="form-check">
345 class="form-check-input"
348 checked={this.state.postForm.nsfw}
349 onChange={linkEvent(this, this.handlePostNsfwChange)}
351 <label class="form-check-label" htmlFor="post-nsfw">
358 <div class="form-group row">
359 <div class="col-sm-10">
360 <button type="submit" class="btn btn-secondary mr-2">
361 {this.state.loading ? (
362 <svg class="icon icon-spinner spin">
363 <use xlinkHref="#icon-spinner"></use>
365 ) : this.props.post ? (
366 capitalizeFirstLetter(i18n.t('save'))
368 capitalizeFirstLetter(i18n.t('create'))
371 {this.props.post && (
374 class="btn btn-secondary"
375 onClick={linkEvent(this, this.handleCancel)}
388 emojiPicker.on('emoji', twemojiHtmlStr => {
389 if (this.state.postForm.body == null) {
390 this.state.postForm.body = '';
392 var el = document.createElement('div');
393 el.innerHTML = twemojiHtmlStr;
394 let nativeUnicode = (el.childNodes[0] as HTMLElement).getAttribute('alt');
395 let shortName = `:${emojiShortName[nativeUnicode]}:`;
396 this.state.postForm.body += shortName;
397 this.setState(this.state);
401 handlePostSubmit(i: PostForm, event: any) {
402 event.preventDefault();
404 WebSocketService.Instance.editPost(i.state.postForm);
406 WebSocketService.Instance.createPost(i.state.postForm);
408 i.state.loading = true;
412 copySuggestedTitle(i: PostForm) {
413 i.state.postForm.name = i.state.suggestedTitle.substring(
415 MAX_POST_TITLE_LENGTH
417 i.state.suggestedTitle = undefined;
421 handlePostUrlChange(i: PostForm, event: any) {
422 i.state.postForm.url = event.target.value;
428 if (validURL(this.state.postForm.url)) {
429 let form: SearchForm = {
430 q: this.state.postForm.url,
431 type_: SearchType[SearchType.Url],
432 sort: SortType[SortType.TopAll],
437 WebSocketService.Instance.search(form);
439 // Fetch the page title
440 getPageTitle(this.state.postForm.url).then(d => {
441 this.state.suggestedTitle = d;
442 this.setState(this.state);
445 this.state.suggestedTitle = undefined;
446 this.state.crossPosts = [];
450 handlePostNameChange(i: PostForm, event: any) {
451 i.state.postForm.name = event.target.value;
453 i.fetchSimilarPosts();
456 fetchSimilarPosts() {
457 let form: SearchForm = {
458 q: this.state.postForm.name,
459 type_: SearchType[SearchType.Posts],
460 sort: SortType[SortType.TopAll],
461 community_id: this.state.postForm.community_id,
466 if (this.state.postForm.name !== '') {
467 WebSocketService.Instance.search(form);
469 this.state.suggestedPosts = [];
472 this.setState(this.state);
475 handlePostBodyChange(i: PostForm, event: any) {
476 i.state.postForm.body = event.target.value;
480 handlePostCommunityChange(i: PostForm, event: any) {
481 i.state.postForm.community_id = Number(event.target.value);
485 handlePostNsfwChange(i: PostForm, event: any) {
486 i.state.postForm.nsfw = event.target.checked;
490 handleCancel(i: PostForm) {
494 handlePreviewToggle(i: PostForm, event: any) {
495 event.preventDefault();
496 i.state.previewMode = !i.state.previewMode;
500 handleImageUploadPaste(i: PostForm, event: any) {
501 let image = event.clipboardData.files[0];
503 i.handleImageUpload(i, image);
507 handleImageUpload(i: PostForm, event: any) {
510 event.preventDefault();
511 file = event.target.files[0];
516 const imageUploadUrl = `/pictshare/api/upload.php`;
517 const formData = new FormData();
518 formData.append('file', file);
520 i.state.imageLoading = true;
523 fetch(imageUploadUrl, {
527 .then(res => res.json())
529 let url = `${window.location.origin}/pictshare/${encodeURI(res.url)}`;
530 if (res.filetype == 'mp4') {
533 i.state.postForm.url = url;
534 i.state.imageLoading = false;
538 i.state.imageLoading = false;
540 toast(error, 'danger');
544 handleEmojiPickerClick(_i: PostForm, event: any) {
545 emojiPicker.togglePicker(event.target);
548 parseMessage(msg: WebSocketJsonResponse) {
549 let res = wsJsonToRes(msg);
551 toast(i18n.t(msg.error), 'danger');
552 this.state.loading = false;
553 this.setState(this.state);
555 } else if (res.op == UserOperation.ListCommunities) {
556 let data = res.data as ListCommunitiesResponse;
557 this.state.communities = data.communities;
558 if (this.props.post) {
559 this.state.postForm.community_id = this.props.post.community_id;
560 } else if (this.props.params && this.props.params.community) {
561 let foundCommunityId = data.communities.find(
562 r => r.name == this.props.params.community
564 this.state.postForm.community_id = foundCommunityId;
566 this.state.postForm.community_id = data.communities[0].id;
568 this.setState(this.state);
570 // Set up select searching
571 let selectId: any = document.getElementById('post-community');
573 let selector = new Selectr(selectId, { nativeDropdown: false });
574 selector.on('selectr.select', option => {
575 this.state.postForm.community_id = Number(option.value);
578 } else if (res.op == UserOperation.CreatePost) {
579 let data = res.data as PostResponse;
580 if (data.post.creator_id == UserService.Instance.user.id) {
581 this.state.loading = false;
582 this.props.onCreate(data.post.id);
584 } else if (res.op == UserOperation.EditPost) {
585 let data = res.data as PostResponse;
586 if (data.post.creator_id == UserService.Instance.user.id) {
587 this.state.loading = false;
588 this.props.onEdit(data.post);
590 } else if (res.op == UserOperation.Search) {
591 let data = res.data as SearchResponse;
593 if (data.type_ == SearchType[SearchType.Posts]) {
594 this.state.suggestedPosts = data.posts;
595 } else if (data.type_ == SearchType[SearchType.Url]) {
596 this.state.crossPosts = data.posts;
598 this.setState(this.state);
599 } else if (res.op == UserOperation.GetSite) {
600 let data = res.data as GetSiteResponse;
601 this.state.enable_nsfw = data.site.enable_nsfw;
602 this.setState(this.state);