]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - 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
CommitLineData
db400f44 1import { debounceTime, distinctUntilChanged } from 'rxjs/operators'
2de96f4d
C
2import { Component, forwardRef, Input, OnInit } from '@angular/core'
3import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms'
4d089429 4import { Subject } from 'rxjs'
24a8e782 5import truncate from 'lodash-es/truncate'
bbe0f064 6import { ScreenService } from '@app/shared/misc/screen.service'
1506307f 7import { MarkdownService } from '@app/shared/renderer'
2de96f4d
C
8
9@Component({
66b16caf
C
10 selector: 'my-markdown-textarea',
11 templateUrl: './markdown-textarea.component.html',
12 styleUrls: [ './markdown-textarea.component.scss' ],
2de96f4d
C
13 providers: [
14 {
15 provide: NG_VALUE_ACCESSOR,
66b16caf 16 useExisting: forwardRef(() => MarkdownTextareaComponent),
2de96f4d
C
17 multi: true
18 }
19 ]
20})
21
66b16caf 22export class MarkdownTextareaComponent implements ControlValueAccessor, OnInit {
07fa4c97 23 @Input() content = ''
66b16caf
C
24 @Input() classes: string[] = []
25 @Input() textareaWidth = '100%'
26 @Input() textareaHeight = '150px'
27 @Input() previewColumn = false
28 @Input() truncate: number
07fa4c97 29 @Input() markdownType: 'text' | 'enhanced' = 'text'
d68ebf0b 30 @Input() markdownVideo = false
66b16caf
C
31
32 textareaMarginRight = '0'
33 flexDirection = 'column'
07fa4c97
C
34 truncatedPreviewHTML = ''
35 previewHTML = ''
2de96f4d 36
07fa4c97 37 private contentChanged = new Subject<string>()
2de96f4d 38
bbe0f064
C
39 constructor (
40 private screenService: ScreenService,
41 private markdownService: MarkdownService
42) {}
2de96f4d
C
43
44 ngOnInit () {
07fa4c97 45 this.contentChanged
db400f44
C
46 .pipe(
47 debounceTime(150),
48 distinctUntilChanged()
49 )
50 .subscribe(() => this.updatePreviews())
2de96f4d 51
07fa4c97 52 this.contentChanged.next(this.content)
66b16caf
C
53
54 if (this.previewColumn) {
55 this.flexDirection = 'row'
56 this.textareaMarginRight = '15px'
57 }
2de96f4d
C
58 }
59
60 propagateChange = (_: any) => { /* empty */ }
61
62 writeValue (description: string) {
07fa4c97 63 this.content = description
2de96f4d 64
07fa4c97 65 this.contentChanged.next(this.content)
2de96f4d
C
66 }
67
68 registerOnChange (fn: (_: any) => void) {
69 this.propagateChange = fn
70 }
71
72 registerOnTouched () {
73 // Unused
74 }
75
76 onModelChange () {
07fa4c97 77 this.propagateChange(this.content)
2de96f4d 78
07fa4c97 79 this.contentChanged.next(this.content)
2de96f4d
C
80 }
81
6693df9d 82 arePreviewsDisplayed () {
bbe0f064 83 return this.screenService.isInSmallView() === false
6693df9d
C
84 }
85
41d71344 86 private async updatePreviews () {
07fa4c97 87 if (this.content === null || this.content === undefined) return
f595d394 88
41d71344
C
89 this.truncatedPreviewHTML = await this.markdownRender(truncate(this.content, { length: this.truncate }))
90 this.previewHTML = await this.markdownRender(this.content)
07fa4c97
C
91 }
92
d68ebf0b
L
93 private async markdownRender (text: string) {
94 const html = this.markdownType === 'text' ?
95 await this.markdownService.textMarkdownToHTML(text) :
96 await this.markdownService.enhancedMarkdownToHTML(text)
07fa4c97 97
d68ebf0b 98 return this.markdownVideo ? this.markdownService.processVideoTimestamps(html) : html
2de96f4d
C
99 }
100}