]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/shared-forms/markdown-textarea.component.ts
Merge branch 'release/3.3.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
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
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
C
63 .pipe(
64 debounceTime(150),
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
K
94 onMaximizeClick () {
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
111 private lockBodyScroll () {
363726fe 112 this.scrollPosition = this.viewportScroller.getScrollPosition()
b15fe00f
K
113 document.getElementById('content').classList.add('lock-scroll')
114 }
115
116 private unlockBodyScroll () {
117 document.getElementById('content').classList.remove('lock-scroll')
363726fe 118 this.viewportScroller.scrollToPosition(this.scrollPosition)
6693df9d
C
119 }
120
41d71344 121 private async updatePreviews () {
07fa4c97 122 if (this.content === null || this.content === undefined) return
f595d394 123
41d71344
C
124 this.truncatedPreviewHTML = await this.markdownRender(truncate(this.content, { length: this.truncate }))
125 this.previewHTML = await this.markdownRender(this.content)
07fa4c97
C
126 }
127
d68ebf0b 128 private async markdownRender (text: string) {
2539932e
C
129 let html: string
130
131 if (this.customMarkdownRenderer) {
132 const result = await this.customMarkdownRenderer(text)
133
134 if (result instanceof HTMLElement) {
135 html = ''
136
137 const wrapperElement = this.previewElement.nativeElement as HTMLElement
138 wrapperElement.innerHTML = ''
139 wrapperElement.appendChild(result)
140 return
141 }
142
143 html = result
144 } else if (this.markdownType === 'text') {
145 html = await this.markdownService.textMarkdownToHTML(text)
146 } else {
147 html = await this.markdownService.enhancedMarkdownToHTML(text)
148 }
149
150 if (this.markdownVideo) {
9162fdd3 151 html = this.markdownService.processVideoTimestamps(this.markdownVideo.shortUUID, html)
2539932e 152 }
07fa4c97 153
2539932e 154 return html
2de96f4d
C
155 }
156}