diff options
Diffstat (limited to 'client/src/app/shared/forms/markdown-textarea.component.ts')
-rw-r--r-- | client/src/app/shared/forms/markdown-textarea.component.ts | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/client/src/app/shared/forms/markdown-textarea.component.ts b/client/src/app/shared/forms/markdown-textarea.component.ts new file mode 100644 index 000000000..20f13b28c --- /dev/null +++ b/client/src/app/shared/forms/markdown-textarea.component.ts | |||
@@ -0,0 +1,86 @@ | |||
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 { MarkdownService } from '@app/videos/shared' | ||
7 | import { Subject } from 'rxjs/Subject' | ||
8 | import truncate from 'lodash-es/truncate' | ||
9 | |||
10 | @Component({ | ||
11 | selector: 'my-markdown-textarea', | ||
12 | templateUrl: './markdown-textarea.component.html', | ||
13 | styleUrls: [ './markdown-textarea.component.scss' ], | ||
14 | providers: [ | ||
15 | { | ||
16 | provide: NG_VALUE_ACCESSOR, | ||
17 | useExisting: forwardRef(() => MarkdownTextareaComponent), | ||
18 | multi: true | ||
19 | } | ||
20 | ] | ||
21 | }) | ||
22 | |||
23 | export class MarkdownTextareaComponent implements ControlValueAccessor, OnInit { | ||
24 | @Input() description = '' | ||
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' | ||
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) | ||
47 | |||
48 | if (this.previewColumn) { | ||
49 | this.flexDirection = 'row' | ||
50 | this.textareaMarginRight = '15px' | ||
51 | } | ||
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 | |||
76 | arePreviewsDisplayed () { | ||
77 | return isInMobileView() === false | ||
78 | } | ||
79 | |||
80 | private updateDescriptionPreviews () { | ||
81 | if (this.description === null || this.description === undefined) return | ||
82 | |||
83 | this.truncatedDescriptionHTML = this.markdownService.markdownToHTML(truncate(this.description, { length: this.truncate })) | ||
84 | this.descriptionHTML = this.markdownService.markdownToHTML(this.description) | ||
85 | } | ||
86 | } | ||