]>
Commit | Line | Data |
---|---|---|
db400f44 | 1 | import { debounceTime, distinctUntilChanged } from 'rxjs/operators' |
b15fe00f | 2 | import { Component, forwardRef, Input, OnInit, ViewChild, ElementRef } from '@angular/core' |
2de96f4d | 3 | import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms' |
4d089429 | 4 | import { Subject } from 'rxjs' |
24a8e782 | 5 | import truncate from 'lodash-es/truncate' |
bbe0f064 | 6 | import { ScreenService } from '@app/shared/misc/screen.service' |
1506307f | 7 | import { MarkdownService } from '@app/shared/renderer' |
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 = '' |
be27ef3b | 24 | @Input() classes: string[] | { [klass: string]: any[] | any } = [] |
b15fe00f | 25 | @Input() textareaMaxWidth = '100%' |
66b16caf | 26 | @Input() textareaHeight = '150px' |
66b16caf | 27 | @Input() truncate: number |
07fa4c97 | 28 | @Input() markdownType: 'text' | 'enhanced' = 'text' |
d68ebf0b | 29 | @Input() markdownVideo = false |
4ee6a8b1 | 30 | @Input() name = 'description' |
66b16caf | 31 | |
b15fe00f K |
32 | @ViewChild('textarea') textareaElement: ElementRef |
33 | ||
07fa4c97 C |
34 | truncatedPreviewHTML = '' |
35 | previewHTML = '' | |
b15fe00f | 36 | isMaximized = false |
2de96f4d | 37 | |
07fa4c97 | 38 | private contentChanged = new Subject<string>() |
2de96f4d | 39 | |
bbe0f064 C |
40 | constructor ( |
41 | private screenService: ScreenService, | |
42 | private markdownService: MarkdownService | |
43 | ) {} | |
2de96f4d C |
44 | |
45 | ngOnInit () { | |
07fa4c97 | 46 | this.contentChanged |
db400f44 C |
47 | .pipe( |
48 | debounceTime(150), | |
49 | distinctUntilChanged() | |
50 | ) | |
51 | .subscribe(() => this.updatePreviews()) | |
2de96f4d | 52 | |
07fa4c97 | 53 | this.contentChanged.next(this.content) |
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 | ||
b15fe00f K |
78 | onMaximizeClick () { |
79 | this.isMaximized = !this.isMaximized | |
80 | ||
81 | // Make sure textarea have the focus | |
82 | this.textareaElement.nativeElement.focus() | |
83 | ||
84 | // Make sure the window has no scrollbars | |
85 | if (!this.isMaximized) { | |
86 | this.unlockBodyScroll() | |
87 | } else { | |
88 | this.lockBodyScroll() | |
89 | } | |
90 | } | |
91 | ||
92 | private lockBodyScroll () { | |
93 | document.getElementById('content').classList.add('lock-scroll') | |
94 | } | |
95 | ||
96 | private unlockBodyScroll () { | |
97 | document.getElementById('content').classList.remove('lock-scroll') | |
6693df9d C |
98 | } |
99 | ||
41d71344 | 100 | private async updatePreviews () { |
07fa4c97 | 101 | if (this.content === null || this.content === undefined) return |
f595d394 | 102 | |
41d71344 C |
103 | this.truncatedPreviewHTML = await this.markdownRender(truncate(this.content, { length: this.truncate })) |
104 | this.previewHTML = await this.markdownRender(this.content) | |
07fa4c97 C |
105 | } |
106 | ||
d68ebf0b L |
107 | private async markdownRender (text: string) { |
108 | const html = this.markdownType === 'text' ? | |
109 | await this.markdownService.textMarkdownToHTML(text) : | |
110 | await this.markdownService.enhancedMarkdownToHTML(text) | |
07fa4c97 | 111 | |
d68ebf0b | 112 | return this.markdownVideo ? this.markdownService.processVideoTimestamps(html) : html |
2de96f4d C |
113 | } |
114 | } |