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