]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/shared-forms/markdown-textarea.component.ts
Merge branch 'release/5.0.0' into develop
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / shared-forms / markdown-textarea.component.ts
CommitLineData
67ed6552
C
1import truncate from 'lodash-es/truncate'
2import { Subject } from 'rxjs'
db400f44 3import { debounceTime, distinctUntilChanged } from 'rxjs/operators'
2539932e 4import { ViewportScroller } from '@angular/common'
67ed6552 5import { Component, ElementRef, forwardRef, Input, OnInit, ViewChild } from '@angular/core'
2de96f4d 6import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms'
2539932e 7import { SafeHtml } from '@angular/platform-browser'
aa9cf3b9 8import { MarkdownService, ScreenService } from '@app/core'
9162fdd3 9import { 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 24export 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
6a056bbe
C
34 @Input() debounceTime = 150
35
9162fdd3 36 @Input() markdownVideo: Video
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
8d8a037e 47 disabled = false
2de96f4d 48
01d0147e 49 maximizeInText = $localize`Maximize editor`
50 maximizeOutText = $localize`Exit maximized editor`
51
07fa4c97 52 private contentChanged = new Subject<string>()
363726fe 53 private scrollPosition: [number, number]
2de96f4d 54
363726fe 55 constructor (
56 private viewportScroller: ViewportScroller,
aa9cf3b9 57 private screenService: ScreenService,
363726fe 58 private markdownService: MarkdownService
59 ) { }
2de96f4d
C
60
61 ngOnInit () {
07fa4c97 62 this.contentChanged
db400f44 63 .pipe(
6a056bbe 64 debounceTime(this.debounceTime),
db400f44
C
65 distinctUntilChanged()
66 )
67 .subscribe(() => this.updatePreviews())
2de96f4d 68
07fa4c97 69 this.contentChanged.next(this.content)
2de96f4d
C
70 }
71
72 propagateChange = (_: any) => { /* empty */ }
73
74 writeValue (description: string) {
07fa4c97 75 this.content = description
2de96f4d 76
07fa4c97 77 this.contentChanged.next(this.content)
2de96f4d
C
78 }
79
80 registerOnChange (fn: (_: any) => void) {
81 this.propagateChange = fn
82 }
83
84 registerOnTouched () {
85 // Unused
86 }
87
88 onModelChange () {
07fa4c97 89 this.propagateChange(this.content)
2de96f4d 90
07fa4c97 91 this.contentChanged.next(this.content)
2de96f4d
C
92 }
93
b15fe00f 94 onMaximizeClick () {
3031971e
C
95 if (this.disabled) return
96
b15fe00f
K
97 this.isMaximized = !this.isMaximized
98
99 // Make sure textarea have the focus
aa9cf3b9 100 // Except on touchscreens devices, the virtual keyboard may move up and hide the textarea in maximized mode
101 if (!this.screenService.isInTouchScreen()) {
102 this.textareaElement.nativeElement.focus()
103 }
b15fe00f
K
104
105 // Make sure the window has no scrollbars
106 if (!this.isMaximized) {
107 this.unlockBodyScroll()
108 } else {
109 this.lockBodyScroll()
110 }
111 }
112
8d8a037e
JB
113 setDisabledState (isDisabled: boolean) {
114 this.disabled = isDisabled
115 }
116
b15fe00f 117 private lockBodyScroll () {
363726fe 118 this.scrollPosition = this.viewportScroller.getScrollPosition()
b15fe00f
K
119 document.getElementById('content').classList.add('lock-scroll')
120 }
121
122 private unlockBodyScroll () {
123 document.getElementById('content').classList.remove('lock-scroll')
363726fe 124 this.viewportScroller.scrollToPosition(this.scrollPosition)
6693df9d
C
125 }
126
41d71344 127 private async updatePreviews () {
07fa4c97 128 if (this.content === null || this.content === undefined) return
f595d394 129
41d71344
C
130 this.truncatedPreviewHTML = await this.markdownRender(truncate(this.content, { length: this.truncate }))
131 this.previewHTML = await this.markdownRender(this.content)
07fa4c97
C
132 }
133
d68ebf0b 134 private async markdownRender (text: string) {
2539932e
C
135 let html: string
136
137 if (this.customMarkdownRenderer) {
138 const result = await this.customMarkdownRenderer(text)
139
140 if (result instanceof HTMLElement) {
2539932e
C
141 const wrapperElement = this.previewElement.nativeElement as HTMLElement
142 wrapperElement.innerHTML = ''
143 wrapperElement.appendChild(result)
144 return
145 }
146
147 html = result
148 } else if (this.markdownType === 'text') {
0e45e336 149 html = await this.markdownService.textMarkdownToHTML({ markdown: text })
2539932e 150 } else {
0e45e336 151 html = await this.markdownService.enhancedMarkdownToHTML({ markdown: text })
2539932e
C
152 }
153
154 if (this.markdownVideo) {
9162fdd3 155 html = this.markdownService.processVideoTimestamps(this.markdownVideo.shortUUID, html)
2539932e 156 }
07fa4c97 157
2539932e 158 return html
2de96f4d
C
159 }
160}