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