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;
33 interface MarkdownTextAreaState {
37 imageLoading: boolean;
40 export class MarkdownTextArea extends Component<
41 MarkdownTextAreaProps,
44 private id = `comment-textarea-${randomStr()}`;
45 private formId = `comment-form-${randomStr()}`;
47 private emptyState: MarkdownTextAreaState = {
48 content: this.props.initialContent,
54 constructor(props: any, context: any) {
55 super(props, context);
58 this.tribute = setupTribute();
60 this.state = this.emptyState;
64 let textarea: any = document.getElementById(this.id);
67 this.tribute.attach(textarea);
68 textarea.addEventListener("tribute-replaced", () => {
69 this.state.content = textarea.value;
70 this.setState(this.state);
71 autosize.update(textarea);
76 if (this.props.focus) {
80 // TODO this is slow for some reason
85 componentDidUpdate() {
86 if (!this.props.hideNavigationWarnings && this.state.content) {
87 window.onbeforeunload = () => true;
89 window.onbeforeunload = undefined;
93 componentWillReceiveProps(nextProps: MarkdownTextAreaProps) {
94 if (nextProps.finished) {
95 this.state.previewMode = false;
96 this.state.loading = false;
97 this.state.content = "";
98 this.setState(this.state);
99 if (this.props.replyType) {
100 this.props.onReplyCancel();
103 let textarea: any = document.getElementById(this.id);
104 let form: any = document.getElementById(this.formId);
106 setTimeout(() => autosize.update(textarea), 10);
107 this.setState(this.state);
111 componentWillUnmount() {
112 window.onbeforeunload = null;
117 <form id={this.formId} onSubmit={linkEvent(this, this.handleSubmit)}>
119 when={!this.props.hideNavigationWarnings && this.state.content}
120 message={i18n.t("block_leaving")}
122 <div class="form-group row">
123 <div className={`col-sm-12`}>
126 className={`form-control ${this.state.previewMode && "d-none"}`}
127 value={this.state.content}
128 onInput={linkEvent(this, this.handleContentChange)}
129 onPaste={linkEvent(this, this.handleImageUploadPaste)}
131 disabled={this.props.disabled}
133 maxLength={this.props.maxLength || 10000}
135 {this.state.previewMode && (
137 className="card border-secondary card-body md-div"
138 dangerouslySetInnerHTML={mdToHtml(this.state.content)}
142 <label class="sr-only" htmlFor={this.id}>
147 <div class="col-sm-12 d-flex flex-wrap">
148 {this.props.buttonTitle && (
151 class="btn btn-sm btn-secondary mr-2"
152 disabled={this.props.disabled || this.state.loading}
154 {this.state.loading ? (
157 <span>{this.props.buttonTitle}</span>
161 {this.props.replyType && (
164 class="btn btn-sm btn-secondary mr-2"
165 onClick={linkEvent(this, this.handleReplyCancel)}
170 {this.state.content && (
172 className={`btn btn-sm btn-secondary mr-2 ${
173 this.state.previewMode && "active"
175 onClick={linkEvent(this, this.handlePreviewToggle)}
180 {/* A flex expander */}
181 <div class="flex-grow-1"></div>
183 class="btn btn-sm text-muted"
184 data-tippy-content={i18n.t("bold")}
185 aria-label={i18n.t("bold")}
186 onClick={linkEvent(this, this.handleInsertBold)}
188 <Icon icon="bold" classes="icon-inline" />
191 class="btn btn-sm text-muted"
192 data-tippy-content={i18n.t("italic")}
193 aria-label={i18n.t("italic")}
194 onClick={linkEvent(this, this.handleInsertItalic)}
196 <Icon icon="italic" classes="icon-inline" />
199 class="btn btn-sm text-muted"
200 data-tippy-content={i18n.t("link")}
201 aria-label={i18n.t("link")}
202 onClick={linkEvent(this, this.handleInsertLink)}
204 <Icon icon="link" classes="icon-inline" />
206 <form class="btn btn-sm text-muted font-weight-bold">
208 htmlFor={`file-upload-${this.id}`}
210 UserService.Instance.localUserView && "pointer"
212 data-tippy-content={i18n.t("upload_image")}
214 {this.state.imageLoading ? (
217 <Icon icon="image" classes="icon-inline" />
221 id={`file-upload-${this.id}`}
223 accept="image/*,video/*"
226 disabled={!UserService.Instance.localUserView}
227 onChange={linkEvent(this, this.handleImageUpload)}
231 class="btn btn-sm text-muted"
232 data-tippy-content={i18n.t("header")}
233 aria-label={i18n.t("header")}
234 onClick={linkEvent(this, this.handleInsertHeader)}
236 <Icon icon="header" classes="icon-inline" />
239 class="btn btn-sm text-muted"
240 data-tippy-content={i18n.t("strikethrough")}
241 aria-label={i18n.t("strikethrough")}
242 onClick={linkEvent(this, this.handleInsertStrikethrough)}
244 <Icon icon="strikethrough" classes="icon-inline" />
247 class="btn btn-sm text-muted"
248 data-tippy-content={i18n.t("quote")}
249 aria-label={i18n.t("quote")}
250 onClick={linkEvent(this, this.handleInsertQuote)}
252 <Icon icon="format_quote" classes="icon-inline" />
255 class="btn btn-sm text-muted"
256 data-tippy-content={i18n.t("list")}
257 aria-label={i18n.t("list")}
258 onClick={linkEvent(this, this.handleInsertList)}
260 <Icon icon="list" classes="icon-inline" />
263 class="btn btn-sm text-muted"
264 data-tippy-content={i18n.t("code")}
265 aria-label={i18n.t("code")}
266 onClick={linkEvent(this, this.handleInsertCode)}
268 <Icon icon="code" classes="icon-inline" />
271 class="btn btn-sm text-muted"
272 data-tippy-content={i18n.t("subscript")}
273 aria-label={i18n.t("subscript")}
274 onClick={linkEvent(this, this.handleInsertSubscript)}
276 <Icon icon="subscript" classes="icon-inline" />
279 class="btn btn-sm text-muted"
280 data-tippy-content={i18n.t("superscript")}
281 aria-label={i18n.t("superscript")}
282 onClick={linkEvent(this, this.handleInsertSuperscript)}
284 <Icon icon="superscript" classes="icon-inline" />
287 class="btn btn-sm text-muted"
288 data-tippy-content={i18n.t("spoiler")}
289 aria-label={i18n.t("spoiler")}
290 onClick={linkEvent(this, this.handleInsertSpoiler)}
292 <Icon icon="alert-triangle" classes="icon-inline" />
295 href={markdownHelpUrl}
296 class="btn btn-sm text-muted font-weight-bold"
297 title={i18n.t("formatting_help")}
300 <Icon icon="help-circle" classes="icon-inline" />
308 handleImageUploadPaste(i: MarkdownTextArea, event: any) {
309 let image = event.clipboardData.files[0];
311 i.handleImageUpload(i, image);
315 handleImageUpload(i: MarkdownTextArea, event: any) {
318 event.preventDefault();
319 file = event.target.files[0];
324 const formData = new FormData();
325 formData.append("images[]", file);
327 i.state.imageLoading = true;
334 .then(res => res.json())
336 console.log("pictrs upload:");
338 if (res.msg == "ok") {
339 let hash = res.files[0].file;
340 let url = `${pictrsUri}/${hash}`;
341 let deleteToken = res.files[0].delete_token;
342 let deleteUrl = `${pictrsUri}/delete/${deleteToken}/${hash}`;
343 let imageMarkdown = `![](${url})`;
344 let content = i.state.content;
345 content = content ? `${content}\n${imageMarkdown}` : imageMarkdown;
346 i.state.content = content;
347 i.state.imageLoading = false;
350 let textarea: any = document.getElementById(i.id);
351 autosize.update(textarea);
353 i18n.t("click_to_delete_picture"),
354 i18n.t("picture_deleted"),
358 i.state.imageLoading = false;
360 toast(JSON.stringify(res), "danger");
364 i.state.imageLoading = false;
366 toast(error, "danger");
371 if (this.props.onContentChange) {
372 this.props.onContentChange(this.state.content);
376 handleContentChange(i: MarkdownTextArea, event: any) {
377 i.state.content = event.target.value;
382 handlePreviewToggle(i: MarkdownTextArea, event: any) {
383 event.preventDefault();
384 i.state.previewMode = !i.state.previewMode;
388 handleSubmit(i: MarkdownTextArea, event: any) {
389 event.preventDefault();
390 i.state.loading = true;
392 let msg = { val: i.state.content, formId: i.formId };
393 i.props.onSubmit(msg);
396 handleReplyCancel(i: MarkdownTextArea) {
397 i.props.onReplyCancel();
400 handleInsertLink(i: MarkdownTextArea, event: any) {
401 event.preventDefault();
402 if (!i.state.content) {
403 i.state.content = "";
405 let textarea: any = document.getElementById(i.id);
406 let start: number = textarea.selectionStart;
407 let end: number = textarea.selectionEnd;
410 let selectedText = i.state.content.substring(start, end);
411 i.state.content = `${i.state.content.substring(
414 )}[${selectedText}]()${i.state.content.substring(end)}`;
416 setTimeout(() => (textarea.selectionEnd = end + 3), 10);
418 i.state.content += "[]()";
420 setTimeout(() => (textarea.selectionEnd -= 1), 10);
426 simpleSurround(chars: string) {
427 this.simpleSurroundBeforeAfter(chars, chars);
430 simpleBeginningofLine(chars: string) {
431 this.simpleSurroundBeforeAfter(`${chars} `, "", "");
434 simpleSurroundBeforeAfter(
439 if (!this.state.content) {
440 this.state.content = "";
442 let textarea: any = document.getElementById(this.id);
443 let start: number = textarea.selectionStart;
444 let end: number = textarea.selectionEnd;
447 let selectedText = this.state.content.substring(start, end);
448 this.state.content = `${this.state.content.substring(
451 )}${beforeChars}${selectedText}${afterChars}${this.state.content.substring(
455 this.state.content += `${beforeChars}${emptyChars}${afterChars}`;
457 this.contentChange();
458 this.setState(this.state);
460 autosize.update(textarea);
464 handleInsertBold(i: MarkdownTextArea, event: any) {
465 event.preventDefault();
466 i.simpleSurround("**");
469 handleInsertItalic(i: MarkdownTextArea, event: any) {
470 event.preventDefault();
471 i.simpleSurround("*");
474 handleInsertCode(i: MarkdownTextArea, event: any) {
475 event.preventDefault();
476 i.simpleSurround("`");
479 handleInsertStrikethrough(i: MarkdownTextArea, event: any) {
480 event.preventDefault();
481 i.simpleSurround("~~");
484 handleInsertList(i: MarkdownTextArea, event: any) {
485 event.preventDefault();
486 i.simpleBeginningofLine("-");
489 handleInsertQuote(i: MarkdownTextArea, event: any) {
490 event.preventDefault();
491 i.simpleBeginningofLine(">");
494 handleInsertHeader(i: MarkdownTextArea, event: any) {
495 event.preventDefault();
496 i.simpleBeginningofLine("#");
499 handleInsertSubscript(i: MarkdownTextArea, event: any) {
500 event.preventDefault();
501 i.simpleSurround("~");
504 handleInsertSuperscript(i: MarkdownTextArea, event: any) {
505 event.preventDefault();
506 i.simpleSurround("^");
509 simpleInsert(chars: string) {
510 if (!this.state.content) {
511 this.state.content = `${chars} `;
513 this.state.content += `\n${chars} `;
516 let textarea: any = document.getElementById(this.id);
519 autosize.update(textarea);
521 this.contentChange();
522 this.setState(this.state);
525 handleInsertSpoiler(i: MarkdownTextArea, event: any) {
526 event.preventDefault();
527 let beforeChars = `\n::: spoiler ${i18n.t("spoiler")}\n`;
528 let afterChars = "\n:::\n";
529 i.simpleSurroundBeforeAfter(beforeChars, afterChars);
533 let textarea: any = document.getElementById(this.id);
534 let selectedText = window.getSelection().toString();
540 .join("\n") + "\n\n";
541 if (this.state.content == null) {
542 this.state.content = "";
544 this.state.content += "\n";
546 this.state.content += quotedText;
547 this.contentChange();
548 this.setState(this.state);
549 // Not sure why this needs a delay
550 setTimeout(() => autosize.update(textarea), 10);