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}`}
209 className={`mb-0 ${UserService.Instance.user && "pointer"}`}
210 data-tippy-content={i18n.t("upload_image")}
212 {this.state.imageLoading ? (
215 <Icon icon="image" classes="icon-inline" />
219 id={`file-upload-${this.id}`}
221 accept="image/*,video/*"
224 disabled={!UserService.Instance.user}
225 onChange={linkEvent(this, this.handleImageUpload)}
229 class="btn btn-sm text-muted"
230 data-tippy-content={i18n.t("header")}
231 aria-label={i18n.t("header")}
232 onClick={linkEvent(this, this.handleInsertHeader)}
234 <Icon icon="header" classes="icon-inline" />
237 class="btn btn-sm text-muted"
238 data-tippy-content={i18n.t("strikethrough")}
239 aria-label={i18n.t("strikethrough")}
240 onClick={linkEvent(this, this.handleInsertStrikethrough)}
242 <Icon icon="strikethrough" classes="icon-inline" />
245 class="btn btn-sm text-muted"
246 data-tippy-content={i18n.t("quote")}
247 aria-label={i18n.t("quote")}
248 onClick={linkEvent(this, this.handleInsertQuote)}
250 <Icon icon="format_quote" classes="icon-inline" />
253 class="btn btn-sm text-muted"
254 data-tippy-content={i18n.t("list")}
255 aria-label={i18n.t("list")}
256 onClick={linkEvent(this, this.handleInsertList)}
258 <Icon icon="list" classes="icon-inline" />
261 class="btn btn-sm text-muted"
262 data-tippy-content={i18n.t("code")}
263 aria-label={i18n.t("code")}
264 onClick={linkEvent(this, this.handleInsertCode)}
266 <Icon icon="code" classes="icon-inline" />
269 class="btn btn-sm text-muted"
270 data-tippy-content={i18n.t("subscript")}
271 aria-label={i18n.t("subscript")}
272 onClick={linkEvent(this, this.handleInsertSubscript)}
274 <Icon icon="subscript" classes="icon-inline" />
277 class="btn btn-sm text-muted"
278 data-tippy-content={i18n.t("superscript")}
279 aria-label={i18n.t("superscript")}
280 onClick={linkEvent(this, this.handleInsertSuperscript)}
282 <Icon icon="superscript" classes="icon-inline" />
285 class="btn btn-sm text-muted"
286 data-tippy-content={i18n.t("spoiler")}
287 aria-label={i18n.t("spoiler")}
288 onClick={linkEvent(this, this.handleInsertSpoiler)}
290 <Icon icon="alert-triangle" classes="icon-inline" />
293 href={markdownHelpUrl}
294 class="btn btn-sm text-muted font-weight-bold"
295 title={i18n.t("formatting_help")}
298 <Icon icon="help-circle" classes="icon-inline" />
306 handleImageUploadPaste(i: MarkdownTextArea, event: any) {
307 let image = event.clipboardData.files[0];
309 i.handleImageUpload(i, image);
313 handleImageUpload(i: MarkdownTextArea, event: any) {
316 event.preventDefault();
317 file = event.target.files[0];
322 const formData = new FormData();
323 formData.append("images[]", file);
325 i.state.imageLoading = true;
332 .then(res => res.json())
334 console.log("pictrs upload:");
336 if (res.msg == "ok") {
337 let hash = res.files[0].file;
338 let url = `${pictrsUri}/${hash}`;
339 let deleteToken = res.files[0].delete_token;
340 let deleteUrl = `${pictrsUri}/delete/${deleteToken}/${hash}`;
341 let imageMarkdown = `![](${url})`;
342 let content = i.state.content;
343 content = content ? `${content}\n${imageMarkdown}` : imageMarkdown;
344 i.state.content = content;
345 i.state.imageLoading = false;
347 let textarea: any = document.getElementById(i.id);
348 autosize.update(textarea);
350 i18n.t("click_to_delete_picture"),
351 i18n.t("picture_deleted"),
355 i.state.imageLoading = false;
357 toast(JSON.stringify(res), "danger");
361 i.state.imageLoading = false;
363 toast(error, "danger");
367 handleContentChange(i: MarkdownTextArea, event: any) {
368 i.state.content = event.target.value;
370 if (i.props.onContentChange) {
371 i.props.onContentChange(i.state.content);
375 handlePreviewToggle(i: MarkdownTextArea, event: any) {
376 event.preventDefault();
377 i.state.previewMode = !i.state.previewMode;
381 handleSubmit(i: MarkdownTextArea, event: any) {
382 event.preventDefault();
383 i.state.loading = true;
385 let msg = { val: i.state.content, formId: i.formId };
386 i.props.onSubmit(msg);
389 handleReplyCancel(i: MarkdownTextArea) {
390 i.props.onReplyCancel();
393 handleInsertLink(i: MarkdownTextArea, event: any) {
394 event.preventDefault();
395 if (!i.state.content) {
396 i.state.content = "";
398 let textarea: any = document.getElementById(i.id);
399 let start: number = textarea.selectionStart;
400 let end: number = textarea.selectionEnd;
403 let selectedText = i.state.content.substring(start, end);
404 i.state.content = `${i.state.content.substring(
407 )}[${selectedText}]()${i.state.content.substring(end)}`;
409 setTimeout(() => (textarea.selectionEnd = end + 3), 10);
411 i.state.content += "[]()";
413 setTimeout(() => (textarea.selectionEnd -= 1), 10);
418 simpleSurround(chars: string) {
419 this.simpleSurroundBeforeAfter(chars, chars);
422 simpleBeginningofLine(chars: string) {
423 this.simpleSurroundBeforeAfter(`${chars} `, "", "");
426 simpleSurroundBeforeAfter(
431 if (!this.state.content) {
432 this.state.content = "";
434 let textarea: any = document.getElementById(this.id);
435 let start: number = textarea.selectionStart;
436 let end: number = textarea.selectionEnd;
439 let selectedText = this.state.content.substring(start, end);
440 this.state.content = `${this.state.content.substring(
443 )}${beforeChars}${selectedText}${afterChars}${this.state.content.substring(
447 this.state.content += `${beforeChars}${emptyChars}${afterChars}`;
449 this.setState(this.state);
451 autosize.update(textarea);
455 handleInsertBold(i: MarkdownTextArea, event: any) {
456 event.preventDefault();
457 i.simpleSurround("**");
460 handleInsertItalic(i: MarkdownTextArea, event: any) {
461 event.preventDefault();
462 i.simpleSurround("*");
465 handleInsertCode(i: MarkdownTextArea, event: any) {
466 event.preventDefault();
467 i.simpleSurround("`");
470 handleInsertStrikethrough(i: MarkdownTextArea, event: any) {
471 event.preventDefault();
472 i.simpleSurround("~~");
475 handleInsertList(i: MarkdownTextArea, event: any) {
476 event.preventDefault();
477 i.simpleBeginningofLine("-");
480 handleInsertQuote(i: MarkdownTextArea, event: any) {
481 event.preventDefault();
482 i.simpleBeginningofLine(">");
485 handleInsertHeader(i: MarkdownTextArea, event: any) {
486 event.preventDefault();
487 i.simpleBeginningofLine("#");
490 handleInsertSubscript(i: MarkdownTextArea, event: any) {
491 event.preventDefault();
492 i.simpleSurround("~");
495 handleInsertSuperscript(i: MarkdownTextArea, event: any) {
496 event.preventDefault();
497 i.simpleSurround("^");
500 simpleInsert(chars: string) {
501 if (!this.state.content) {
502 this.state.content = `${chars} `;
504 this.state.content += `\n${chars} `;
507 let textarea: any = document.getElementById(this.id);
510 autosize.update(textarea);
512 this.setState(this.state);
515 handleInsertSpoiler(i: MarkdownTextArea, event: any) {
516 event.preventDefault();
517 let beforeChars = `\n::: spoiler ${i18n.t("spoiler")}\n`;
518 let afterChars = "\n:::\n";
519 i.simpleSurroundBeforeAfter(beforeChars, afterChars);
523 let textarea: any = document.getElementById(this.id);
524 let selectedText = window.getSelection().toString();
530 .join("\n") + "\n\n";
531 if (this.state.content == null) {
532 this.state.content = "";
534 this.state.content += "\n";
536 this.state.content += quotedText;
537 this.setState(this.state);
538 // Not sure why this needs a delay
539 setTimeout(() => autosize.update(textarea), 10);