]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/shared/forms/markdown-textarea.component.ts
Set a default background color for account avatar
[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 } 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[] = []
25 @Input() textareaWidth = '100%'
26 @Input() textareaHeight = '150px'
27 @Input() previewColumn = false
28 @Input() truncate: number
29 @Input() markdownType: 'text' | 'enhanced' = 'text'
30 @Input() markdownVideo = false
31
32 textareaMarginRight = '0'
33 flexDirection = 'column'
34 truncatedPreviewHTML = ''
35 previewHTML = ''
36
37 private contentChanged = new Subject<string>()
38
39 constructor (
40 private screenService: ScreenService,
41 private markdownService: MarkdownService
42 ) {}
43
44 ngOnInit () {
45 this.contentChanged
46 .pipe(
47 debounceTime(150),
48 distinctUntilChanged()
49 )
50 .subscribe(() => this.updatePreviews())
51
52 this.contentChanged.next(this.content)
53
54 if (this.previewColumn) {
55 this.flexDirection = 'row'
56 this.textareaMarginRight = '15px'
57 }
58 }
59
60 propagateChange = (_: any) => { /* empty */ }
61
62 writeValue (description: string) {
63 this.content = description
64
65 this.contentChanged.next(this.content)
66 }
67
68 registerOnChange (fn: (_: any) => void) {
69 this.propagateChange = fn
70 }
71
72 registerOnTouched () {
73 // Unused
74 }
75
76 onModelChange () {
77 this.propagateChange(this.content)
78
79 this.contentChanged.next(this.content)
80 }
81
82 arePreviewsDisplayed () {
83 return this.screenService.isInSmallView() === false
84 }
85
86 private async updatePreviews () {
87 if (this.content === null || this.content === undefined) return
88
89 this.truncatedPreviewHTML = await this.markdownRender(truncate(this.content, { length: this.truncate }))
90 this.previewHTML = await this.markdownRender(this.content)
91 }
92
93 private async markdownRender (text: string) {
94 const html = this.markdownType === 'text' ?
95 await this.markdownService.textMarkdownToHTML(text) :
96 await this.markdownService.enhancedMarkdownToHTML(text)
97
98 return this.markdownVideo ? this.markdownService.processVideoTimestamps(html) : html
99 }
100 }