1 import autosize from "autosize";
2 import { Component, linkEvent } from "inferno";
3 import { Prompt } from "inferno-router";
4 import { pictrsUri } from "../../env";
5 import { i18n } from "../../i18next";
6 import { UserService } from "../../services";
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.myUserInfo && "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.myUserInfo}
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 console.error(error);
369 toast(error, "danger");
374 if (this.props.onContentChange) {
375 this.props.onContentChange(this.state.content);
379 handleContentChange(i: MarkdownTextArea, event: any) {
380 i.state.content = event.target.value;
385 handlePreviewToggle(i: MarkdownTextArea, event: any) {
386 event.preventDefault();
387 i.state.previewMode = !i.state.previewMode;
391 handleSubmit(i: MarkdownTextArea, event: any) {
392 event.preventDefault();
393 i.state.loading = true;
395 let msg = { val: i.state.content, formId: i.formId };
396 i.props.onSubmit(msg);
399 handleReplyCancel(i: MarkdownTextArea) {
400 i.props.onReplyCancel();
403 handleInsertLink(i: MarkdownTextArea, event: any) {
404 event.preventDefault();
405 if (!i.state.content) {
406 i.state.content = "";
408 let textarea: any = document.getElementById(i.id);
409 let start: number = textarea.selectionStart;
410 let end: number = textarea.selectionEnd;
413 let selectedText = i.state.content.substring(start, end);
414 i.state.content = `${i.state.content.substring(
417 )}[${selectedText}]()${i.state.content.substring(end)}`;
419 setTimeout(() => (textarea.selectionEnd = end + 3), 10);
421 i.state.content += "[]()";
423 setTimeout(() => (textarea.selectionEnd -= 1), 10);
429 simpleSurround(chars: string) {
430 this.simpleSurroundBeforeAfter(chars, chars);
433 simpleBeginningofLine(chars: string) {
434 this.simpleSurroundBeforeAfter(`${chars} `, "", "");
437 simpleSurroundBeforeAfter(
442 if (!this.state.content) {
443 this.state.content = "";
445 let textarea: any = document.getElementById(this.id);
446 let start: number = textarea.selectionStart;
447 let end: number = textarea.selectionEnd;
450 let selectedText = this.state.content.substring(start, end);
451 this.state.content = `${this.state.content.substring(
454 )}${beforeChars}${selectedText}${afterChars}${this.state.content.substring(
458 this.state.content += `${beforeChars}${emptyChars}${afterChars}`;
460 this.contentChange();
461 this.setState(this.state);
466 textarea.setSelectionRange(
467 start + beforeChars.length,
468 end + afterChars.length
471 textarea.setSelectionRange(
472 start + beforeChars.length,
473 end + emptyChars.length + afterChars.length
478 autosize.update(textarea);
482 handleInsertBold(i: MarkdownTextArea, event: any) {
483 event.preventDefault();
484 i.simpleSurround("**");
487 handleInsertItalic(i: MarkdownTextArea, event: any) {
488 event.preventDefault();
489 i.simpleSurround("*");
492 handleInsertCode(i: MarkdownTextArea, event: any) {
493 event.preventDefault();
494 if (i.getSelectedText().split(/\r*\n/).length > 1) {
495 i.simpleSurroundBeforeAfter("```\n", "\n```");
497 i.simpleSurround("`");
501 handleInsertStrikethrough(i: MarkdownTextArea, event: any) {
502 event.preventDefault();
503 i.simpleSurround("~~");
506 handleInsertList(i: MarkdownTextArea, event: any) {
507 event.preventDefault();
508 i.simpleBeginningofLine("-");
511 handleInsertQuote(i: MarkdownTextArea, event: any) {
512 event.preventDefault();
513 i.simpleBeginningofLine(">");
516 handleInsertHeader(i: MarkdownTextArea, event: any) {
517 event.preventDefault();
518 i.simpleBeginningofLine("#");
521 handleInsertSubscript(i: MarkdownTextArea, event: any) {
522 event.preventDefault();
523 i.simpleSurround("~");
526 handleInsertSuperscript(i: MarkdownTextArea, event: any) {
527 event.preventDefault();
528 i.simpleSurround("^");
531 simpleInsert(chars: string) {
532 if (!this.state.content) {
533 this.state.content = `${chars} `;
535 this.state.content += `\n${chars} `;
538 let textarea: any = document.getElementById(this.id);
541 autosize.update(textarea);
543 this.contentChange();
544 this.setState(this.state);
547 handleInsertSpoiler(i: MarkdownTextArea, event: any) {
548 event.preventDefault();
549 let beforeChars = `\n::: spoiler ${i18n.t("spoiler")}\n`;
550 let afterChars = "\n:::\n";
551 i.simpleSurroundBeforeAfter(beforeChars, afterChars);
555 let textarea: any = document.getElementById(this.id);
556 let selectedText = window.getSelection().toString();
562 .join("\n") + "\n\n";
563 if (this.state.content == null) {
564 this.state.content = "";
566 this.state.content += "\n";
568 this.state.content += quotedText;
569 this.contentChange();
570 this.setState(this.state);
571 // Not sure why this needs a delay
572 setTimeout(() => autosize.update(textarea), 10);
576 getSelectedText(): string {
577 let textarea: any = document.getElementById(this.id);
578 let start: number = textarea.selectionStart;
579 let end: number = textarea.selectionEnd;
580 return start !== end ? this.state.content.substring(start, end) : "";