]>
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() classes: string[] | { [klass: string]: any[] | any } = [] | |
28 | ||
29 | @Input() textareaMaxWidth = '100%' | |
30 | @Input() textareaHeight = '150px' | |
31 | ||
32 | @Input() truncate: number | |
33 | ||
34 | @Input() markdownType: 'text' | 'enhanced' = 'text' | |
35 | @Input() customMarkdownRenderer?: (text: string) => Promise<string | HTMLElement> | |
36 | ||
37 | @Input() markdownVideo: Video | |
38 | ||
39 | @Input() name = 'description' | |
40 | ||
41 | @ViewChild('textarea') textareaElement: ElementRef | |
42 | @ViewChild('previewElement') previewElement: ElementRef | |
43 | ||
44 | truncatedPreviewHTML: SafeHtml | string = '' | |
45 | previewHTML: SafeHtml | string = '' | |
46 | ||
47 | isMaximized = false | |
48 | disabled = false | |
49 | ||
50 | maximizeInText = $localize`Maximize editor` | |
51 | maximizeOutText = $localize`Exit maximized editor` | |
52 | ||
53 | private contentChanged = new Subject<string>() | |
54 | private scrollPosition: [number, number] | |
55 | ||
56 | constructor ( | |
57 | private viewportScroller: ViewportScroller, | |
58 | private screenService: ScreenService, | |
59 | private markdownService: MarkdownService | |
60 | ) { } | |
61 | ||
62 | ngOnInit () { | |
63 | this.contentChanged | |
64 | .pipe( | |
65 | debounceTime(150), | |
66 | distinctUntilChanged() | |
67 | ) | |
68 | .subscribe(() => this.updatePreviews()) | |
69 | ||
70 | this.contentChanged.next(this.content) | |
71 | } | |
72 | ||
73 | propagateChange = (_: any) => { /* empty */ } | |
74 | ||
75 | writeValue (description: string) { | |
76 | this.content = description | |
77 | ||
78 | this.contentChanged.next(this.content) | |
79 | } | |
80 | ||
81 | registerOnChange (fn: (_: any) => void) { | |
82 | this.propagateChange = fn | |
83 | } | |
84 | ||
85 | registerOnTouched () { | |
86 | // Unused | |
87 | } | |
88 | ||
89 | onModelChange () { | |
90 | this.propagateChange(this.content) | |
91 | ||
92 | this.contentChanged.next(this.content) | |
93 | } | |
94 | ||
95 | onMaximizeClick () { | |
96 | this.isMaximized = !this.isMaximized | |
97 | ||
98 | // Make sure textarea have the focus | |
99 | // Except on touchscreens devices, the virtual keyboard may move up and hide the textarea in maximized mode | |
100 | if (!this.screenService.isInTouchScreen()) { | |
101 | this.textareaElement.nativeElement.focus() | |
102 | } | |
103 | ||
104 | // Make sure the window has no scrollbars | |
105 | if (!this.isMaximized) { | |
106 | this.unlockBodyScroll() | |
107 | } else { | |
108 | this.lockBodyScroll() | |
109 | } | |
110 | } | |
111 | ||
112 | setDisabledState (isDisabled: boolean) { | |
113 | this.disabled = isDisabled | |
114 | } | |
115 | ||
116 | private lockBodyScroll () { | |
117 | this.scrollPosition = this.viewportScroller.getScrollPosition() | |
118 | document.getElementById('content').classList.add('lock-scroll') | |
119 | } | |
120 | ||
121 | private unlockBodyScroll () { | |
122 | document.getElementById('content').classList.remove('lock-scroll') | |
123 | this.viewportScroller.scrollToPosition(this.scrollPosition) | |
124 | } | |
125 | ||
126 | private async updatePreviews () { | |
127 | if (this.content === null || this.content === undefined) return | |
128 | ||
129 | this.truncatedPreviewHTML = await this.markdownRender(truncate(this.content, { length: this.truncate })) | |
130 | this.previewHTML = await this.markdownRender(this.content) | |
131 | } | |
132 | ||
133 | private async markdownRender (text: string) { | |
134 | let html: string | |
135 | ||
136 | if (this.customMarkdownRenderer) { | |
137 | const result = await this.customMarkdownRenderer(text) | |
138 | ||
139 | if (result instanceof HTMLElement) { | |
140 | const wrapperElement = this.previewElement.nativeElement as HTMLElement | |
141 | wrapperElement.innerHTML = '' | |
142 | wrapperElement.appendChild(result) | |
143 | return | |
144 | } | |
145 | ||
146 | html = result | |
147 | } else if (this.markdownType === 'text') { | |
148 | html = await this.markdownService.textMarkdownToHTML(text) | |
149 | } else { | |
150 | html = await this.markdownService.enhancedMarkdownToHTML(text) | |
151 | } | |
152 | ||
153 | if (this.markdownVideo) { | |
154 | html = this.markdownService.processVideoTimestamps(this.markdownVideo.shortUUID, html) | |
155 | } | |
156 | ||
157 | return html | |
158 | } | |
159 | } |