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);
55 this.tribute = setupTribute();
56 this.state = this.emptyState;
60 let textarea: any = document.getElementById(this.id);
63 this.tribute.attach(textarea);
64 textarea.addEventListener('tribute-replaced', () => {
65 this.state.content = textarea.value;
66 this.setState(this.state);
67 autosize.update(textarea);
72 if (this.props.focus) {
76 // TODO this is slow for some reason
81 componentDidUpdate() {
82 if (!this.props.hideNavigationWarnings && this.state.content) {
83 window.onbeforeunload = () => true;
85 window.onbeforeunload = undefined;
89 componentWillReceiveProps(nextProps: MarkdownTextAreaProps) {
90 if (nextProps.finished) {
91 this.state.previewMode = false;
92 this.state.loading = false;
93 this.state.content = '';
94 this.setState(this.state);
95 if (this.props.replyType) {
96 this.props.onReplyCancel();
99 let textarea: any = document.getElementById(this.id);
100 let form: any = document.getElementById(this.formId);
102 setTimeout(() => autosize.update(textarea), 10);
103 this.setState(this.state);
107 componentWillUnmount() {
108 window.onbeforeunload = null;
113 <form id={this.formId} onSubmit={linkEvent(this, this.handleSubmit)}>
115 when={!this.props.hideNavigationWarnings && this.state.content}
116 message={i18n.t('block_leaving')}
118 <div class="form-group row">
119 <div className={`col-sm-12`}>
122 className={`form-control ${this.state.previewMode && 'd-none'}`}
123 value={this.state.content}
124 onInput={linkEvent(this, this.handleContentChange)}
125 onPaste={linkEvent(this, this.handleImageUploadPaste)}
127 disabled={this.props.disabled}
129 maxLength={this.props.maxLength || 10000}
131 {this.state.previewMode && (
133 className="card bg-transparent border-secondary card-body md-div"
134 dangerouslySetInnerHTML={mdToHtml(this.state.content)}
140 <div class="col-sm-12 d-flex flex-wrap">
141 {this.props.buttonTitle && (
144 class="btn btn-sm btn-secondary mr-2"
145 disabled={this.props.disabled || this.state.loading}
147 {this.state.loading ? (
148 <svg class="icon icon-spinner spin">
149 <use xlinkHref="#icon-spinner"></use>
152 <span>{this.props.buttonTitle}</span>
156 {this.props.replyType && (
159 class="btn btn-sm btn-secondary mr-2"
160 onClick={linkEvent(this, this.handleReplyCancel)}
165 {this.state.content && (
167 className={`btn btn-sm btn-secondary mr-2 ${
168 this.state.previewMode && 'active'
170 onClick={linkEvent(this, this.handlePreviewToggle)}
175 {/* A flex expander */}
176 <div class="flex-grow-1"></div>
178 class="btn btn-sm text-muted"
179 data-tippy-content={i18n.t('bold')}
180 onClick={linkEvent(this, this.handleInsertBold)}
182 <svg class="icon icon-inline">
183 <use xlinkHref="#icon-bold"></use>
187 class="btn btn-sm text-muted"
188 data-tippy-content={i18n.t('italic')}
189 onClick={linkEvent(this, this.handleInsertItalic)}
191 <svg class="icon icon-inline">
192 <use xlinkHref="#icon-italic"></use>
196 class="btn btn-sm text-muted"
197 data-tippy-content={i18n.t('link')}
198 onClick={linkEvent(this, this.handleInsertLink)}
200 <svg class="icon icon-inline">
201 <use xlinkHref="#icon-link"></use>
204 <form class="btn btn-sm text-muted font-weight-bold">
206 htmlFor={`file-upload-${this.id}`}
207 className={`mb-0 ${UserService.Instance.user && 'pointer'}`}
208 data-tippy-content={i18n.t('upload_image')}
210 {this.state.imageLoading ? (
211 <svg class="icon icon-spinner spin">
212 <use xlinkHref="#icon-spinner"></use>
215 <svg class="icon icon-inline">
216 <use xlinkHref="#icon-image"></use>
221 id={`file-upload-${this.id}`}
223 accept="image/*,video/*"
226 disabled={!UserService.Instance.user}
227 onChange={linkEvent(this, this.handleImageUpload)}
231 class="btn btn-sm text-muted"
232 data-tippy-content={i18n.t('header')}
233 onClick={linkEvent(this, this.handleInsertHeader)}
235 <svg class="icon icon-inline">
236 <use xlinkHref="#icon-header"></use>
240 class="btn btn-sm text-muted"
241 data-tippy-content={i18n.t('strikethrough')}
242 onClick={linkEvent(this, this.handleInsertStrikethrough)}
244 <svg class="icon icon-inline">
245 <use xlinkHref="#icon-strikethrough"></use>
249 class="btn btn-sm text-muted"
250 data-tippy-content={i18n.t('quote')}
251 onClick={linkEvent(this, this.handleInsertQuote)}
253 <svg class="icon icon-inline">
254 <use xlinkHref="#icon-format_quote"></use>
258 class="btn btn-sm text-muted"
259 data-tippy-content={i18n.t('list')}
260 onClick={linkEvent(this, this.handleInsertList)}
262 <svg class="icon icon-inline">
263 <use xlinkHref="#icon-list"></use>
267 class="btn btn-sm text-muted"
268 data-tippy-content={i18n.t('code')}
269 onClick={linkEvent(this, this.handleInsertCode)}
271 <svg class="icon icon-inline">
272 <use xlinkHref="#icon-code"></use>
276 class="btn btn-sm text-muted"
277 data-tippy-content={i18n.t('subscript')}
278 onClick={linkEvent(this, this.handleInsertSubscript)}
280 <svg class="icon icon-inline">
281 <use xlinkHref="#icon-subscript"></use>
285 class="btn btn-sm text-muted"
286 data-tippy-content={i18n.t('superscript')}
287 onClick={linkEvent(this, this.handleInsertSuperscript)}
289 <svg class="icon icon-inline">
290 <use xlinkHref="#icon-superscript"></use>
294 class="btn btn-sm text-muted"
295 data-tippy-content={i18n.t('spoiler')}
296 onClick={linkEvent(this, this.handleInsertSpoiler)}
298 <svg class="icon icon-inline">
299 <use xlinkHref="#icon-alert-triangle"></use>
303 href={markdownHelpUrl}
305 class="btn btn-sm text-muted font-weight-bold"
306 title={i18n.t('formatting_help')}
309 <svg class="icon icon-inline">
310 <use xlinkHref="#icon-help-circle"></use>
319 handleImageUploadPaste(i: MarkdownTextArea, event: any) {
320 let image = event.clipboardData.files[0];
322 i.handleImageUpload(i, image);
326 handleImageUpload(i: MarkdownTextArea, event: any) {
329 event.preventDefault();
330 file = event.target.files[0];
335 const imageUploadUrl = `/pictrs/image`;
336 const formData = new FormData();
337 formData.append('images[]', file);
339 i.state.imageLoading = true;
342 fetch(imageUploadUrl, {
346 .then(res => res.json())
348 console.log('pictrs upload:');
350 if (res.msg == 'ok') {
351 let hash = res.files[0].file;
352 let url = `${window.location.origin}/pictrs/image/${hash}`;
353 let deleteToken = res.files[0].delete_token;
354 let deleteUrl = `${window.location.origin}/pictrs/image/delete/${deleteToken}/${hash}`;
355 let imageMarkdown = `![](${url})`;
356 let content = i.state.content;
357 content = content ? `${content}\n${imageMarkdown}` : imageMarkdown;
358 i.state.content = content;
359 i.state.imageLoading = false;
361 let textarea: any = document.getElementById(i.id);
362 autosize.update(textarea);
364 i18n.t('click_to_delete_picture'),
365 i18n.t('picture_deleted'),
369 i.state.imageLoading = false;
371 toast(JSON.stringify(res), 'danger');
375 i.state.imageLoading = false;
377 toast(error, 'danger');
381 handleContentChange(i: MarkdownTextArea, event: any) {
382 i.state.content = event.target.value;
384 if (i.props.onContentChange) {
385 i.props.onContentChange(i.state.content);
389 handlePreviewToggle(i: MarkdownTextArea, event: any) {
390 event.preventDefault();
391 i.state.previewMode = !i.state.previewMode;
395 handleSubmit(i: MarkdownTextArea, event: any) {
396 event.preventDefault();
397 i.state.loading = true;
399 let msg = { val: i.state.content, formId: i.formId };
400 i.props.onSubmit(msg);
403 handleReplyCancel(i: MarkdownTextArea) {
404 i.props.onReplyCancel();
407 handleInsertLink(i: MarkdownTextArea, event: any) {
408 event.preventDefault();
409 if (!i.state.content) {
410 i.state.content = '';
412 let textarea: any = document.getElementById(i.id);
413 let start: number = textarea.selectionStart;
414 let end: number = textarea.selectionEnd;
417 let selectedText = i.state.content.substring(start, end);
418 i.state.content = `${i.state.content.substring(
421 )} [${selectedText}]() ${i.state.content.substring(end)}`;
423 setTimeout(() => (textarea.selectionEnd = end + 4), 10);
425 i.state.content += '[]()';
427 setTimeout(() => (textarea.selectionEnd -= 1), 10);
432 simpleSurround(chars: string) {
433 this.simpleSurroundBeforeAfter(chars, chars);
436 simpleSurroundBeforeAfter(beforeChars: string, afterChars: string) {
437 if (!this.state.content) {
438 this.state.content = '';
440 let textarea: any = document.getElementById(this.id);
441 let start: number = textarea.selectionStart;
442 let end: number = textarea.selectionEnd;
445 let selectedText = this.state.content.substring(start, end);
446 this.state.content = `${this.state.content.substring(
449 )} ${beforeChars}${selectedText}${afterChars} ${this.state.content.substring(
453 this.state.content += `${beforeChars}___${afterChars}`;
455 this.setState(this.state);
457 autosize.update(textarea);
461 handleInsertBold(i: MarkdownTextArea, event: any) {
462 event.preventDefault();
463 i.simpleSurround('**');
466 handleInsertItalic(i: MarkdownTextArea, event: any) {
467 event.preventDefault();
468 i.simpleSurround('*');
471 handleInsertCode(i: MarkdownTextArea, event: any) {
472 event.preventDefault();
473 i.simpleSurround('`');
476 handleInsertStrikethrough(i: MarkdownTextArea, event: any) {
477 event.preventDefault();
478 i.simpleSurround('~~');
481 handleInsertList(i: MarkdownTextArea, event: any) {
482 event.preventDefault();
486 handleInsertQuote(i: MarkdownTextArea, event: any) {
487 event.preventDefault();
491 handleInsertHeader(i: MarkdownTextArea, event: any) {
492 event.preventDefault();
496 handleInsertSubscript(i: MarkdownTextArea, event: any) {
497 event.preventDefault();
498 i.simpleSurround('~');
501 handleInsertSuperscript(i: MarkdownTextArea, event: any) {
502 event.preventDefault();
503 i.simpleSurround('^');
506 simpleInsert(chars: string) {
507 if (!this.state.content) {
508 this.state.content = `${chars} `;
510 this.state.content += `\n${chars} `;
513 let textarea: any = document.getElementById(this.id);
516 autosize.update(textarea);
518 this.setState(this.state);
521 handleInsertSpoiler(i: MarkdownTextArea, event: any) {
522 event.preventDefault();
523 let beforeChars = `\n::: spoiler ${i18n.t('spoiler')}\n`;
524 let afterChars = '\n:::\n';
525 i.simpleSurroundBeforeAfter(beforeChars, afterChars);
529 let textarea: any = document.getElementById(this.id);
530 let selectedText = window.getSelection().toString();
536 .join('\n') + '\n\n';
537 this.state.content = quotedText;
538 this.setState(this.state);
539 // Not sure why this needs a delay
540 setTimeout(() => autosize.update(textarea), 10);