1 import { Component, forwardRef, Input, OnInit } from '@angular/core'
2 import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms'
3 import 'rxjs/add/operator/debounceTime'
4 import 'rxjs/add/operator/distinctUntilChanged'
5 import { isInSmallView } from '@app/shared/misc/utils'
6 import { MarkdownService } from '@app/videos/shared'
7 import { Subject } from 'rxjs/Subject'
8 import truncate from 'lodash-es/truncate'
11 selector: 'my-markdown-textarea',
12 templateUrl: './markdown-textarea.component.html',
13 styleUrls: [ './markdown-textarea.component.scss' ],
16 provide: NG_VALUE_ACCESSOR,
17 useExisting: forwardRef(() => MarkdownTextareaComponent),
23 export class MarkdownTextareaComponent implements ControlValueAccessor, OnInit {
24 @Input() description = ''
25 @Input() classes: string[] = []
26 @Input() textareaWidth = '100%'
27 @Input() textareaHeight = '150px'
28 @Input() previewColumn = false
29 @Input() truncate: number
31 textareaMarginRight = '0'
32 flexDirection = 'column'
33 truncatedDescriptionHTML = ''
36 private descriptionChanged = new Subject<string>()
38 constructor (private markdownService: MarkdownService) {}
41 this.descriptionChanged
43 .distinctUntilChanged()
44 .subscribe(() => this.updateDescriptionPreviews())
46 this.descriptionChanged.next(this.description)
48 if (this.previewColumn) {
49 this.flexDirection = 'row'
50 this.textareaMarginRight = '15px'
54 propagateChange = (_: any) => { /* empty */ }
56 writeValue (description: string) {
57 this.description = description
59 this.descriptionChanged.next(this.description)
62 registerOnChange (fn: (_: any) => void) {
63 this.propagateChange = fn
66 registerOnTouched () {
71 this.propagateChange(this.description)
73 this.descriptionChanged.next(this.description)
76 arePreviewsDisplayed () {
77 return isInSmallView() === false
80 private updateDescriptionPreviews () {
81 if (this.description === null || this.description === undefined) return
83 this.truncatedDescriptionHTML = this.markdownService.markdownToHTML(truncate(this.description, { length: this.truncate }))
84 this.descriptionHTML = this.markdownService.markdownToHTML(this.description)