1 import { Component, forwardRef, Input, OnInit } from '@angular/core'
2 import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms'
3 import { truncate } from 'lodash'
4 import 'rxjs/add/operator/debounceTime'
5 import 'rxjs/add/operator/distinctUntilChanged'
6 import { Subject } from 'rxjs/Subject'
7 import { MarkdownService } from '../../shared'
10 selector: 'my-video-description',
11 templateUrl: './video-description.component.html',
12 styleUrls: [ './video-description.component.scss' ],
15 provide: NG_VALUE_ACCESSOR,
16 useExisting: forwardRef(() => VideoDescriptionComponent),
22 export class VideoDescriptionComponent implements ControlValueAccessor, OnInit {
23 @Input() description = ''
24 truncatedDescriptionHTML = ''
27 private descriptionChanged = new Subject<string>()
29 constructor (private markdownService: MarkdownService) {}
32 this.descriptionChanged
34 .distinctUntilChanged()
35 .subscribe(() => this.updateDescriptionPreviews())
37 this.descriptionChanged.next(this.description)
40 propagateChange = (_: any) => { /* empty */ }
42 writeValue (description: string) {
43 this.description = description
45 this.descriptionChanged.next(this.description)
48 registerOnChange (fn: (_: any) => void) {
49 this.propagateChange = fn
52 registerOnTouched () {
57 this.propagateChange(this.description)
59 this.descriptionChanged.next(this.description)
62 private updateDescriptionPreviews () {
63 if (!this.description) return
65 this.truncatedDescriptionHTML = this.markdownService.markdownToHTML(truncate(this.description, { length: 250 }))
66 this.descriptionHTML = this.markdownService.markdownToHTML(this.description)