1 import { Component, linkEvent } from 'inferno';
2 import { Prompt } from 'inferno-router';
12 import { UserService } from '../services';
13 import autosize from 'autosize';
14 import Tribute from 'tributejs/src/Tribute.js';
15 import { i18n } from '../i18next';
17 interface MarkdownTextAreaProps {
18 initialContent: string;
25 onSubmit?(msg: { val: string; formId: string }): any;
26 onContentChange?(val: string): any;
27 onReplyCancel?(): any;
28 hideNavigationWarnings?: boolean;
31 interface MarkdownTextAreaState {
35 imageLoading: boolean;
38 export class MarkdownTextArea extends Component<
39 MarkdownTextAreaProps,
42 private id = `comment-textarea-${randomStr()}`;
43 private formId = `comment-form-${randomStr()}`;
44 private tribute: Tribute;
45 private emptyState: MarkdownTextAreaState = {
46 content: this.props.initialContent,
52 constructor(props: any, context: any) {
53 super(props, context);
56 /* this.tribute = setupTribute(); */
57 this.state = this.emptyState;
61 let textarea: any = document.getElementById(this.id);
64 this.tribute.attach(textarea);
65 textarea.addEventListener('tribute-replaced', () => {
66 this.state.content = textarea.value;
67 this.setState(this.state);
68 autosize.update(textarea);
73 if (this.props.focus) {
77 // TODO this is slow for some reason
82 componentDidUpdate() {
83 if (!this.props.hideNavigationWarnings && this.state.content) {
84 window.onbeforeunload = () => true;
86 window.onbeforeunload = undefined;
90 componentWillReceiveProps(nextProps: MarkdownTextAreaProps) {
91 if (nextProps.finished) {
92 this.state.previewMode = false;
93 this.state.loading = false;
94 this.state.content = '';
95 this.setState(this.state);
96 if (this.props.replyType) {
97 this.props.onReplyCancel();
100 let textarea: any = document.getElementById(this.id);
101 let form: any = document.getElementById(this.formId);
103 setTimeout(() => autosize.update(textarea), 10);
104 this.setState(this.state);
108 componentWillUnmount() {
109 window.onbeforeunload = null;
114 <form id={this.formId} onSubmit={linkEvent(this, this.handleSubmit)}>
116 when={!this.props.hideNavigationWarnings && this.state.content}
117 message={i18n.t('block_leaving')}
119 <div class="form-group row">
120 <div className={`col-sm-12`}>
123 className={`form-control ${this.state.previewMode && 'd-none'}`}
124 value={this.state.content}
125 onInput={linkEvent(this, this.handleContentChange)}
126 onPaste={linkEvent(this, this.handleImageUploadPaste)}
128 disabled={this.props.disabled}
130 maxLength={this.props.maxLength || 10000}
132 {this.state.previewMode && (
134 className="card bg-transparent border-secondary card-body md-div"
135 dangerouslySetInnerHTML={mdToHtml(this.state.content)}
141 <div class="col-sm-12 d-flex flex-wrap">
142 {this.props.buttonTitle && (
145 class="btn btn-sm btn-secondary mr-2"
146 disabled={this.props.disabled || this.state.loading}
148 {this.state.loading ? (
149 <svg class="icon icon-spinner spin">
150 <use xlinkHref="#icon-spinner"></use>
153 <span>{this.props.buttonTitle}</span>
157 {this.props.replyType && (
160 class="btn btn-sm btn-secondary mr-2"
161 onClick={linkEvent(this, this.handleReplyCancel)}
166 {this.state.content && (
168 className={`btn btn-sm btn-secondary mr-2 ${
169 this.state.previewMode && 'active'
171 onClick={linkEvent(this, this.handlePreviewToggle)}
176 {/* A flex expander */}
177 <div class="flex-grow-1"></div>
179 class="btn btn-sm text-muted"
180 data-tippy-content={i18n.t('bold')}
181 onClick={linkEvent(this, this.handleInsertBold)}
183 <svg class="icon icon-inline">
184 <use xlinkHref="#icon-bold"></use>
188 class="btn btn-sm text-muted"
189 data-tippy-content={i18n.t('italic')}
190 onClick={linkEvent(this, this.handleInsertItalic)}
192 <svg class="icon icon-inline">
193 <use xlinkHref="#icon-italic"></use>
197 class="btn btn-sm text-muted"
198 data-tippy-content={i18n.t('link')}
199 onClick={linkEvent(this, this.handleInsertLink)}
201 <svg class="icon icon-inline">
202 <use xlinkHref="#icon-link"></use>
205 <form class="btn btn-sm text-muted font-weight-bold">
207 htmlFor={`file-upload-${this.id}`}
208 className={`mb-0 ${UserService.Instance.user && 'pointer'}`}
209 data-tippy-content={i18n.t('upload_image')}
211 {this.state.imageLoading ? (
212 <svg class="icon icon-spinner spin">
213 <use xlinkHref="#icon-spinner"></use>
216 <svg class="icon icon-inline">
217 <use xlinkHref="#icon-image"></use>
222 id={`file-upload-${this.id}`}
224 accept="image/*,video/*"
227 disabled={!UserService.Instance.user}
228 onChange={linkEvent(this, this.handleImageUpload)}
232 class="btn btn-sm text-muted"
233 data-tippy-content={i18n.t('header')}
234 onClick={linkEvent(this, this.handleInsertHeader)}
236 <svg class="icon icon-inline">
237 <use xlinkHref="#icon-header"></use>
241 class="btn btn-sm text-muted"
242 data-tippy-content={i18n.t('strikethrough')}
243 onClick={linkEvent(this, this.handleInsertStrikethrough)}
245 <svg class="icon icon-inline">
246 <use xlinkHref="#icon-strikethrough"></use>
250 class="btn btn-sm text-muted"
251 data-tippy-content={i18n.t('quote')}
252 onClick={linkEvent(this, this.handleInsertQuote)}
254 <svg class="icon icon-inline">
255 <use xlinkHref="#icon-format_quote"></use>
259 class="btn btn-sm text-muted"
260 data-tippy-content={i18n.t('list')}
261 onClick={linkEvent(this, this.handleInsertList)}
263 <svg class="icon icon-inline">
264 <use xlinkHref="#icon-list"></use>
268 class="btn btn-sm text-muted"
269 data-tippy-content={i18n.t('code')}
270 onClick={linkEvent(this, this.handleInsertCode)}
272 <svg class="icon icon-inline">
273 <use xlinkHref="#icon-code"></use>
277 class="btn btn-sm text-muted"
278 data-tippy-content={i18n.t('subscript')}
279 onClick={linkEvent(this, this.handleInsertSubscript)}
281 <svg class="icon icon-inline">
282 <use xlinkHref="#icon-subscript"></use>
286 class="btn btn-sm text-muted"
287 data-tippy-content={i18n.t('superscript')}
288 onClick={linkEvent(this, this.handleInsertSuperscript)}
290 <svg class="icon icon-inline">
291 <use xlinkHref="#icon-superscript"></use>
295 class="btn btn-sm text-muted"
296 data-tippy-content={i18n.t('spoiler')}
297 onClick={linkEvent(this, this.handleInsertSpoiler)}
299 <svg class="icon icon-inline">
300 <use xlinkHref="#icon-alert-triangle"></use>
304 href={markdownHelpUrl}
306 class="btn btn-sm text-muted font-weight-bold"
307 title={i18n.t('formatting_help')}
310 <svg class="icon icon-inline">
311 <use xlinkHref="#icon-help-circle"></use>
320 handleImageUploadPaste(i: MarkdownTextArea, event: any) {
321 let image = event.clipboardData.files[0];
323 i.handleImageUpload(i, image);
327 handleImageUpload(i: MarkdownTextArea, event: any) {
330 event.preventDefault();
331 file = event.target.files[0];
336 const imageUploadUrl = `/pictrs/image`;
337 const formData = new FormData();
338 formData.append('images[]', file);
340 i.state.imageLoading = true;
343 fetch(imageUploadUrl, {
347 .then(res => res.json())
349 console.log('pictrs upload:');
351 if (res.msg == 'ok') {
352 let hash = res.files[0].file;
353 let url = `${window.location.origin}/pictrs/image/${hash}`;
354 let deleteToken = res.files[0].delete_token;
355 let deleteUrl = `${window.location.origin}/pictrs/image/delete/${deleteToken}/${hash}`;
356 let imageMarkdown = `![](${url})`;
357 let content = i.state.content;
358 content = content ? `${content}\n${imageMarkdown}` : imageMarkdown;
359 i.state.content = content;
360 i.state.imageLoading = false;
362 let textarea: any = document.getElementById(i.id);
363 autosize.update(textarea);
365 i18n.t('click_to_delete_picture'),
366 i18n.t('picture_deleted'),
370 i.state.imageLoading = false;
372 toast(JSON.stringify(res), 'danger');
376 i.state.imageLoading = false;
378 toast(error, 'danger');
382 handleContentChange(i: MarkdownTextArea, event: any) {
383 i.state.content = event.target.value;
385 if (i.props.onContentChange) {
386 i.props.onContentChange(i.state.content);
390 handlePreviewToggle(i: MarkdownTextArea, event: any) {
391 event.preventDefault();
392 i.state.previewMode = !i.state.previewMode;
396 handleSubmit(i: MarkdownTextArea, event: any) {
397 event.preventDefault();
398 i.state.loading = true;
400 let msg = { val: i.state.content, formId: i.formId };
401 i.props.onSubmit(msg);
404 handleReplyCancel(i: MarkdownTextArea) {
405 i.props.onReplyCancel();
408 handleInsertLink(i: MarkdownTextArea, event: any) {
409 event.preventDefault();
410 if (!i.state.content) {
411 i.state.content = '';
413 let textarea: any = document.getElementById(i.id);
414 let start: number = textarea.selectionStart;
415 let end: number = textarea.selectionEnd;
418 let selectedText = i.state.content.substring(start, end);
419 i.state.content = `${i.state.content.substring(
422 )} [${selectedText}]() ${i.state.content.substring(end)}`;
424 setTimeout(() => (textarea.selectionEnd = end + 4), 10);
426 i.state.content += '[]()';
428 setTimeout(() => (textarea.selectionEnd -= 1), 10);
433 simpleSurround(chars: string) {
434 this.simpleSurroundBeforeAfter(chars, chars);
437 simpleSurroundBeforeAfter(beforeChars: string, afterChars: string) {
438 if (!this.state.content) {
439 this.state.content = '';
441 let textarea: any = document.getElementById(this.id);
442 let start: number = textarea.selectionStart;
443 let end: number = textarea.selectionEnd;
446 let selectedText = this.state.content.substring(start, end);
447 this.state.content = `${this.state.content.substring(
450 )} ${beforeChars}${selectedText}${afterChars} ${this.state.content.substring(
454 this.state.content += `${beforeChars}___${afterChars}`;
456 this.setState(this.state);
458 autosize.update(textarea);
462 handleInsertBold(i: MarkdownTextArea, event: any) {
463 event.preventDefault();
464 i.simpleSurround('**');
467 handleInsertItalic(i: MarkdownTextArea, event: any) {
468 event.preventDefault();
469 i.simpleSurround('*');
472 handleInsertCode(i: MarkdownTextArea, event: any) {
473 event.preventDefault();
474 i.simpleSurround('`');
477 handleInsertStrikethrough(i: MarkdownTextArea, event: any) {
478 event.preventDefault();
479 i.simpleSurround('~~');
482 handleInsertList(i: MarkdownTextArea, event: any) {
483 event.preventDefault();
487 handleInsertQuote(i: MarkdownTextArea, event: any) {
488 event.preventDefault();
492 handleInsertHeader(i: MarkdownTextArea, event: any) {
493 event.preventDefault();
497 handleInsertSubscript(i: MarkdownTextArea, event: any) {
498 event.preventDefault();
499 i.simpleSurround('~');
502 handleInsertSuperscript(i: MarkdownTextArea, event: any) {
503 event.preventDefault();
504 i.simpleSurround('^');
507 simpleInsert(chars: string) {
508 if (!this.state.content) {
509 this.state.content = `${chars} `;
511 this.state.content += `\n${chars} `;
514 let textarea: any = document.getElementById(this.id);
517 autosize.update(textarea);
519 this.setState(this.state);
522 handleInsertSpoiler(i: MarkdownTextArea, event: any) {
523 event.preventDefault();
524 let beforeChars = `\n::: spoiler ${i18n.t('spoiler')}\n`;
525 let afterChars = '\n:::\n';
526 i.simpleSurroundBeforeAfter(beforeChars, afterChars);
530 let textarea: any = document.getElementById(this.id);
531 let selectedText = window.getSelection().toString();
537 .join('\n') + '\n\n';
538 this.state.content = quotedText;
539 this.setState(this.state);
540 // Not sure why this needs a delay
541 setTimeout(() => autosize.update(textarea), 10);