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;
30 interface MarkdownTextAreaState {
34 imageLoading: boolean;
37 export class MarkdownTextArea extends Component<
38 MarkdownTextAreaProps,
41 private id = `comment-textarea-${randomStr()}`;
42 private formId = `comment-form-${randomStr()}`;
43 private tribute: Tribute;
44 private emptyState: MarkdownTextAreaState = {
45 content: this.props.initialContent,
51 constructor(props: any, context: any) {
52 super(props, context);
54 this.tribute = setupTribute();
55 this.state = this.emptyState;
59 let textarea: any = document.getElementById(this.id);
62 this.tribute.attach(textarea);
63 textarea.addEventListener('tribute-replaced', () => {
64 this.state.content = textarea.value;
65 this.setState(this.state);
66 autosize.update(textarea);
71 if (this.props.focus) {
75 // TODO this is slow for some reason
80 componentDidUpdate() {
81 if (this.state.content) {
82 window.onbeforeunload = () => true;
84 window.onbeforeunload = undefined;
88 componentWillReceiveProps(nextProps: MarkdownTextAreaProps) {
89 if (nextProps.finished) {
90 this.state.previewMode = false;
91 this.state.loading = false;
92 this.state.content = '';
93 this.setState(this.state);
94 if (this.props.replyType) {
95 this.props.onReplyCancel();
98 let textarea: any = document.getElementById(this.id);
99 let form: any = document.getElementById(this.formId);
101 setTimeout(() => autosize.update(textarea), 10);
102 this.setState(this.state);
106 componentWillUnmount() {
107 window.onbeforeunload = null;
112 <form id={this.formId} onSubmit={linkEvent(this, this.handleSubmit)}>
113 <Prompt when={this.state.content} message={i18n.t('block_leaving')} />
114 <div class="form-group row">
115 <div className={`col-sm-12`}>
118 className={`form-control ${this.state.previewMode && 'd-none'}`}
119 value={this.state.content}
120 onInput={linkEvent(this, this.handleContentChange)}
121 onPaste={linkEvent(this, this.handleImageUploadPaste)}
123 disabled={this.props.disabled}
125 maxLength={this.props.maxLength || 10000}
127 {this.state.previewMode && (
129 className="card bg-transparent border-secondary card-body md-div"
130 dangerouslySetInnerHTML={mdToHtml(this.state.content)}
136 <div class="col-sm-12 d-flex flex-wrap">
137 {this.props.buttonTitle && (
140 class="btn btn-sm btn-secondary mr-2"
141 disabled={this.props.disabled || this.state.loading}
143 {this.state.loading ? (
144 <svg class="icon icon-spinner spin">
145 <use xlinkHref="#icon-spinner"></use>
148 <span>{this.props.buttonTitle}</span>
152 {this.props.replyType && (
155 class="btn btn-sm btn-secondary mr-2"
156 onClick={linkEvent(this, this.handleReplyCancel)}
161 {this.state.content && (
163 className={`btn btn-sm btn-secondary mr-2 ${
164 this.state.previewMode && 'active'
166 onClick={linkEvent(this, this.handlePreviewToggle)}
171 {/* A flex expander */}
172 <div class="flex-grow-1"></div>
174 class="btn btn-sm text-muted"
175 data-tippy-content={i18n.t('bold')}
176 onClick={linkEvent(this, this.handleInsertBold)}
178 <svg class="icon icon-inline">
179 <use xlinkHref="#icon-bold"></use>
183 class="btn btn-sm text-muted"
184 data-tippy-content={i18n.t('italic')}
185 onClick={linkEvent(this, this.handleInsertItalic)}
187 <svg class="icon icon-inline">
188 <use xlinkHref="#icon-italic"></use>
192 class="btn btn-sm text-muted"
193 data-tippy-content={i18n.t('link')}
194 onClick={linkEvent(this, this.handleInsertLink)}
196 <svg class="icon icon-inline">
197 <use xlinkHref="#icon-link"></use>
200 <form class="btn btn-sm text-muted font-weight-bold">
202 htmlFor={`file-upload-${this.id}`}
203 className={`mb-0 ${UserService.Instance.user && 'pointer'}`}
204 data-tippy-content={i18n.t('upload_image')}
206 {this.state.imageLoading ? (
207 <svg class="icon icon-spinner spin">
208 <use xlinkHref="#icon-spinner"></use>
211 <svg class="icon icon-inline">
212 <use xlinkHref="#icon-image"></use>
217 id={`file-upload-${this.id}`}
219 accept="image/*,video/*"
222 disabled={!UserService.Instance.user}
223 onChange={linkEvent(this, this.handleImageUpload)}
227 class="btn btn-sm text-muted"
228 data-tippy-content={i18n.t('header')}
229 onClick={linkEvent(this, this.handleInsertHeader)}
231 <svg class="icon icon-inline">
232 <use xlinkHref="#icon-header"></use>
236 class="btn btn-sm text-muted"
237 data-tippy-content={i18n.t('strikethrough')}
238 onClick={linkEvent(this, this.handleInsertStrikethrough)}
240 <svg class="icon icon-inline">
241 <use xlinkHref="#icon-strikethrough"></use>
245 class="btn btn-sm text-muted"
246 data-tippy-content={i18n.t('quote')}
247 onClick={linkEvent(this, this.handleInsertQuote)}
249 <svg class="icon icon-inline">
250 <use xlinkHref="#icon-format_quote"></use>
254 class="btn btn-sm text-muted"
255 data-tippy-content={i18n.t('list')}
256 onClick={linkEvent(this, this.handleInsertList)}
258 <svg class="icon icon-inline">
259 <use xlinkHref="#icon-list"></use>
263 class="btn btn-sm text-muted"
264 data-tippy-content={i18n.t('code')}
265 onClick={linkEvent(this, this.handleInsertCode)}
267 <svg class="icon icon-inline">
268 <use xlinkHref="#icon-code"></use>
272 class="btn btn-sm text-muted"
273 data-tippy-content={i18n.t('subscript')}
274 onClick={linkEvent(this, this.handleInsertSubscript)}
276 <svg class="icon icon-inline">
277 <use xlinkHref="#icon-subscript"></use>
281 class="btn btn-sm text-muted"
282 data-tippy-content={i18n.t('superscript')}
283 onClick={linkEvent(this, this.handleInsertSuperscript)}
285 <svg class="icon icon-inline">
286 <use xlinkHref="#icon-superscript"></use>
290 class="btn btn-sm text-muted"
291 data-tippy-content={i18n.t('spoiler')}
292 onClick={linkEvent(this, this.handleInsertSpoiler)}
294 <svg class="icon icon-inline">
295 <use xlinkHref="#icon-alert-triangle"></use>
299 href={markdownHelpUrl}
301 class="btn btn-sm text-muted font-weight-bold"
302 title={i18n.t('formatting_help')}
305 <svg class="icon icon-inline">
306 <use xlinkHref="#icon-help-circle"></use>
315 handleImageUploadPaste(i: MarkdownTextArea, event: any) {
316 let image = event.clipboardData.files[0];
318 i.handleImageUpload(i, image);
322 handleImageUpload(i: MarkdownTextArea, event: any) {
325 event.preventDefault();
326 file = event.target.files[0];
331 const imageUploadUrl = `/pictrs/image`;
332 const formData = new FormData();
333 formData.append('images[]', file);
335 i.state.imageLoading = true;
338 fetch(imageUploadUrl, {
342 .then(res => res.json())
344 console.log('pictrs upload:');
346 if (res.msg == 'ok') {
347 let hash = res.files[0].file;
348 let url = `${window.location.origin}/pictrs/image/${hash}`;
349 let deleteToken = res.files[0].delete_token;
350 let deleteUrl = `${window.location.origin}/pictrs/image/delete/${deleteToken}/${hash}`;
351 let imageMarkdown = `![](${url})`;
352 let content = i.state.content;
353 content = content ? `${content}\n${imageMarkdown}` : imageMarkdown;
354 i.state.content = content;
355 i.state.imageLoading = false;
357 let textarea: any = document.getElementById(i.id);
358 autosize.update(textarea);
360 i18n.t('click_to_delete_picture'),
361 i18n.t('picture_deleted'),
365 i.state.imageLoading = false;
367 toast(JSON.stringify(res), 'danger');
371 i.state.imageLoading = false;
373 toast(error, 'danger');
377 handleContentChange(i: MarkdownTextArea, event: any) {
378 i.state.content = event.target.value;
380 if (i.props.onContentChange) {
381 i.props.onContentChange(i.state.content);
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 + 4), 10);
421 i.state.content += '[]()';
423 setTimeout(() => (textarea.selectionEnd -= 1), 10);
428 simpleSurround(chars: string) {
429 this.simpleSurroundBeforeAfter(chars, chars);
432 simpleSurroundBeforeAfter(beforeChars: string, afterChars: string) {
433 if (!this.state.content) {
434 this.state.content = '';
436 let textarea: any = document.getElementById(this.id);
437 let start: number = textarea.selectionStart;
438 let end: number = textarea.selectionEnd;
441 let selectedText = this.state.content.substring(start, end);
442 this.state.content = `${this.state.content.substring(
445 )} ${beforeChars}${selectedText}${afterChars} ${this.state.content.substring(
449 this.state.content += `${beforeChars}___${afterChars}`;
451 this.setState(this.state);
453 autosize.update(textarea);
457 handleInsertBold(i: MarkdownTextArea, event: any) {
458 event.preventDefault();
459 i.simpleSurround('**');
462 handleInsertItalic(i: MarkdownTextArea, event: any) {
463 event.preventDefault();
464 i.simpleSurround('*');
467 handleInsertCode(i: MarkdownTextArea, event: any) {
468 event.preventDefault();
469 i.simpleSurround('`');
472 handleInsertStrikethrough(i: MarkdownTextArea, event: any) {
473 event.preventDefault();
474 i.simpleSurround('~~');
477 handleInsertList(i: MarkdownTextArea, event: any) {
478 event.preventDefault();
482 handleInsertQuote(i: MarkdownTextArea, event: any) {
483 event.preventDefault();
487 handleInsertHeader(i: MarkdownTextArea, event: any) {
488 event.preventDefault();
492 handleInsertSubscript(i: MarkdownTextArea, event: any) {
493 event.preventDefault();
494 i.simpleSurround('~');
497 handleInsertSuperscript(i: MarkdownTextArea, event: any) {
498 event.preventDefault();
499 i.simpleSurround('^');
502 simpleInsert(chars: string) {
503 if (!this.state.content) {
504 this.state.content = `${chars} `;
506 this.state.content += `\n${chars} `;
509 let textarea: any = document.getElementById(this.id);
512 autosize.update(textarea);
514 this.setState(this.state);
517 handleInsertSpoiler(i: MarkdownTextArea, event: any) {
518 event.preventDefault();
519 let beforeChars = `\n::: spoiler ${i18n.t('spoiler')}\n`;
520 let afterChars = '\n:::\n';
521 i.simpleSurroundBeforeAfter(beforeChars, afterChars);
525 let textarea: any = document.getElementById(this.id);
526 let selectedText = window.getSelection().toString();
532 .join('\n') + '\n\n';
533 this.state.content = quotedText;
534 this.setState(this.state);
535 // Not sure why this needs a delay
536 setTimeout(() => autosize.update(textarea), 10);