aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/videos/+video-edit/shared/video-description.component.ts
blob: 34dbc21a6af662c8907f304c7391426c81016434 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import { Component, forwardRef, Input, OnInit } from '@angular/core'
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms'
import 'rxjs/add/operator/debounceTime'
import 'rxjs/add/operator/distinctUntilChanged'
import { Subject } from 'rxjs/Subject'
import { MarkdownService } from '../../shared'
import truncate from 'lodash-es/truncate'

@Component({
  selector: 'my-video-description',
  templateUrl: './video-description.component.html',
  styleUrls: [ './video-description.component.scss' ],
  providers: [
    {
      provide: NG_VALUE_ACCESSOR,
      useExisting: forwardRef(() => VideoDescriptionComponent),
      multi: true
    }
  ]
})

export class VideoDescriptionComponent implements ControlValueAccessor, OnInit {
  @Input() description = ''
  truncatedDescriptionHTML = ''
  descriptionHTML = ''

  private descriptionChanged = new Subject<string>()

  constructor (private markdownService: MarkdownService) {}

  ngOnInit () {
    this.descriptionChanged
      .debounceTime(150)
      .distinctUntilChanged()
      .subscribe(() => this.updateDescriptionPreviews())

    this.descriptionChanged.next(this.description)
  }

  propagateChange = (_: any) => { /* empty */ }

  writeValue (description: string) {
    this.description = description

    this.descriptionChanged.next(this.description)
  }

  registerOnChange (fn: (_: any) => void) {
    this.propagateChange = fn
  }

  registerOnTouched () {
    // Unused
  }

  onModelChange () {
    this.propagateChange(this.description)

    this.descriptionChanged.next(this.description)
  }

  private updateDescriptionPreviews () {
    if (!this.description) return

    this.truncatedDescriptionHTML = this.markdownService.markdownToHTML(truncate(this.description, { length: 250 }))
    this.descriptionHTML = this.markdownService.markdownToHTML(this.description)
  }
}