1 import { Component, linkEvent } from 'inferno';
2 import { Prompt } from 'inferno-router';
3 import { PostListings } from './post-listings';
4 import { MarkdownTextArea } from './markdown-textarea';
5 import { Subscription } from 'rxjs';
17 WebSocketJsonResponse,
18 } from 'lemmy-js-client';
19 import { WebSocketService, UserService } from '../services';
24 capitalizeFirstLetter,
40 Choices = require('choices.js');
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 communities: Community[];
50 params?: PostFormParams;
52 onCreate?(id: number): any;
53 onEdit?(post: Post): any;
55 enableDownvotes: boolean;
58 interface PostFormState {
61 imageLoading: boolean;
63 suggestedTitle: string;
64 suggestedPosts: Post[];
68 export class PostForm extends Component<PostFormProps, PostFormState> {
69 private id = `post-form-${randomStr()}`;
70 private subscription: Subscription;
72 private emptyState: PostFormState = {
82 suggestedTitle: undefined,
87 constructor(props: any, context: any) {
88 super(props, context);
89 this.fetchSimilarPosts = debounce(this.fetchSimilarPosts).bind(this);
90 this.fetchPageTitle = debounce(this.fetchPageTitle).bind(this);
91 this.handlePostBodyChange = this.handlePostBodyChange.bind(this);
93 this.state = this.emptyState;
95 if (this.props.post) {
96 this.state.postForm = {
97 body: this.props.post.body,
98 // NOTE: debouncing breaks both these for some reason, unless you use defaultValue
99 name: this.props.post.name,
100 community_id: this.props.post.community_id,
101 edit_id: this.props.post.id,
102 url: this.props.post.url,
103 nsfw: this.props.post.nsfw,
108 if (this.props.params) {
109 this.state.postForm.name = this.props.params.name;
110 if (this.props.params.url) {
111 this.state.postForm.url = this.props.params.url;
113 if (this.props.params.body) {
114 this.state.postForm.body = this.props.params.body;
118 this.parseMessage = this.parseMessage.bind(this);
119 this.subscription = wsSubscribe(this.parseMessage);
122 componentDidMount() {
124 this.setupCommunities();
127 componentDidUpdate() {
129 !this.state.loading &&
130 (this.state.postForm.name ||
131 this.state.postForm.url ||
132 this.state.postForm.body)
134 window.onbeforeunload = () => true;
136 window.onbeforeunload = undefined;
140 componentWillUnmount() {
141 this.subscription.unsubscribe();
142 /* this.choices && this.choices.destroy(); */
143 window.onbeforeunload = null;
151 !this.state.loading &&
152 (this.state.postForm.name ||
153 this.state.postForm.url ||
154 this.state.postForm.body)
156 message={i18n.t('block_leaving')}
158 <form onSubmit={linkEvent(this, this.handlePostSubmit)}>
159 <div class="form-group row">
160 <label class="col-sm-2 col-form-label" htmlFor="post-url">
163 <div class="col-sm-10">
168 value={this.state.postForm.url}
169 onInput={linkEvent(this, this.handlePostUrlChange)}
170 onPaste={linkEvent(this, this.handleImageUploadPaste)}
172 {this.state.suggestedTitle && (
174 class="mt-1 text-muted small font-weight-bold pointer"
175 onClick={linkEvent(this, this.copySuggestedTitle)}
177 {i18n.t('copy_suggested_title', {
178 title: this.state.suggestedTitle,
184 htmlFor="file-upload"
186 UserService.Instance.user && 'pointer'
187 } d-inline-block float-right text-muted font-weight-bold`}
188 data-tippy-content={i18n.t('upload_image')}
190 <svg class="icon icon-inline">
191 <use xlinkHref="#icon-image"></use>
197 accept="image/*,video/*"
200 disabled={!UserService.Instance.user}
201 onChange={linkEvent(this, this.handleImageUpload)}
204 {this.state.postForm.url && validURL(this.state.postForm.url) && (
206 href={`${archiveUrl}/?run=1&url=${encodeURIComponent(
207 this.state.postForm.url
210 class="mr-2 d-inline-block float-right text-muted small font-weight-bold"
213 {i18n.t('archive_link')}
216 {this.state.imageLoading && (
217 <svg class="icon icon-spinner spin">
218 <use xlinkHref="#icon-spinner"></use>
221 {isImage(this.state.postForm.url) && (
222 <img src={this.state.postForm.url} class="img-fluid" />
224 {this.state.crossPosts.length > 0 && (
226 <div class="my-1 text-muted small font-weight-bold">
227 {i18n.t('cross_posts')}
231 posts={this.state.crossPosts}
232 enableDownvotes={this.props.enableDownvotes}
233 enableNsfw={this.props.enableNsfw}
239 <div class="form-group row">
240 <label class="col-sm-2 col-form-label" htmlFor="post-title">
243 <div class="col-sm-10">
245 value={this.state.postForm.name}
247 onInput={linkEvent(this, this.handlePostNameChange)}
248 class={`form-control ${
249 !validTitle(this.state.postForm.name) && 'is-invalid'
254 maxLength={MAX_POST_TITLE_LENGTH}
256 {!validTitle(this.state.postForm.name) && (
257 <div class="invalid-feedback">
258 {i18n.t('invalid_post_title')}
261 {this.state.suggestedPosts.length > 0 && (
263 <div class="my-1 text-muted small font-weight-bold">
264 {i18n.t('related_posts')}
267 posts={this.state.suggestedPosts}
268 enableDownvotes={this.props.enableDownvotes}
269 enableNsfw={this.props.enableNsfw}
276 <div class="form-group row">
277 <label class="col-sm-2 col-form-label" htmlFor={this.id}>
280 <div class="col-sm-10">
282 initialContent={this.state.postForm.body}
283 onContentChange={this.handlePostBodyChange}
287 {!this.props.post && (
288 <div class="form-group row">
289 <label class="col-sm-2 col-form-label" htmlFor="post-community">
290 {i18n.t('community')}
292 <div class="col-sm-10">
296 value={this.state.postForm.community_id}
297 onInput={linkEvent(this, this.handlePostCommunityChange)}
299 <option>{i18n.t('select_a_community')}</option>
300 {this.props.communities.map(community => (
301 <option value={community.id}>
304 : `${hostname(community.actor_id)}/${community.name}`}
311 {this.props.enableNsfw && (
312 <div class="form-group row">
313 <div class="col-sm-10">
314 <div class="form-check">
316 class="form-check-input"
319 checked={this.state.postForm.nsfw}
320 onChange={linkEvent(this, this.handlePostNsfwChange)}
322 <label class="form-check-label" htmlFor="post-nsfw">
329 <div class="form-group row">
330 <div class="col-sm-10">
333 !this.state.postForm.community_id || this.state.loading
336 class="btn btn-secondary mr-2"
338 {this.state.loading ? (
339 <svg class="icon icon-spinner spin">
340 <use xlinkHref="#icon-spinner"></use>
342 ) : this.props.post ? (
343 capitalizeFirstLetter(i18n.t('save'))
345 capitalizeFirstLetter(i18n.t('create'))
348 {this.props.post && (
351 class="btn btn-secondary"
352 onClick={linkEvent(this, this.handleCancel)}
364 handlePostSubmit(i: PostForm, event: any) {
365 event.preventDefault();
367 // Coerce empty url string to undefined
368 if (i.state.postForm.url && i.state.postForm.url === '') {
369 i.state.postForm.url = undefined;
373 WebSocketService.Instance.editPost(i.state.postForm);
375 WebSocketService.Instance.createPost(i.state.postForm);
377 i.state.loading = true;
381 copySuggestedTitle(i: PostForm) {
382 i.state.postForm.name = i.state.suggestedTitle.substring(
384 MAX_POST_TITLE_LENGTH
386 i.state.suggestedTitle = undefined;
390 handlePostUrlChange(i: PostForm, event: any) {
391 i.state.postForm.url = event.target.value;
397 if (validURL(this.state.postForm.url)) {
398 let form: SearchForm = {
399 q: this.state.postForm.url,
400 type_: SearchType.Url,
401 sort: SortType.TopAll,
406 WebSocketService.Instance.search(form);
408 // Fetch the page title
409 getPageTitle(this.state.postForm.url).then(d => {
410 this.state.suggestedTitle = d;
411 this.setState(this.state);
414 this.state.suggestedTitle = undefined;
415 this.state.crossPosts = [];
419 handlePostNameChange(i: PostForm, event: any) {
420 i.state.postForm.name = event.target.value;
422 i.fetchSimilarPosts();
425 fetchSimilarPosts() {
426 let form: SearchForm = {
427 q: this.state.postForm.name,
428 type_: SearchType.Posts,
429 sort: SortType.TopAll,
430 community_id: this.state.postForm.community_id,
435 if (this.state.postForm.name !== '') {
436 WebSocketService.Instance.search(form);
438 this.state.suggestedPosts = [];
441 this.setState(this.state);
444 handlePostBodyChange(val: string) {
445 this.state.postForm.body = val;
446 this.setState(this.state);
449 handlePostCommunityChange(i: PostForm, event: any) {
450 i.state.postForm.community_id = Number(event.target.value);
454 handlePostNsfwChange(i: PostForm, event: any) {
455 i.state.postForm.nsfw = event.target.checked;
459 handleCancel(i: PostForm) {
463 handlePreviewToggle(i: PostForm, event: any) {
464 event.preventDefault();
465 i.state.previewMode = !i.state.previewMode;
469 handleImageUploadPaste(i: PostForm, event: any) {
470 let image = event.clipboardData.files[0];
472 i.handleImageUpload(i, image);
476 handleImageUpload(i: PostForm, event: any) {
479 event.preventDefault();
480 file = event.target.files[0];
485 const imageUploadUrl = `/pictrs/image`;
486 const formData = new FormData();
487 formData.append('images[]', file);
489 i.state.imageLoading = true;
492 fetch(imageUploadUrl, {
496 .then(res => res.json())
498 console.log('pictrs upload:');
500 if (res.msg == 'ok') {
501 let hash = res.files[0].file;
502 let url = `${window.location.origin}/pictrs/image/${hash}`;
503 let deleteToken = res.files[0].delete_token;
504 let deleteUrl = `${window.location.origin}/pictrs/image/delete/${deleteToken}/${hash}`;
505 i.state.postForm.url = url;
506 i.state.imageLoading = false;
509 i18n.t('click_to_delete_picture'),
510 i18n.t('picture_deleted'),
514 i.state.imageLoading = false;
516 toast(JSON.stringify(res), 'danger');
520 i.state.imageLoading = false;
522 toast(error, 'danger');
527 if (this.props.post) {
528 this.state.postForm.community_id = this.props.post.community_id;
529 } else if (this.props.params && this.props.params.community) {
530 let foundCommunityId = this.props.communities.find(
531 r => r.name == this.props.params.community
533 this.state.postForm.community_id = foundCommunityId;
535 // By default, the null valued 'Select a Community'
538 // Set up select searching
540 let selectId: any = document.getElementById('post-community');
542 this.choices = new Choices(selectId, {
545 containerOuter: 'choices',
546 containerInner: 'choices__inner bg-secondary border-0',
547 input: 'form-control',
548 inputCloned: 'choices__input--cloned',
549 list: 'choices__list',
550 listItems: 'choices__list--multiple',
551 listSingle: 'choices__list--single',
552 listDropdown: 'choices__list--dropdown',
553 item: 'choices__item bg-secondary',
554 itemSelectable: 'choices__item--selectable',
555 itemDisabled: 'choices__item--disabled',
556 itemChoice: 'choices__item--choice',
557 placeholder: 'choices__placeholder',
558 group: 'choices__group',
559 groupHeading: 'choices__heading',
560 button: 'choices__button',
561 activeState: 'is-active',
562 focusState: 'is-focused',
563 openState: 'is-open',
564 disabledState: 'is-disabled',
565 highlightedState: 'text-info',
566 selectedState: 'text-info',
567 flippedState: 'is-flipped',
568 loadingState: 'is-loading',
569 noResults: 'has-no-results',
570 noChoices: 'has-no-choices',
573 this.choices.passedElement.element.addEventListener(
576 this.state.postForm.community_id = Number(e.detail.choice.value);
577 this.setState(this.state);
585 parseMessage(msg: WebSocketJsonResponse) {
586 let res = wsJsonToRes(msg);
588 toast(i18n.t(msg.error), 'danger');
589 this.state.loading = false;
590 this.setState(this.state);
592 } else if (res.op == UserOperation.CreatePost) {
593 let data = res.data as PostResponse;
594 if (data.post.creator_id == UserService.Instance.user.id) {
595 this.state.loading = false;
596 this.props.onCreate(data.post.id);
598 } else if (res.op == UserOperation.EditPost) {
599 let data = res.data as PostResponse;
600 if (data.post.creator_id == UserService.Instance.user.id) {
601 this.state.loading = false;
602 this.props.onEdit(data.post);
604 } else if (res.op == UserOperation.Search) {
605 let data = res.data as SearchResponse;
607 if (data.type_ == SearchType[SearchType.Posts]) {
608 this.state.suggestedPosts = data.posts;
609 } else if (data.type_ == SearchType[SearchType.Url]) {
610 this.state.crossPosts = data.posts;
612 this.setState(this.state);