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 { isInSmallView } from '@app/shared/misc/utils'
5 import { MarkdownService } from '@app/videos/shared'
6 import { Subject } from 'rxjs/Subject'
7 import truncate from 'lodash-es/truncate'
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'
31 textareaMarginRight = '0'
32 flexDirection = 'column'
33 truncatedPreviewHTML = ''
36 private contentChanged = new Subject<string>()
38 constructor (private markdownService: MarkdownService) {}
44 distinctUntilChanged()
46 .subscribe(() => this.updatePreviews())
48 this.contentChanged.next(this.content)
50 if (this.previewColumn) {
51 this.flexDirection = 'row'
52 this.textareaMarginRight = '15px'
56 propagateChange = (_: any) => { /* empty */ }
58 writeValue (description: string) {
59 this.content = description
61 this.contentChanged.next(this.content)
64 registerOnChange (fn: (_: any) => void) {
65 this.propagateChange = fn
68 registerOnTouched () {
73 this.propagateChange(this.content)
75 this.contentChanged.next(this.content)
78 arePreviewsDisplayed () {
79 return isInSmallView() === false
82 private updatePreviews () {
83 if (this.content === null || this.content === undefined) return
85 this.truncatedPreviewHTML = this.markdownRender(truncate(this.content, { length: this.truncate }))
86 this.previewHTML = this.markdownRender(this.content)
89 private markdownRender (text: string) {
90 if (this.markdownType === 'text') return this.markdownService.textMarkdownToHTML(text)
92 return this.markdownService.enhancedMarkdownToHTML(text)