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