diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2017-10-30 20:26:06 +0100 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2017-10-30 20:26:06 +0100 |
commit | 2de96f4d6b800076743ed2073f9529816cfd5c8a (patch) | |
tree | 8c2dbd29fe5560e0b24edaa58038581bddd49778 /client/src/app/videos/shared/video-description.component.ts | |
parent | 8bf89b095a711d5ac5e6ef55575b166257d0d526 (diff) | |
download | PeerTube-2de96f4d6b800076743ed2073f9529816cfd5c8a.tar.gz PeerTube-2de96f4d6b800076743ed2073f9529816cfd5c8a.tar.zst PeerTube-2de96f4d6b800076743ed2073f9529816cfd5c8a.zip |
Lazy description and previews to video form
Diffstat (limited to 'client/src/app/videos/shared/video-description.component.ts')
-rw-r--r-- | client/src/app/videos/shared/video-description.component.ts | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/client/src/app/videos/shared/video-description.component.ts b/client/src/app/videos/shared/video-description.component.ts new file mode 100644 index 000000000..d9ffb7800 --- /dev/null +++ b/client/src/app/videos/shared/video-description.component.ts | |||
@@ -0,0 +1,68 @@ | |||
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' | ||
6 | |||
7 | import { truncate } from 'lodash' | ||
8 | |||
9 | import { MarkdownService } from './markdown.service' | ||
10 | |||
11 | @Component({ | ||
12 | selector: 'my-video-description', | ||
13 | templateUrl: './video-description.component.html', | ||
14 | styleUrls: [ './video-description.component.scss' ], | ||
15 | providers: [ | ||
16 | { | ||
17 | provide: NG_VALUE_ACCESSOR, | ||
18 | useExisting: forwardRef(() => VideoDescriptionComponent), | ||
19 | multi: true | ||
20 | } | ||
21 | ] | ||
22 | }) | ||
23 | |||
24 | export class VideoDescriptionComponent implements ControlValueAccessor, OnInit { | ||
25 | @Input() description = '' | ||
26 | truncatedDescriptionHTML = '' | ||
27 | descriptionHTML = '' | ||
28 | |||
29 | private descriptionChanged = new Subject<string>() | ||
30 | |||
31 | constructor (private markdownService: MarkdownService) {} | ||
32 | |||
33 | ngOnInit () { | ||
34 | this.descriptionChanged | ||
35 | .debounceTime(150) | ||
36 | .distinctUntilChanged() | ||
37 | .subscribe(() => this.updateDescriptionPreviews()) | ||
38 | |||
39 | this.descriptionChanged.next(this.description) | ||
40 | } | ||
41 | |||
42 | propagateChange = (_: any) => { /* empty */ } | ||
43 | |||
44 | writeValue (description: string) { | ||
45 | this.description = description | ||
46 | |||
47 | this.descriptionChanged.next(this.description) | ||
48 | } | ||
49 | |||
50 | registerOnChange (fn: (_: any) => void) { | ||
51 | this.propagateChange = fn | ||
52 | } | ||
53 | |||
54 | registerOnTouched () { | ||
55 | // Unused | ||
56 | } | ||
57 | |||
58 | onModelChange () { | ||
59 | this.propagateChange(this.description) | ||
60 | |||
61 | this.descriptionChanged.next(this.description) | ||
62 | } | ||
63 | |||
64 | private updateDescriptionPreviews () { | ||
65 | this.truncatedDescriptionHTML = this.markdownService.markdownToHTML(truncate(this.description, { length: 250 })) | ||
66 | this.descriptionHTML = this.markdownService.markdownToHTML(this.description) | ||
67 | } | ||
68 | } | ||