lemmy-ui/src/shared/components/common/markdown-textarea.tsx

702 lines
20 KiB
TypeScript
Raw Normal View History

import autosize from "autosize";
import { NoOptionI18nKeys } from "i18next";
import { Component, linkEvent } from "inferno";
import { Language } from "lemmy-js-client";
import { i18n } from "../../i18next";
Use http client (#1081) * Beginning work on websocket -> http client conversion. * About 30% done. * half done. * more done. * Almost passing lint. * Passing lint, but untested. * Add back in event listeners. * Fixing some community forms. * Remove webpack cache. * fixing some more. * Fixed ISOwrappers. * A few more fixes. * Refactor utils * Fix instance add/remove buttons * Not catching errors in isoWrapper. * Wrap Http client * Fixing up tagline and ratelimit forms. * Make all http client wrapping be in one place * Reworking some more forms. * Upgrading lemmy-js-client. * Fixing verify email. * Fix linting errors * Upgrading woodpecker node. * Fix comment scrolling rerender bug. * Fixing a few things, commenting out props for now. * v0.18.0-beta.1 * Trying to fix woodpecker, 1. * Trying to fix woodpecker, 2. * Handroll prompt * Add navigation prompt to other pages * Fix prompt navigation bug * Fix prompt bug introduced from last bug fix * Fix PWA bug * Fix isoData not working * Fix search page update url * Fix sharp issue. * v0.18.0-beta.2 * Make create post pre-fetch communities * Fix bug from last commit * Fix issue of posts/comments not being switched when changing select options * Fix unnecessary fetches on home screen * Make circular icon buttons not look stupid * Prevent unnecessary fetches * Make login experience smoother * Add PWA shortcuts * Add related application to PWA * Update translations * Forgot to add post editing. * Fixing site setup. * Deploy script setup. * v0.18.0-beta.4 * Sanitize again. * Adding sanitize json function. * Upping version. * Another sanitize fix. * Upping version. * Prevent search nav item from disappearing when on search page * Allow admin and mod actions on non-local comments. * Fix mobile menu collapse bug * Completely fix prompt component * Fix undefined value checks in use_http_client_2 (#1230) * fix: filter out undefined from posts * fix: emoji initialisation passing undefined * fix: || => ?? to be more explicit * linting --------- Co-authored-by: Alex Maras <alexmaras@gmail.com> * Re-add accidentally removed state * Fix dropdown bug * Use linkEvent where appropriate * Fix navigation warnings. --------- Co-authored-by: Dessalines <tyhou13@gmx.com> Co-authored-by: Alex Maras <dev@alexmaras.com> Co-authored-by: Alex Maras <alexmaras@gmail.com>
2023-06-14 12:20:40 +00:00
import { HttpService, UserService } from "../../services";
import {
concurrentImageUpload,
2022-11-27 18:02:32 +00:00
customEmojisLookup,
markdownFieldCharacterLimit,
markdownHelpUrl,
maxUploadImages,
mdToHtml,
numToSI,
pictrsDeleteToast,
randomStr,
relTags,
setupTippy,
setupTribute,
toast,
} from "../../utils";
2023-06-16 21:25:53 +00:00
import { isBrowser } from "../../utils/browser/is-browser";
2022-11-27 18:02:32 +00:00
import { EmojiPicker } from "./emoji-picker";
import { Icon, Spinner } from "./icon";
import { LanguageSelect } from "./language-select";
import NavigationPrompt from "./navigation-prompt";
import ProgressBar from "./progress-bar";
interface MarkdownTextAreaProps {
initialContent?: string;
initialLanguageId?: number;
placeholder?: string;
buttonTitle?: string;
maxLength?: number;
replyType?: boolean;
focus?: boolean;
disabled?: boolean;
finished?: boolean;
showLanguage?: boolean;
hideNavigationWarnings?: boolean;
Use http client (#1081) * Beginning work on websocket -> http client conversion. * About 30% done. * half done. * more done. * Almost passing lint. * Passing lint, but untested. * Add back in event listeners. * Fixing some community forms. * Remove webpack cache. * fixing some more. * Fixed ISOwrappers. * A few more fixes. * Refactor utils * Fix instance add/remove buttons * Not catching errors in isoWrapper. * Wrap Http client * Fixing up tagline and ratelimit forms. * Make all http client wrapping be in one place * Reworking some more forms. * Upgrading lemmy-js-client. * Fixing verify email. * Fix linting errors * Upgrading woodpecker node. * Fix comment scrolling rerender bug. * Fixing a few things, commenting out props for now. * v0.18.0-beta.1 * Trying to fix woodpecker, 1. * Trying to fix woodpecker, 2. * Handroll prompt * Add navigation prompt to other pages * Fix prompt navigation bug * Fix prompt bug introduced from last bug fix * Fix PWA bug * Fix isoData not working * Fix search page update url * Fix sharp issue. * v0.18.0-beta.2 * Make create post pre-fetch communities * Fix bug from last commit * Fix issue of posts/comments not being switched when changing select options * Fix unnecessary fetches on home screen * Make circular icon buttons not look stupid * Prevent unnecessary fetches * Make login experience smoother * Add PWA shortcuts * Add related application to PWA * Update translations * Forgot to add post editing. * Fixing site setup. * Deploy script setup. * v0.18.0-beta.4 * Sanitize again. * Adding sanitize json function. * Upping version. * Another sanitize fix. * Upping version. * Prevent search nav item from disappearing when on search page * Allow admin and mod actions on non-local comments. * Fix mobile menu collapse bug * Completely fix prompt component * Fix undefined value checks in use_http_client_2 (#1230) * fix: filter out undefined from posts * fix: emoji initialisation passing undefined * fix: || => ?? to be more explicit * linting --------- Co-authored-by: Alex Maras <alexmaras@gmail.com> * Re-add accidentally removed state * Fix dropdown bug * Use linkEvent where appropriate * Fix navigation warnings. --------- Co-authored-by: Dessalines <tyhou13@gmx.com> Co-authored-by: Alex Maras <dev@alexmaras.com> Co-authored-by: Alex Maras <alexmaras@gmail.com>
2023-06-14 12:20:40 +00:00
onContentChange?(val: string): void;
onReplyCancel?(): void;
onSubmit?(content: string, formId: string, languageId?: number): void;
allLanguages: Language[]; // TODO should probably be nullable
siteLanguages: number[]; // TODO same
}
interface ImageUploadStatus {
total: number;
uploaded: number;
}
interface MarkdownTextAreaState {
content?: string;
languageId?: number;
previewMode: boolean;
imageUploadStatus?: ImageUploadStatus;
Use http client (#1081) * Beginning work on websocket -> http client conversion. * About 30% done. * half done. * more done. * Almost passing lint. * Passing lint, but untested. * Add back in event listeners. * Fixing some community forms. * Remove webpack cache. * fixing some more. * Fixed ISOwrappers. * A few more fixes. * Refactor utils * Fix instance add/remove buttons * Not catching errors in isoWrapper. * Wrap Http client * Fixing up tagline and ratelimit forms. * Make all http client wrapping be in one place * Reworking some more forms. * Upgrading lemmy-js-client. * Fixing verify email. * Fix linting errors * Upgrading woodpecker node. * Fix comment scrolling rerender bug. * Fixing a few things, commenting out props for now. * v0.18.0-beta.1 * Trying to fix woodpecker, 1. * Trying to fix woodpecker, 2. * Handroll prompt * Add navigation prompt to other pages * Fix prompt navigation bug * Fix prompt bug introduced from last bug fix * Fix PWA bug * Fix isoData not working * Fix search page update url * Fix sharp issue. * v0.18.0-beta.2 * Make create post pre-fetch communities * Fix bug from last commit * Fix issue of posts/comments not being switched when changing select options * Fix unnecessary fetches on home screen * Make circular icon buttons not look stupid * Prevent unnecessary fetches * Make login experience smoother * Add PWA shortcuts * Add related application to PWA * Update translations * Forgot to add post editing. * Fixing site setup. * Deploy script setup. * v0.18.0-beta.4 * Sanitize again. * Adding sanitize json function. * Upping version. * Another sanitize fix. * Upping version. * Prevent search nav item from disappearing when on search page * Allow admin and mod actions on non-local comments. * Fix mobile menu collapse bug * Completely fix prompt component * Fix undefined value checks in use_http_client_2 (#1230) * fix: filter out undefined from posts * fix: emoji initialisation passing undefined * fix: || => ?? to be more explicit * linting --------- Co-authored-by: Alex Maras <alexmaras@gmail.com> * Re-add accidentally removed state * Fix dropdown bug * Use linkEvent where appropriate * Fix navigation warnings. --------- Co-authored-by: Dessalines <tyhou13@gmx.com> Co-authored-by: Alex Maras <dev@alexmaras.com> Co-authored-by: Alex Maras <alexmaras@gmail.com>
2023-06-14 12:20:40 +00:00
loading: boolean;
submitted: boolean;
}
export class MarkdownTextArea extends Component<
MarkdownTextAreaProps,
MarkdownTextAreaState
> {
2023-06-16 17:09:35 +00:00
private id = `markdown-textarea-${randomStr()}`;
private formId = `markdown-form-${randomStr()}`;
private tribute: any;
state: MarkdownTextAreaState = {
content: this.props.initialContent,
languageId: this.props.initialLanguageId,
previewMode: false,
loading: false,
Use http client (#1081) * Beginning work on websocket -> http client conversion. * About 30% done. * half done. * more done. * Almost passing lint. * Passing lint, but untested. * Add back in event listeners. * Fixing some community forms. * Remove webpack cache. * fixing some more. * Fixed ISOwrappers. * A few more fixes. * Refactor utils * Fix instance add/remove buttons * Not catching errors in isoWrapper. * Wrap Http client * Fixing up tagline and ratelimit forms. * Make all http client wrapping be in one place * Reworking some more forms. * Upgrading lemmy-js-client. * Fixing verify email. * Fix linting errors * Upgrading woodpecker node. * Fix comment scrolling rerender bug. * Fixing a few things, commenting out props for now. * v0.18.0-beta.1 * Trying to fix woodpecker, 1. * Trying to fix woodpecker, 2. * Handroll prompt * Add navigation prompt to other pages * Fix prompt navigation bug * Fix prompt bug introduced from last bug fix * Fix PWA bug * Fix isoData not working * Fix search page update url * Fix sharp issue. * v0.18.0-beta.2 * Make create post pre-fetch communities * Fix bug from last commit * Fix issue of posts/comments not being switched when changing select options * Fix unnecessary fetches on home screen * Make circular icon buttons not look stupid * Prevent unnecessary fetches * Make login experience smoother * Add PWA shortcuts * Add related application to PWA * Update translations * Forgot to add post editing. * Fixing site setup. * Deploy script setup. * v0.18.0-beta.4 * Sanitize again. * Adding sanitize json function. * Upping version. * Another sanitize fix. * Upping version. * Prevent search nav item from disappearing when on search page * Allow admin and mod actions on non-local comments. * Fix mobile menu collapse bug * Completely fix prompt component * Fix undefined value checks in use_http_client_2 (#1230) * fix: filter out undefined from posts * fix: emoji initialisation passing undefined * fix: || => ?? to be more explicit * linting --------- Co-authored-by: Alex Maras <alexmaras@gmail.com> * Re-add accidentally removed state * Fix dropdown bug * Use linkEvent where appropriate * Fix navigation warnings. --------- Co-authored-by: Dessalines <tyhou13@gmx.com> Co-authored-by: Alex Maras <dev@alexmaras.com> Co-authored-by: Alex Maras <alexmaras@gmail.com>
2023-06-14 12:20:40 +00:00
submitted: false,
};
constructor(props: any, context: any) {
super(props, context);
this.handleLanguageChange = this.handleLanguageChange.bind(this);
if (isBrowser()) {
this.tribute = setupTribute();
}
}
componentDidMount() {
2023-06-05 21:31:12 +00:00
const textarea: any = document.getElementById(this.id);
if (textarea) {
autosize(textarea);
this.tribute.attach(textarea);
textarea.addEventListener("tribute-replaced", () => {
this.setState({ content: textarea.value });
autosize.update(textarea);
});
this.quoteInsert();
if (this.props.focus) {
textarea.focus();
}
// TODO this is slow for some reason
setupTippy();
}
}
componentWillReceiveProps(nextProps: MarkdownTextAreaProps) {
if (nextProps.finished) {
Use http client (#1081) * Beginning work on websocket -> http client conversion. * About 30% done. * half done. * more done. * Almost passing lint. * Passing lint, but untested. * Add back in event listeners. * Fixing some community forms. * Remove webpack cache. * fixing some more. * Fixed ISOwrappers. * A few more fixes. * Refactor utils * Fix instance add/remove buttons * Not catching errors in isoWrapper. * Wrap Http client * Fixing up tagline and ratelimit forms. * Make all http client wrapping be in one place * Reworking some more forms. * Upgrading lemmy-js-client. * Fixing verify email. * Fix linting errors * Upgrading woodpecker node. * Fix comment scrolling rerender bug. * Fixing a few things, commenting out props for now. * v0.18.0-beta.1 * Trying to fix woodpecker, 1. * Trying to fix woodpecker, 2. * Handroll prompt * Add navigation prompt to other pages * Fix prompt navigation bug * Fix prompt bug introduced from last bug fix * Fix PWA bug * Fix isoData not working * Fix search page update url * Fix sharp issue. * v0.18.0-beta.2 * Make create post pre-fetch communities * Fix bug from last commit * Fix issue of posts/comments not being switched when changing select options * Fix unnecessary fetches on home screen * Make circular icon buttons not look stupid * Prevent unnecessary fetches * Make login experience smoother * Add PWA shortcuts * Add related application to PWA * Update translations * Forgot to add post editing. * Fixing site setup. * Deploy script setup. * v0.18.0-beta.4 * Sanitize again. * Adding sanitize json function. * Upping version. * Another sanitize fix. * Upping version. * Prevent search nav item from disappearing when on search page * Allow admin and mod actions on non-local comments. * Fix mobile menu collapse bug * Completely fix prompt component * Fix undefined value checks in use_http_client_2 (#1230) * fix: filter out undefined from posts * fix: emoji initialisation passing undefined * fix: || => ?? to be more explicit * linting --------- Co-authored-by: Alex Maras <alexmaras@gmail.com> * Re-add accidentally removed state * Fix dropdown bug * Use linkEvent where appropriate * Fix navigation warnings. --------- Co-authored-by: Dessalines <tyhou13@gmx.com> Co-authored-by: Alex Maras <dev@alexmaras.com> Co-authored-by: Alex Maras <alexmaras@gmail.com>
2023-06-14 12:20:40 +00:00
this.setState({
previewMode: false,
imageUploadStatus: undefined,
loading: false,
content: undefined,
});
if (this.props.replyType) {
this.props.onReplyCancel?.();
}
const textarea: any = document.getElementById(this.id);
const form: any = document.getElementById(this.formId);
form.reset();
setTimeout(() => autosize.update(textarea), 10);
}
}
render() {
2023-06-05 21:31:12 +00:00
const languageId = this.state.languageId;
Use http client (#1081) * Beginning work on websocket -> http client conversion. * About 30% done. * half done. * more done. * Almost passing lint. * Passing lint, but untested. * Add back in event listeners. * Fixing some community forms. * Remove webpack cache. * fixing some more. * Fixed ISOwrappers. * A few more fixes. * Refactor utils * Fix instance add/remove buttons * Not catching errors in isoWrapper. * Wrap Http client * Fixing up tagline and ratelimit forms. * Make all http client wrapping be in one place * Reworking some more forms. * Upgrading lemmy-js-client. * Fixing verify email. * Fix linting errors * Upgrading woodpecker node. * Fix comment scrolling rerender bug. * Fixing a few things, commenting out props for now. * v0.18.0-beta.1 * Trying to fix woodpecker, 1. * Trying to fix woodpecker, 2. * Handroll prompt * Add navigation prompt to other pages * Fix prompt navigation bug * Fix prompt bug introduced from last bug fix * Fix PWA bug * Fix isoData not working * Fix search page update url * Fix sharp issue. * v0.18.0-beta.2 * Make create post pre-fetch communities * Fix bug from last commit * Fix issue of posts/comments not being switched when changing select options * Fix unnecessary fetches on home screen * Make circular icon buttons not look stupid * Prevent unnecessary fetches * Make login experience smoother * Add PWA shortcuts * Add related application to PWA * Update translations * Forgot to add post editing. * Fixing site setup. * Deploy script setup. * v0.18.0-beta.4 * Sanitize again. * Adding sanitize json function. * Upping version. * Another sanitize fix. * Upping version. * Prevent search nav item from disappearing when on search page * Allow admin and mod actions on non-local comments. * Fix mobile menu collapse bug * Completely fix prompt component * Fix undefined value checks in use_http_client_2 (#1230) * fix: filter out undefined from posts * fix: emoji initialisation passing undefined * fix: || => ?? to be more explicit * linting --------- Co-authored-by: Alex Maras <alexmaras@gmail.com> * Re-add accidentally removed state * Fix dropdown bug * Use linkEvent where appropriate * Fix navigation warnings. --------- Co-authored-by: Dessalines <tyhou13@gmx.com> Co-authored-by: Alex Maras <dev@alexmaras.com> Co-authored-by: Alex Maras <alexmaras@gmail.com>
2023-06-14 12:20:40 +00:00
// TODO add these prompts back in at some point
// <Prompt
// when={!this.props.hideNavigationWarnings && this.state.content}
// message={i18n.t("block_leaving")}
// />
return (
<form id={this.formId} onSubmit={linkEvent(this, this.handleSubmit)}>
Use http client (#1081) * Beginning work on websocket -> http client conversion. * About 30% done. * half done. * more done. * Almost passing lint. * Passing lint, but untested. * Add back in event listeners. * Fixing some community forms. * Remove webpack cache. * fixing some more. * Fixed ISOwrappers. * A few more fixes. * Refactor utils * Fix instance add/remove buttons * Not catching errors in isoWrapper. * Wrap Http client * Fixing up tagline and ratelimit forms. * Make all http client wrapping be in one place * Reworking some more forms. * Upgrading lemmy-js-client. * Fixing verify email. * Fix linting errors * Upgrading woodpecker node. * Fix comment scrolling rerender bug. * Fixing a few things, commenting out props for now. * v0.18.0-beta.1 * Trying to fix woodpecker, 1. * Trying to fix woodpecker, 2. * Handroll prompt * Add navigation prompt to other pages * Fix prompt navigation bug * Fix prompt bug introduced from last bug fix * Fix PWA bug * Fix isoData not working * Fix search page update url * Fix sharp issue. * v0.18.0-beta.2 * Make create post pre-fetch communities * Fix bug from last commit * Fix issue of posts/comments not being switched when changing select options * Fix unnecessary fetches on home screen * Make circular icon buttons not look stupid * Prevent unnecessary fetches * Make login experience smoother * Add PWA shortcuts * Add related application to PWA * Update translations * Forgot to add post editing. * Fixing site setup. * Deploy script setup. * v0.18.0-beta.4 * Sanitize again. * Adding sanitize json function. * Upping version. * Another sanitize fix. * Upping version. * Prevent search nav item from disappearing when on search page * Allow admin and mod actions on non-local comments. * Fix mobile menu collapse bug * Completely fix prompt component * Fix undefined value checks in use_http_client_2 (#1230) * fix: filter out undefined from posts * fix: emoji initialisation passing undefined * fix: || => ?? to be more explicit * linting --------- Co-authored-by: Alex Maras <alexmaras@gmail.com> * Re-add accidentally removed state * Fix dropdown bug * Use linkEvent where appropriate * Fix navigation warnings. --------- Co-authored-by: Dessalines <tyhou13@gmx.com> Co-authored-by: Alex Maras <dev@alexmaras.com> Co-authored-by: Alex Maras <alexmaras@gmail.com>
2023-06-14 12:20:40 +00:00
<NavigationPrompt
when={
!this.props.hideNavigationWarnings &&
!!this.state.content &&
!this.state.submitted
}
/>
<div className="form-group row">
<div className={`col-sm-12`}>
<textarea
id={this.id}
className={`form-control ${this.state.previewMode && "d-none"}`}
value={this.state.content}
onInput={linkEvent(this, this.handleContentChange)}
onPaste={linkEvent(this, this.handleImageUploadPaste)}
2023-06-09 21:30:12 +00:00
onKeyDown={linkEvent(this, this.handleKeyBinds)}
required
disabled={this.isDisabled}
rows={2}
maxLength={this.props.maxLength ?? markdownFieldCharacterLimit}
placeholder={this.props.placeholder}
/>
{this.state.previewMode && this.state.content && (
<div
className="card border-secondary card-body md-div"
dangerouslySetInnerHTML={mdToHtml(this.state.content)}
/>
)}
{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,
})}
/>
)}
</div>
<label className="sr-only" htmlFor={this.id}>
{i18n.t("body")}
</label>
</div>
<div className="row">
<div className="col-sm-12 d-flex flex-wrap">
2023-06-16 22:56:23 +00:00
{this.props.buttonTitle && (
<button
type="submit"
className="btn btn-sm btn-secondary mr-2"
disabled={this.isDisabled}
>
{this.state.loading ? (
<Spinner />
) : (
<span>{this.props.buttonTitle}</span>
)}
</button>
)}
{this.props.replyType && (
<button
type="button"
className="btn btn-sm btn-secondary mr-2"
onClick={linkEvent(this, this.handleReplyCancel)}
>
{i18n.t("cancel")}
</button>
)}
{this.state.content && (
<button
className={`btn btn-sm btn-secondary mr-2 ${
this.state.previewMode && "active"
}`}
onClick={linkEvent(this, this.handlePreviewToggle)}
>
{this.state.previewMode ? i18n.t("edit") : i18n.t("preview")}
</button>
)}
{/* A flex expander */}
<div className="flex-grow-1"></div>
{this.props.showLanguage && (
<LanguageSelect
iconVersion
allLanguages={this.props.allLanguages}
selectedLanguageIds={
languageId ? Array.of(languageId) : undefined
}
siteLanguages={this.props.siteLanguages}
onChange={this.handleLanguageChange}
disabled={this.isDisabled}
/>
)}
{this.getFormatButton("bold", this.handleInsertBold)}
{this.getFormatButton("italic", this.handleInsertItalic)}
{this.getFormatButton("link", this.handleInsertLink)}
<EmojiPicker
onEmojiClick={e => this.handleEmoji(this, e)}
disabled={this.isDisabled}
></EmojiPicker>
<form className="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 ${
UserService.Instance.myUserInfo && "pointer"
2021-03-15 18:09:31 +00:00
}`}
data-tippy-content={i18n.t("upload_image")}
>
{this.state.imageUploadStatus ? (
2021-02-11 20:35:27 +00:00
<Spinner />
) : (
2021-02-11 20:35:27 +00:00
<Icon icon="image" classes="icon-inline" />
)}
</label>
<input
id={`file-upload-${this.id}`}
type="file"
accept="image/*,video/*"
name="file"
className="d-none"
multiple
disabled={!UserService.Instance.myUserInfo || this.isDisabled}
onChange={linkEvent(this, this.handleImageUpload)}
/>
</form>
{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)}
<a
href={markdownHelpUrl}
className="btn btn-sm text-muted font-weight-bold"
title={i18n.t("formatting_help")}
rel={relTags}
>
2021-02-11 20:35:27 +00:00
<Icon icon="help-circle" classes="icon-inline" />
</a>
</div>
</div>
</form>
);
}
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;
if (value == null) {
2023-06-05 21:31:12 +00:00
const emoji = customEmojisLookup.get(e.id)?.custom_emoji;
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();
2023-06-05 21:31:12 +00:00
const textarea: any = document.getElementById(i.id);
2022-11-27 18:02:32 +00:00
autosize.update(textarea);
}
handleImageUploadPaste(i: MarkdownTextArea, event: any) {
const image = event.clipboardData.files[0];
if (image) {
i.handleImageUpload(i, image);
}
}
handleImageUpload(i: MarkdownTextArea, event: any) {
const files: File[] = [];
if (event.target) {
event.preventDefault();
files.push(...event.target.files);
} else {
files.push(event);
}
if (files.length > maxUploadImages) {
toast(
i18n.t("too_many_images_upload", {
count: Number(maxUploadImages),
formattedCount: numToSI(maxUploadImages),
}),
"danger"
);
} else {
i.setState({
imageUploadStatus: { total: files.length, uploaded: 0 },
});
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;
}
}
}
Use http client (#1081) * Beginning work on websocket -> http client conversion. * About 30% done. * half done. * more done. * Almost passing lint. * Passing lint, but untested. * Add back in event listeners. * Fixing some community forms. * Remove webpack cache. * fixing some more. * Fixed ISOwrappers. * A few more fixes. * Refactor utils * Fix instance add/remove buttons * Not catching errors in isoWrapper. * Wrap Http client * Fixing up tagline and ratelimit forms. * Make all http client wrapping be in one place * Reworking some more forms. * Upgrading lemmy-js-client. * Fixing verify email. * Fix linting errors * Upgrading woodpecker node. * Fix comment scrolling rerender bug. * Fixing a few things, commenting out props for now. * v0.18.0-beta.1 * Trying to fix woodpecker, 1. * Trying to fix woodpecker, 2. * Handroll prompt * Add navigation prompt to other pages * Fix prompt navigation bug * Fix prompt bug introduced from last bug fix * Fix PWA bug * Fix isoData not working * Fix search page update url * Fix sharp issue. * v0.18.0-beta.2 * Make create post pre-fetch communities * Fix bug from last commit * Fix issue of posts/comments not being switched when changing select options * Fix unnecessary fetches on home screen * Make circular icon buttons not look stupid * Prevent unnecessary fetches * Make login experience smoother * Add PWA shortcuts * Add related application to PWA * Update translations * Forgot to add post editing. * Fixing site setup. * Deploy script setup. * v0.18.0-beta.4 * Sanitize again. * Adding sanitize json function. * Upping version. * Another sanitize fix. * Upping version. * Prevent search nav item from disappearing when on search page * Allow admin and mod actions on non-local comments. * Fix mobile menu collapse bug * Completely fix prompt component * Fix undefined value checks in use_http_client_2 (#1230) * fix: filter out undefined from posts * fix: emoji initialisation passing undefined * fix: || => ?? to be more explicit * linting --------- Co-authored-by: Alex Maras <alexmaras@gmail.com> * Re-add accidentally removed state * Fix dropdown bug * Use linkEvent where appropriate * Fix navigation warnings. --------- Co-authored-by: Dessalines <tyhou13@gmx.com> Co-authored-by: Alex Maras <dev@alexmaras.com> Co-authored-by: Alex Maras <alexmaras@gmail.com>
2023-06-14 12:20:40 +00:00
async uploadSingleImage(i: MarkdownTextArea, image: File) {
const res = await HttpService.client.uploadImage({ image });
console.log("pictrs upload:");
console.log(res);
if (res.state === "success") {
if (res.data.msg === "ok") {
const imageMarkdown = `![](${res.data.url})`;
i.setState(({ content }) => ({
content: content ? `${content}\n${imageMarkdown}` : imageMarkdown,
}));
i.contentChange();
const textarea: any = document.getElementById(i.id);
autosize.update(textarea);
Use http client (#1081) * Beginning work on websocket -> http client conversion. * About 30% done. * half done. * more done. * Almost passing lint. * Passing lint, but untested. * Add back in event listeners. * Fixing some community forms. * Remove webpack cache. * fixing some more. * Fixed ISOwrappers. * A few more fixes. * Refactor utils * Fix instance add/remove buttons * Not catching errors in isoWrapper. * Wrap Http client * Fixing up tagline and ratelimit forms. * Make all http client wrapping be in one place * Reworking some more forms. * Upgrading lemmy-js-client. * Fixing verify email. * Fix linting errors * Upgrading woodpecker node. * Fix comment scrolling rerender bug. * Fixing a few things, commenting out props for now. * v0.18.0-beta.1 * Trying to fix woodpecker, 1. * Trying to fix woodpecker, 2. * Handroll prompt * Add navigation prompt to other pages * Fix prompt navigation bug * Fix prompt bug introduced from last bug fix * Fix PWA bug * Fix isoData not working * Fix search page update url * Fix sharp issue. * v0.18.0-beta.2 * Make create post pre-fetch communities * Fix bug from last commit * Fix issue of posts/comments not being switched when changing select options * Fix unnecessary fetches on home screen * Make circular icon buttons not look stupid * Prevent unnecessary fetches * Make login experience smoother * Add PWA shortcuts * Add related application to PWA * Update translations * Forgot to add post editing. * Fixing site setup. * Deploy script setup. * v0.18.0-beta.4 * Sanitize again. * Adding sanitize json function. * Upping version. * Another sanitize fix. * Upping version. * Prevent search nav item from disappearing when on search page * Allow admin and mod actions on non-local comments. * Fix mobile menu collapse bug * Completely fix prompt component * Fix undefined value checks in use_http_client_2 (#1230) * fix: filter out undefined from posts * fix: emoji initialisation passing undefined * fix: || => ?? to be more explicit * linting --------- Co-authored-by: Alex Maras <alexmaras@gmail.com> * Re-add accidentally removed state * Fix dropdown bug * Use linkEvent where appropriate * Fix navigation warnings. --------- Co-authored-by: Dessalines <tyhou13@gmx.com> Co-authored-by: Alex Maras <dev@alexmaras.com> Co-authored-by: Alex Maras <alexmaras@gmail.com>
2023-06-14 12:20:40 +00:00
pictrsDeleteToast(image.name, res.data.delete_url as string);
} else {
Use http client (#1081) * Beginning work on websocket -> http client conversion. * About 30% done. * half done. * more done. * Almost passing lint. * Passing lint, but untested. * Add back in event listeners. * Fixing some community forms. * Remove webpack cache. * fixing some more. * Fixed ISOwrappers. * A few more fixes. * Refactor utils * Fix instance add/remove buttons * Not catching errors in isoWrapper. * Wrap Http client * Fixing up tagline and ratelimit forms. * Make all http client wrapping be in one place * Reworking some more forms. * Upgrading lemmy-js-client. * Fixing verify email. * Fix linting errors * Upgrading woodpecker node. * Fix comment scrolling rerender bug. * Fixing a few things, commenting out props for now. * v0.18.0-beta.1 * Trying to fix woodpecker, 1. * Trying to fix woodpecker, 2. * Handroll prompt * Add navigation prompt to other pages * Fix prompt navigation bug * Fix prompt bug introduced from last bug fix * Fix PWA bug * Fix isoData not working * Fix search page update url * Fix sharp issue. * v0.18.0-beta.2 * Make create post pre-fetch communities * Fix bug from last commit * Fix issue of posts/comments not being switched when changing select options * Fix unnecessary fetches on home screen * Make circular icon buttons not look stupid * Prevent unnecessary fetches * Make login experience smoother * Add PWA shortcuts * Add related application to PWA * Update translations * Forgot to add post editing. * Fixing site setup. * Deploy script setup. * v0.18.0-beta.4 * Sanitize again. * Adding sanitize json function. * Upping version. * Another sanitize fix. * Upping version. * Prevent search nav item from disappearing when on search page * Allow admin and mod actions on non-local comments. * Fix mobile menu collapse bug * Completely fix prompt component * Fix undefined value checks in use_http_client_2 (#1230) * fix: filter out undefined from posts * fix: emoji initialisation passing undefined * fix: || => ?? to be more explicit * linting --------- Co-authored-by: Alex Maras <alexmaras@gmail.com> * Re-add accidentally removed state * Fix dropdown bug * Use linkEvent where appropriate * Fix navigation warnings. --------- Co-authored-by: Dessalines <tyhou13@gmx.com> Co-authored-by: Alex Maras <dev@alexmaras.com> Co-authored-by: Alex Maras <alexmaras@gmail.com>
2023-06-14 12:20:40 +00:00
throw JSON.stringify(res.data);
}
Use http client (#1081) * Beginning work on websocket -> http client conversion. * About 30% done. * half done. * more done. * Almost passing lint. * Passing lint, but untested. * Add back in event listeners. * Fixing some community forms. * Remove webpack cache. * fixing some more. * Fixed ISOwrappers. * A few more fixes. * Refactor utils * Fix instance add/remove buttons * Not catching errors in isoWrapper. * Wrap Http client * Fixing up tagline and ratelimit forms. * Make all http client wrapping be in one place * Reworking some more forms. * Upgrading lemmy-js-client. * Fixing verify email. * Fix linting errors * Upgrading woodpecker node. * Fix comment scrolling rerender bug. * Fixing a few things, commenting out props for now. * v0.18.0-beta.1 * Trying to fix woodpecker, 1. * Trying to fix woodpecker, 2. * Handroll prompt * Add navigation prompt to other pages * Fix prompt navigation bug * Fix prompt bug introduced from last bug fix * Fix PWA bug * Fix isoData not working * Fix search page update url * Fix sharp issue. * v0.18.0-beta.2 * Make create post pre-fetch communities * Fix bug from last commit * Fix issue of posts/comments not being switched when changing select options * Fix unnecessary fetches on home screen * Make circular icon buttons not look stupid * Prevent unnecessary fetches * Make login experience smoother * Add PWA shortcuts * Add related application to PWA * Update translations * Forgot to add post editing. * Fixing site setup. * Deploy script setup. * v0.18.0-beta.4 * Sanitize again. * Adding sanitize json function. * Upping version. * Another sanitize fix. * Upping version. * Prevent search nav item from disappearing when on search page * Allow admin and mod actions on non-local comments. * Fix mobile menu collapse bug * Completely fix prompt component * Fix undefined value checks in use_http_client_2 (#1230) * fix: filter out undefined from posts * fix: emoji initialisation passing undefined * fix: || => ?? to be more explicit * linting --------- Co-authored-by: Alex Maras <alexmaras@gmail.com> * Re-add accidentally removed state * Fix dropdown bug * Use linkEvent where appropriate * Fix navigation warnings. --------- Co-authored-by: Dessalines <tyhou13@gmx.com> Co-authored-by: Alex Maras <dev@alexmaras.com> Co-authored-by: Alex Maras <alexmaras@gmail.com>
2023-06-14 12:20:40 +00:00
} else if (res.state === "failed") {
i.setState({ imageUploadStatus: undefined });
Use http client (#1081) * Beginning work on websocket -> http client conversion. * About 30% done. * half done. * more done. * Almost passing lint. * Passing lint, but untested. * Add back in event listeners. * Fixing some community forms. * Remove webpack cache. * fixing some more. * Fixed ISOwrappers. * A few more fixes. * Refactor utils * Fix instance add/remove buttons * Not catching errors in isoWrapper. * Wrap Http client * Fixing up tagline and ratelimit forms. * Make all http client wrapping be in one place * Reworking some more forms. * Upgrading lemmy-js-client. * Fixing verify email. * Fix linting errors * Upgrading woodpecker node. * Fix comment scrolling rerender bug. * Fixing a few things, commenting out props for now. * v0.18.0-beta.1 * Trying to fix woodpecker, 1. * Trying to fix woodpecker, 2. * Handroll prompt * Add navigation prompt to other pages * Fix prompt navigation bug * Fix prompt bug introduced from last bug fix * Fix PWA bug * Fix isoData not working * Fix search page update url * Fix sharp issue. * v0.18.0-beta.2 * Make create post pre-fetch communities * Fix bug from last commit * Fix issue of posts/comments not being switched when changing select options * Fix unnecessary fetches on home screen * Make circular icon buttons not look stupid * Prevent unnecessary fetches * Make login experience smoother * Add PWA shortcuts * Add related application to PWA * Update translations * Forgot to add post editing. * Fixing site setup. * Deploy script setup. * v0.18.0-beta.4 * Sanitize again. * Adding sanitize json function. * Upping version. * Another sanitize fix. * Upping version. * Prevent search nav item from disappearing when on search page * Allow admin and mod actions on non-local comments. * Fix mobile menu collapse bug * Completely fix prompt component * Fix undefined value checks in use_http_client_2 (#1230) * fix: filter out undefined from posts * fix: emoji initialisation passing undefined * fix: || => ?? to be more explicit * linting --------- Co-authored-by: Alex Maras <alexmaras@gmail.com> * Re-add accidentally removed state * Fix dropdown bug * Use linkEvent where appropriate * Fix navigation warnings. --------- Co-authored-by: Dessalines <tyhou13@gmx.com> Co-authored-by: Alex Maras <dev@alexmaras.com> Co-authored-by: Alex Maras <alexmaras@gmail.com>
2023-06-14 12:20:40 +00:00
console.error(res.msg);
toast(res.msg, "danger");
Use http client (#1081) * Beginning work on websocket -> http client conversion. * About 30% done. * half done. * more done. * Almost passing lint. * Passing lint, but untested. * Add back in event listeners. * Fixing some community forms. * Remove webpack cache. * fixing some more. * Fixed ISOwrappers. * A few more fixes. * Refactor utils * Fix instance add/remove buttons * Not catching errors in isoWrapper. * Wrap Http client * Fixing up tagline and ratelimit forms. * Make all http client wrapping be in one place * Reworking some more forms. * Upgrading lemmy-js-client. * Fixing verify email. * Fix linting errors * Upgrading woodpecker node. * Fix comment scrolling rerender bug. * Fixing a few things, commenting out props for now. * v0.18.0-beta.1 * Trying to fix woodpecker, 1. * Trying to fix woodpecker, 2. * Handroll prompt * Add navigation prompt to other pages * Fix prompt navigation bug * Fix prompt bug introduced from last bug fix * Fix PWA bug * Fix isoData not working * Fix search page update url * Fix sharp issue. * v0.18.0-beta.2 * Make create post pre-fetch communities * Fix bug from last commit * Fix issue of posts/comments not being switched when changing select options * Fix unnecessary fetches on home screen * Make circular icon buttons not look stupid * Prevent unnecessary fetches * Make login experience smoother * Add PWA shortcuts * Add related application to PWA * Update translations * Forgot to add post editing. * Fixing site setup. * Deploy script setup. * v0.18.0-beta.4 * Sanitize again. * Adding sanitize json function. * Upping version. * Another sanitize fix. * Upping version. * Prevent search nav item from disappearing when on search page * Allow admin and mod actions on non-local comments. * Fix mobile menu collapse bug * Completely fix prompt component * Fix undefined value checks in use_http_client_2 (#1230) * fix: filter out undefined from posts * fix: emoji initialisation passing undefined * fix: || => ?? to be more explicit * linting --------- Co-authored-by: Alex Maras <alexmaras@gmail.com> * Re-add accidentally removed state * Fix dropdown bug * Use linkEvent where appropriate * Fix navigation warnings. --------- Co-authored-by: Dessalines <tyhou13@gmx.com> Co-authored-by: Alex Maras <dev@alexmaras.com> Co-authored-by: Alex Maras <alexmaras@gmail.com>
2023-06-14 12:20:40 +00:00
throw res.msg;
}
}
contentChange() {
// Coerces the undefineds to empty strings, for replacing in the DB
2023-06-05 21:31:12 +00:00
const content = this.state.content ?? "";
this.props.onContentChange?.(content);
}
handleContentChange(i: MarkdownTextArea, event: any) {
i.setState({ content: event.target.value });
i.contentChange();
}
2023-06-09 21:30:12 +00:00
// Keybind handler
2023-06-10 09:15:22 +00:00
// Keybinds inspired by github comment area
2023-06-09 21:30:12 +00:00
handleKeyBinds(i: MarkdownTextArea, event: KeyboardEvent) {
if (event.ctrlKey) {
switch (event.key) {
case "k": {
2023-06-09 21:30:12 +00:00
i.handleInsertLink(i, event);
2023-06-10 09:15:22 +00:00
break;
}
case "Enter": {
if (!this.isDisabled) {
i.handleSubmit(i, event);
}
break;
}
case "b": {
2023-06-10 09:15:22 +00:00
i.handleInsertBold(i, event);
break;
}
case "i": {
2023-06-10 09:15:22 +00:00
i.handleInsertItalic(i, event);
break;
}
case "e": {
2023-06-10 09:15:22 +00:00
i.handleInsertCode(i, event);
break;
}
case "8": {
2023-06-10 09:15:22 +00:00
i.handleInsertList(i, event);
break;
}
case "s": {
2023-06-10 09:15:22 +00:00
i.handleInsertSpoiler(i, event);
break;
}
case "p": {
2023-06-10 09:15:22 +00:00
if (i.state.content) i.handlePreviewToggle(i, event);
break;
}
case ".": {
2023-06-10 09:15:22 +00:00
i.handleInsertQuote(i, event);
break;
}
2023-06-09 21:30:12 +00:00
}
}
}
handlePreviewToggle(i: MarkdownTextArea, event: any) {
event.preventDefault();
i.setState({ previewMode: !i.state.previewMode });
}
handleLanguageChange(val: number[]) {
this.setState({ languageId: val[0] });
}
handleSubmit(i: MarkdownTextArea, event: any) {
event.preventDefault();
Use http client (#1081) * Beginning work on websocket -> http client conversion. * About 30% done. * half done. * more done. * Almost passing lint. * Passing lint, but untested. * Add back in event listeners. * Fixing some community forms. * Remove webpack cache. * fixing some more. * Fixed ISOwrappers. * A few more fixes. * Refactor utils * Fix instance add/remove buttons * Not catching errors in isoWrapper. * Wrap Http client * Fixing up tagline and ratelimit forms. * Make all http client wrapping be in one place * Reworking some more forms. * Upgrading lemmy-js-client. * Fixing verify email. * Fix linting errors * Upgrading woodpecker node. * Fix comment scrolling rerender bug. * Fixing a few things, commenting out props for now. * v0.18.0-beta.1 * Trying to fix woodpecker, 1. * Trying to fix woodpecker, 2. * Handroll prompt * Add navigation prompt to other pages * Fix prompt navigation bug * Fix prompt bug introduced from last bug fix * Fix PWA bug * Fix isoData not working * Fix search page update url * Fix sharp issue. * v0.18.0-beta.2 * Make create post pre-fetch communities * Fix bug from last commit * Fix issue of posts/comments not being switched when changing select options * Fix unnecessary fetches on home screen * Make circular icon buttons not look stupid * Prevent unnecessary fetches * Make login experience smoother * Add PWA shortcuts * Add related application to PWA * Update translations * Forgot to add post editing. * Fixing site setup. * Deploy script setup. * v0.18.0-beta.4 * Sanitize again. * Adding sanitize json function. * Upping version. * Another sanitize fix. * Upping version. * Prevent search nav item from disappearing when on search page * Allow admin and mod actions on non-local comments. * Fix mobile menu collapse bug * Completely fix prompt component * Fix undefined value checks in use_http_client_2 (#1230) * fix: filter out undefined from posts * fix: emoji initialisation passing undefined * fix: || => ?? to be more explicit * linting --------- Co-authored-by: Alex Maras <alexmaras@gmail.com> * Re-add accidentally removed state * Fix dropdown bug * Use linkEvent where appropriate * Fix navigation warnings. --------- Co-authored-by: Dessalines <tyhou13@gmx.com> Co-authored-by: Alex Maras <dev@alexmaras.com> Co-authored-by: Alex Maras <alexmaras@gmail.com>
2023-06-14 12:20:40 +00:00
if (i.state.content) {
i.setState({ loading: true, submitted: true });
i.props.onSubmit?.(i.state.content, i.formId, i.state.languageId);
}
}
handleReplyCancel(i: MarkdownTextArea) {
i.props.onReplyCancel?.();
}
handleInsertLink(i: MarkdownTextArea, event: any) {
event.preventDefault();
const textarea: any = document.getElementById(i.id);
const start: number = textarea.selectionStart;
const end: number = textarea.selectionEnd;
const content = i.state.content ?? "";
if (!i.state.content) {
i.setState({ content: "" });
}
if (start !== end) {
const selectedText = content?.substring(start, end);
i.setState({
content: `${content?.substring(
0,
start
)}[${selectedText}]()${content?.substring(end)}`,
});
textarea.focus();
setTimeout(() => (textarea.selectionEnd = end + 3), 10);
} else {
i.setState({ content: `${content} []()` });
textarea.focus();
setTimeout(() => (textarea.selectionEnd -= 1), 10);
}
i.contentChange();
}
simpleSurround(chars: string) {
this.simpleSurroundBeforeAfter(chars, chars);
}
simpleBeginningofLine(chars: string) {
this.simpleSurroundBeforeAfter(`${chars}`, "", "");
}
simpleSurroundBeforeAfter(
beforeChars: string,
afterChars: string,
emptyChars = "___"
) {
const content = this.state.content ?? "";
if (!this.state.content) {
this.setState({ content: "" });
}
const textarea: any = document.getElementById(this.id);
const start: number = textarea.selectionStart;
const end: number = textarea.selectionEnd;
if (start !== end) {
const selectedText = content?.substring(start, end);
this.setState({
content: `${content?.substring(
0,
start
)}${beforeChars}${selectedText}${afterChars}${content?.substring(end)}`,
});
} else {
this.setState({
content: `${content}${beforeChars}${emptyChars}${afterChars}`,
});
}
this.contentChange();
textarea.focus();
if (start !== end) {
textarea.setSelectionRange(
start + beforeChars.length,
end + afterChars.length
);
} else {
textarea.setSelectionRange(
start + beforeChars.length,
end + emptyChars.length + afterChars.length
);
}
setTimeout(() => {
autosize.update(textarea);
}, 10);
}
handleInsertBold(i: MarkdownTextArea, event: any) {
event.preventDefault();
i.simpleSurround("**");
}
handleInsertItalic(i: MarkdownTextArea, event: any) {
event.preventDefault();
i.simpleSurround("*");
}
handleInsertCode(i: MarkdownTextArea, event: any) {
event.preventDefault();
if (i.getSelectedText().split(/\r*\n/).length > 1) {
i.simpleSurroundBeforeAfter("```\n", "\n```");
} else {
i.simpleSurround("`");
}
}
handleInsertStrikethrough(i: MarkdownTextArea, event: any) {
event.preventDefault();
i.simpleSurround("~~");
}
handleInsertList(i: MarkdownTextArea, event: any) {
event.preventDefault();
2023-06-10 09:15:22 +00:00
i.simpleBeginningofLine(`-${i.getSelectedText() ? " " : ""}`);
}
handleInsertQuote(i: MarkdownTextArea, event: any) {
event.preventDefault();
i.simpleBeginningofLine(">");
}
handleInsertHeader(i: MarkdownTextArea, event: any) {
event.preventDefault();
i.simpleBeginningofLine("#");
}
handleInsertSubscript(i: MarkdownTextArea, event: any) {
event.preventDefault();
i.simpleSurround("~");
}
handleInsertSuperscript(i: MarkdownTextArea, event: any) {
event.preventDefault();
i.simpleSurround("^");
}
simpleInsert(chars: string) {
2023-06-05 21:31:12 +00:00
const content = this.state.content;
if (!content) {
this.setState({ content: `${chars} ` });
} else {
this.setState({
content: `${content}\n${chars} `,
});
}
2023-06-05 21:31:12 +00:00
const textarea: any = document.getElementById(this.id);
textarea.focus();
setTimeout(() => {
autosize.update(textarea);
}, 10);
this.contentChange();
}
handleInsertSpoiler(i: MarkdownTextArea, event: any) {
event.preventDefault();
2023-06-05 21:31:12 +00:00
const beforeChars = `\n::: spoiler ${i18n.t("spoiler")}\n`;
const afterChars = "\n:::\n";
i.simpleSurroundBeforeAfter(beforeChars, afterChars);
}
quoteInsert() {
const textarea: any = document.getElementById(this.id);
const selectedText = window.getSelection()?.toString();
const { content } = this.state;
if (selectedText) {
const quotedText =
selectedText
.split("\n")
.map(t => `> ${t}`)
.join("\n") + "\n\n";
if (!content) {
this.setState({ content: "" });
} else {
this.setState({ content: `${content}\n` });
}
this.setState({
content: `${content}${quotedText}`,
});
this.contentChange();
// Not sure why this needs a delay
setTimeout(() => autosize.update(textarea), 10);
}
}
getSelectedText(): string {
const { selectionStart: start, selectionEnd: end } =
document.getElementById(this.id) as any;
return start !== end ? this.state.content?.substring(start, end) ?? "" : "";
}
get isDisabled() {
return (
this.state.loading ||
this.props.disabled ||
!!this.state.imageUploadStatus
);
}
}