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'
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>()
39 private scrollPosition: [number, number]
42 private viewportScroller: ViewportScroller,
43 private screenService: ScreenService,
44 private markdownService: MarkdownService
51 distinctUntilChanged()
53 .subscribe(() => this.updatePreviews())
55 this.contentChanged.next(this.content)
58 propagateChange = (_: any) => { /* empty */ }
60 writeValue (description: string) {
61 this.content = description
63 this.contentChanged.next(this.content)
66 registerOnChange (fn: (_: any) => void) {
67 this.propagateChange = fn
70 registerOnTouched () {
75 this.propagateChange(this.content)
77 this.contentChanged.next(this.content)
81 this.isMaximized = !this.isMaximized
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()
89 // Make sure the window has no scrollbars
90 if (!this.isMaximized) {
91 this.unlockBodyScroll()
97 private lockBodyScroll () {
98 this.scrollPosition = this.viewportScroller.getScrollPosition()
99 document.getElementById('content').classList.add('lock-scroll')
102 private unlockBodyScroll () {
103 document.getElementById('content').classList.remove('lock-scroll')
104 this.viewportScroller.scrollToPosition(this.scrollPosition)
107 private async updatePreviews () {
108 if (this.content === null || this.content === undefined) return
110 this.truncatedPreviewHTML = await this.markdownRender(truncate(this.content, { length: this.truncate }))
111 this.previewHTML = await this.markdownRender(this.content)
114 private async markdownRender (text: string) {
115 const html = this.markdownType === 'text' ?
116 await this.markdownService.textMarkdownToHTML(text) :
117 await this.markdownService.enhancedMarkdownToHTML(text)
119 return this.markdownVideo ? this.markdownService.processVideoTimestamps(html) : html