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