1 import { debounceTime, distinctUntilChanged } from 'rxjs/operators'
2 import { Component, forwardRef, Input, OnInit, ViewChild, ElementRef } from '@angular/core'
3 import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms'
4 import { Subject } from 'rxjs'
5 import truncate from 'lodash-es/truncate'
6 import { ScreenService } from '@app/shared/misc/screen.service'
7 import { MarkdownService } from '@app/shared/renderer'
10 selector: 'my-markdown-textarea',
11 templateUrl: './markdown-textarea.component.html',
12 styleUrls: [ './markdown-textarea.component.scss' ],
15 provide: NG_VALUE_ACCESSOR,
16 useExisting: forwardRef(() => MarkdownTextareaComponent),
22 export class MarkdownTextareaComponent implements ControlValueAccessor, OnInit {
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'
32 @ViewChild('textarea') textareaElement: ElementRef
34 truncatedPreviewHTML = ''
38 private contentChanged = new Subject<string>()
41 private screenService: ScreenService,
42 private markdownService: MarkdownService
49 distinctUntilChanged()
51 .subscribe(() => this.updatePreviews())
53 this.contentChanged.next(this.content)
56 propagateChange = (_: any) => { /* empty */ }
58 writeValue (description: string) {
59 this.content = description
61 this.contentChanged.next(this.content)
64 registerOnChange (fn: (_: any) => void) {
65 this.propagateChange = fn
68 registerOnTouched () {
73 this.propagateChange(this.content)
75 this.contentChanged.next(this.content)
79 this.isMaximized = !this.isMaximized
81 // Make sure textarea have the focus
82 this.textareaElement.nativeElement.focus()
84 // Make sure the window has no scrollbars
85 if (!this.isMaximized) {
86 this.unlockBodyScroll()
92 private lockBodyScroll () {
93 document.getElementById('content').classList.add('lock-scroll')
96 private unlockBodyScroll () {
97 document.getElementById('content').classList.remove('lock-scroll')
100 private async updatePreviews () {
101 if (this.content === null || this.content === undefined) return
103 this.truncatedPreviewHTML = await this.markdownRender(truncate(this.content, { length: this.truncate }))
104 this.previewHTML = await this.markdownRender(this.content)
107 private async markdownRender (text: string) {
108 const html = this.markdownType === 'text' ?
109 await this.markdownService.textMarkdownToHTML(text) :
110 await this.markdownService.enhancedMarkdownToHTML(text)
112 return this.markdownVideo ? this.markdownService.processVideoTimestamps(html) : html