]>
Commit | Line | Data |
---|---|---|
2de96f4d C |
1 | import { Component, forwardRef, Input, OnInit } from '@angular/core' |
2 | import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms' | |
2de96f4d C |
3 | import 'rxjs/add/operator/debounceTime' |
4 | import 'rxjs/add/operator/distinctUntilChanged' | |
6693df9d | 5 | import { isInMobileView } from '@app/shared/misc/utils' |
66b16caf | 6 | import { MarkdownService } from '@app/videos/shared' |
cadb46d8 | 7 | import { Subject } from 'rxjs/Subject' |
24a8e782 | 8 | import truncate from 'lodash-es/truncate' |
2de96f4d C |
9 | |
10 | @Component({ | |
66b16caf C |
11 | selector: 'my-markdown-textarea', |
12 | templateUrl: './markdown-textarea.component.html', | |
13 | styleUrls: [ './markdown-textarea.component.scss' ], | |
2de96f4d C |
14 | providers: [ |
15 | { | |
16 | provide: NG_VALUE_ACCESSOR, | |
66b16caf | 17 | useExisting: forwardRef(() => MarkdownTextareaComponent), |
2de96f4d C |
18 | multi: true |
19 | } | |
20 | ] | |
21 | }) | |
22 | ||
66b16caf | 23 | export class MarkdownTextareaComponent implements ControlValueAccessor, OnInit { |
2de96f4d | 24 | @Input() description = '' |
66b16caf C |
25 | @Input() classes: string[] = [] |
26 | @Input() textareaWidth = '100%' | |
27 | @Input() textareaHeight = '150px' | |
28 | @Input() previewColumn = false | |
29 | @Input() truncate: number | |
30 | ||
31 | textareaMarginRight = '0' | |
32 | flexDirection = 'column' | |
2de96f4d C |
33 | truncatedDescriptionHTML = '' |
34 | descriptionHTML = '' | |
35 | ||
36 | private descriptionChanged = new Subject<string>() | |
37 | ||
38 | constructor (private markdownService: MarkdownService) {} | |
39 | ||
40 | ngOnInit () { | |
41 | this.descriptionChanged | |
42 | .debounceTime(150) | |
43 | .distinctUntilChanged() | |
44 | .subscribe(() => this.updateDescriptionPreviews()) | |
45 | ||
46 | this.descriptionChanged.next(this.description) | |
66b16caf C |
47 | |
48 | if (this.previewColumn) { | |
49 | this.flexDirection = 'row' | |
50 | this.textareaMarginRight = '15px' | |
51 | } | |
2de96f4d C |
52 | } |
53 | ||
54 | propagateChange = (_: any) => { /* empty */ } | |
55 | ||
56 | writeValue (description: string) { | |
57 | this.description = description | |
58 | ||
59 | this.descriptionChanged.next(this.description) | |
60 | } | |
61 | ||
62 | registerOnChange (fn: (_: any) => void) { | |
63 | this.propagateChange = fn | |
64 | } | |
65 | ||
66 | registerOnTouched () { | |
67 | // Unused | |
68 | } | |
69 | ||
70 | onModelChange () { | |
71 | this.propagateChange(this.description) | |
72 | ||
73 | this.descriptionChanged.next(this.description) | |
74 | } | |
75 | ||
6693df9d C |
76 | arePreviewsDisplayed () { |
77 | return isInMobileView() === false | |
78 | } | |
79 | ||
2de96f4d | 80 | private updateDescriptionPreviews () { |
66b16caf | 81 | if (this.description === null || this.description === undefined) return |
f595d394 | 82 | |
66b16caf | 83 | this.truncatedDescriptionHTML = this.markdownService.markdownToHTML(truncate(this.description, { length: this.truncate })) |
2de96f4d C |
84 | this.descriptionHTML = this.markdownService.markdownToHTML(this.description) |
85 | } | |
86 | } |