]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/shared/shared-forms/markdown-textarea.component.ts
Merge branch 'develop' into shorter-URLs-channels-accounts
[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
10 @Component({
11 selector: 'my-markdown-textarea',
12 templateUrl: './markdown-textarea.component.html',
13 styleUrls: [ './markdown-textarea.component.scss' ],
14 providers: [
15 {
16 provide: NG_VALUE_ACCESSOR,
17 useExisting: forwardRef(() => MarkdownTextareaComponent),
18 multi: true
19 }
20 ]
21 })
22
23 export class MarkdownTextareaComponent implements ControlValueAccessor, OnInit {
24 @Input() content = ''
25
26 @Input() classes: string[] | { [klass: string]: any[] | any } = []
27
28 @Input() textareaMaxWidth = '100%'
29 @Input() textareaHeight = '150px'
30
31 @Input() truncate: number
32
33 @Input() markdownType: 'text' | 'enhanced' = 'text'
34 @Input() customMarkdownRenderer?: (text: string) => Promise<string | HTMLElement>
35
36 @Input() markdownVideo = false
37
38 @Input() name = 'description'
39
40 @ViewChild('textarea') textareaElement: ElementRef
41 @ViewChild('previewElement') previewElement: ElementRef
42
43 truncatedPreviewHTML: SafeHtml | string = ''
44 previewHTML: SafeHtml | string = ''
45
46 isMaximized = false
47
48 maximizeInText = $localize`Maximize editor`
49 maximizeOutText = $localize`Exit maximized editor`
50
51 private contentChanged = new Subject<string>()
52 private scrollPosition: [number, number]
53
54 constructor (
55 private viewportScroller: ViewportScroller,
56 private screenService: ScreenService,
57 private markdownService: MarkdownService
58 ) { }
59
60 ngOnInit () {
61 this.contentChanged
62 .pipe(
63 debounceTime(150),
64 distinctUntilChanged()
65 )
66 .subscribe(() => this.updatePreviews())
67
68 this.contentChanged.next(this.content)
69 }
70
71 propagateChange = (_: any) => { /* empty */ }
72
73 writeValue (description: string) {
74 this.content = description
75
76 this.contentChanged.next(this.content)
77 }
78
79 registerOnChange (fn: (_: any) => void) {
80 this.propagateChange = fn
81 }
82
83 registerOnTouched () {
84 // Unused
85 }
86
87 onModelChange () {
88 this.propagateChange(this.content)
89
90 this.contentChanged.next(this.content)
91 }
92
93 onMaximizeClick () {
94 this.isMaximized = !this.isMaximized
95
96 // Make sure textarea have the focus
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 }
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 () {
111 this.scrollPosition = this.viewportScroller.getScrollPosition()
112 document.getElementById('content').classList.add('lock-scroll')
113 }
114
115 private unlockBodyScroll () {
116 document.getElementById('content').classList.remove('lock-scroll')
117 this.viewportScroller.scrollToPosition(this.scrollPosition)
118 }
119
120 private async updatePreviews () {
121 if (this.content === null || this.content === undefined) return
122
123 this.truncatedPreviewHTML = await this.markdownRender(truncate(this.content, { length: this.truncate }))
124 this.previewHTML = await this.markdownRender(this.content)
125 }
126
127 private async markdownRender (text: string) {
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 }
152
153 return html
154 }
155 }