]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/shared/shared-forms/markdown-textarea.component.ts
8f51d47df638a7ae21f869a1423ba043fc996f80
[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() classes: string[] | { [klass: string]: any[] | any } = []
28
29 @Input() textareaMaxWidth = '100%'
30 @Input() textareaHeight = '150px'
31
32 @Input() truncate: number
33
34 @Input() markdownType: 'text' | 'enhanced' = 'text'
35 @Input() customMarkdownRenderer?: (text: string) => Promise<string | HTMLElement>
36
37 @Input() markdownVideo: Video
38
39 @Input() name = 'description'
40
41 @ViewChild('textarea') textareaElement: ElementRef
42 @ViewChild('previewElement') previewElement: ElementRef
43
44 truncatedPreviewHTML: SafeHtml | string = ''
45 previewHTML: SafeHtml | string = ''
46
47 isMaximized = false
48
49 maximizeInText = $localize`Maximize editor`
50 maximizeOutText = $localize`Exit maximized editor`
51
52 private contentChanged = new Subject<string>()
53 private scrollPosition: [number, number]
54
55 constructor (
56 private viewportScroller: ViewportScroller,
57 private screenService: ScreenService,
58 private markdownService: MarkdownService
59 ) { }
60
61 ngOnInit () {
62 this.contentChanged
63 .pipe(
64 debounceTime(150),
65 distinctUntilChanged()
66 )
67 .subscribe(() => this.updatePreviews())
68
69 this.contentChanged.next(this.content)
70 }
71
72 propagateChange = (_: any) => { /* empty */ }
73
74 writeValue (description: string) {
75 this.content = description
76
77 this.contentChanged.next(this.content)
78 }
79
80 registerOnChange (fn: (_: any) => void) {
81 this.propagateChange = fn
82 }
83
84 registerOnTouched () {
85 // Unused
86 }
87
88 onModelChange () {
89 this.propagateChange(this.content)
90
91 this.contentChanged.next(this.content)
92 }
93
94 onMaximizeClick () {
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 private lockBodyScroll () {
112 this.scrollPosition = this.viewportScroller.getScrollPosition()
113 document.getElementById('content').classList.add('lock-scroll')
114 }
115
116 private unlockBodyScroll () {
117 document.getElementById('content').classList.remove('lock-scroll')
118 this.viewportScroller.scrollToPosition(this.scrollPosition)
119 }
120
121 private async updatePreviews () {
122 if (this.content === null || this.content === undefined) return
123
124 this.truncatedPreviewHTML = await this.markdownRender(truncate(this.content, { length: this.truncate }))
125 this.previewHTML = await this.markdownRender(this.content)
126 }
127
128 private async markdownRender (text: string) {
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) {
151 html = this.markdownService.processVideoTimestamps(this.markdownVideo.shortUUID, html)
152 }
153
154 return html
155 }
156 }