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 {
25 @Input() classes: string[] = []
26 @Input() textareaWidth = '100%'
27 @Input() textareaHeight = '150px'
28 @Input() previewColumn = false
29 @Input() truncate: number
30 @Input() markdownType: 'text' | 'enhanced' = 'text'
32 textareaMarginRight = '0'
33 flexDirection = 'column'
34 truncatedPreviewHTML = ''
37 private contentChanged = new Subject<string>()
39 constructor (private markdownService: MarkdownService) {}
44 .distinctUntilChanged()
45 .subscribe(() => this.updatePreviews())
47 this.contentChanged.next(this.content)
49 if (this.previewColumn) {
50 this.flexDirection = 'row'
51 this.textareaMarginRight = '15px'
55 propagateChange = (_: any) => { /* empty */ }
57 writeValue (description: string) {
58 this.content = description
60 this.contentChanged.next(this.content)
63 registerOnChange (fn: (_: any) => void) {
64 this.propagateChange = fn
67 registerOnTouched () {
72 this.propagateChange(this.content)
74 this.contentChanged.next(this.content)
77 arePreviewsDisplayed () {
78 return isInSmallView() === false
81 private updatePreviews () {
82 if (this.content === null || this.content === undefined) return
84 this.truncatedPreviewHTML = this.markdownRender(truncate(this.content, { length: this.truncate }))
85 this.previewHTML = this.markdownRender(this.content)
88 private markdownRender (text: string) {
89 if (this.markdownType === 'text') return this.markdownService.textMarkdownToHTML(text)
91 return this.markdownService.enhancedMarkdownToHTML(text)