1 import { Component, forwardRef, Input, OnInit } from '@angular/core'
2 import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms'
3 import { Subject } from 'rxjs/Subject'
4 import 'rxjs/add/operator/debounceTime'
5 import 'rxjs/add/operator/distinctUntilChanged'
7 import { truncate } from 'lodash'
9 import { MarkdownService } from './markdown.service'
12 selector: 'my-video-description',
13 templateUrl: './video-description.component.html',
14 styleUrls: [ './video-description.component.scss' ],
17 provide: NG_VALUE_ACCESSOR,
18 useExisting: forwardRef(() => VideoDescriptionComponent),
24 export class VideoDescriptionComponent implements ControlValueAccessor, OnInit {
25 @Input() description = ''
26 truncatedDescriptionHTML = ''
29 private descriptionChanged = new Subject<string>()
31 constructor (private markdownService: MarkdownService) {}
34 this.descriptionChanged
36 .distinctUntilChanged()
37 .subscribe(() => this.updateDescriptionPreviews())
39 this.descriptionChanged.next(this.description)
42 propagateChange = (_: any) => { /* empty */ }
44 writeValue (description: string) {
45 this.description = description
47 this.descriptionChanged.next(this.description)
50 registerOnChange (fn: (_: any) => void) {
51 this.propagateChange = fn
54 registerOnTouched () {
59 this.propagateChange(this.description)
61 this.descriptionChanged.next(this.description)
64 private updateDescriptionPreviews () {
65 this.truncatedDescriptionHTML = this.markdownService.markdownToHTML(truncate(this.description, { length: 250 }))
66 this.descriptionHTML = this.markdownService.markdownToHTML(this.description)