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('spoiler')}
273 onClick={linkEvent(this, this.handleInsertSpoiler)}
275 <svg class="icon icon-inline">
276 <use xlinkHref="#icon-alert-triangle"></use>
280 href={markdownHelpUrl}
282 class="btn btn-sm text-muted font-weight-bold"
283 title={i18n.t('formatting_help')}
286 <svg class="icon icon-inline">
287 <use xlinkHref="#icon-help-circle"></use>
296 handleImageUploadPaste(i: MarkdownTextArea, event: any) {
297 let image = event.clipboardData.files[0];
299 i.handleImageUpload(i, image);
303 handleImageUpload(i: MarkdownTextArea, event: any) {
306 event.preventDefault();
307 file = event.target.files[0];
312 const imageUploadUrl = `/pictrs/image`;
313 const formData = new FormData();
314 formData.append('images[]', file);
316 i.state.imageLoading = true;
319 fetch(imageUploadUrl, {
323 .then(res => res.json())
325 console.log('pictrs upload:');
327 if (res.msg == 'ok') {
328 let hash = res.files[0].file;
329 let url = `${window.location.origin}/pictrs/image/${hash}`;
330 let deleteToken = res.files[0].delete_token;
331 let deleteUrl = `${window.location.origin}/pictrs/image/delete/${deleteToken}/${hash}`;
332 let imageMarkdown = `![](${url})`;
333 let content = i.state.content;
334 content = content ? `${content}\n${imageMarkdown}` : imageMarkdown;
335 i.state.content = content;
336 i.state.imageLoading = false;
338 let textarea: any = document.getElementById(i.id);
339 autosize.update(textarea);
341 i18n.t('click_to_delete_picture'),
342 i18n.t('picture_deleted'),
346 i.state.imageLoading = false;
348 toast(JSON.stringify(res), 'danger');
352 i.state.imageLoading = false;
354 toast(error, 'danger');
358 handleContentChange(i: MarkdownTextArea, event: any) {
359 i.state.content = event.target.value;
361 if (i.props.onContentChange) {
362 i.props.onContentChange(i.state.content);
366 handlePreviewToggle(i: MarkdownTextArea, event: any) {
367 event.preventDefault();
368 i.state.previewMode = !i.state.previewMode;
372 handleSubmit(i: MarkdownTextArea, event: any) {
373 event.preventDefault();
374 i.state.loading = true;
376 let msg = { val: i.state.content, formId: i.formId };
377 i.props.onSubmit(msg);
380 handleReplyCancel(i: MarkdownTextArea) {
381 i.props.onReplyCancel();
384 handleInsertLink(i: MarkdownTextArea, event: any) {
385 event.preventDefault();
386 if (!i.state.content) {
387 i.state.content = '';
389 let textarea: any = document.getElementById(i.id);
390 let start: number = textarea.selectionStart;
391 let end: number = textarea.selectionEnd;
394 let selectedText = i.state.content.substring(start, end);
395 i.state.content = `${i.state.content.substring(
398 )} [${selectedText}]() ${i.state.content.substring(end)}`;
400 setTimeout(() => (textarea.selectionEnd = end + 4), 10);
402 i.state.content += '[]()';
404 setTimeout(() => (textarea.selectionEnd -= 1), 10);
409 simpleSurround(chars: string) {
410 this.simpleSurroundBeforeAfter(chars, chars);
413 simpleSurroundBeforeAfter(beforeChars: string, afterChars: string) {
414 if (!this.state.content) {
415 this.state.content = '';
417 let textarea: any = document.getElementById(this.id);
418 let start: number = textarea.selectionStart;
419 let end: number = textarea.selectionEnd;
422 let selectedText = this.state.content.substring(start, end);
423 this.state.content = `${this.state.content.substring(
426 )} ${beforeChars}${selectedText}${afterChars} ${this.state.content.substring(
430 this.state.content += `${beforeChars}___${afterChars}`;
432 this.setState(this.state);
434 autosize.update(textarea);
438 handleInsertBold(i: MarkdownTextArea, event: any) {
439 event.preventDefault();
440 i.simpleSurround('**');
443 handleInsertItalic(i: MarkdownTextArea, event: any) {
444 event.preventDefault();
445 i.simpleSurround('*');
448 handleInsertCode(i: MarkdownTextArea, event: any) {
449 event.preventDefault();
450 i.simpleSurround('`');
453 handleInsertStrikethrough(i: MarkdownTextArea, event: any) {
454 event.preventDefault();
455 i.simpleSurround('~~');
458 handleInsertList(i: MarkdownTextArea, event: any) {
459 event.preventDefault();
463 handleInsertQuote(i: MarkdownTextArea, event: any) {
464 event.preventDefault();
468 handleInsertHeader(i: MarkdownTextArea, event: any) {
469 event.preventDefault();
473 simpleInsert(chars: string) {
474 if (!this.state.content) {
475 this.state.content = `${chars} `;
477 this.state.content += `\n${chars} `;
480 let textarea: any = document.getElementById(this.id);
483 autosize.update(textarea);
485 this.setState(this.state);
488 handleInsertSpoiler(i: MarkdownTextArea, event: any) {
489 event.preventDefault();
490 let beforeChars = `\n::: spoiler ${i18n.t('spoiler')}\n`;
491 let afterChars = '\n:::\n';
492 i.simpleSurroundBeforeAfter(beforeChars, afterChars);
496 let textarea: any = document.getElementById(this.id);
497 let selectedText = window.getSelection().toString();
503 .join('\n') + '\n\n';
504 this.state.content = quotedText;
505 this.setState(this.state);
506 // Not sure why this needs a delay
507 setTimeout(() => autosize.update(textarea), 10);