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";
|
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";
|
2020-09-06 16:15:25 +00:00
|
|
|
|
|
|
|
interface MarkdownTextAreaProps {
|
2021-12-30 15:26:45 +00:00
|
|
|
initialContent?: string;
|
2020-09-06 16:15:25 +00:00
|
|
|
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;
|
2021-06-03 15:24:45 +00:00
|
|
|
placeholder?: string;
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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()}`;
|
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,
|
|
|
|
previewMode: false,
|
|
|
|
loading: false,
|
|
|
|
imageLoading: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
constructor(props: any, context: any) {
|
|
|
|
super(props, context);
|
|
|
|
|
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", () => {
|
2020-09-06 16:15:25 +00:00
|
|
|
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;
|
2021-02-22 02:30:11 +00:00
|
|
|
this.state.content = "";
|
2020-09-06 16:15:25 +00:00
|
|
|
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 (
|
|
|
|
<form id={this.formId} onSubmit={linkEvent(this, this.handleSubmit)}>
|
|
|
|
<Prompt
|
|
|
|
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
|
|
|
/>
|
|
|
|
<div class="form-group row">
|
|
|
|
<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"}`}
|
2020-09-06 16:15:25 +00:00
|
|
|
value={this.state.content}
|
|
|
|
onInput={linkEvent(this, this.handleContentChange)}
|
|
|
|
onPaste={linkEvent(this, this.handleImageUploadPaste)}
|
|
|
|
required
|
|
|
|
disabled={this.props.disabled}
|
|
|
|
rows={2}
|
|
|
|
maxLength={this.props.maxLength || 10000}
|
2021-06-03 15:24:45 +00:00
|
|
|
placeholder={this.props.placeholder}
|
2020-09-06 16:15:25 +00:00
|
|
|
/>
|
|
|
|
{this.state.previewMode && (
|
|
|
|
<div
|
2020-09-23 23:01:45 +00:00
|
|
|
className="card border-secondary card-body md-div"
|
2020-09-06 16:15:25 +00:00
|
|
|
dangerouslySetInnerHTML={mdToHtml(this.state.content)}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</div>
|
2021-02-09 16:21:24 +00:00
|
|
|
<label class="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>
|
|
|
|
<div class="row">
|
|
|
|
<div class="col-sm-12 d-flex flex-wrap">
|
|
|
|
{this.props.buttonTitle && (
|
|
|
|
<button
|
|
|
|
type="submit"
|
|
|
|
class="btn btn-sm btn-secondary mr-2"
|
|
|
|
disabled={this.props.disabled || this.state.loading}
|
|
|
|
>
|
|
|
|
{this.state.loading ? (
|
2021-02-11 20:35:27 +00:00
|
|
|
<Spinner />
|
2020-09-06 16:15:25 +00:00
|
|
|
) : (
|
|
|
|
<span>{this.props.buttonTitle}</span>
|
|
|
|
)}
|
|
|
|
</button>
|
|
|
|
)}
|
|
|
|
{this.props.replyType && (
|
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
class="btn btn-sm btn-secondary mr-2"
|
|
|
|
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>
|
|
|
|
)}
|
|
|
|
{this.state.content && (
|
|
|
|
<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 */}
|
|
|
|
<div class="flex-grow-1"></div>
|
|
|
|
<button
|
|
|
|
class="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
|
|
|
|
class="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
|
|
|
|
class="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>
|
|
|
|
<form class="btn btn-sm text-muted font-weight-bold">
|
|
|
|
<label
|
|
|
|
htmlFor={`file-upload-${this.id}`}
|
2021-03-15 18:09:31 +00:00
|
|
|
className={`mb-0 ${
|
2021-08-20 02:56:18 +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"
|
|
|
|
class="d-none"
|
2021-08-20 02:56:18 +00:00
|
|
|
disabled={!UserService.Instance.myUserInfo}
|
2020-09-06 16:15:25 +00:00
|
|
|
onChange={linkEvent(this, this.handleImageUpload)}
|
|
|
|
/>
|
|
|
|
</form>
|
|
|
|
<button
|
|
|
|
class="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
|
|
|
|
class="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
|
|
|
|
class="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
|
|
|
|
class="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
|
|
|
|
class="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
|
|
|
|
class="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
|
|
|
|
class="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
|
|
|
|
class="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}
|
|
|
|
class="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
|
|
|
|
|
|
|
i.state.imageLoading = true;
|
|
|
|
i.setState(i.state);
|
|
|
|
|
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})`;
|
|
|
|
let content = i.state.content;
|
|
|
|
content = content ? `${content}\n${imageMarkdown}` : imageMarkdown;
|
|
|
|
i.state.content = content;
|
|
|
|
i.state.imageLoading = false;
|
2021-02-22 02:30:11 +00:00
|
|
|
i.contentChange();
|
2020-09-06 16:15:25 +00:00
|
|
|
i.setState(i.state);
|
|
|
|
let textarea: any = document.getElementById(i.id);
|
|
|
|
autosize.update(textarea);
|
|
|
|
pictrsDeleteToast(
|
2021-02-22 02:30:11 +00:00
|
|
|
i18n.t("click_to_delete_picture"),
|
|
|
|
i18n.t("picture_deleted"),
|
2020-09-06 16:15:25 +00:00
|
|
|
deleteUrl
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
i.state.imageLoading = false;
|
|
|
|
i.setState(i.state);
|
2021-02-22 02:30:11 +00:00
|
|
|
toast(JSON.stringify(res), "danger");
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
i.state.imageLoading = false;
|
|
|
|
i.setState(i.state);
|
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) {
|
|
|
|
this.props.onContentChange(this.state.content);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-06 16:15:25 +00:00
|
|
|
handleContentChange(i: MarkdownTextArea, event: any) {
|
|
|
|
i.state.content = event.target.value;
|
2021-02-22 02:30:11 +00:00
|
|
|
i.contentChange();
|
2020-09-06 16:15:25 +00:00
|
|
|
i.setState(i.state);
|
|
|
|
}
|
|
|
|
|
|
|
|
handlePreviewToggle(i: MarkdownTextArea, event: any) {
|
|
|
|
event.preventDefault();
|
|
|
|
i.state.previewMode = !i.state.previewMode;
|
|
|
|
i.setState(i.state);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleSubmit(i: MarkdownTextArea, event: any) {
|
|
|
|
event.preventDefault();
|
|
|
|
i.state.loading = true;
|
|
|
|
i.setState(i.state);
|
|
|
|
let msg = { val: i.state.content, formId: i.formId };
|
|
|
|
i.props.onSubmit(msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleReplyCancel(i: MarkdownTextArea) {
|
|
|
|
i.props.onReplyCancel();
|
|
|
|
}
|
|
|
|
|
|
|
|
handleInsertLink(i: MarkdownTextArea, event: any) {
|
|
|
|
event.preventDefault();
|
|
|
|
if (!i.state.content) {
|
2021-02-22 02:30:11 +00:00
|
|
|
i.state.content = "";
|
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;
|
|
|
|
|
|
|
|
if (start !== end) {
|
|
|
|
let selectedText = i.state.content.substring(start, end);
|
|
|
|
i.state.content = `${i.state.content.substring(
|
|
|
|
0,
|
|
|
|
start
|
2021-01-24 01:28:37 +00:00
|
|
|
)}[${selectedText}]()${i.state.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 {
|
2021-02-22 02:30:11 +00:00
|
|
|
i.state.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
|
|
|
i.setState(i.state);
|
|
|
|
}
|
|
|
|
|
|
|
|
simpleSurround(chars: string) {
|
|
|
|
this.simpleSurroundBeforeAfter(chars, chars);
|
|
|
|
}
|
|
|
|
|
2021-01-24 01:28:37 +00:00
|
|
|
simpleBeginningofLine(chars: string) {
|
2021-02-22 02:30:11 +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
|
|
|
) {
|
2020-09-06 16:15:25 +00:00
|
|
|
if (!this.state.content) {
|
2021-02-22 02:30:11 +00:00
|
|
|
this.state.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) {
|
|
|
|
let selectedText = this.state.content.substring(start, end);
|
|
|
|
this.state.content = `${this.state.content.substring(
|
|
|
|
0,
|
2021-01-24 01:28:37 +00:00
|
|
|
start
|
|
|
|
)}${beforeChars}${selectedText}${afterChars}${this.state.content.substring(
|
|
|
|
end
|
2020-09-06 16:15:25 +00:00
|
|
|
)}`;
|
|
|
|
} else {
|
2021-01-24 01:28:37 +00:00
|
|
|
this.state.content += `${beforeChars}${emptyChars}${afterChars}`;
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
2021-02-22 02:30:11 +00:00
|
|
|
this.contentChange();
|
2020-09-06 16:15:25 +00:00
|
|
|
this.setState(this.state);
|
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) {
|
|
|
|
if (!this.state.content) {
|
|
|
|
this.state.content = `${chars} `;
|
|
|
|
} else {
|
|
|
|
this.state.content += `\n${chars} `;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
this.setState(this.state);
|
|
|
|
}
|
|
|
|
|
|
|
|
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";
|
2021-01-23 23:48:20 +00:00
|
|
|
if (this.state.content == null) {
|
2021-02-22 02:30:11 +00:00
|
|
|
this.state.content = "";
|
2021-01-23 23:48:20 +00:00
|
|
|
} else {
|
2021-02-22 02:30:11 +00:00
|
|
|
this.state.content += "\n";
|
2021-01-23 23:48:20 +00:00
|
|
|
}
|
|
|
|
this.state.content += quotedText;
|
2021-02-22 02:30:11 +00:00
|
|
|
this.contentChange();
|
2020-09-06 16:15:25 +00:00
|
|
|
this.setState(this.state);
|
|
|
|
// 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;
|
2021-07-17 20:42:55 +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
|
|
|
}
|