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;
24 onSubmit?(msg: { val: string; formId: string }): any;
25 onContentChange?(val: string): any;
26 onReplyCancel?(): any;
29 interface MarkdownTextAreaState {
33 imageLoading: boolean;
36 export class MarkdownTextArea extends Component<
37 MarkdownTextAreaProps,
40 private id = `comment-textarea-${randomStr()}`;
41 private formId = `comment-form-${randomStr()}`;
42 private tribute: Tribute;
43 private emptyState: MarkdownTextAreaState = {
44 content: this.props.initialContent,
50 constructor(props: any, context: any) {
51 super(props, context);
53 this.tribute = setupTribute();
54 this.state = this.emptyState;
58 let textarea: any = document.getElementById(this.id);
61 this.tribute.attach(textarea);
62 textarea.addEventListener('tribute-replaced', () => {
63 this.state.content = textarea.value;
64 this.setState(this.state);
65 autosize.update(textarea);
70 if (this.props.focus) {
74 // TODO this is slow for some reason
79 componentDidUpdate() {
80 if (this.state.content) {
81 window.onbeforeunload = () => true;
83 window.onbeforeunload = undefined;
87 componentWillReceiveProps(nextProps: MarkdownTextAreaProps) {
88 if (nextProps.finished) {
89 this.state.previewMode = false;
90 this.state.loading = false;
91 this.state.content = '';
92 this.setState(this.state);
93 if (this.props.replyType) {
94 this.props.onReplyCancel();
97 let textarea: any = document.getElementById(this.id);
98 let form: any = document.getElementById(this.formId);
100 setTimeout(() => autosize.update(textarea), 10);
101 this.setState(this.state);
105 componentWillUnmount() {
106 window.onbeforeunload = null;
111 <form id={this.formId} onSubmit={linkEvent(this, this.handleSubmit)}>
112 <Prompt when={this.state.content} message={i18n.t('block_leaving')} />
113 <div class="form-group row">
114 <div className={`col-sm-12`}>
117 className={`form-control ${this.state.previewMode && 'd-none'}`}
118 value={this.state.content}
119 onInput={linkEvent(this, this.handleContentChange)}
120 onPaste={linkEvent(this, this.handleImageUploadPaste)}
122 disabled={this.props.disabled}
126 {this.state.previewMode && (
128 className="card card-body md-div"
129 dangerouslySetInnerHTML={mdToHtml(this.state.content)}
135 <div class="col-sm-12 d-flex flex-wrap">
136 {this.props.buttonTitle && (
139 class="btn btn-sm btn-secondary mr-2"
140 disabled={this.props.disabled || this.state.loading}
142 {this.state.loading ? (
143 <svg class="icon icon-spinner spin">
144 <use xlinkHref="#icon-spinner"></use>
147 <span>{this.props.buttonTitle}</span>
151 {this.props.replyType && (
154 class="btn btn-sm btn-secondary mr-2"
155 onClick={linkEvent(this, this.handleReplyCancel)}
160 {this.state.content && (
162 className={`btn btn-sm btn-secondary mr-2 ${
163 this.state.previewMode && 'active'
165 onClick={linkEvent(this, this.handlePreviewToggle)}
170 {/* A flex expander */}
171 <div class="flex-grow-1"></div>
173 class="btn btn-sm text-muted"
174 data-tippy-content={i18n.t('bold')}
175 onClick={linkEvent(this, this.handleInsertBold)}
177 <svg class="icon icon-inline">
178 <use xlinkHref="#icon-bold"></use>
182 class="btn btn-sm text-muted"
183 data-tippy-content={i18n.t('italic')}
184 onClick={linkEvent(this, this.handleInsertItalic)}
186 <svg class="icon icon-inline">
187 <use xlinkHref="#icon-italic"></use>
191 class="btn btn-sm text-muted"
192 data-tippy-content={i18n.t('link')}
193 onClick={linkEvent(this, this.handleInsertLink)}
195 <svg class="icon icon-inline">
196 <use xlinkHref="#icon-link"></use>
199 <form class="btn btn-sm text-muted font-weight-bold">
201 htmlFor={`file-upload-${this.id}`}
202 className={`mb-0 ${UserService.Instance.user && 'pointer'}`}
203 data-tippy-content={i18n.t('upload_image')}
205 {this.state.imageLoading ? (
206 <svg class="icon icon-spinner spin">
207 <use xlinkHref="#icon-spinner"></use>
210 <svg class="icon icon-inline">
211 <use xlinkHref="#icon-image"></use>
216 id={`file-upload-${this.id}`}
218 accept="image/*,video/*"
221 disabled={!UserService.Instance.user}
222 onChange={linkEvent(this, this.handleImageUpload)}
226 class="btn btn-sm text-muted"
227 data-tippy-content={i18n.t('header')}
228 onClick={linkEvent(this, this.handleInsertHeader)}
230 <svg class="icon icon-inline">
231 <use xlinkHref="#icon-header"></use>
235 class="btn btn-sm text-muted"
236 data-tippy-content={i18n.t('strikethrough')}
237 onClick={linkEvent(this, this.handleInsertStrikethrough)}
239 <svg class="icon icon-inline">
240 <use xlinkHref="#icon-strikethrough"></use>
244 class="btn btn-sm text-muted"
245 data-tippy-content={i18n.t('quote')}
246 onClick={linkEvent(this, this.handleInsertQuote)}
248 <svg class="icon icon-inline">
249 <use xlinkHref="#icon-format_quote"></use>
253 class="btn btn-sm text-muted"
254 data-tippy-content={i18n.t('list')}
255 onClick={linkEvent(this, this.handleInsertList)}
257 <svg class="icon icon-inline">
258 <use xlinkHref="#icon-list"></use>
262 class="btn btn-sm text-muted"
263 data-tippy-content={i18n.t('code')}
264 onClick={linkEvent(this, this.handleInsertCode)}
266 <svg class="icon icon-inline">
267 <use xlinkHref="#icon-code"></use>
271 class="btn btn-sm text-muted"
272 data-tippy-content={i18n.t('subscript')}
273 onClick={linkEvent(this, this.handleInsertSubscript)}
275 <svg class="icon icon-inline">
276 <use xlinkHref="#icon-subscript"></use>
280 class="btn btn-sm text-muted"
281 data-tippy-content={i18n.t('superscript')}
282 onClick={linkEvent(this, this.handleInsertSuperscript)}
284 <svg class="icon icon-inline">
285 <use xlinkHref="#icon-superscript"></use>
289 class="btn btn-sm text-muted"
290 data-tippy-content={i18n.t('spoiler')}
291 onClick={linkEvent(this, this.handleInsertSpoiler)}
293 <svg class="icon icon-inline">
294 <use xlinkHref="#icon-alert-triangle"></use>
298 href={markdownHelpUrl}
300 class="btn btn-sm text-muted font-weight-bold"
301 title={i18n.t('formatting_help')}
304 <svg class="icon icon-inline">
305 <use xlinkHref="#icon-help-circle"></use>
314 handleImageUploadPaste(i: MarkdownTextArea, event: any) {
315 let image = event.clipboardData.files[0];
317 i.handleImageUpload(i, image);
321 handleImageUpload(i: MarkdownTextArea, event: any) {
324 event.preventDefault();
325 file = event.target.files[0];
330 const imageUploadUrl = `/pictrs/image`;
331 const formData = new FormData();
332 formData.append('images[]', file);
334 i.state.imageLoading = true;
337 fetch(imageUploadUrl, {
341 .then(res => res.json())
343 console.log('pictrs upload:');
345 if (res.msg == 'ok') {
346 let hash = res.files[0].file;
347 let url = `${window.location.origin}/pictrs/image/${hash}`;
348 let deleteToken = res.files[0].delete_token;
349 let deleteUrl = `${window.location.origin}/pictrs/image/delete/${deleteToken}/${hash}`;
350 let imageMarkdown = `![](${url})`;
351 let content = i.state.content;
352 content = content ? `${content}\n${imageMarkdown}` : imageMarkdown;
353 i.state.content = content;
354 i.state.imageLoading = false;
356 let textarea: any = document.getElementById(i.id);
357 autosize.update(textarea);
359 i18n.t('click_to_delete_picture'),
360 i18n.t('picture_deleted'),
364 i.state.imageLoading = false;
366 toast(JSON.stringify(res), 'danger');
370 i.state.imageLoading = false;
372 toast(error, 'danger');
376 handleContentChange(i: MarkdownTextArea, event: any) {
377 i.state.content = event.target.value;
379 if (i.props.onContentChange) {
380 i.props.onContentChange(i.state.content);
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 + 4), 10);
420 i.state.content += '[]()';
422 setTimeout(() => (textarea.selectionEnd -= 1), 10);
427 simpleSurround(chars: string) {
428 this.simpleSurroundBeforeAfter(chars, chars);
431 simpleSurroundBeforeAfter(beforeChars: string, afterChars: string) {
432 if (!this.state.content) {
433 this.state.content = '';
435 let textarea: any = document.getElementById(this.id);
436 let start: number = textarea.selectionStart;
437 let end: number = textarea.selectionEnd;
440 let selectedText = this.state.content.substring(start, end);
441 this.state.content = `${this.state.content.substring(
444 )} ${beforeChars}${selectedText}${afterChars} ${this.state.content.substring(
448 this.state.content += `${beforeChars}___${afterChars}`;
450 this.setState(this.state);
452 autosize.update(textarea);
456 handleInsertBold(i: MarkdownTextArea, event: any) {
457 event.preventDefault();
458 i.simpleSurround('**');
461 handleInsertItalic(i: MarkdownTextArea, event: any) {
462 event.preventDefault();
463 i.simpleSurround('*');
466 handleInsertCode(i: MarkdownTextArea, event: any) {
467 event.preventDefault();
468 i.simpleSurround('`');
471 handleInsertStrikethrough(i: MarkdownTextArea, event: any) {
472 event.preventDefault();
473 i.simpleSurround('~~');
476 handleInsertList(i: MarkdownTextArea, event: any) {
477 event.preventDefault();
481 handleInsertQuote(i: MarkdownTextArea, event: any) {
482 event.preventDefault();
486 handleInsertHeader(i: MarkdownTextArea, event: any) {
487 event.preventDefault();
491 handleInsertSubscript(i: MarkdownTextArea, event: any) {
492 event.preventDefault();
493 i.simpleSurround('~');
496 handleInsertSuperscript(i: MarkdownTextArea, event: any) {
497 event.preventDefault();
498 i.simpleSurround('^');
501 simpleInsert(chars: string) {
502 if (!this.state.content) {
503 this.state.content = `${chars} `;
505 this.state.content += `\n${chars} `;
508 let textarea: any = document.getElementById(this.id);
511 autosize.update(textarea);
513 this.setState(this.state);
516 handleInsertSpoiler(i: MarkdownTextArea, event: any) {
517 event.preventDefault();
518 let beforeChars = `\n::: spoiler ${i18n.t('spoiler')}\n`;
519 let afterChars = '\n:::\n';
520 i.simpleSurroundBeforeAfter(beforeChars, afterChars);
524 let textarea: any = document.getElementById(this.id);
525 let selectedText = window.getSelection().toString();
531 .join('\n') + '\n\n';
532 this.state.content = quotedText;
533 this.setState(this.state);
534 // Not sure why this needs a delay
535 setTimeout(() => autosize.update(textarea), 10);