]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/shared/forms/markdown-textarea.component.ts
Merge branch 'release/2.2.0' into develop
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / forms / markdown-textarea.component.ts
1 import { debounceTime, distinctUntilChanged } from 'rxjs/operators'
2 import { Component, forwardRef, Input, OnInit, ViewChild, ElementRef } from '@angular/core'
3 import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms'
4 import { Subject } from 'rxjs'
5 import truncate from 'lodash-es/truncate'
6 import { ScreenService } from '@app/shared/misc/screen.service'
7 import { MarkdownService } from '@app/shared/renderer'
8
9 @Component({
10 selector: 'my-markdown-textarea',
11 templateUrl: './markdown-textarea.component.html',
12 styleUrls: [ './markdown-textarea.component.scss' ],
13 providers: [
14 {
15 provide: NG_VALUE_ACCESSOR,
16 useExisting: forwardRef(() => MarkdownTextareaComponent),
17 multi: true
18 }
19 ]
20 })
21
22 export class MarkdownTextareaComponent implements ControlValueAccessor, OnInit {
23 @Input() content = ''
24 @Input() classes: string[] | { [klass: string]: any[] | any } = []
25 @Input() textareaMaxWidth = '100%'
26 @Input() textareaHeight = '150px'
27 @Input() truncate: number
28 @Input() markdownType: 'text' | 'enhanced' = 'text'
29 @Input() markdownVideo = false
30 @Input() name = 'description'
31
32 @ViewChild('textarea') textareaElement: ElementRef
33
34 truncatedPreviewHTML = ''
35 previewHTML = ''
36 isMaximized = false
37
38 private contentChanged = new Subject<string>()
39
40 constructor (
41 private screenService: ScreenService,
42 private markdownService: MarkdownService
43 ) {}
44
45 ngOnInit () {
46 this.contentChanged
47 .pipe(
48 debounceTime(150),
49 distinctUntilChanged()
50 )
51 .subscribe(() => this.updatePreviews())
52
53 this.contentChanged.next(this.content)
54 }
55
56 propagateChange = (_: any) => { /* empty */ }
57
58 writeValue (description: string) {
59 this.content = description
60
61 this.contentChanged.next(this.content)
62 }
63
64 registerOnChange (fn: (_: any) => void) {
65 this.propagateChange = fn
66 }
67
68 registerOnTouched () {
69 // Unused
70 }
71
72 onModelChange () {
73 this.propagateChange(this.content)
74
75 this.contentChanged.next(this.content)
76 }
77
78 onMaximizeClick () {
79 this.isMaximized = !this.isMaximized
80
81 // Make sure textarea have the focus
82 this.textareaElement.nativeElement.focus()
83
84 // Make sure the window has no scrollbars
85 if (!this.isMaximized) {
86 this.unlockBodyScroll()
87 } else {
88 this.lockBodyScroll()
89 }
90 }
91
92 private lockBodyScroll () {
93 document.getElementById('content').classList.add('lock-scroll')
94 }
95
96 private unlockBodyScroll () {
97 document.getElementById('content').classList.remove('lock-scroll')
98 }
99
100 private async updatePreviews () {
101 if (this.content === null || this.content === undefined) return
102
103 this.truncatedPreviewHTML = await this.markdownRender(truncate(this.content, { length: this.truncate }))
104 this.previewHTML = await this.markdownRender(this.content)
105 }
106
107 private async markdownRender (text: string) {
108 const html = this.markdownType === 'text' ?
109 await this.markdownService.textMarkdownToHTML(text) :
110 await this.markdownService.enhancedMarkdownToHTML(text)
111
112 return this.markdownVideo ? this.markdownService.processVideoTimestamps(html) : html
113 }
114 }