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