]>
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 | |
be27ef3b | 27 | @Input() classes: string[] | { [klass: string]: any[] | any } = [] |
2539932e | 28 | |
b15fe00f | 29 | @Input() textareaMaxWidth = '100%' |
66b16caf | 30 | @Input() textareaHeight = '150px' |
2539932e | 31 | |
66b16caf | 32 | @Input() truncate: number |
2539932e | 33 | |
07fa4c97 | 34 | @Input() markdownType: 'text' | 'enhanced' = 'text' |
2539932e C |
35 | @Input() customMarkdownRenderer?: (text: string) => Promise<string | HTMLElement> |
36 | ||
9162fdd3 | 37 | @Input() markdownVideo: Video |
2539932e | 38 | |
4ee6a8b1 | 39 | @Input() name = 'description' |
66b16caf | 40 | |
b15fe00f | 41 | @ViewChild('textarea') textareaElement: ElementRef |
2539932e C |
42 | @ViewChild('previewElement') previewElement: ElementRef |
43 | ||
44 | truncatedPreviewHTML: SafeHtml | string = '' | |
45 | previewHTML: SafeHtml | string = '' | |
b15fe00f | 46 | |
b15fe00f | 47 | isMaximized = false |
8d8a037e | 48 | disabled = false |
2de96f4d | 49 | |
01d0147e | 50 | maximizeInText = $localize`Maximize editor` |
51 | maximizeOutText = $localize`Exit maximized editor` | |
52 | ||
07fa4c97 | 53 | private contentChanged = new Subject<string>() |
363726fe | 54 | private scrollPosition: [number, number] |
2de96f4d | 55 | |
363726fe | 56 | constructor ( |
57 | private viewportScroller: ViewportScroller, | |
aa9cf3b9 | 58 | private screenService: ScreenService, |
363726fe | 59 | private markdownService: MarkdownService |
60 | ) { } | |
2de96f4d C |
61 | |
62 | ngOnInit () { | |
07fa4c97 | 63 | this.contentChanged |
db400f44 C |
64 | .pipe( |
65 | debounceTime(150), | |
66 | distinctUntilChanged() | |
67 | ) | |
68 | .subscribe(() => this.updatePreviews()) | |
2de96f4d | 69 | |
07fa4c97 | 70 | this.contentChanged.next(this.content) |
2de96f4d C |
71 | } |
72 | ||
73 | propagateChange = (_: any) => { /* empty */ } | |
74 | ||
75 | writeValue (description: string) { | |
07fa4c97 | 76 | this.content = description |
2de96f4d | 77 | |
07fa4c97 | 78 | this.contentChanged.next(this.content) |
2de96f4d C |
79 | } |
80 | ||
81 | registerOnChange (fn: (_: any) => void) { | |
82 | this.propagateChange = fn | |
83 | } | |
84 | ||
85 | registerOnTouched () { | |
86 | // Unused | |
87 | } | |
88 | ||
89 | onModelChange () { | |
07fa4c97 | 90 | this.propagateChange(this.content) |
2de96f4d | 91 | |
07fa4c97 | 92 | this.contentChanged.next(this.content) |
2de96f4d C |
93 | } |
94 | ||
b15fe00f K |
95 | onMaximizeClick () { |
96 | this.isMaximized = !this.isMaximized | |
97 | ||
98 | // Make sure textarea have the focus | |
aa9cf3b9 | 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 | } | |
b15fe00f K |
103 | |
104 | // Make sure the window has no scrollbars | |
105 | if (!this.isMaximized) { | |
106 | this.unlockBodyScroll() | |
107 | } else { | |
108 | this.lockBodyScroll() | |
109 | } | |
110 | } | |
111 | ||
8d8a037e JB |
112 | setDisabledState (isDisabled: boolean) { |
113 | this.disabled = isDisabled | |
114 | } | |
115 | ||
b15fe00f | 116 | private lockBodyScroll () { |
363726fe | 117 | this.scrollPosition = this.viewportScroller.getScrollPosition() |
b15fe00f K |
118 | document.getElementById('content').classList.add('lock-scroll') |
119 | } | |
120 | ||
121 | private unlockBodyScroll () { | |
122 | document.getElementById('content').classList.remove('lock-scroll') | |
363726fe | 123 | this.viewportScroller.scrollToPosition(this.scrollPosition) |
6693df9d C |
124 | } |
125 | ||
41d71344 | 126 | private async updatePreviews () { |
07fa4c97 | 127 | if (this.content === null || this.content === undefined) return |
f595d394 | 128 | |
41d71344 C |
129 | this.truncatedPreviewHTML = await this.markdownRender(truncate(this.content, { length: this.truncate })) |
130 | this.previewHTML = await this.markdownRender(this.content) | |
07fa4c97 C |
131 | } |
132 | ||
d68ebf0b | 133 | private async markdownRender (text: string) { |
2539932e C |
134 | let html: string |
135 | ||
136 | if (this.customMarkdownRenderer) { | |
137 | const result = await this.customMarkdownRenderer(text) | |
138 | ||
139 | if (result instanceof HTMLElement) { | |
2539932e C |
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) { | |
9162fdd3 | 154 | html = this.markdownService.processVideoTimestamps(this.markdownVideo.shortUUID, html) |
2539932e | 155 | } |
07fa4c97 | 156 | |
2539932e | 157 | return html |
2de96f4d C |
158 | } |
159 | } |