]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/shared-forms/markdown-textarea.component.ts
fix video download modal select width
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / shared-forms / markdown-textarea.component.ts
CommitLineData
363726fe 1import { ViewportScroller } from '@angular/common'
67ed6552
C
2import truncate from 'lodash-es/truncate'
3import { Subject } from 'rxjs'
db400f44 4import { debounceTime, distinctUntilChanged } from 'rxjs/operators'
67ed6552 5import { Component, ElementRef, forwardRef, Input, OnInit, ViewChild } from '@angular/core'
2de96f4d 6import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms'
aa9cf3b9 7import { MarkdownService, ScreenService } from '@app/core'
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 } = []
b15fe00f 25 @Input() textareaMaxWidth = '100%'
66b16caf 26 @Input() textareaHeight = '150px'
66b16caf 27 @Input() truncate: number
07fa4c97 28 @Input() markdownType: 'text' | 'enhanced' = 'text'
d68ebf0b 29 @Input() markdownVideo = false
4ee6a8b1 30 @Input() name = 'description'
66b16caf 31
b15fe00f
K
32 @ViewChild('textarea') textareaElement: ElementRef
33
07fa4c97
C
34 truncatedPreviewHTML = ''
35 previewHTML = ''
b15fe00f 36 isMaximized = false
2de96f4d 37
01d0147e 38 maximizeInText = $localize`Maximize editor`
39 maximizeOutText = $localize`Exit maximized editor`
40
07fa4c97 41 private contentChanged = new Subject<string>()
363726fe 42 private scrollPosition: [number, number]
2de96f4d 43
363726fe 44 constructor (
45 private viewportScroller: ViewportScroller,
aa9cf3b9 46 private screenService: ScreenService,
363726fe 47 private markdownService: MarkdownService
48 ) { }
2de96f4d
C
49
50 ngOnInit () {
07fa4c97 51 this.contentChanged
db400f44
C
52 .pipe(
53 debounceTime(150),
54 distinctUntilChanged()
55 )
56 .subscribe(() => this.updatePreviews())
2de96f4d 57
07fa4c97 58 this.contentChanged.next(this.content)
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
b15fe00f
K
83 onMaximizeClick () {
84 this.isMaximized = !this.isMaximized
85
86 // Make sure textarea have the focus
aa9cf3b9 87 // Except on touchscreens devices, the virtual keyboard may move up and hide the textarea in maximized mode
88 if (!this.screenService.isInTouchScreen()) {
89 this.textareaElement.nativeElement.focus()
90 }
b15fe00f
K
91
92 // Make sure the window has no scrollbars
93 if (!this.isMaximized) {
94 this.unlockBodyScroll()
95 } else {
96 this.lockBodyScroll()
97 }
98 }
99
100 private lockBodyScroll () {
363726fe 101 this.scrollPosition = this.viewportScroller.getScrollPosition()
b15fe00f
K
102 document.getElementById('content').classList.add('lock-scroll')
103 }
104
105 private unlockBodyScroll () {
106 document.getElementById('content').classList.remove('lock-scroll')
363726fe 107 this.viewportScroller.scrollToPosition(this.scrollPosition)
6693df9d
C
108 }
109
41d71344 110 private async updatePreviews () {
07fa4c97 111 if (this.content === null || this.content === undefined) return
f595d394 112
41d71344
C
113 this.truncatedPreviewHTML = await this.markdownRender(truncate(this.content, { length: this.truncate }))
114 this.previewHTML = await this.markdownRender(this.content)
07fa4c97
C
115 }
116
d68ebf0b
L
117 private async markdownRender (text: string) {
118 const html = this.markdownType === 'text' ?
119 await this.markdownService.textMarkdownToHTML(text) :
120 await this.markdownService.enhancedMarkdownToHTML(text)
07fa4c97 121
d68ebf0b 122 return this.markdownVideo ? this.markdownService.processVideoTimestamps(html) : html
2de96f4d
C
123 }
124}