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