2021-07-17 20:42:55 +00:00
|
|
|
import autosize from "autosize";
|
2023-04-04 12:40:00 +00:00
|
|
|
import { NoOptionI18nKeys } from "i18next";
|
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 { i18n } from "../../i18next";
|
|
|
|
import { UserService } from "../../services";
|
2020-09-06 16:15:25 +00:00
|
|
|
import {
|
2023-04-04 12:40:00 +00:00
|
|
|
concurrentImageUpload,
|
2022-11-27 18:02:32 +00:00
|
|
|
customEmojisLookup,
|
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,
|
2023-04-04 12:40:00 +00:00
|
|
|
maxUploadImages,
|
2021-07-17 20:42:55 +00:00
|
|
|
mdToHtml,
|
2023-04-04 12:40:00 +00:00
|
|
|
numToSI,
|
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,
|
2023-03-27 02:06:42 +00:00
|
|
|
uploadImage,
|
2021-07-17 20:42:55 +00:00
|
|
|
} from "../../utils";
|
2022-11-27 18:02:32 +00:00
|
|
|
import { EmojiPicker } from "./emoji-picker";
|
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";
|
2023-04-04 12:40:00 +00:00
|
|
|
import ProgressBar from "./progress-bar";
|
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
|
|
|
}
|
|
|
|
|
2023-04-04 12:40:00 +00:00
|
|
|
interface ImageUploadStatus {
|
|
|
|
total: number;
|
|
|
|
uploaded: number;
|
|
|
|
}
|
|
|
|
|
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;
|
2023-04-04 12:40:00 +00:00
|
|
|
imageUploadStatus?: ImageUploadStatus;
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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-04-04 12:40:00 +00:00
|
|
|
|
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,
|
|
|
|
};
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-04-04 12:40:00 +00:00
|
|
|
const textarea: any = document.getElementById(this.id);
|
|
|
|
const form: any = document.getElementById(this.formId);
|
2020-09-06 16:15:25 +00:00
|
|
|
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
|
2023-04-04 12:40:00 +00:00
|
|
|
disabled={this.isDisabled}
|
2020-09-06 16:15:25 +00:00
|
|
|
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)}
|
|
|
|
/>
|
|
|
|
)}
|
2023-04-04 12:40:00 +00:00
|
|
|
{this.state.imageUploadStatus &&
|
|
|
|
this.state.imageUploadStatus.total > 1 && (
|
|
|
|
<ProgressBar
|
|
|
|
className="mt-2"
|
|
|
|
striped
|
|
|
|
animated
|
|
|
|
value={this.state.imageUploadStatus.uploaded}
|
|
|
|
max={this.state.imageUploadStatus.total}
|
|
|
|
text={i18n.t("pictures_uploded_progess", {
|
|
|
|
uploaded: this.state.imageUploadStatus.uploaded,
|
|
|
|
total: this.state.imageUploadStatus.total,
|
|
|
|
})}
|
|
|
|
/>
|
|
|
|
)}
|
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"
|
2023-04-04 12:40:00 +00:00
|
|
|
disabled={this.isDisabled}
|
2023-01-04 16:56:24 +00:00
|
|
|
>
|
|
|
|
{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}
|
|
|
|
onChange={this.handleLanguageChange}
|
2023-04-04 12:40:00 +00:00
|
|
|
disabled={this.isDisabled}
|
2022-12-19 15:57:29 +00:00
|
|
|
/>
|
|
|
|
)}
|
2023-04-04 12:40:00 +00:00
|
|
|
{this.getFormatButton("bold", this.handleInsertBold)}
|
|
|
|
{this.getFormatButton("italic", this.handleInsertItalic)}
|
|
|
|
{this.getFormatButton("link", this.handleInsertLink)}
|
2023-03-27 16:49:46 +00:00
|
|
|
<EmojiPicker
|
|
|
|
onEmojiClick={e => this.handleEmoji(this, e)}
|
2023-04-04 12:40:00 +00:00
|
|
|
disabled={this.isDisabled}
|
2023-03-27 16:49:46 +00:00
|
|
|
></EmojiPicker>
|
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
|
|
|
>
|
2023-04-04 12:40:00 +00:00
|
|
|
{this.state.imageUploadStatus ? (
|
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-04-04 12:40:00 +00:00
|
|
|
multiple
|
|
|
|
disabled={!UserService.Instance.myUserInfo || this.isDisabled}
|
2020-09-06 16:15:25 +00:00
|
|
|
onChange={linkEvent(this, this.handleImageUpload)}
|
|
|
|
/>
|
|
|
|
</form>
|
2023-04-04 12:40:00 +00:00
|
|
|
{this.getFormatButton("header", this.handleInsertHeader)}
|
|
|
|
{this.getFormatButton(
|
|
|
|
"strikethrough",
|
|
|
|
this.handleInsertStrikethrough
|
|
|
|
)}
|
|
|
|
{this.getFormatButton("quote", this.handleInsertQuote)}
|
|
|
|
{this.getFormatButton("list", this.handleInsertList)}
|
|
|
|
{this.getFormatButton("code", this.handleInsertCode)}
|
|
|
|
{this.getFormatButton("subscript", this.handleInsertSubscript)}
|
|
|
|
{this.getFormatButton("superscript", this.handleInsertSuperscript)}
|
|
|
|
{this.getFormatButton("spoiler", this.handleInsertSpoiler)}
|
2020-09-06 16:15:25 +00:00
|
|
|
<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>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-04-04 12:40:00 +00:00
|
|
|
getFormatButton(
|
|
|
|
type: NoOptionI18nKeys,
|
|
|
|
handleClick: (i: MarkdownTextArea, event: any) => void
|
|
|
|
) {
|
|
|
|
let iconType: string;
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case "spoiler": {
|
|
|
|
iconType = "alert-triangle";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "quote": {
|
|
|
|
iconType = "format_quote";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
iconType = type;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<button
|
|
|
|
className="btn btn-sm text-muted"
|
|
|
|
data-tippy-content={i18n.t(type)}
|
|
|
|
aria-label={i18n.t(type)}
|
|
|
|
onClick={linkEvent(this, handleClick)}
|
|
|
|
disabled={this.isDisabled}
|
|
|
|
>
|
|
|
|
<Icon icon={iconType} classes="icon-inline" />
|
|
|
|
</button>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-11-27 18:02:32 +00:00
|
|
|
handleEmoji(i: MarkdownTextArea, e: any) {
|
|
|
|
let value = e.native;
|
2023-03-27 16:49:46 +00:00
|
|
|
if (value == null) {
|
2022-11-27 18:02:32 +00:00
|
|
|
let emoji = customEmojisLookup.get(e.id)?.custom_emoji;
|
2023-03-27 16:49:46 +00:00
|
|
|
if (emoji) {
|
2022-11-27 18:02:32 +00:00
|
|
|
value = `![${emoji.alt_text}](${emoji.image_url} "${emoji.shortcode}")`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
i.setState({
|
|
|
|
content: `${i.state.content ?? ""} ${value} `,
|
|
|
|
});
|
|
|
|
i.contentChange();
|
|
|
|
let textarea: any = document.getElementById(i.id);
|
|
|
|
autosize.update(textarea);
|
|
|
|
}
|
|
|
|
|
2020-09-06 16:15:25 +00:00
|
|
|
handleImageUploadPaste(i: MarkdownTextArea, event: any) {
|
2023-04-04 12:40:00 +00:00
|
|
|
const image = event.clipboardData.files[0];
|
2020-09-06 16:15:25 +00:00
|
|
|
if (image) {
|
|
|
|
i.handleImageUpload(i, image);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
handleImageUpload(i: MarkdownTextArea, event: any) {
|
2023-04-04 12:40:00 +00:00
|
|
|
const files: File[] = [];
|
2020-09-06 16:15:25 +00:00
|
|
|
if (event.target) {
|
|
|
|
event.preventDefault();
|
2023-04-04 12:40:00 +00:00
|
|
|
files.push(...event.target.files);
|
2020-09-06 16:15:25 +00:00
|
|
|
} else {
|
2023-04-04 12:40:00 +00:00
|
|
|
files.push(event);
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
2023-04-04 12:40:00 +00:00
|
|
|
if (files.length > maxUploadImages) {
|
|
|
|
toast(
|
|
|
|
i18n.t("too_many_images_upload", {
|
2023-05-11 18:32:32 +00:00
|
|
|
count: Number(maxUploadImages),
|
2023-04-04 12:40:00 +00:00
|
|
|
formattedCount: numToSI(maxUploadImages),
|
|
|
|
}),
|
|
|
|
"danger"
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
i.setState({
|
|
|
|
imageUploadStatus: { total: files.length, uploaded: 0 },
|
2020-09-06 16:15:25 +00:00
|
|
|
});
|
2023-04-04 12:40:00 +00:00
|
|
|
|
|
|
|
i.uploadImages(i, files).then(() => {
|
|
|
|
i.setState({ imageUploadStatus: undefined });
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async uploadImages(i: MarkdownTextArea, files: File[]) {
|
|
|
|
let errorOccurred = false;
|
|
|
|
const filesCopy = [...files];
|
|
|
|
while (filesCopy.length > 0 && !errorOccurred) {
|
|
|
|
try {
|
|
|
|
await Promise.all(
|
|
|
|
filesCopy.splice(0, concurrentImageUpload).map(async file => {
|
|
|
|
await i.uploadSingleImage(i, file);
|
|
|
|
|
|
|
|
this.setState(({ imageUploadStatus }) => ({
|
|
|
|
imageUploadStatus: {
|
|
|
|
...(imageUploadStatus as Required<ImageUploadStatus>),
|
|
|
|
uploaded: (imageUploadStatus?.uploaded ?? 0) + 1,
|
|
|
|
},
|
|
|
|
}));
|
|
|
|
})
|
|
|
|
);
|
|
|
|
} catch (e) {
|
|
|
|
errorOccurred = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async uploadSingleImage(i: MarkdownTextArea, file: File) {
|
|
|
|
try {
|
|
|
|
const res = await uploadImage(file);
|
|
|
|
console.log("pictrs upload:");
|
|
|
|
console.log(res);
|
|
|
|
if (res.msg === "ok") {
|
|
|
|
const imageMarkdown = `![](${res.url})`;
|
|
|
|
i.setState(({ content }) => ({
|
|
|
|
content: content ? `${content}\n${imageMarkdown}` : imageMarkdown,
|
|
|
|
}));
|
|
|
|
i.contentChange();
|
|
|
|
const textarea: any = document.getElementById(i.id);
|
|
|
|
autosize.update(textarea);
|
|
|
|
pictrsDeleteToast(file.name, res.delete_url as string);
|
|
|
|
} else {
|
|
|
|
throw JSON.stringify(res);
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
i.setState({ imageUploadStatus: undefined });
|
|
|
|
console.error(error);
|
|
|
|
toast(error, "danger");
|
|
|
|
|
|
|
|
throw error;
|
|
|
|
}
|
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
|
|
|
|
2023-03-27 02:10:08 +00:00
|
|
|
const textarea: any = document.getElementById(i.id);
|
|
|
|
const start: number = textarea.selectionStart;
|
|
|
|
const end: number = textarea.selectionEnd;
|
2020-09-06 16:15:25 +00:00
|
|
|
|
2023-03-27 02:10:08 +00:00
|
|
|
const 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-03-27 02:10:08 +00:00
|
|
|
const 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-03-27 02:10:08 +00:00
|
|
|
const content = this.state.content ?? "";
|
2023-01-04 16:56:24 +00:00
|
|
|
if (!this.state.content) {
|
|
|
|
this.setState({ content: "" });
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
2023-03-27 02:10:08 +00:00
|
|
|
const textarea: any = document.getElementById(this.id);
|
|
|
|
const start: number = textarea.selectionStart;
|
|
|
|
const end: number = textarea.selectionEnd;
|
2020-09-06 16:15:25 +00:00
|
|
|
|
|
|
|
if (start !== end) {
|
2023-03-27 02:10:08 +00:00
|
|
|
const 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() {
|
2023-04-04 12:40:00 +00:00
|
|
|
const textarea: any = document.getElementById(this.id);
|
|
|
|
const selectedText = window.getSelection()?.toString();
|
|
|
|
const { content } = this.state;
|
2020-09-06 16:15:25 +00:00
|
|
|
if (selectedText) {
|
2023-04-04 12:40:00 +00:00
|
|
|
const quotedText =
|
2020-09-06 16:15:25 +00:00
|
|
|
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 {
|
2023-04-04 12:40:00 +00:00
|
|
|
const { selectionStart: start, selectionEnd: end } =
|
|
|
|
document.getElementById(this.id) as any;
|
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
|
|
|
}
|
2023-04-04 12:40:00 +00:00
|
|
|
|
|
|
|
get isDisabled() {
|
|
|
|
return (
|
|
|
|
this.state.loading ||
|
|
|
|
this.props.disabled ||
|
|
|
|
!!this.state.imageUploadStatus
|
|
|
|
);
|
|
|
|
}
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|