]>
Commit | Line | Data |
---|---|---|
67ed6552 C |
1 | import truncate from 'lodash-es/truncate' |
2 | import { Subject } from 'rxjs' | |
db400f44 | 3 | import { debounceTime, distinctUntilChanged } from 'rxjs/operators' |
67ed6552 | 4 | import { Component, ElementRef, forwardRef, Input, OnInit, ViewChild } from '@angular/core' |
2de96f4d | 5 | import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms' |
67ed6552 | 6 | import { MarkdownService } from '@app/core' |
2de96f4d C |
7 | |
8 | @Component({ | |
66b16caf C |
9 | selector: 'my-markdown-textarea', |
10 | templateUrl: './markdown-textarea.component.html', | |
11 | styleUrls: [ './markdown-textarea.component.scss' ], | |
2de96f4d C |
12 | providers: [ |
13 | { | |
14 | provide: NG_VALUE_ACCESSOR, | |
66b16caf | 15 | useExisting: forwardRef(() => MarkdownTextareaComponent), |
2de96f4d C |
16 | multi: true |
17 | } | |
18 | ] | |
19 | }) | |
20 | ||
66b16caf | 21 | export class MarkdownTextareaComponent implements ControlValueAccessor, OnInit { |
07fa4c97 | 22 | @Input() content = '' |
be27ef3b | 23 | @Input() classes: string[] | { [klass: string]: any[] | any } = [] |
b15fe00f | 24 | @Input() textareaMaxWidth = '100%' |
66b16caf | 25 | @Input() textareaHeight = '150px' |
66b16caf | 26 | @Input() truncate: number |
07fa4c97 | 27 | @Input() markdownType: 'text' | 'enhanced' = 'text' |
d68ebf0b | 28 | @Input() markdownVideo = false |
4ee6a8b1 | 29 | @Input() name = 'description' |
66b16caf | 30 | |
b15fe00f K |
31 | @ViewChild('textarea') textareaElement: ElementRef |
32 | ||
07fa4c97 C |
33 | truncatedPreviewHTML = '' |
34 | previewHTML = '' | |
b15fe00f | 35 | isMaximized = false |
2de96f4d | 36 | |
07fa4c97 | 37 | private contentChanged = new Subject<string>() |
2de96f4d | 38 | |
67ed6552 | 39 | constructor (private markdownService: MarkdownService) {} |
2de96f4d C |
40 | |
41 | ngOnInit () { | |
07fa4c97 | 42 | this.contentChanged |
db400f44 C |
43 | .pipe( |
44 | debounceTime(150), | |
45 | distinctUntilChanged() | |
46 | ) | |
47 | .subscribe(() => this.updatePreviews()) | |
2de96f4d | 48 | |
07fa4c97 | 49 | this.contentChanged.next(this.content) |
2de96f4d C |
50 | } |
51 | ||
52 | propagateChange = (_: any) => { /* empty */ } | |
53 | ||
54 | writeValue (description: string) { | |
07fa4c97 | 55 | this.content = description |
2de96f4d | 56 | |
07fa4c97 | 57 | this.contentChanged.next(this.content) |
2de96f4d C |
58 | } |
59 | ||
60 | registerOnChange (fn: (_: any) => void) { | |
61 | this.propagateChange = fn | |
62 | } | |
63 | ||
64 | registerOnTouched () { | |
65 | // Unused | |
66 | } | |
67 | ||
68 | onModelChange () { | |
07fa4c97 | 69 | this.propagateChange(this.content) |
2de96f4d | 70 | |
07fa4c97 | 71 | this.contentChanged.next(this.content) |
2de96f4d C |
72 | } |
73 | ||
b15fe00f K |
74 | onMaximizeClick () { |
75 | this.isMaximized = !this.isMaximized | |
76 | ||
77 | // Make sure textarea have the focus | |
78 | this.textareaElement.nativeElement.focus() | |
79 | ||
80 | // Make sure the window has no scrollbars | |
81 | if (!this.isMaximized) { | |
82 | this.unlockBodyScroll() | |
83 | } else { | |
84 | this.lockBodyScroll() | |
85 | } | |
86 | } | |
87 | ||
88 | private lockBodyScroll () { | |
89 | document.getElementById('content').classList.add('lock-scroll') | |
90 | } | |
91 | ||
92 | private unlockBodyScroll () { | |
93 | document.getElementById('content').classList.remove('lock-scroll') | |
6693df9d C |
94 | } |
95 | ||
41d71344 | 96 | private async updatePreviews () { |
07fa4c97 | 97 | if (this.content === null || this.content === undefined) return |
f595d394 | 98 | |
41d71344 C |
99 | this.truncatedPreviewHTML = await this.markdownRender(truncate(this.content, { length: this.truncate })) |
100 | this.previewHTML = await this.markdownRender(this.content) | |
07fa4c97 C |
101 | } |
102 | ||
d68ebf0b L |
103 | private async markdownRender (text: string) { |
104 | const html = this.markdownType === 'text' ? | |
105 | await this.markdownService.textMarkdownToHTML(text) : | |
106 | await this.markdownService.enhancedMarkdownToHTML(text) | |
07fa4c97 | 107 | |
d68ebf0b | 108 | return this.markdownVideo ? this.markdownService.processVideoTimestamps(html) : html |
2de96f4d C |
109 | } |
110 | } |