import autosize from "autosize"; import { Component, linkEvent } from "inferno"; import { Prompt } from "inferno-router"; import { pictrsUri } from "../../env"; import { i18n } from "../../i18next"; import { UserService } from "../../services"; import { isBrowser, markdownHelpUrl, mdToHtml, pictrsDeleteToast, randomStr, setupTippy, setupTribute, toast, } from "../../utils"; import { Icon, Spinner } from "./icon"; interface MarkdownTextAreaProps { initialContent: string; finished?: boolean; buttonTitle?: string; replyType?: boolean; focus?: boolean; disabled?: boolean; maxLength?: number; onSubmit?(msg: { val: string; formId: string }): any; onContentChange?(val: string): any; onReplyCancel?(): any; hideNavigationWarnings?: boolean; placeholder?: string; } interface MarkdownTextAreaState { content: string; previewMode: boolean; loading: boolean; imageLoading: boolean; } export class MarkdownTextArea extends Component< MarkdownTextAreaProps, MarkdownTextAreaState > { private id = `comment-textarea-${randomStr()}`; private formId = `comment-form-${randomStr()}`; private tribute: any; private emptyState: MarkdownTextAreaState = { content: this.props.initialContent, previewMode: false, loading: false, imageLoading: false, }; constructor(props: any, context: any) { super(props, context); if (isBrowser()) { this.tribute = setupTribute(); } this.state = this.emptyState; } componentDidMount() { let textarea: any = document.getElementById(this.id); if (textarea) { autosize(textarea); this.tribute.attach(textarea); textarea.addEventListener("tribute-replaced", () => { this.state.content = textarea.value; this.setState(this.state); autosize.update(textarea); }); this.quoteInsert(); if (this.props.focus) { textarea.focus(); } // TODO this is slow for some reason setupTippy(); } } componentDidUpdate() { if (!this.props.hideNavigationWarnings && this.state.content) { window.onbeforeunload = () => true; } else { window.onbeforeunload = undefined; } } componentWillReceiveProps(nextProps: MarkdownTextAreaProps) { if (nextProps.finished) { this.state.previewMode = false; this.state.loading = false; this.state.content = ""; this.setState(this.state); if (this.props.replyType) { this.props.onReplyCancel(); } let textarea: any = document.getElementById(this.id); let form: any = document.getElementById(this.formId); form.reset(); setTimeout(() => autosize.update(textarea), 10); this.setState(this.state); } } componentWillUnmount() { window.onbeforeunload = null; } render() { return (