1 import { debounceTime, distinctUntilChanged } from 'rxjs/operators'
2 import { Component, forwardRef, Input, OnInit } from '@angular/core'
3 import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms'
4 import { Subject } from 'rxjs'
5 import truncate from 'lodash-es/truncate'
6 import { ScreenService } from '@app/shared/misc/screen.service'
7 import { MarkdownService } from '@app/shared/renderer'
10 selector: 'my-markdown-textarea',
11 templateUrl: './markdown-textarea.component.html',
12 styleUrls: [ './markdown-textarea.component.scss' ],
15 provide: NG_VALUE_ACCESSOR,
16 useExisting: forwardRef(() => MarkdownTextareaComponent),
22 export class MarkdownTextareaComponent implements ControlValueAccessor, OnInit {
24 @Input() classes: string[] = []
25 @Input() textareaWidth = '100%'
26 @Input() textareaHeight = '150px'
27 @Input() previewColumn = false
28 @Input() truncate: number
29 @Input() markdownType: 'text' | 'enhanced' = 'text'
30 @Input() markdownVideo = false
32 textareaMarginRight = '0'
33 flexDirection = 'column'
34 truncatedPreviewHTML = ''
37 private contentChanged = new Subject<string>()
40 private screenService: ScreenService,
41 private markdownService: MarkdownService
48 distinctUntilChanged()
50 .subscribe(() => this.updatePreviews())
52 this.contentChanged.next(this.content)
54 if (this.previewColumn) {
55 this.flexDirection = 'row'
56 this.textareaMarginRight = '15px'
60 propagateChange = (_: any) => { /* empty */ }
62 writeValue (description: string) {
63 this.content = description
65 this.contentChanged.next(this.content)
68 registerOnChange (fn: (_: any) => void) {
69 this.propagateChange = fn
72 registerOnTouched () {
77 this.propagateChange(this.content)
79 this.contentChanged.next(this.content)
82 arePreviewsDisplayed () {
83 return this.screenService.isInSmallView() === false
86 private async updatePreviews () {
87 if (this.content === null || this.content === undefined) return
89 this.truncatedPreviewHTML = await this.markdownRender(truncate(this.content, { length: this.truncate }))
90 this.previewHTML = await this.markdownRender(this.content)
93 private async markdownRender (text: string) {
94 const html = this.markdownType === 'text' ?
95 await this.markdownService.textMarkdownToHTML(text) :
96 await this.markdownService.enhancedMarkdownToHTML(text)
98 return this.markdownVideo ? this.markdownService.processVideoTimestamps(html) : html