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