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 maximizeInText = $localize`Maximize editor`
39 maximizeOutText = $localize`Exit maximized editor`
41 private contentChanged = new Subject<string>()
42 private scrollPosition: [number, number]
45 private viewportScroller: ViewportScroller,
46 private screenService: ScreenService,
47 private markdownService: MarkdownService
54 distinctUntilChanged()
56 .subscribe(() => this.updatePreviews())
58 this.contentChanged.next(this.content)
61 propagateChange = (_: any) => { /* empty */ }
63 writeValue (description: string) {
64 this.content = description
66 this.contentChanged.next(this.content)
69 registerOnChange (fn: (_: any) => void) {
70 this.propagateChange = fn
73 registerOnTouched () {
78 this.propagateChange(this.content)
80 this.contentChanged.next(this.content)
84 this.isMaximized = !this.isMaximized
86 // Make sure textarea have the focus
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()
92 // Make sure the window has no scrollbars
93 if (!this.isMaximized) {
94 this.unlockBodyScroll()
100 private lockBodyScroll () {
101 this.scrollPosition = this.viewportScroller.getScrollPosition()
102 document.getElementById('content').classList.add('lock-scroll')
105 private unlockBodyScroll () {
106 document.getElementById('content').classList.remove('lock-scroll')
107 this.viewportScroller.scrollToPosition(this.scrollPosition)
110 private async updatePreviews () {
111 if (this.content === null || this.content === undefined) return
113 this.truncatedPreviewHTML = await this.markdownRender(truncate(this.content, { length: this.truncate }))
114 this.previewHTML = await this.markdownRender(this.content)
117 private async markdownRender (text: string) {
118 const html = this.markdownType === 'text' ?
119 await this.markdownService.textMarkdownToHTML(text) :
120 await this.markdownService.enhancedMarkdownToHTML(text)
122 return this.markdownVideo ? this.markdownService.processVideoTimestamps(html) : html