2022-06-21 21:42:29 +00:00
|
|
|
import { None, Option, Some } from "@sniptt/monads";
|
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";
|
2022-09-22 15:14:58 +00:00
|
|
|
import { Language, toUndefined } 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,
|
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 {
|
2022-06-21 21:42:29 +00:00
|
|
|
initialContent: Option<string>;
|
2022-09-22 15:14:58 +00:00
|
|
|
initialLanguageId: Option<number>;
|
2022-06-21 21:42:29 +00:00
|
|
|
placeholder: Option<string>;
|
|
|
|
buttonTitle: Option<string>;
|
|
|
|
maxLength: Option<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;
|
2022-09-22 15:14:58 +00:00
|
|
|
onSubmit?(msg: {
|
|
|
|
val: Option<string>;
|
|
|
|
formId: string;
|
|
|
|
languageId: Option<number>;
|
|
|
|
}): any;
|
|
|
|
allLanguages: Language[];
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
interface MarkdownTextAreaState {
|
2022-06-21 21:42:29 +00:00
|
|
|
content: Option<string>;
|
2022-09-22 15:14:58 +00:00
|
|
|
languageId: Option<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;
|
2020-09-06 16:15:25 +00:00
|
|
|
private emptyState: MarkdownTextAreaState = {
|
|
|
|
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
|
|
|
this.state = this.emptyState;
|
|
|
|
}
|
|
|
|
|
|
|
|
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", () => {
|
2022-09-22 15:03:35 +00:00
|
|
|
this.setState({ content: Some(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() {
|
2022-06-21 21:42:29 +00:00
|
|
|
if (!this.props.hideNavigationWarnings && this.state.content.isSome()) {
|
2020-09-06 16:15:25 +00:00
|
|
|
window.onbeforeunload = () => true;
|
|
|
|
} else {
|
|
|
|
window.onbeforeunload = undefined;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillReceiveProps(nextProps: MarkdownTextAreaProps) {
|
|
|
|
if (nextProps.finished) {
|
2022-09-22 15:03:35 +00:00
|
|
|
this.setState({ previewMode: false, loading: false, content: None });
|
2020-09-06 16:15:25 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
window.onbeforeunload = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<form id={this.formId} onSubmit={linkEvent(this, this.handleSubmit)}>
|
|
|
|
<Prompt
|
2022-06-21 21:42:29 +00:00
|
|
|
when={
|
|
|
|
!this.props.hideNavigationWarnings && this.state.content.isSome()
|
|
|
|
}
|
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"}`}
|
2022-06-21 21:42:29 +00:00
|
|
|
value={toUndefined(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}
|
2022-06-21 21:42:29 +00:00
|
|
|
maxLength={this.props.maxLength.unwrapOr(10000)}
|
|
|
|
placeholder={toUndefined(this.props.placeholder)}
|
2020-09-06 16:15:25 +00:00
|
|
|
/>
|
2022-06-21 21:42:29 +00:00
|
|
|
{this.state.previewMode &&
|
|
|
|
this.state.content.match({
|
|
|
|
some: content => (
|
|
|
|
<div
|
|
|
|
className="card border-secondary card-body md-div"
|
|
|
|
dangerouslySetInnerHTML={mdToHtml(content)}
|
|
|
|
/>
|
|
|
|
),
|
|
|
|
none: <></>,
|
|
|
|
})}
|
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:14:58 +00:00
|
|
|
{this.props.showLanguage && (
|
|
|
|
<div className="row justify-content-end">
|
|
|
|
<div className="col-sm-8">
|
|
|
|
<LanguageSelect
|
|
|
|
allLanguages={this.props.allLanguages}
|
|
|
|
selectedLanguageIds={this.state.languageId.map(Array.of)}
|
|
|
|
multiple={false}
|
|
|
|
onChange={this.handleLanguageChange}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
)}
|
2022-09-22 15:03:35 +00:00
|
|
|
<div className="row">
|
|
|
|
<div className="col-sm-12 d-flex flex-wrap">
|
2022-06-21 21:42:29 +00:00
|
|
|
{this.props.buttonTitle.match({
|
|
|
|
some: buttonTitle => (
|
|
|
|
<button
|
|
|
|
type="submit"
|
2022-09-22 15:03:35 +00:00
|
|
|
className="btn btn-sm btn-secondary mr-2"
|
2022-06-21 21:42:29 +00:00
|
|
|
disabled={this.props.disabled || this.state.loading}
|
|
|
|
>
|
|
|
|
{this.state.loading ? (
|
|
|
|
<Spinner />
|
|
|
|
) : (
|
|
|
|
<span>{buttonTitle}</span>
|
|
|
|
)}
|
|
|
|
</button>
|
|
|
|
),
|
|
|
|
none: <></>,
|
|
|
|
})}
|
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>
|
|
|
|
)}
|
2022-06-21 21:42:29 +00:00
|
|
|
{this.state.content.isSome() && (
|
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>
|
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 ${
|
2022-06-21 21:42:29 +00:00
|
|
|
UserService.Instance.myUserInfo.isSome() && "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"
|
2022-06-21 21:42:29 +00:00
|
|
|
disabled={UserService.Instance.myUserInfo.isNone()}
|
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})`;
|
2022-09-22 15:03:35 +00:00
|
|
|
i.setState({
|
|
|
|
content: Some(
|
|
|
|
i.state.content.match({
|
|
|
|
some: content => `${content}\n${imageMarkdown}`,
|
|
|
|
none: imageMarkdown,
|
|
|
|
})
|
|
|
|
),
|
|
|
|
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-01 20:05:17 +00:00
|
|
|
i18n.t("click_to_delete_picture").concat(' (', file.name,')'),
|
|
|
|
i18n.t("picture_deleted").concat(' (', file.name,')'),
|
|
|
|
i18n.t("fail_picture_deleted").concat(' (', 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() {
|
|
|
|
if (this.props.onContentChange) {
|
2022-06-21 21:42:29 +00:00
|
|
|
this.props.onContentChange(toUndefined(this.state.content));
|
2021-02-22 02:30:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-06 16:15:25 +00:00
|
|
|
handleContentChange(i: MarkdownTextArea, event: any) {
|
2022-09-22 15:03:35 +00:00
|
|
|
i.setState({ content: Some(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[]) {
|
|
|
|
this.setState({ languageId: Some(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,
|
|
|
|
};
|
2020-09-06 16:15:25 +00:00
|
|
|
i.props.onSubmit(msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleReplyCancel(i: MarkdownTextArea) {
|
|
|
|
i.props.onReplyCancel();
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2022-06-21 21:42:29 +00:00
|
|
|
if (i.state.content.isNone()) {
|
2022-09-22 15:03:35 +00:00
|
|
|
i.setState({ content: Some("") });
|
2022-06-21 21:42:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let content = i.state.content.unwrap();
|
|
|
|
|
2020-09-06 16:15:25 +00:00
|
|
|
if (start !== end) {
|
2022-06-21 21:42:29 +00:00
|
|
|
let selectedText = content.substring(start, end);
|
2022-09-22 15:03:35 +00:00
|
|
|
i.setState({
|
|
|
|
content: Some(
|
|
|
|
`${content.substring(0, start)}[${selectedText}]()${content.substring(
|
|
|
|
end
|
|
|
|
)}`
|
|
|
|
),
|
|
|
|
});
|
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 {
|
2022-09-22 15:03:35 +00:00
|
|
|
i.setState({ content: Some(`${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
|
|
|
) {
|
2022-06-21 21:42:29 +00:00
|
|
|
if (this.state.content.isNone()) {
|
2022-09-22 15:03:35 +00:00
|
|
|
this.setState({ content: Some("") });
|
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;
|
|
|
|
|
2022-06-21 21:42:29 +00:00
|
|
|
let content = this.state.content.unwrap();
|
|
|
|
|
2020-09-06 16:15:25 +00:00
|
|
|
if (start !== end) {
|
2022-06-21 21:42:29 +00:00
|
|
|
let selectedText = content.substring(start, end);
|
2022-09-22 15:03:35 +00:00
|
|
|
this.setState({
|
|
|
|
content: Some(
|
|
|
|
`${content.substring(
|
|
|
|
0,
|
|
|
|
start
|
|
|
|
)}${beforeChars}${selectedText}${afterChars}${content.substring(end)}`
|
|
|
|
),
|
|
|
|
});
|
2020-09-06 16:15:25 +00:00
|
|
|
} else {
|
2022-09-22 15:03:35 +00:00
|
|
|
this.setState({
|
|
|
|
content: Some(`${content}${beforeChars}${emptyChars}${afterChars}`),
|
|
|
|
});
|
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) {
|
2022-06-21 21:42:29 +00:00
|
|
|
if (this.state.content.isNone()) {
|
2022-09-22 15:03:35 +00:00
|
|
|
this.setState({ content: Some(`${chars} `) });
|
2020-09-06 16:15:25 +00:00
|
|
|
} else {
|
2022-09-22 15:03:35 +00:00
|
|
|
this.setState({
|
|
|
|
content: Some(`${this.state.content.unwrap()}\n${chars} `),
|
|
|
|
});
|
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);
|
|
|
|
let selectedText = window.getSelection().toString();
|
|
|
|
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";
|
2022-06-21 21:42:29 +00:00
|
|
|
if (this.state.content.isNone()) {
|
2022-09-22 15:03:35 +00:00
|
|
|
this.setState({ content: Some("") });
|
2021-01-23 23:48:20 +00:00
|
|
|
} else {
|
2022-09-22 15:03:35 +00:00
|
|
|
this.setState({ content: Some(`${this.state.content.unwrap()}\n`) });
|
2021-01-23 23:48:20 +00:00
|
|
|
}
|
2022-09-22 15:03:35 +00:00
|
|
|
this.setState({
|
|
|
|
content: Some(`${this.state.content.unwrap()}${quotedText}`),
|
|
|
|
});
|
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;
|
2022-06-21 21:42:29 +00:00
|
|
|
return start !== end
|
|
|
|
? this.state.content.unwrap().substring(start, end)
|
|
|
|
: "";
|
2021-06-24 19:05:11 +00:00
|
|
|
}
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|