]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/shared/shared-forms/markdown-textarea.component.ts
Disable auto-focus markdown textarea on touchdevices
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / shared-forms / markdown-textarea.component.ts
1 import { ViewportScroller } from '@angular/common'
2 import truncate from 'lodash-es/truncate'
3 import { Subject } from 'rxjs'
4 import { debounceTime, distinctUntilChanged } from 'rxjs/operators'
5 import { Component, ElementRef, forwardRef, Input, OnInit, ViewChild } from '@angular/core'
6 import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms'
7 import { MarkdownService, ScreenService } from '@app/core'
8
9 @Component({
10 selector: 'my-markdown-textarea',
11 templateUrl: './markdown-textarea.component.html',
12 styleUrls: [ './markdown-textarea.component.scss' ],
13 providers: [
14 {
15 provide: NG_VALUE_ACCESSOR,
16 useExisting: forwardRef(() => MarkdownTextareaComponent),
17 multi: true
18 }
19 ]
20 })
21
22 export class MarkdownTextareaComponent implements ControlValueAccessor, OnInit {
23 @Input() content = ''
24 @Input() classes: string[] | { [klass: string]: any[] | any } = []
25 @Input() textareaMaxWidth = '100%'
26 @Input() textareaHeight = '150px'
27 @Input() truncate: number
28 @Input() markdownType: 'text' | 'enhanced' = 'text'
29 @Input() markdownVideo = false
30 @Input() name = 'description'
31
32 @ViewChild('textarea') textareaElement: ElementRef
33
34 truncatedPreviewHTML = ''
35 previewHTML = ''
36 isMaximized = false
37
38 private contentChanged = new Subject<string>()
39 private scrollPosition: [number, number]
40
41 constructor (
42 private viewportScroller: ViewportScroller,
43 private screenService: ScreenService,
44 private markdownService: MarkdownService
45 ) { }
46
47 ngOnInit () {
48 this.contentChanged
49 .pipe(
50 debounceTime(150),
51 distinctUntilChanged()
52 )
53 .subscribe(() => this.updatePreviews())
54
55 this.contentChanged.next(this.content)
56 }
57
58 propagateChange = (_: any) => { /* empty */ }
59
60 writeValue (description: string) {
61 this.content = description
62
63 this.contentChanged.next(this.content)
64 }
65
66 registerOnChange (fn: (_: any) => void) {
67 this.propagateChange = fn
68 }
69
70 registerOnTouched () {
71 // Unused
72 }
73
74 onModelChange () {
75 this.propagateChange(this.content)
76
77 this.contentChanged.next(this.content)
78 }
79
80 onMaximizeClick () {
81 this.isMaximized = !this.isMaximized
82
83 // Make sure textarea have the focus
84 // Except on touchscreens devices, the virtual keyboard may move up and hide the textarea in maximized mode
85 if (!this.screenService.isInTouchScreen()) {
86 this.textareaElement.nativeElement.focus()
87 }
88
89 // Make sure the window has no scrollbars
90 if (!this.isMaximized) {
91 this.unlockBodyScroll()
92 } else {
93 this.lockBodyScroll()
94 }
95 }
96
97 private lockBodyScroll () {
98 this.scrollPosition = this.viewportScroller.getScrollPosition()
99 document.getElementById('content').classList.add('lock-scroll')
100 }
101
102 private unlockBodyScroll () {
103 document.getElementById('content').classList.remove('lock-scroll')
104 this.viewportScroller.scrollToPosition(this.scrollPosition)
105 }
106
107 private async updatePreviews () {
108 if (this.content === null || this.content === undefined) return
109
110 this.truncatedPreviewHTML = await this.markdownRender(truncate(this.content, { length: this.truncate }))
111 this.previewHTML = await this.markdownRender(this.content)
112 }
113
114 private async markdownRender (text: string) {
115 const html = this.markdownType === 'text' ?
116 await this.markdownService.textMarkdownToHTML(text) :
117 await this.markdownService.enhancedMarkdownToHTML(text)
118
119 return this.markdownVideo ? this.markdownService.processVideoTimestamps(html) : html
120 }
121 }