2021-07-17 20:42:55 +00:00
|
|
|
import autosize from "autosize";
|
2021-02-22 02:30:11 +00:00
|
|
|
import { Component, linkEvent } from "inferno";
|
|
|
|
import { Prompt } from "inferno-router";
|
2023-01-04 16:56:24 +00:00
|
|
|
import { Language } from "lemmy-js-client";
|
2021-07-17 20:42:55 +00:00
|
|
|
import { pictrsUri } from "../../env";
|
|
|
|
import { i18n } from "../../i18next";
|
|
|
|
import { UserService } from "../../services";
|
2020-09-06 16:15:25 +00:00
|
|
|
import {
|
2021-07-17 20:42:55 +00:00
|
|
|
isBrowser,
|
2022-11-12 17:06:41 +00:00
|
|
|
markdownFieldCharacterLimit,
|
2020-09-06 16:15:25 +00:00
|
|
|
markdownHelpUrl,
|
2021-07-17 20:42:55 +00:00
|
|
|
mdToHtml,
|
2020-09-06 16:15:25 +00:00
|
|
|
pictrsDeleteToast,
|
2021-07-17 20:42:55 +00:00
|
|
|
randomStr,
|
2022-02-24 15:31:44 +00:00
|
|
|
relTags,
|
2020-09-06 16:15:25 +00:00
|
|
|
setupTippy,
|
2021-07-17 20:42:55 +00:00
|
|
|
setupTribute,
|
|
|
|
toast,
|
|
|
|
} from "../../utils";
|
2021-02-22 02:30:11 +00:00
|
|
|
import { Icon, Spinner } from "./icon";
|
2022-09-22 15:14:58 +00:00
|
|
|
import { LanguageSelect } from "./language-select";
|
2020-09-06 16:15:25 +00:00
|
|
|
|
|
|
|
interface MarkdownTextAreaProps {
|
2023-01-04 16:56:24 +00:00
|
|
|
initialContent?: string;
|
|
|
|
initialLanguageId?: number;
|
|
|
|
placeholder?: string;
|
|
|
|
buttonTitle?: string;
|
|
|
|
maxLength?: number;
|
2020-09-06 16:15:25 +00:00
|
|
|
replyType?: boolean;
|
|
|
|
focus?: boolean;
|
|
|
|
disabled?: boolean;
|
2022-06-21 21:42:29 +00:00
|
|
|
finished?: boolean;
|
2022-09-22 15:14:58 +00:00
|
|
|
showLanguage?: boolean;
|
2022-06-21 21:42:29 +00:00
|
|
|
hideNavigationWarnings?: boolean;
|
2020-09-06 16:15:25 +00:00
|
|
|
onContentChange?(val: string): any;
|
|
|
|
onReplyCancel?(): any;
|
2023-01-04 16:56:24 +00:00
|
|
|
onSubmit?(msg: { val?: string; formId: string; languageId?: number }): any;
|
|
|
|
allLanguages: Language[]; // TODO should probably be nullable
|
|
|
|
siteLanguages: number[]; // TODO same
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
interface MarkdownTextAreaState {
|
2023-01-04 16:56:24 +00:00
|
|
|
content?: string;
|
|
|
|
languageId?: number;
|
2020-09-06 16:15:25 +00:00
|
|
|
previewMode: boolean;
|
|
|
|
loading: boolean;
|
|
|
|
imageLoading: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class MarkdownTextArea extends Component<
|
|
|
|
MarkdownTextAreaProps,
|
|
|
|
MarkdownTextAreaState
|
|
|
|
> {
|
|
|
|
private id = `comment-textarea-${randomStr()}`;
|
|
|
|
private formId = `comment-form-${randomStr()}`;
|
2020-09-07 22:24:48 +00:00
|
|
|
private tribute: any;
|
2023-01-04 16:56:24 +00:00
|
|
|
state: MarkdownTextAreaState = {
|
2020-09-06 16:15:25 +00:00
|
|
|
content: this.props.initialContent,
|
2022-09-22 15:14:58 +00:00
|
|
|
languageId: this.props.initialLanguageId,
|
2020-09-06 16:15:25 +00:00
|
|
|
previewMode: false,
|
|
|
|
loading: false,
|
|
|
|
imageLoading: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
constructor(props: any, context: any) {
|
|
|
|
super(props, context);
|
|
|
|
|
2022-09-22 15:14:58 +00:00
|
|
|
this.handleLanguageChange = this.handleLanguageChange.bind(this);
|
|
|
|
|
2020-09-07 22:24:48 +00:00
|
|
|
if (isBrowser()) {
|
|
|
|
this.tribute = setupTribute();
|
|
|
|
}
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
let textarea: any = document.getElementById(this.id);
|
|
|
|
if (textarea) {
|
|
|
|
autosize(textarea);
|
|
|
|
this.tribute.attach(textarea);
|
2021-02-22 02:30:11 +00:00
|
|
|
textarea.addEventListener("tribute-replaced", () => {
|
2023-01-04 16:56:24 +00:00
|
|
|
this.setState({ content: textarea.value });
|
2020-09-06 16:15:25 +00:00
|
|
|
autosize.update(textarea);
|
|
|
|
});
|
|
|
|
|
|
|
|
this.quoteInsert();
|
|
|
|
|
|
|
|
if (this.props.focus) {
|
|
|
|
textarea.focus();
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO this is slow for some reason
|
|
|
|
setupTippy();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidUpdate() {
|
2023-01-04 16:56:24 +00:00
|
|
|
if (!this.props.hideNavigationWarnings && this.state.content) {
|
2020-09-06 16:15:25 +00:00
|
|
|
window.onbeforeunload = () => true;
|
|
|
|
} else {
|
2023-01-04 16:56:24 +00:00
|
|
|
window.onbeforeunload = null;
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillReceiveProps(nextProps: MarkdownTextAreaProps) {
|
|
|
|
if (nextProps.finished) {
|
2023-01-04 16:56:24 +00:00
|
|
|
this.setState({ previewMode: false, loading: false, content: undefined });
|
2020-09-06 16:15:25 +00:00
|
|
|
if (this.props.replyType) {
|
2023-01-04 16:56:24 +00:00
|
|
|
this.props.onReplyCancel?.();
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let textarea: any = document.getElementById(this.id);
|
|
|
|
let form: any = document.getElementById(this.formId);
|
|
|
|
form.reset();
|
|
|
|
setTimeout(() => autosize.update(textarea), 10);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
window.onbeforeunload = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2023-01-04 16:56:24 +00:00
|
|
|
let languageId = this.state.languageId;
|
|
|
|
|
2020-09-06 16:15:25 +00:00
|
|
|
return (
|
|
|
|
<form id={this.formId} onSubmit={linkEvent(this, this.handleSubmit)}>
|
|
|
|
<Prompt
|
2023-01-04 16:56:24 +00:00
|
|
|
when={!this.props.hideNavigationWarnings && this.state.content}
|
2021-02-22 02:30:11 +00:00
|
|
|
message={i18n.t("block_leaving")}
|
2020-09-06 16:15:25 +00:00
|
|
|
/>
|
2022-09-22 15:03:35 +00:00
|
|
|
<div className="form-group row">
|
2020-09-06 16:15:25 +00:00
|
|
|
<div className={`col-sm-12`}>
|
|
|
|
<textarea
|
|
|
|
id={this.id}
|
2021-02-22 02:30:11 +00:00
|
|
|
className={`form-control ${this.state.previewMode && "d-none"}`}
|
2023-01-04 16:56:24 +00:00
|
|
|
value={this.state.content}
|
2020-09-06 16:15:25 +00:00
|
|
|
onInput={linkEvent(this, this.handleContentChange)}
|
|
|
|
onPaste={linkEvent(this, this.handleImageUploadPaste)}
|
|
|
|
required
|
|
|
|
disabled={this.props.disabled}
|
|
|
|
rows={2}
|
2023-01-04 16:56:24 +00:00
|
|
|
maxLength={this.props.maxLength ?? markdownFieldCharacterLimit}
|
|
|
|
placeholder={this.props.placeholder}
|
2020-09-06 16:15:25 +00:00
|
|
|
/>
|
2023-01-04 16:56:24 +00:00
|
|
|
{this.state.previewMode && this.state.content && (
|
|
|
|
<div
|
|
|
|
className="card border-secondary card-body md-div"
|
|
|
|
dangerouslySetInnerHTML={mdToHtml(this.state.content)}
|
|
|
|
/>
|
|
|
|
)}
|
2020-09-06 16:15:25 +00:00
|
|
|
</div>
|
2022-09-22 15:03:35 +00:00
|
|
|
<label className="sr-only" htmlFor={this.id}>
|
2021-02-22 02:30:11 +00:00
|
|
|
{i18n.t("body")}
|
2021-02-09 16:21:24 +00:00
|
|
|
</label>
|
2020-09-06 16:15:25 +00:00
|
|
|
</div>
|
2022-09-22 15:03:35 +00:00
|
|
|
<div className="row">
|
|
|
|
<div className="col-sm-12 d-flex flex-wrap">
|
2023-01-04 16:56:24 +00:00
|
|
|
{this.props.buttonTitle && (
|
|
|
|
<button
|
|
|
|
type="submit"
|
|
|
|
className="btn btn-sm btn-secondary mr-2"
|
|
|
|
disabled={this.props.disabled || this.state.loading}
|
|
|
|
>
|
|
|
|
{this.state.loading ? (
|
|
|
|
<Spinner />
|
|
|
|
) : (
|
|
|
|
<span>{this.props.buttonTitle}</span>
|
|
|
|
)}
|
|
|
|
</button>
|
|
|
|
)}
|
2020-09-06 16:15:25 +00:00
|
|
|
{this.props.replyType && (
|
|
|
|
<button
|
|
|
|
type="button"
|
2022-09-22 15:03:35 +00:00
|
|
|
className="btn btn-sm btn-secondary mr-2"
|
2020-09-06 16:15:25 +00:00
|
|
|
onClick={linkEvent(this, this.handleReplyCancel)}
|
|
|
|
>
|
2021-02-22 02:30:11 +00:00
|
|
|
{i18n.t("cancel")}
|
2020-09-06 16:15:25 +00:00
|
|
|
</button>
|
|
|
|
)}
|
2023-01-04 16:56:24 +00:00
|
|
|
{this.state.content && (
|
2020-09-06 16:15:25 +00:00
|
|
|
<button
|
|
|
|
className={`btn btn-sm btn-secondary mr-2 ${
|
2021-02-22 02:30:11 +00:00
|
|
|
this.state.previewMode && "active"
|
2020-09-06 16:15:25 +00:00
|
|
|
}`}
|
|
|
|
onClick={linkEvent(this, this.handlePreviewToggle)}
|
|
|
|
>
|
2021-02-22 02:30:11 +00:00
|
|
|
{i18n.t("preview")}
|
2020-09-06 16:15:25 +00:00
|
|
|
</button>
|
|
|
|
)}
|
|
|
|
{/* A flex expander */}
|
2022-09-22 15:03:35 +00:00
|
|
|
<div className="flex-grow-1"></div>
|
2022-12-19 15:57:29 +00:00
|
|
|
|
|
|
|
{this.props.showLanguage && (
|
|
|
|
<LanguageSelect
|
|
|
|
iconVersion
|
|
|
|
allLanguages={this.props.allLanguages}
|
2023-01-04 16:56:24 +00:00
|
|
|
selectedLanguageIds={
|
|
|
|
languageId ? Array.of(languageId) : undefined
|
|
|
|
}
|
2022-12-19 15:57:29 +00:00
|
|
|
siteLanguages={this.props.siteLanguages}
|
|
|
|
multiple={false}
|
|
|
|
onChange={this.handleLanguageChange}
|
|
|
|
/>
|
|
|
|
)}
|
2020-09-06 16:15:25 +00:00
|
|
|
<button
|
2022-09-22 15:03:35 +00:00
|
|
|
className="btn btn-sm text-muted"
|
2021-02-22 02:30:11 +00:00
|
|
|
data-tippy-content={i18n.t("bold")}
|
|
|
|
aria-label={i18n.t("bold")}
|
2020-09-06 16:15:25 +00:00
|
|
|
onClick={linkEvent(this, this.handleInsertBold)}
|
|
|
|
>
|
2021-02-11 20:35:27 +00:00
|
|
|
<Icon icon="bold" classes="icon-inline" />
|
2020-09-06 16:15:25 +00:00
|
|
|
</button>
|
|
|
|
<button
|
2022-09-22 15:03:35 +00:00
|
|
|
className="btn btn-sm text-muted"
|
2021-02-22 02:30:11 +00:00
|
|
|
data-tippy-content={i18n.t("italic")}
|
|
|
|
aria-label={i18n.t("italic")}
|
2020-09-06 16:15:25 +00:00
|
|
|
onClick={linkEvent(this, this.handleInsertItalic)}
|
|
|
|
>
|
2021-02-11 20:35:27 +00:00
|
|
|
<Icon icon="italic" classes="icon-inline" />
|
2020-09-06 16:15:25 +00:00
|
|
|
</button>
|
|
|
|
<button
|
2022-09-22 15:03:35 +00:00
|
|
|
className="btn btn-sm text-muted"
|
2021-02-22 02:30:11 +00:00
|
|
|
data-tippy-content={i18n.t("link")}
|
|
|
|
aria-label={i18n.t("link")}
|
2020-09-06 16:15:25 +00:00
|
|
|
onClick={linkEvent(this, this.handleInsertLink)}
|
|
|
|
>
|
2021-02-11 20:35:27 +00:00
|
|
|
<Icon icon="link" classes="icon-inline" />
|
2020-09-06 16:15:25 +00:00
|
|
|
</button>
|
2022-09-22 15:03:35 +00:00
|
|
|
<form className="btn btn-sm text-muted font-weight-bold">
|
2020-09-06 16:15:25 +00:00
|
|
|
<label
|
|
|
|
htmlFor={`file-upload-${this.id}`}
|
2021-03-15 18:09:31 +00:00
|
|
|
className={`mb-0 ${
|
2023-01-04 16:56:24 +00:00
|
|
|
UserService.Instance.myUserInfo && "pointer"
|
2021-03-15 18:09:31 +00:00
|
|
|
}`}
|
2021-02-22 02:30:11 +00:00
|
|
|
data-tippy-content={i18n.t("upload_image")}
|
2020-09-06 16:15:25 +00:00
|
|
|
>
|
|
|
|
{this.state.imageLoading ? (
|
2021-02-11 20:35:27 +00:00
|
|
|
<Spinner />
|
2020-09-06 16:15:25 +00:00
|
|
|
) : (
|
2021-02-11 20:35:27 +00:00
|
|
|
<Icon icon="image" classes="icon-inline" />
|
2020-09-06 16:15:25 +00:00
|
|
|
)}
|
|
|
|
</label>
|
|
|
|
<input
|
|
|
|
id={`file-upload-${this.id}`}
|
|
|
|
type="file"
|
|
|
|
accept="image/*,video/*"
|
|
|
|
name="file"
|
2022-09-22 15:03:35 +00:00
|
|
|
className="d-none"
|
2023-01-04 16:56:24 +00:00
|
|
|
disabled={!UserService.Instance.myUserInfo}
|
2020-09-06 16:15:25 +00:00
|
|
|
onChange={linkEvent(this, this.handleImageUpload)}
|
|
|
|
/>
|
|
|
|
</form>
|
|
|
|
<button
|
2022-09-22 15:03:35 +00:00
|
|
|
className="btn btn-sm text-muted"
|
2021-02-22 02:30:11 +00:00
|
|
|
data-tippy-content={i18n.t("header")}
|
|
|
|
aria-label={i18n.t("header")}
|
2020-09-06 16:15:25 +00:00
|
|
|
onClick={linkEvent(this, this.handleInsertHeader)}
|
|
|
|
>
|
2021-02-11 20:35:27 +00:00
|
|
|
<Icon icon="header" classes="icon-inline" />
|
2020-09-06 16:15:25 +00:00
|
|
|
</button>
|
|
|
|
<button
|
2022-09-22 15:03:35 +00:00
|
|
|
className="btn btn-sm text-muted"
|
2021-02-22 02:30:11 +00:00
|
|
|
data-tippy-content={i18n.t("strikethrough")}
|
|
|
|
aria-label={i18n.t("strikethrough")}
|
2020-09-06 16:15:25 +00:00
|
|
|
onClick={linkEvent(this, this.handleInsertStrikethrough)}
|
|
|
|
>
|
2021-02-11 20:35:27 +00:00
|
|
|
<Icon icon="strikethrough" classes="icon-inline" />
|
2020-09-06 16:15:25 +00:00
|
|
|
</button>
|
|
|
|
<button
|
2022-09-22 15:03:35 +00:00
|
|
|
className="btn btn-sm text-muted"
|
2021-02-22 02:30:11 +00:00
|
|
|
data-tippy-content={i18n.t("quote")}
|
|
|
|
aria-label={i18n.t("quote")}
|
2020-09-06 16:15:25 +00:00
|
|
|
onClick={linkEvent(this, this.handleInsertQuote)}
|
|
|
|
>
|
2021-02-11 20:35:27 +00:00
|
|
|
<Icon icon="format_quote" classes="icon-inline" />
|
2020-09-06 16:15:25 +00:00
|
|
|
</button>
|
|
|
|
<button
|
2022-09-22 15:03:35 +00:00
|
|
|
className="btn btn-sm text-muted"
|
2021-02-22 02:30:11 +00:00
|
|
|
data-tippy-content={i18n.t("list")}
|
|
|
|
aria-label={i18n.t("list")}
|
2020-09-06 16:15:25 +00:00
|
|
|
onClick={linkEvent(this, this.handleInsertList)}
|
|
|
|
>
|
2021-02-11 20:35:27 +00:00
|
|
|
<Icon icon="list" classes="icon-inline" />
|
2020-09-06 16:15:25 +00:00
|
|
|
</button>
|
|
|
|
<button
|
2022-09-22 15:03:35 +00:00
|
|
|
className="btn btn-sm text-muted"
|
2021-02-22 02:30:11 +00:00
|
|
|
data-tippy-content={i18n.t("code")}
|
|
|
|
aria-label={i18n.t("code")}
|
2020-09-06 16:15:25 +00:00
|
|
|
onClick={linkEvent(this, this.handleInsertCode)}
|
|
|
|
>
|
2021-02-11 20:35:27 +00:00
|
|
|
<Icon icon="code" classes="icon-inline" />
|
2020-09-06 16:15:25 +00:00
|
|
|
</button>
|
|
|
|
<button
|
2022-09-22 15:03:35 +00:00
|
|
|
className="btn btn-sm text-muted"
|
2021-02-22 02:30:11 +00:00
|
|
|
data-tippy-content={i18n.t("subscript")}
|
|
|
|
aria-label={i18n.t("subscript")}
|
2020-09-06 16:15:25 +00:00
|
|
|
onClick={linkEvent(this, this.handleInsertSubscript)}
|
|
|
|
>
|
2021-02-11 20:35:27 +00:00
|
|
|
<Icon icon="subscript" classes="icon-inline" />
|
2020-09-06 16:15:25 +00:00
|
|
|
</button>
|
|
|
|
<button
|
2022-09-22 15:03:35 +00:00
|
|
|
className="btn btn-sm text-muted"
|
2021-02-22 02:30:11 +00:00
|
|
|
data-tippy-content={i18n.t("superscript")}
|
|
|
|
aria-label={i18n.t("superscript")}
|
2020-09-06 16:15:25 +00:00
|
|
|
onClick={linkEvent(this, this.handleInsertSuperscript)}
|
|
|
|
>
|
2021-02-11 20:35:27 +00:00
|
|
|
<Icon icon="superscript" classes="icon-inline" />
|
2020-09-06 16:15:25 +00:00
|
|
|
</button>
|
|
|
|
<button
|
2022-09-22 15:03:35 +00:00
|
|
|
className="btn btn-sm text-muted"
|
2021-02-22 02:30:11 +00:00
|
|
|
data-tippy-content={i18n.t("spoiler")}
|
|
|
|
aria-label={i18n.t("spoiler")}
|
2020-09-06 16:15:25 +00:00
|
|
|
onClick={linkEvent(this, this.handleInsertSpoiler)}
|
|
|
|
>
|
2021-02-11 20:35:27 +00:00
|
|
|
<Icon icon="alert-triangle" classes="icon-inline" />
|
2020-09-06 16:15:25 +00:00
|
|
|
</button>
|
|
|
|
<a
|
|
|
|
href={markdownHelpUrl}
|
2022-09-22 15:03:35 +00:00
|
|
|
className="btn btn-sm text-muted font-weight-bold"
|
2021-02-22 02:30:11 +00:00
|
|
|
title={i18n.t("formatting_help")}
|
2022-02-24 15:31:44 +00:00
|
|
|
rel={relTags}
|
2020-09-06 16:15:25 +00:00
|
|
|
>
|
2021-02-11 20:35:27 +00:00
|
|
|
<Icon icon="help-circle" classes="icon-inline" />
|
2020-09-06 16:15:25 +00:00
|
|
|
</a>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</form>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleImageUploadPaste(i: MarkdownTextArea, event: any) {
|
|
|
|
let image = event.clipboardData.files[0];
|
|
|
|
if (image) {
|
|
|
|
i.handleImageUpload(i, image);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
handleImageUpload(i: MarkdownTextArea, event: any) {
|
|
|
|
let file: any;
|
|
|
|
if (event.target) {
|
|
|
|
event.preventDefault();
|
|
|
|
file = event.target.files[0];
|
|
|
|
} else {
|
|
|
|
file = event;
|
|
|
|
}
|
|
|
|
|
|
|
|
const formData = new FormData();
|
2021-02-22 02:30:11 +00:00
|
|
|
formData.append("images[]", file);
|
2020-09-06 16:15:25 +00:00
|
|
|
|
2022-09-22 15:03:35 +00:00
|
|
|
i.setState({ imageLoading: true });
|
2020-09-06 16:15:25 +00:00
|
|
|
|
2020-09-11 03:19:43 +00:00
|
|
|
fetch(pictrsUri, {
|
2021-02-22 02:30:11 +00:00
|
|
|
method: "POST",
|
2020-09-06 16:15:25 +00:00
|
|
|
body: formData,
|
|
|
|
})
|
|
|
|
.then(res => res.json())
|
|
|
|
.then(res => {
|
2021-02-22 02:30:11 +00:00
|
|
|
console.log("pictrs upload:");
|
2020-09-06 16:15:25 +00:00
|
|
|
console.log(res);
|
2021-02-22 02:30:11 +00:00
|
|
|
if (res.msg == "ok") {
|
2020-09-06 16:15:25 +00:00
|
|
|
let hash = res.files[0].file;
|
2020-09-11 03:19:43 +00:00
|
|
|
let url = `${pictrsUri}/${hash}`;
|
2020-09-06 16:15:25 +00:00
|
|
|
let deleteToken = res.files[0].delete_token;
|
2020-09-11 03:19:43 +00:00
|
|
|
let deleteUrl = `${pictrsUri}/delete/${deleteToken}/${hash}`;
|
2020-09-06 16:15:25 +00:00
|
|
|
let imageMarkdown = `![](${url})`;
|
2023-01-04 16:56:24 +00:00
|
|
|
let content = i.state.content;
|
2022-09-22 15:03:35 +00:00
|
|
|
i.setState({
|
2023-01-04 16:56:24 +00:00
|
|
|
content: content ? `${content}\n${imageMarkdown}` : imageMarkdown,
|
2022-09-22 15:03:35 +00:00
|
|
|
imageLoading: false,
|
|
|
|
});
|
2021-02-22 02:30:11 +00:00
|
|
|
i.contentChange();
|
2020-09-06 16:15:25 +00:00
|
|
|
let textarea: any = document.getElementById(i.id);
|
|
|
|
autosize.update(textarea);
|
|
|
|
pictrsDeleteToast(
|
2022-10-21 18:14:28 +00:00
|
|
|
`${i18n.t("click_to_delete_picture")}: ${file.name}`,
|
|
|
|
`${i18n.t("picture_deleted")}: ${file.name}`,
|
|
|
|
`${i18n.t("failed_to_delete_picture")}: ${file.name}`,
|
2020-09-06 16:15:25 +00:00
|
|
|
deleteUrl
|
|
|
|
);
|
|
|
|
} else {
|
2022-09-22 15:03:35 +00:00
|
|
|
i.setState({ imageLoading: false });
|
2021-02-22 02:30:11 +00:00
|
|
|
toast(JSON.stringify(res), "danger");
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(error => {
|
2022-09-22 15:03:35 +00:00
|
|
|
i.setState({ imageLoading: false });
|
2021-12-02 16:46:32 +00:00
|
|
|
console.error(error);
|
2021-02-22 02:30:11 +00:00
|
|
|
toast(error, "danger");
|
2020-09-06 16:15:25 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-02-22 02:30:11 +00:00
|
|
|
contentChange() {
|
2023-02-17 02:10:03 +00:00
|
|
|
// Coerces the undefineds to empty strings, for replacing in the DB
|
|
|
|
let content = this.state.content ?? "";
|
|
|
|
this.props.onContentChange?.(content);
|
2021-02-22 02:30:11 +00:00
|
|
|
}
|
|
|
|
|
2020-09-06 16:15:25 +00:00
|
|
|
handleContentChange(i: MarkdownTextArea, event: any) {
|
2023-01-04 16:56:24 +00:00
|
|
|
i.setState({ content: event.target.value });
|
2021-02-22 02:30:11 +00:00
|
|
|
i.contentChange();
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
handlePreviewToggle(i: MarkdownTextArea, event: any) {
|
|
|
|
event.preventDefault();
|
2022-09-22 15:03:35 +00:00
|
|
|
i.setState({ previewMode: !i.state.previewMode });
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
2022-09-22 15:14:58 +00:00
|
|
|
handleLanguageChange(val: number[]) {
|
2023-01-04 16:56:24 +00:00
|
|
|
this.setState({ languageId: val[0] });
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
handleSubmit(i: MarkdownTextArea, event: any) {
|
|
|
|
event.preventDefault();
|
2022-09-22 15:03:35 +00:00
|
|
|
i.setState({ loading: true });
|
2022-09-22 15:14:58 +00:00
|
|
|
let msg = {
|
|
|
|
val: i.state.content,
|
|
|
|
formId: i.formId,
|
|
|
|
languageId: i.state.languageId,
|
|
|
|
};
|
2023-01-04 16:56:24 +00:00
|
|
|
i.props.onSubmit?.(msg);
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
handleReplyCancel(i: MarkdownTextArea) {
|
2023-01-04 16:56:24 +00:00
|
|
|
i.props.onReplyCancel?.();
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
handleInsertLink(i: MarkdownTextArea, event: any) {
|
|
|
|
event.preventDefault();
|
2022-06-21 21:42:29 +00:00
|
|
|
|
2020-09-06 16:15:25 +00:00
|
|
|
let textarea: any = document.getElementById(i.id);
|
|
|
|
let start: number = textarea.selectionStart;
|
|
|
|
let end: number = textarea.selectionEnd;
|
|
|
|
|
2023-01-04 16:56:24 +00:00
|
|
|
let content = i.state.content;
|
2022-06-21 21:42:29 +00:00
|
|
|
|
2023-01-04 16:56:24 +00:00
|
|
|
if (!i.state.content) {
|
|
|
|
i.setState({ content: "" });
|
|
|
|
}
|
2022-06-21 21:42:29 +00:00
|
|
|
|
2020-09-06 16:15:25 +00:00
|
|
|
if (start !== end) {
|
2023-01-04 16:56:24 +00:00
|
|
|
let selectedText = content?.substring(start, end);
|
2022-09-22 15:03:35 +00:00
|
|
|
i.setState({
|
2023-01-04 16:56:24 +00:00
|
|
|
content: `${content?.substring(
|
|
|
|
0,
|
|
|
|
start
|
|
|
|
)}[${selectedText}]()${content?.substring(end)}`,
|
2022-09-22 15:03:35 +00:00
|
|
|
});
|
2020-09-06 16:15:25 +00:00
|
|
|
textarea.focus();
|
2021-02-02 16:45:38 +00:00
|
|
|
setTimeout(() => (textarea.selectionEnd = end + 3), 10);
|
2020-09-06 16:15:25 +00:00
|
|
|
} else {
|
2023-01-04 16:56:24 +00:00
|
|
|
i.setState({ content: `${content} []()` });
|
2020-09-06 16:15:25 +00:00
|
|
|
textarea.focus();
|
2021-02-02 16:45:38 +00:00
|
|
|
setTimeout(() => (textarea.selectionEnd -= 1), 10);
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
2021-02-22 02:30:11 +00:00
|
|
|
i.contentChange();
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
simpleSurround(chars: string) {
|
|
|
|
this.simpleSurroundBeforeAfter(chars, chars);
|
|
|
|
}
|
|
|
|
|
2021-01-24 01:28:37 +00:00
|
|
|
simpleBeginningofLine(chars: string) {
|
2022-06-21 21:42:29 +00:00
|
|
|
this.simpleSurroundBeforeAfter(`${chars}`, "", "");
|
2021-01-24 01:28:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
simpleSurroundBeforeAfter(
|
|
|
|
beforeChars: string,
|
|
|
|
afterChars: string,
|
2021-02-22 02:30:11 +00:00
|
|
|
emptyChars = "___"
|
2021-01-24 01:28:37 +00:00
|
|
|
) {
|
2023-01-04 16:56:24 +00:00
|
|
|
let content = this.state.content;
|
|
|
|
if (!this.state.content) {
|
|
|
|
this.setState({ content: "" });
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
let textarea: any = document.getElementById(this.id);
|
|
|
|
let start: number = textarea.selectionStart;
|
|
|
|
let end: number = textarea.selectionEnd;
|
|
|
|
|
|
|
|
if (start !== end) {
|
2023-01-04 16:56:24 +00:00
|
|
|
let selectedText = content?.substring(start, end);
|
2022-09-22 15:03:35 +00:00
|
|
|
this.setState({
|
2023-01-04 16:56:24 +00:00
|
|
|
content: `${content?.substring(
|
|
|
|
0,
|
|
|
|
start
|
|
|
|
)}${beforeChars}${selectedText}${afterChars}${content?.substring(end)}`,
|
2022-09-22 15:03:35 +00:00
|
|
|
});
|
2020-09-06 16:15:25 +00:00
|
|
|
} else {
|
2022-09-22 15:03:35 +00:00
|
|
|
this.setState({
|
2023-01-04 16:56:24 +00:00
|
|
|
content: `${content}${beforeChars}${emptyChars}${afterChars}`,
|
2022-09-22 15:03:35 +00:00
|
|
|
});
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
2021-02-22 02:30:11 +00:00
|
|
|
this.contentChange();
|
2022-02-14 19:43:07 +00:00
|
|
|
|
|
|
|
textarea.focus();
|
|
|
|
|
|
|
|
if (start !== end) {
|
|
|
|
textarea.setSelectionRange(
|
|
|
|
start + beforeChars.length,
|
|
|
|
end + afterChars.length
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
textarea.setSelectionRange(
|
|
|
|
start + beforeChars.length,
|
|
|
|
end + emptyChars.length + afterChars.length
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-09-06 16:15:25 +00:00
|
|
|
setTimeout(() => {
|
|
|
|
autosize.update(textarea);
|
|
|
|
}, 10);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleInsertBold(i: MarkdownTextArea, event: any) {
|
|
|
|
event.preventDefault();
|
2021-02-22 02:30:11 +00:00
|
|
|
i.simpleSurround("**");
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
handleInsertItalic(i: MarkdownTextArea, event: any) {
|
|
|
|
event.preventDefault();
|
2021-02-22 02:30:11 +00:00
|
|
|
i.simpleSurround("*");
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
handleInsertCode(i: MarkdownTextArea, event: any) {
|
|
|
|
event.preventDefault();
|
2021-07-17 20:42:55 +00:00
|
|
|
if (i.getSelectedText().split(/\r*\n/).length > 1) {
|
2021-06-24 19:05:11 +00:00
|
|
|
i.simpleSurroundBeforeAfter("```\n", "\n```");
|
|
|
|
} else {
|
2021-07-17 20:42:55 +00:00
|
|
|
i.simpleSurround("`");
|
2021-06-24 19:05:11 +00:00
|
|
|
}
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
handleInsertStrikethrough(i: MarkdownTextArea, event: any) {
|
|
|
|
event.preventDefault();
|
2021-02-22 02:30:11 +00:00
|
|
|
i.simpleSurround("~~");
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
handleInsertList(i: MarkdownTextArea, event: any) {
|
|
|
|
event.preventDefault();
|
2021-02-22 02:30:11 +00:00
|
|
|
i.simpleBeginningofLine("-");
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
handleInsertQuote(i: MarkdownTextArea, event: any) {
|
|
|
|
event.preventDefault();
|
2021-02-22 02:30:11 +00:00
|
|
|
i.simpleBeginningofLine(">");
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
handleInsertHeader(i: MarkdownTextArea, event: any) {
|
|
|
|
event.preventDefault();
|
2021-02-22 02:30:11 +00:00
|
|
|
i.simpleBeginningofLine("#");
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
handleInsertSubscript(i: MarkdownTextArea, event: any) {
|
|
|
|
event.preventDefault();
|
2021-02-22 02:30:11 +00:00
|
|
|
i.simpleSurround("~");
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
handleInsertSuperscript(i: MarkdownTextArea, event: any) {
|
|
|
|
event.preventDefault();
|
2021-02-22 02:30:11 +00:00
|
|
|
i.simpleSurround("^");
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
simpleInsert(chars: string) {
|
2023-01-04 16:56:24 +00:00
|
|
|
let content = this.state.content;
|
|
|
|
if (!content) {
|
|
|
|
this.setState({ content: `${chars} ` });
|
2020-09-06 16:15:25 +00:00
|
|
|
} else {
|
2022-09-22 15:03:35 +00:00
|
|
|
this.setState({
|
2023-01-04 16:56:24 +00:00
|
|
|
content: `${content}\n${chars} `,
|
2022-09-22 15:03:35 +00:00
|
|
|
});
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let textarea: any = document.getElementById(this.id);
|
|
|
|
textarea.focus();
|
|
|
|
setTimeout(() => {
|
|
|
|
autosize.update(textarea);
|
|
|
|
}, 10);
|
2021-02-22 02:30:11 +00:00
|
|
|
this.contentChange();
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
handleInsertSpoiler(i: MarkdownTextArea, event: any) {
|
|
|
|
event.preventDefault();
|
2021-02-22 02:30:11 +00:00
|
|
|
let beforeChars = `\n::: spoiler ${i18n.t("spoiler")}\n`;
|
|
|
|
let afterChars = "\n:::\n";
|
2020-09-06 16:15:25 +00:00
|
|
|
i.simpleSurroundBeforeAfter(beforeChars, afterChars);
|
|
|
|
}
|
|
|
|
|
|
|
|
quoteInsert() {
|
|
|
|
let textarea: any = document.getElementById(this.id);
|
2023-01-04 16:56:24 +00:00
|
|
|
let selectedText = window.getSelection()?.toString();
|
|
|
|
let content = this.state.content;
|
2020-09-06 16:15:25 +00:00
|
|
|
if (selectedText) {
|
|
|
|
let quotedText =
|
|
|
|
selectedText
|
2021-02-22 02:30:11 +00:00
|
|
|
.split("\n")
|
2020-09-06 16:15:25 +00:00
|
|
|
.map(t => `> ${t}`)
|
2021-02-22 02:30:11 +00:00
|
|
|
.join("\n") + "\n\n";
|
2023-01-04 16:56:24 +00:00
|
|
|
if (!content) {
|
|
|
|
this.setState({ content: "" });
|
2021-01-23 23:48:20 +00:00
|
|
|
} else {
|
2023-01-04 16:56:24 +00:00
|
|
|
this.setState({ content: `${content}\n` });
|
2021-01-23 23:48:20 +00:00
|
|
|
}
|
2022-09-22 15:03:35 +00:00
|
|
|
this.setState({
|
2023-01-04 16:56:24 +00:00
|
|
|
content: `${content}${quotedText}`,
|
2022-09-22 15:03:35 +00:00
|
|
|
});
|
2021-02-22 02:30:11 +00:00
|
|
|
this.contentChange();
|
2020-09-06 16:15:25 +00:00
|
|
|
// Not sure why this needs a delay
|
|
|
|
setTimeout(() => autosize.update(textarea), 10);
|
|
|
|
}
|
|
|
|
}
|
2021-06-24 19:05:11 +00:00
|
|
|
|
|
|
|
getSelectedText(): string {
|
|
|
|
let textarea: any = document.getElementById(this.id);
|
|
|
|
let start: number = textarea.selectionStart;
|
|
|
|
let end: number = textarea.selectionEnd;
|
2023-01-04 16:56:24 +00:00
|
|
|
return start !== end ? this.state.content?.substring(start, end) ?? "" : "";
|
2021-06-24 19:05:11 +00:00
|
|
|
}
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|