1 import { Component, linkEvent } from "inferno";
2 import { Prompt } from "inferno-router";
13 import { UserService } from "../services";
14 import autosize from "autosize";
15 import { i18n } from "../i18next";
16 import { pictrsUri } from "../env";
17 import { Icon, Spinner } from "./icon";
19 interface MarkdownTextAreaProps {
20 initialContent: string;
27 onSubmit?(msg: { val: string; formId: string }): any;
28 onContentChange?(val: string): any;
29 onReplyCancel?(): any;
30 hideNavigationWarnings?: boolean;
34 interface MarkdownTextAreaState {
38 imageLoading: boolean;
41 export class MarkdownTextArea extends Component<
42 MarkdownTextAreaProps,
45 private id = `comment-textarea-${randomStr()}`;
46 private formId = `comment-form-${randomStr()}`;
48 private emptyState: MarkdownTextAreaState = {
49 content: this.props.initialContent,
55 constructor(props: any, context: any) {
56 super(props, context);
59 this.tribute = setupTribute();
61 this.state = this.emptyState;
65 let textarea: any = document.getElementById(this.id);
68 this.tribute.attach(textarea);
69 textarea.addEventListener("tribute-replaced", () => {
70 this.state.content = textarea.value;
71 this.setState(this.state);
72 autosize.update(textarea);
77 if (this.props.focus) {
81 // TODO this is slow for some reason
86 componentDidUpdate() {
87 if (!this.props.hideNavigationWarnings && this.state.content) {
88 window.onbeforeunload = () => true;
90 window.onbeforeunload = undefined;
94 componentWillReceiveProps(nextProps: MarkdownTextAreaProps) {
95 if (nextProps.finished) {
96 this.state.previewMode = false;
97 this.state.loading = false;
98 this.state.content = "";
99 this.setState(this.state);
100 if (this.props.replyType) {
101 this.props.onReplyCancel();
104 let textarea: any = document.getElementById(this.id);
105 let form: any = document.getElementById(this.formId);
107 setTimeout(() => autosize.update(textarea), 10);
108 this.setState(this.state);
112 componentWillUnmount() {
113 window.onbeforeunload = null;
118 <form id={this.formId} onSubmit={linkEvent(this, this.handleSubmit)}>
120 when={!this.props.hideNavigationWarnings && this.state.content}
121 message={i18n.t("block_leaving")}
123 <div class="form-group row">
124 <div className={`col-sm-12`}>
127 className={`form-control ${this.state.previewMode && "d-none"}`}
128 value={this.state.content}
129 onInput={linkEvent(this, this.handleContentChange)}
130 onPaste={linkEvent(this, this.handleImageUploadPaste)}
132 disabled={this.props.disabled}
134 maxLength={this.props.maxLength || 10000}
135 placeholder={this.props.placeholder}
137 {this.state.previewMode && (
139 className="card border-secondary card-body md-div"
140 dangerouslySetInnerHTML={mdToHtml(this.state.content)}
144 <label class="sr-only" htmlFor={this.id}>
149 <div class="col-sm-12 d-flex flex-wrap">
150 {this.props.buttonTitle && (
153 class="btn btn-sm btn-secondary mr-2"
154 disabled={this.props.disabled || this.state.loading}
156 {this.state.loading ? (
159 <span>{this.props.buttonTitle}</span>
163 {this.props.replyType && (
166 class="btn btn-sm btn-secondary mr-2"
167 onClick={linkEvent(this, this.handleReplyCancel)}
172 {this.state.content && (
174 className={`btn btn-sm btn-secondary mr-2 ${
175 this.state.previewMode && "active"
177 onClick={linkEvent(this, this.handlePreviewToggle)}
182 {/* A flex expander */}
183 <div class="flex-grow-1"></div>
185 class="btn btn-sm text-muted"
186 data-tippy-content={i18n.t("bold")}
187 aria-label={i18n.t("bold")}
188 onClick={linkEvent(this, this.handleInsertBold)}
190 <Icon icon="bold" classes="icon-inline" />
193 class="btn btn-sm text-muted"
194 data-tippy-content={i18n.t("italic")}
195 aria-label={i18n.t("italic")}
196 onClick={linkEvent(this, this.handleInsertItalic)}
198 <Icon icon="italic" classes="icon-inline" />
201 class="btn btn-sm text-muted"
202 data-tippy-content={i18n.t("link")}
203 aria-label={i18n.t("link")}
204 onClick={linkEvent(this, this.handleInsertLink)}
206 <Icon icon="link" classes="icon-inline" />
208 <form class="btn btn-sm text-muted font-weight-bold">
210 htmlFor={`file-upload-${this.id}`}
212 UserService.Instance.localUserView && "pointer"
214 data-tippy-content={i18n.t("upload_image")}
216 {this.state.imageLoading ? (
219 <Icon icon="image" classes="icon-inline" />
223 id={`file-upload-${this.id}`}
225 accept="image/*,video/*"
228 disabled={!UserService.Instance.localUserView}
229 onChange={linkEvent(this, this.handleImageUpload)}
233 class="btn btn-sm text-muted"
234 data-tippy-content={i18n.t("header")}
235 aria-label={i18n.t("header")}
236 onClick={linkEvent(this, this.handleInsertHeader)}
238 <Icon icon="header" classes="icon-inline" />
241 class="btn btn-sm text-muted"
242 data-tippy-content={i18n.t("strikethrough")}
243 aria-label={i18n.t("strikethrough")}
244 onClick={linkEvent(this, this.handleInsertStrikethrough)}
246 <Icon icon="strikethrough" classes="icon-inline" />
249 class="btn btn-sm text-muted"
250 data-tippy-content={i18n.t("quote")}
251 aria-label={i18n.t("quote")}
252 onClick={linkEvent(this, this.handleInsertQuote)}
254 <Icon icon="format_quote" classes="icon-inline" />
257 class="btn btn-sm text-muted"
258 data-tippy-content={i18n.t("list")}
259 aria-label={i18n.t("list")}
260 onClick={linkEvent(this, this.handleInsertList)}
262 <Icon icon="list" classes="icon-inline" />
265 class="btn btn-sm text-muted"
266 data-tippy-content={i18n.t("code")}
267 aria-label={i18n.t("code")}
268 onClick={linkEvent(this, this.handleInsertCode)}
270 <Icon icon="code" classes="icon-inline" />
273 class="btn btn-sm text-muted"
274 data-tippy-content={i18n.t("subscript")}
275 aria-label={i18n.t("subscript")}
276 onClick={linkEvent(this, this.handleInsertSubscript)}
278 <Icon icon="subscript" classes="icon-inline" />
281 class="btn btn-sm text-muted"
282 data-tippy-content={i18n.t("superscript")}
283 aria-label={i18n.t("superscript")}
284 onClick={linkEvent(this, this.handleInsertSuperscript)}
286 <Icon icon="superscript" classes="icon-inline" />
289 class="btn btn-sm text-muted"
290 data-tippy-content={i18n.t("spoiler")}
291 aria-label={i18n.t("spoiler")}
292 onClick={linkEvent(this, this.handleInsertSpoiler)}
294 <Icon icon="alert-triangle" classes="icon-inline" />
297 href={markdownHelpUrl}
298 class="btn btn-sm text-muted font-weight-bold"
299 title={i18n.t("formatting_help")}
302 <Icon icon="help-circle" classes="icon-inline" />
310 handleImageUploadPaste(i: MarkdownTextArea, event: any) {
311 let image = event.clipboardData.files[0];
313 i.handleImageUpload(i, image);
317 handleImageUpload(i: MarkdownTextArea, event: any) {
320 event.preventDefault();
321 file = event.target.files[0];
326 const formData = new FormData();
327 formData.append("images[]", file);
329 i.state.imageLoading = true;
336 .then(res => res.json())
338 console.log("pictrs upload:");
340 if (res.msg == "ok") {
341 let hash = res.files[0].file;
342 let url = `${pictrsUri}/${hash}`;
343 let deleteToken = res.files[0].delete_token;
344 let deleteUrl = `${pictrsUri}/delete/${deleteToken}/${hash}`;
345 let imageMarkdown = `![](${url})`;
346 let content = i.state.content;
347 content = content ? `${content}\n${imageMarkdown}` : imageMarkdown;
348 i.state.content = content;
349 i.state.imageLoading = false;
352 let textarea: any = document.getElementById(i.id);
353 autosize.update(textarea);
355 i18n.t("click_to_delete_picture"),
356 i18n.t("picture_deleted"),
360 i.state.imageLoading = false;
362 toast(JSON.stringify(res), "danger");
366 i.state.imageLoading = false;
368 toast(error, "danger");
373 if (this.props.onContentChange) {
374 this.props.onContentChange(this.state.content);
378 handleContentChange(i: MarkdownTextArea, event: any) {
379 i.state.content = event.target.value;
384 handlePreviewToggle(i: MarkdownTextArea, event: any) {
385 event.preventDefault();
386 i.state.previewMode = !i.state.previewMode;
390 handleSubmit(i: MarkdownTextArea, event: any) {
391 event.preventDefault();
392 i.state.loading = true;
394 let msg = { val: i.state.content, formId: i.formId };
395 i.props.onSubmit(msg);
398 handleReplyCancel(i: MarkdownTextArea) {
399 i.props.onReplyCancel();
402 handleInsertLink(i: MarkdownTextArea, event: any) {
403 event.preventDefault();
404 if (!i.state.content) {
405 i.state.content = "";
407 let textarea: any = document.getElementById(i.id);
408 let start: number = textarea.selectionStart;
409 let end: number = textarea.selectionEnd;
412 let selectedText = i.state.content.substring(start, end);
413 i.state.content = `${i.state.content.substring(
416 )}[${selectedText}]()${i.state.content.substring(end)}`;
418 setTimeout(() => (textarea.selectionEnd = end + 3), 10);
420 i.state.content += "[]()";
422 setTimeout(() => (textarea.selectionEnd -= 1), 10);
428 simpleSurround(chars: string) {
429 this.simpleSurroundBeforeAfter(chars, chars);
432 simpleBeginningofLine(chars: string) {
433 this.simpleSurroundBeforeAfter(`${chars} `, "", "");
436 simpleSurroundBeforeAfter(
441 if (!this.state.content) {
442 this.state.content = "";
444 let textarea: any = document.getElementById(this.id);
445 let start: number = textarea.selectionStart;
446 let end: number = textarea.selectionEnd;
449 let selectedText = this.state.content.substring(start, end);
450 this.state.content = `${this.state.content.substring(
453 )}${beforeChars}${selectedText}${afterChars}${this.state.content.substring(
457 this.state.content += `${beforeChars}${emptyChars}${afterChars}`;
459 this.contentChange();
460 this.setState(this.state);
462 autosize.update(textarea);
466 handleInsertBold(i: MarkdownTextArea, event: any) {
467 event.preventDefault();
468 i.simpleSurround("**");
471 handleInsertItalic(i: MarkdownTextArea, event: any) {
472 event.preventDefault();
473 i.simpleSurround("*");
476 handleInsertCode(i: MarkdownTextArea, event: any) {
477 event.preventDefault();
478 if (i.getSelectedText().split(/\r*\n/).length > 1){
479 i.simpleSurroundBeforeAfter("```\n", "\n```");
481 i.simpleSurround('`');
485 handleInsertStrikethrough(i: MarkdownTextArea, event: any) {
486 event.preventDefault();
487 i.simpleSurround("~~");
490 handleInsertList(i: MarkdownTextArea, event: any) {
491 event.preventDefault();
492 i.simpleBeginningofLine("-");
495 handleInsertQuote(i: MarkdownTextArea, event: any) {
496 event.preventDefault();
497 i.simpleBeginningofLine(">");
500 handleInsertHeader(i: MarkdownTextArea, event: any) {
501 event.preventDefault();
502 i.simpleBeginningofLine("#");
505 handleInsertSubscript(i: MarkdownTextArea, event: any) {
506 event.preventDefault();
507 i.simpleSurround("~");
510 handleInsertSuperscript(i: MarkdownTextArea, event: any) {
511 event.preventDefault();
512 i.simpleSurround("^");
515 simpleInsert(chars: string) {
516 if (!this.state.content) {
517 this.state.content = `${chars} `;
519 this.state.content += `\n${chars} `;
522 let textarea: any = document.getElementById(this.id);
525 autosize.update(textarea);
527 this.contentChange();
528 this.setState(this.state);
531 handleInsertSpoiler(i: MarkdownTextArea, event: any) {
532 event.preventDefault();
533 let beforeChars = `\n::: spoiler ${i18n.t("spoiler")}\n`;
534 let afterChars = "\n:::\n";
535 i.simpleSurroundBeforeAfter(beforeChars, afterChars);
539 let textarea: any = document.getElementById(this.id);
540 let selectedText = window.getSelection().toString();
546 .join("\n") + "\n\n";
547 if (this.state.content == null) {
548 this.state.content = "";
550 this.state.content += "\n";
552 this.state.content += quotedText;
553 this.contentChange();
554 this.setState(this.state);
555 // Not sure why this needs a delay
556 setTimeout(() => autosize.update(textarea), 10);
560 getSelectedText(): string {
561 let textarea: any = document.getElementById(this.id);
562 let start: number = textarea.selectionStart;
563 let end: number = textarea.selectionEnd;
564 return start !== end ? this.state.content.substring(start, end) : '';