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
31 @Input() name = 'description'
33 textareaMarginRight = '0'
34 flexDirection = 'column'
35 truncatedPreviewHTML = ''
38 private contentChanged = new Subject<string>()
41 private screenService: ScreenService,
42 private markdownService: MarkdownService
49 distinctUntilChanged()
51 .subscribe(() => this.updatePreviews())
53 this.contentChanged.next(this.content)
55 if (this.previewColumn) {
56 this.flexDirection = 'row'
57 this.textareaMarginRight = '15px'
61 propagateChange = (_: any) => { /* empty */ }
63 writeValue (description: string) {
64 this.content = description
66 this.contentChanged.next(this.content)
69 registerOnChange (fn: (_: any) => void) {
70 this.propagateChange = fn
73 registerOnTouched () {
78 this.propagateChange(this.content)
80 this.contentChanged.next(this.content)
83 arePreviewsDisplayed () {
84 return this.screenService.isInSmallView() === false
87 private async updatePreviews () {
88 if (this.content === null || this.content === undefined) return
90 this.truncatedPreviewHTML = await this.markdownRender(truncate(this.content, { length: this.truncate }))
91 this.previewHTML = await this.markdownRender(this.content)
94 private async markdownRender (text: string) {
95 const html = this.markdownType === 'text' ?
96 await this.markdownService.textMarkdownToHTML(text) :
97 await this.markdownService.enhancedMarkdownToHTML(text)
99 return this.markdownVideo ? this.markdownService.processVideoTimestamps(html) : html