]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/forms/markdown-textarea.component.ts
Strict templates enabled
[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 = ''
be27ef3b 24 @Input() classes: string[] | { [klass: string]: any[] | any } = []
66b16caf
C
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
4ee6a8b1 31 @Input() name = 'description'
66b16caf
C
32
33 textareaMarginRight = '0'
34 flexDirection = 'column'
07fa4c97
C
35 truncatedPreviewHTML = ''
36 previewHTML = ''
2de96f4d 37
07fa4c97 38 private contentChanged = new Subject<string>()
2de96f4d 39
bbe0f064
C
40 constructor (
41 private screenService: ScreenService,
42 private markdownService: MarkdownService
43) {}
2de96f4d
C
44
45 ngOnInit () {
07fa4c97 46 this.contentChanged
db400f44
C
47 .pipe(
48 debounceTime(150),
49 distinctUntilChanged()
50 )
51 .subscribe(() => this.updatePreviews())
2de96f4d 52
07fa4c97 53 this.contentChanged.next(this.content)
66b16caf
C
54
55 if (this.previewColumn) {
56 this.flexDirection = 'row'
57 this.textareaMarginRight = '15px'
58 }
2de96f4d
C
59 }
60
61 propagateChange = (_: any) => { /* empty */ }
62
63 writeValue (description: string) {
07fa4c97 64 this.content = description
2de96f4d 65
07fa4c97 66 this.contentChanged.next(this.content)
2de96f4d
C
67 }
68
69 registerOnChange (fn: (_: any) => void) {
70 this.propagateChange = fn
71 }
72
73 registerOnTouched () {
74 // Unused
75 }
76
77 onModelChange () {
07fa4c97 78 this.propagateChange(this.content)
2de96f4d 79
07fa4c97 80 this.contentChanged.next(this.content)
2de96f4d
C
81 }
82
6693df9d 83 arePreviewsDisplayed () {
bbe0f064 84 return this.screenService.isInSmallView() === false
6693df9d
C
85 }
86
41d71344 87 private async updatePreviews () {
07fa4c97 88 if (this.content === null || this.content === undefined) return
f595d394 89
41d71344
C
90 this.truncatedPreviewHTML = await this.markdownRender(truncate(this.content, { length: this.truncate }))
91 this.previewHTML = await this.markdownRender(this.content)
07fa4c97
C
92 }
93
d68ebf0b
L
94 private async markdownRender (text: string) {
95 const html = this.markdownType === 'text' ?
96 await this.markdownService.textMarkdownToHTML(text) :
97 await this.markdownService.enhancedMarkdownToHTML(text)
07fa4c97 98
d68ebf0b 99 return this.markdownVideo ? this.markdownService.processVideoTimestamps(html) : html
2de96f4d
C
100 }
101}