]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/shared/shared-forms/markdown-textarea.component.ts
Fix HTML in account/channel description
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / shared-forms / markdown-textarea.component.ts
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() markdownVideo: Video
35
36 @Input() name = 'description'
37
38 @ViewChild('textarea') textareaElement: ElementRef
39 @ViewChild('previewElement') previewElement: ElementRef
40
41 truncatedPreviewHTML: SafeHtml | string = ''
42 previewHTML: SafeHtml | string = ''
43
44 isMaximized = false
45 disabled = false
46
47 maximizeInText = $localize`Maximize editor`
48 maximizeOutText = $localize`Exit maximized editor`
49
50 private contentChanged = new Subject<string>()
51 private scrollPosition: [number, number]
52
53 constructor (
54 private viewportScroller: ViewportScroller,
55 private screenService: ScreenService,
56 private markdownService: MarkdownService
57 ) { }
58
59 ngOnInit () {
60 this.contentChanged
61 .pipe(
62 debounceTime(150),
63 distinctUntilChanged()
64 )
65 .subscribe(() => this.updatePreviews())
66
67 this.contentChanged.next(this.content)
68 }
69
70 propagateChange = (_: any) => { /* empty */ }
71
72 writeValue (description: string) {
73 this.content = description
74
75 this.contentChanged.next(this.content)
76 }
77
78 registerOnChange (fn: (_: any) => void) {
79 this.propagateChange = fn
80 }
81
82 registerOnTouched () {
83 // Unused
84 }
85
86 onModelChange () {
87 this.propagateChange(this.content)
88
89 this.contentChanged.next(this.content)
90 }
91
92 onMaximizeClick () {
93 if (this.disabled) return
94
95 this.isMaximized = !this.isMaximized
96
97 // Make sure textarea have the focus
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 }
102
103 // Make sure the window has no scrollbars
104 if (!this.isMaximized) {
105 this.unlockBodyScroll()
106 } else {
107 this.lockBodyScroll()
108 }
109 }
110
111 setDisabledState (isDisabled: boolean) {
112 this.disabled = isDisabled
113 }
114
115 private lockBodyScroll () {
116 this.scrollPosition = this.viewportScroller.getScrollPosition()
117 document.getElementById('content').classList.add('lock-scroll')
118 }
119
120 private unlockBodyScroll () {
121 document.getElementById('content').classList.remove('lock-scroll')
122 this.viewportScroller.scrollToPosition(this.scrollPosition)
123 }
124
125 private async updatePreviews () {
126 if (this.content === null || this.content === undefined) return
127
128 this.truncatedPreviewHTML = await this.markdownRender(truncate(this.content, { length: this.truncate }))
129 this.previewHTML = await this.markdownRender(this.content)
130 }
131
132 private async markdownRender (text: string) {
133 let html: string
134
135 if (this.customMarkdownRenderer) {
136 const result = await this.customMarkdownRenderer(text)
137
138 if (result instanceof HTMLElement) {
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({ markdown: text })
148 } else {
149 html = await this.markdownService.enhancedMarkdownToHTML({ markdown: text })
150 }
151
152 if (this.markdownVideo) {
153 html = this.markdownService.processVideoTimestamps(this.markdownVideo.shortUUID, html)
154 }
155
156 return html
157 }
158 }