import { Component, linkEvent } from 'inferno';
import { Prompt } from 'inferno-router';
import { PostListings } from './post-listings';
+import { MarkdownTextArea } from './markdown-textarea';
import { Subscription } from 'rxjs';
import { retryWhen, delay, take } from 'rxjs/operators';
import {
SearchType,
SearchResponse,
WebSocketJsonResponse,
-} from '../interfaces';
+} from 'lemmy-js-client';
import { WebSocketService, UserService } from '../services';
import {
wsJsonToRes,
getPageTitle,
validURL,
capitalizeFirstLetter,
- markdownHelpUrl,
archiveUrl,
- mdToHtml,
debounce,
isImage,
toast,
randomStr,
- setupTribute,
setupTippy,
- emojiPicker,
hostname,
pictrsDeleteToast,
+ validTitle,
} from '../utils';
-import autosize from 'autosize';
-import Tribute from 'tributejs/src/Tribute.js';
-import emojiShortName from 'emoji-short-name';
-import Selectr from 'mobius1-selectr';
+import Choices from 'choices.js';
import { i18n } from '../i18next';
const MAX_POST_TITLE_LENGTH = 200;
export class PostForm extends Component<PostFormProps, PostFormState> {
private id = `post-form-${randomStr()}`;
- private tribute: Tribute;
private subscription: Subscription;
+ private choices: Choices;
private emptyState: PostFormState = {
postForm: {
name: null,
nsfw: false,
auth: null,
community_id: null,
- creator_id: UserService.Instance.user
- ? UserService.Instance.user.id
- : null,
},
communities: [],
loading: false,
super(props, context);
this.fetchSimilarPosts = debounce(this.fetchSimilarPosts).bind(this);
this.fetchPageTitle = debounce(this.fetchPageTitle).bind(this);
-
- this.tribute = setupTribute();
- this.setupEmojiPicker();
+ this.handlePostBodyChange = this.handlePostBodyChange.bind(this);
this.state = this.emptyState;
name: this.props.post.name,
community_id: this.props.post.community_id,
edit_id: this.props.post.id,
- creator_id: this.props.post.creator_id,
url: this.props.post.url,
nsfw: this.props.post.nsfw,
auth: null,
);
let listCommunitiesForm: ListCommunitiesForm = {
- sort: SortType[SortType.TopAll],
+ sort: SortType.TopAll,
limit: 9999,
};
}
componentDidMount() {
- var textarea: any = document.getElementById(this.id);
- autosize(textarea);
- this.tribute.attach(textarea);
- textarea.addEventListener('tribute-replaced', () => {
- this.state.postForm.body = textarea.value;
- this.setState(this.state);
- autosize.update(textarea);
- });
setupTippy();
}
componentWillUnmount() {
this.subscription.unsubscribe();
+ /* this.choices && this.choices.destroy(); */
window.onbeforeunload = null;
}
value={this.state.postForm.name}
id="post-title"
onInput={linkEvent(this, this.handlePostNameChange)}
- class="form-control"
+ class={`form-control ${
+ !validTitle(this.state.postForm.name) && 'is-invalid'
+ }`}
required
rows={2}
minLength={3}
maxLength={MAX_POST_TITLE_LENGTH}
/>
+ {!validTitle(this.state.postForm.name) && (
+ <div class="invalid-feedback">
+ {i18n.t('invalid_post_title')}
+ </div>
+ )}
{this.state.suggestedPosts.length > 0 && (
<>
<div class="my-1 text-muted small font-weight-bold">
{i18n.t('body')}
</label>
<div class="col-sm-10">
- <textarea
- id={this.id}
- value={this.state.postForm.body}
- onInput={linkEvent(this, this.handlePostBodyChange)}
- className={`form-control ${this.state.previewMode && 'd-none'}`}
- rows={4}
- maxLength={10000}
+ <MarkdownTextArea
+ initialContent={this.state.postForm.body}
+ onContentChange={this.handlePostBodyChange}
/>
- {this.state.previewMode && (
- <div
- className="card card-body md-div"
- dangerouslySetInnerHTML={mdToHtml(this.state.postForm.body)}
- />
- )}
- {this.state.postForm.body && (
- <button
- className={`mt-1 mr-2 btn btn-sm btn-secondary ${
- this.state.previewMode && 'active'
- }`}
- onClick={linkEvent(this, this.handlePreviewToggle)}
- >
- {i18n.t('preview')}
- </button>
- )}
- <a
- href={markdownHelpUrl}
- target="_blank"
- rel="noopener"
- class="d-inline-block float-right text-muted font-weight-bold"
- title={i18n.t('formatting_help')}
- >
- <svg class="icon icon-inline">
- <use xlinkHref="#icon-help-circle"></use>
- </svg>
- </a>
- <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>
</div>
</div>
{!this.props.post && (
);
}
- setupEmojiPicker() {
- emojiPicker.on('emoji', twemojiHtmlStr => {
- if (this.state.postForm.body == null) {
- this.state.postForm.body = '';
- }
- var el = document.createElement('div');
- el.innerHTML = twemojiHtmlStr;
- let nativeUnicode = (el.childNodes[0] as HTMLElement).getAttribute('alt');
- let shortName = `:${emojiShortName[nativeUnicode]}:`;
- this.state.postForm.body += shortName;
- this.setState(this.state);
- });
- }
-
handlePostSubmit(i: PostForm, event: any) {
event.preventDefault();
if (validURL(this.state.postForm.url)) {
let form: SearchForm = {
q: this.state.postForm.url,
- type_: SearchType[SearchType.Url],
- sort: SortType[SortType.TopAll],
+ type_: SearchType.Url,
+ sort: SortType.TopAll,
page: 1,
limit: 6,
};
fetchSimilarPosts() {
let form: SearchForm = {
q: this.state.postForm.name,
- type_: SearchType[SearchType.Posts],
- sort: SortType[SortType.TopAll],
+ type_: SearchType.Posts,
+ sort: SortType.TopAll,
community_id: this.state.postForm.community_id,
page: 1,
limit: 6,
this.setState(this.state);
}
- handlePostBodyChange(i: PostForm, event: any) {
- i.state.postForm.body = event.target.value;
- i.setState(i.state);
+ handlePostBodyChange(val: string) {
+ this.state.postForm.body = val;
+ this.setState(this.state);
}
handlePostCommunityChange(i: PostForm, event: any) {
});
}
- handleEmojiPickerClick(_i: PostForm, event: any) {
- emojiPicker.togglePicker(event.target);
- }
-
parseMessage(msg: WebSocketJsonResponse) {
let res = wsJsonToRes(msg);
if (msg.error) {
// Set up select searching
let selectId: any = document.getElementById('post-community');
if (selectId) {
- let selector = new Selectr(selectId, { nativeDropdown: false });
- selector.on('selectr.select', option => {
- this.state.postForm.community_id = Number(option.value);
- this.setState(this.state);
+ this.choices = new Choices(selectId, {
+ shouldSort: false,
+ classNames: {
+ containerOuter: 'choices',
+ containerInner: 'choices__inner bg-secondary border-0',
+ input: 'form-control',
+ inputCloned: 'choices__input--cloned',
+ list: 'choices__list',
+ listItems: 'choices__list--multiple',
+ listSingle: 'choices__list--single',
+ listDropdown: 'choices__list--dropdown',
+ item: 'choices__item bg-secondary',
+ itemSelectable: 'choices__item--selectable',
+ itemDisabled: 'choices__item--disabled',
+ itemChoice: 'choices__item--choice',
+ placeholder: 'choices__placeholder',
+ group: 'choices__group',
+ groupHeading: 'choices__heading',
+ button: 'choices__button',
+ activeState: 'is-active',
+ focusState: 'is-focused',
+ openState: 'is-open',
+ disabledState: 'is-disabled',
+ highlightedState: 'text-info',
+ selectedState: 'text-info',
+ flippedState: 'is-flipped',
+ loadingState: 'is-loading',
+ noResults: 'has-no-results',
+ noChoices: 'has-no-choices',
+ },
});
+ this.choices.passedElement.element.addEventListener(
+ 'choice',
+ (e: any) => {
+ this.state.postForm.community_id = Number(e.detail.choice.value);
+ this.setState(this.state);
+ },
+ false
+ );
}
} else if (res.op == UserOperation.CreatePost) {
let data = res.data as PostResponse;