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 { isInMobileView } from '@app/shared/misc/utils'
6 import { Subject } from 'rxjs/Subject'
7 import { MarkdownService } from '../../shared'
8 import truncate from 'lodash-es/truncate'
11 selector: 'my-video-description',
12 templateUrl: './video-description.component.html',
13 styleUrls: [ './video-description.component.scss' ],
16 provide: NG_VALUE_ACCESSOR,
17 useExisting: forwardRef(() => VideoDescriptionComponent),
23 export class VideoDescriptionComponent implements ControlValueAccessor, OnInit {
24 @Input() description = ''
25 truncatedDescriptionHTML = ''
28 private descriptionChanged = new Subject<string>()
30 constructor (private markdownService: MarkdownService) {}
33 this.descriptionChanged
35 .distinctUntilChanged()
36 .subscribe(() => this.updateDescriptionPreviews())
38 this.descriptionChanged.next(this.description)
41 propagateChange = (_: any) => { /* empty */ }
43 writeValue (description: string) {
44 this.description = description
46 this.descriptionChanged.next(this.description)
49 registerOnChange (fn: (_: any) => void) {
50 this.propagateChange = fn
53 registerOnTouched () {
58 this.propagateChange(this.description)
60 this.descriptionChanged.next(this.description)
63 arePreviewsDisplayed () {
64 return isInMobileView() === false
67 private updateDescriptionPreviews () {
68 if (!this.description) return
70 this.truncatedDescriptionHTML = this.markdownService.markdownToHTML(truncate(this.description, { length: 250 }))
71 this.descriptionHTML = this.markdownService.markdownToHTML(this.description)